git repos / blockattack-game

commit b9eda2d4

Poul Sander · 2023-10-04 20:16
b9eda2d45cc585fbad0b33ed19d1ecfd2148ee6d patch · browse files
parent 00759e5a751ec11e08b016a54815df947b1a5436

Move the alt theme to a file

Changed files

A Game/data/themes/alt_theme.json
M source/code/themes.cpp before
diff --git a/Game/data/themes/alt_theme.json b/Game/data/themes/alt_theme.json new file mode 100644 index 0000000..ae0857a --- /dev/null +++ b/Game/data/themes/alt_theme.json
@@ -0,0 +1,20 @@
+{
+ "background_data": [
+ {
+ "background_name": "alt_background",
+ "background_scale": "tile",
+ "background_sprite": "background_sample",
+ "background_sprite_16x9": "",
+ "tileMoveSpeedX": 50,
+ "tileMoveSpeedY": 100
+ }
+ ],
+ "themes": [
+ {
+ "back_board": "back_board_sample_snow",
+ "background_name": "alt_background",
+ "decoration_name": "smilies",
+ "theme_name": "snow"
+ }
+ ]
+}
diff --git a/source/code/themes.cpp b/source/code/themes.cpp index bb0b016..24f4f0d 100644 --- a/source/code/themes.cpp +++ b/source/code/themes.cpp
@@ -27,12 +27,53 @@ https://www.blockattack.net
#include <vector>
#include <map>
#include "nlohmann/json.hpp"
+#include <iostream>
+#include "global.hpp"
using json = nlohmann::json;
+void to_json(json& j, const ImgScale& p) {
+ switch (p) {
+ case ImgScale::Stretch:
+ j = "stretch";
+ break;
+ case ImgScale::Tile:
+ j = "tile";
+ break;
+ case ImgScale::Resize:
+ j = "resize";
+ break;
+ case ImgScale::Cut:
+ j = "cut";
+ break;
+ default:
+ j = "stretch";
+ break;
+ }
+}
+
+void from_json(const json& j, ImgScale& p) {
+ std::string s = j;
+ if (s == "stretch") {
+ p = ImgScale::Stretch;
+ }
+ else if (s == "tile") {
+ p = ImgScale::Tile;
+ }
+ else if (s == "resize") {
+ p = ImgScale::Resize;
+ }
+ else if (s == "cut") {
+ p = ImgScale::Cut;
+ }
+ else {
+ p = ImgScale::Stretch;
+ }
+}
+
void to_json(json& j, const BackGroundData& p) {
- j = json{ {"background_name", p.name}, {"background_sprite", p.background_sprite} };
+ j = json{ {"background_name", p.name}, {"background_sprite", p.background_sprite}, {"background_sprite_16x9", p.background_sprite_16x9}, {"background_scale", p.background_scale}, {"tileMoveSpeedX", p.tileMoveSpeedX}, {"tileMoveSpeedY", p.tileMoveSpeedY} };
}
void to_json(json& j, const Theme& p) {
@@ -47,6 +88,38 @@ void to_json(json& j, const ThemeFileData& p) {
j = json{ {"background_data", p.background_data}, {"decoration_data", p.decoration_data}, {"themes", p.themes} };
}
+void from_json(const json& j, BackGroundData& p) {
+ j.at("background_name").get_to(p.name);
+ j.at("background_sprite").get_to(p.background_sprite);
+ j.at("background_sprite_16x9").get_to(p.background_sprite_16x9);
+ j.at("background_scale").get_to(p.background_scale);
+ j.at("tileMoveSpeedX").get_to(p.tileMoveSpeedX);
+ j.at("tileMoveSpeedY").get_to(p.tileMoveSpeedY);
+}
+
+void from_json(const json& j, Theme& p) {
+ j.at("theme_name").get_to(p.theme_name);
+ j.at("back_board").get_to(p.back_board);
+ j.at("background_name").get_to(p.background.name);
+ j.at("decoration_name").get_to(p.decoration.name);
+}
+
+void from_json(const json& j, DecorationData& p) {
+ j.at("name").get_to(p.name);
+ j.at("decoration_sprites").get_to(p.decoration_sprites);
+}
+
+void from_json(const json& j, ThemeFileData& p) {
+ if (j.contains("background_data")) {
+ j.at("background_data").get_to(p.background_data);
+ }
+ if (j.contains("decoration_data")) {
+ j.at("decoration_data").get_to(p.decoration_data);
+ }
+ if (j.contains("themes")) {
+ j.at("themes").get_to(p.themes);
+ }
+}
@@ -57,6 +130,37 @@ static bool initialized = false;
static size_t current_theme = 0;
+static void FillMissingFields(Theme& theme) {
+ if (theme.background.name.empty()) {
+ //If the theme does not define a background then use the standard.
+ theme.background.name = "standard";
+ }
+ theme.background = background_data[theme.background.name];
+}
+
+static void ReadThemeDataFromFile(const char* filename) {
+ if (globalData.verboseLevel) {
+ std::cout << "Reading theme data from " << filename << "\n";
+ }
+ std::string s = sago::GetFileContent(filename);
+ json j = json::parse(s);
+ ThemeFileData tfd = j;
+ for (auto& bg : tfd.background_data) {
+ background_data[bg.name] = bg;
+ }
+ for (auto& dec : tfd.decoration_data) {
+ decoration_data[dec.name] = dec;
+ }
+ for (const Theme& theme : tfd.themes) {
+ if (globalData.verboseLevel) {
+ std::cout << "Adding theme " << theme.theme_name << "\n";
+ }
+ themes.push_back(theme);
+ FillMissingFields(themes.back());
+ }
+}
+
+
static void InitBackGroundData() {
BackGroundData standard;
standard.name = "standard";
@@ -64,25 +168,10 @@ static void InitBackGroundData() {
standard.background_sprite_16x9 = "background_sixteen_nine";
standard.background_scale = ImgScale::Stretch;
background_data[standard.name] = standard;
- BackGroundData alt_background;
- alt_background.name = "alt_background";
- alt_background.background_sprite = "background_sample";
- alt_background.background_sprite_16x9 = "";
- alt_background.background_scale = ImgScale::Tile;
- alt_background.tileMoveSpeedX = 50;
- alt_background.tileMoveSpeedY = 100;
- background_data[alt_background.name] = alt_background;
DecorationData smileys;
decoration_data[smileys.name] = smileys;
}
-static void FillMissingFields(Theme& theme) {
- if (theme.background.name.empty()) {
- //If the theme does not define a background then use the standard.
- theme.background.name = "standard";
- }
- theme.background = background_data[theme.background.name];
-}
void DumpThemeData() {
@@ -103,15 +192,11 @@ void InitThemes() {
if (initialized) {
return;
}
+ initialized = true;
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.name = "alt_background";
- FillMissingFields(snow);
- themes.push_back(snow);
+ ReadThemeDataFromFile("themes/alt_theme.json");
DumpThemeData();
}