git repos / blockattack-game

commit 081d847a

Poul Sander · 2023-07-29 19:59
081d847ab67afcf2def54c77c8601af6b215b5b8 patch · browse files
parent 342ea50768dc00b61b09920df6492e7fcaac976a

Alt background. Tile support

Changed files

M Game/data/sprites/sample_alt_gfx.sprite before
A Game/data/textures/alt_gfx/background.png
M source/code/main.cpp before
M source/code/themes.cpp before
M source/code/themes.hpp before
diff --git a/Game/data/sprites/sample_alt_gfx.sprite b/Game/data/sprites/sample_alt_gfx.sprite index 23eb1ad..4d9aaef 100644 --- a/Game/data/sprites/sample_alt_gfx.sprite +++ b/Game/data/sprites/sample_alt_gfx.sprite
@@ -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
+}
}
diff --git a/Game/data/textures/alt_gfx/background.png b/Game/data/textures/alt_gfx/background.png new file mode 100755 index 0000000..4040de3
Binary files /dev/null and b/Game/data/textures/alt_gfx/background.png differ
diff --git a/source/code/main.cpp b/source/code/main.cpp index 2cbd294..8ec9eb0 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp
@@ -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");
@@ -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);
}
/**
@@ -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);
diff --git a/source/code/themes.cpp b/source/code/themes.cpp index 999da72..0b50be8 100644 --- a/source/code/themes.cpp +++ b/source/code/themes.cpp
@@ -25,19 +25,47 @@ https://www.blockattack.net
#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);
}
@@ -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());
+}
diff --git a/source/code/themes.hpp b/source/code/themes.hpp index 7a5db3c..34fe300 100644 --- a/source/code/themes.hpp +++ b/source/code/themes.hpp
@@ -25,9 +25,22 @@ https://www.blockattack.net
#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;
};
/**
@@ -35,3 +48,10 @@ struct Theme {
* @return A copy of a theme
*/
Theme getNextTheme();
+
+/**
+ * @brief getTheme returns a specific theme
+ * @param theme_number
+ * @return
+ */
+Theme getTheme(size_t theme_number);