Skip to content

Commit

Permalink
Alt background. Tile support
Browse files Browse the repository at this point in the history
  • Loading branch information
sago007 committed Jul 29, 2023
1 parent 342ea50 commit 081d847
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
9 changes: 9 additions & 0 deletions Game/data/sprites/sample_alt_gfx.sprite
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,14 @@
"number_of_frames" : 1,
"frame_time" : 1
}
,"background_sample" : {
"texture" : "alt_gfx/background",
"topx" : 0,
"topy" : 0,
"height" : 150,
"width" : 150,
"number_of_frames" : 1,
"frame_time" : 1
}

}
Binary file added Game/data/textures/alt_gfx/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 18 additions & 6 deletions source/code/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ static int InitImages(sago::SagoSpriteHolder& holder) {
bricks[5] = holder.GetSprite("block_yellow");
bricks[6] = holder.GetSprite("block_grey");
bomb = holder.GetSprite("block_bomb");
backgroundImage = holder.GetSprite("background");
backgroundSixteenNineImage = holder.GetSprite("background_sixteen_nine");
//backgroundImage = holder.GetSprite("background");
//backgroundSixteenNineImage = holder.GetSprite("background_sixteen_nine");
globalData.bHighScore = holder.GetSprite("b_highscore");
globalData.bBack = holder.GetSprite("b_blank");
bForward = holder.GetSprite("b_forward");
Expand Down Expand Up @@ -271,12 +271,23 @@ static bool logicalRenderer = false;

void DrawBackground(SDL_Renderer* target) {
SDL_RenderClear(target);
if ( (double)globalData.xsize/globalData.ysize > 1.5) {
backgroundSixteenNineImage.DrawScaled(target, SDL_GetTicks(), 0, 0, globalData.xsize, globalData.ysize);
sago::SagoSprite background = globalData.spriteHolder->GetSprite(globalData.theme.background.background_sprite);
if ( (double)globalData.xsize/globalData.ysize > 1.5 && globalData.theme.background.background_sprite_16x9.length()) {
background = globalData.spriteHolder->GetSprite(globalData.theme.background.background_sprite_16x9);
}
else {
backgroundImage.DrawScaled(target, SDL_GetTicks(), 0, 0, globalData.xsize, globalData.ysize);
if (globalData.theme.background.background_scale == ImgScale::Tile) {
int nextX = 0;
while (nextX < globalData.xsize) {
int nextY = 0;
while (nextY < globalData.ysize) {
background.Draw(target, SDL_GetTicks(), nextX, nextY);
nextY += background.GetHeight();
}
nextX += background.GetWidth();
}
return;
}
background.DrawScaled(target, SDL_GetTicks(), 0, 0, globalData.xsize, globalData.ysize);
}

/**
Expand Down Expand Up @@ -1182,6 +1193,7 @@ int main(int argc, char* argv[]) {
std::cout << "Renderer: " << info.name << "\n";
}
globalData.screen = renderer;
globalData.theme = getTheme(0);
ResetFullscreen();
SetSDLIcon(sdlWindow);

Expand Down
33 changes: 33 additions & 0 deletions source/code/themes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,47 @@ Source information and contacts persons can be found at
#include "themes.hpp"

#include <vector>
#include <unordered_map>

static std::vector<Theme> themes(1);
static std::unordered_map<std::string, BackGroundData> background_data;
static bool initialized = false;
static size_t current_theme = 0;

static void InitBackGroundData() {
BackGroundData standard;
standard.background_name = "standard";
standard.background_sprite = "background";
standard.background_sprite_16x9 = "background_sixteen_nine";
standard.background_scale = ImgScale::Stretch;
background_data["standard"] = standard;
BackGroundData alt_background;
alt_background.background_name = "alt_background";
alt_background.background_sprite = "background_sample";
alt_background.background_sprite_16x9 = "";
alt_background.background_scale = ImgScale::Tile;
background_data["alt_background"] = alt_background;
}

static void FillMissingFields(Theme &theme) {
if (theme.background.background_name.empty()) {
//If the theme does not define a background then use the standard.
theme.background = background_data["standard"];
}
}

void InitThemes() {
if (initialized) {
return;
}
InitBackGroundData();
themes.resize(1); //Add the default theme
FillMissingFields(themes[0]);
Theme snow;
snow.theme_name = "snow";
snow.back_board = "back_board_sample_snow";
snow.background = background_data["alt_background"];
FillMissingFields(snow);
themes.push_back(snow);
}

Expand All @@ -47,3 +75,8 @@ Theme getNextTheme() {
current_theme = current_theme % themes.size();
return themes.at(current_theme);
}

Theme getTheme(size_t theme_number) {
InitThemes();
return themes.at(theme_number % themes.size());
}
20 changes: 20 additions & 0 deletions source/code/themes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,33 @@ Source information and contacts persons can be found at

#include <string>

enum class ImgScale { Stretch,
Tile,
Resize,
Cut };

struct BackGroundData {
std::string background_name = "";
std::string background_sprite = "";
std::string background_sprite_16x9 = "";
ImgScale background_scale = ImgScale::Stretch;
};

struct Theme {
std::string theme_name = "standard";
std::string back_board = "back_board"; // Can also be "back_board_sample_snow" or "trans_cover"
BackGroundData background;
};

/**
* @brief returns a theme from a list
* @return A copy of a theme
*/
Theme getNextTheme();

/**
* @brief getTheme returns a specific theme
* @param theme_number
* @return
*/
Theme getTheme(size_t theme_number);

0 comments on commit 081d847

Please sign in to comment.