git repos / blockattack-game

blame: source/code/themes.cpp

normal view · raw

aaba4f1e Poul Sander 2023-07-25 13:34 1
/*
aaba4f1e Poul Sander 2023-07-25 13:34 2
===========================================================================
aaba4f1e Poul Sander 2023-07-25 13:34 3
blockattack - Block Attack - Rise of the Blocks
aaba4f1e Poul Sander 2023-07-25 13:34 4
Copyright (C) 2005-2023 Poul Sander
aaba4f1e Poul Sander 2023-07-25 13:34 5
aaba4f1e Poul Sander 2023-07-25 13:34 6
This program is free software: you can redistribute it and/or modify
aaba4f1e Poul Sander 2023-07-25 13:34 7
it under the terms of the GNU General Public License as published by
aaba4f1e Poul Sander 2023-07-25 13:34 8
the Free Software Foundation, either version 2 of the License, or
aaba4f1e Poul Sander 2023-07-25 13:34 9
(at your option) any later version.
aaba4f1e Poul Sander 2023-07-25 13:34 10
aaba4f1e Poul Sander 2023-07-25 13:34 11
This program is distributed in the hope that it will be useful,
aaba4f1e Poul Sander 2023-07-25 13:34 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
aaba4f1e Poul Sander 2023-07-25 13:34 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
aaba4f1e Poul Sander 2023-07-25 13:34 14
GNU General Public License for more details.
aaba4f1e Poul Sander 2023-07-25 13:34 15
aaba4f1e Poul Sander 2023-07-25 13:34 16
You should have received a copy of the GNU General Public License
aaba4f1e Poul Sander 2023-07-25 13:34 17
along with this program.  If not, see http://www.gnu.org/licenses/
aaba4f1e Poul Sander 2023-07-25 13:34 18
aaba4f1e Poul Sander 2023-07-25 13:34 19
Source information and contacts persons can be found at
aaba4f1e Poul Sander 2023-07-25 13:34 20
https://www.blockattack.net
aaba4f1e Poul Sander 2023-07-25 13:34 21
===========================================================================
aaba4f1e Poul Sander 2023-07-25 13:34 22
*/
aaba4f1e Poul Sander 2023-07-25 13:34 23
aaba4f1e Poul Sander 2023-07-25 13:34 24
aaba4f1e Poul Sander 2023-07-25 13:34 25
#include "themes.hpp"
2660ccea Poul Sander 2023-09-22 19:25 26
#include "sago/SagoMisc.hpp"
aaba4f1e Poul Sander 2023-07-25 13:34 27
#include <vector>
00759e5a Poul Sander 2023-09-24 20:01 28
#include <map>
2660ccea Poul Sander 2023-09-22 19:25 29
#include "nlohmann/json.hpp"
b9eda2d4 Poul Sander 2023-10-04 20:16 30
#include <iostream>
b9eda2d4 Poul Sander 2023-10-04 20:16 31
#include "global.hpp"
7d88cb38 Poul Sander 2023-10-05 20:48 32
#include <boost/algorithm/string/predicate.hpp>
68c75938 Poul Sander 2023-11-13 23:03 33
#include <fmt/core.h>
2660ccea Poul Sander 2023-09-22 19:25 34
2660ccea Poul Sander 2023-09-22 19:25 35
using json = nlohmann::json;
2660ccea Poul Sander 2023-09-22 19:25 36
2660ccea Poul Sander 2023-09-22 19:25 37
b9eda2d4 Poul Sander 2023-10-04 20:16 38
void to_json(json& j, const ImgScale& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 39
	switch (p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 40
	case ImgScale::Stretch:
b9eda2d4 Poul Sander 2023-10-04 20:16 41
		j = "stretch";
b9eda2d4 Poul Sander 2023-10-04 20:16 42
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 43
	case ImgScale::Tile:
b9eda2d4 Poul Sander 2023-10-04 20:16 44
		j = "tile";
b9eda2d4 Poul Sander 2023-10-04 20:16 45
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 46
	case ImgScale::Resize:
b9eda2d4 Poul Sander 2023-10-04 20:16 47
		j = "resize";
b9eda2d4 Poul Sander 2023-10-04 20:16 48
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 49
	case ImgScale::Cut:
b9eda2d4 Poul Sander 2023-10-04 20:16 50
		j = "cut";
b9eda2d4 Poul Sander 2023-10-04 20:16 51
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 52
	default:
b9eda2d4 Poul Sander 2023-10-04 20:16 53
		j = "stretch";
b9eda2d4 Poul Sander 2023-10-04 20:16 54
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 55
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 56
}
b9eda2d4 Poul Sander 2023-10-04 20:16 57
b9eda2d4 Poul Sander 2023-10-04 20:16 58
void from_json(const json& j, ImgScale& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 59
	std::string s = j;
b9eda2d4 Poul Sander 2023-10-04 20:16 60
	if (s == "stretch") {
b9eda2d4 Poul Sander 2023-10-04 20:16 61
		p = ImgScale::Stretch;
b9eda2d4 Poul Sander 2023-10-04 20:16 62
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 63
	else if (s == "tile") {
b9eda2d4 Poul Sander 2023-10-04 20:16 64
		p = ImgScale::Tile;
b9eda2d4 Poul Sander 2023-10-04 20:16 65
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 66
	else if (s == "resize") {
b9eda2d4 Poul Sander 2023-10-04 20:16 67
		p = ImgScale::Resize;
b9eda2d4 Poul Sander 2023-10-04 20:16 68
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 69
	else if (s == "cut") {
b9eda2d4 Poul Sander 2023-10-04 20:16 70
		p = ImgScale::Cut;
b9eda2d4 Poul Sander 2023-10-04 20:16 71
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 72
	else {
b9eda2d4 Poul Sander 2023-10-04 20:16 73
		p = ImgScale::Stretch;
b9eda2d4 Poul Sander 2023-10-04 20:16 74
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 75
}
b9eda2d4 Poul Sander 2023-10-04 20:16 76
2660ccea Poul Sander 2023-09-22 19:25 77
void to_json(json& j, const BackGroundData& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 78
	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} };
2660ccea Poul Sander 2023-09-22 19:25 79
}
2660ccea Poul Sander 2023-09-22 19:25 80
2660ccea Poul Sander 2023-09-22 19:25 81
void to_json(json& j, const Theme& p) {
173b1d55 Poul Sander 2023-09-23 21:29 82
	j = json{ {"theme_name", p.theme_name}, {"back_board", p.back_board}, {"background_name", p.background.name}, {"decoration_name", p.decoration.name} };
2660ccea Poul Sander 2023-09-22 19:25 83
}
2660ccea Poul Sander 2023-09-22 19:25 84
00759e5a Poul Sander 2023-09-24 20:01 85
void to_json(json& j, const DecorationData& p) {
00759e5a Poul Sander 2023-09-24 20:01 86
	j = json{ {"name", p.name}, {"decoration_sprites", p.decoration_sprites} };
00759e5a Poul Sander 2023-09-24 20:01 87
}
00759e5a Poul Sander 2023-09-24 20:01 88
2660ccea Poul Sander 2023-09-22 19:25 89
void to_json(json& j, const ThemeFileData& p) {
00759e5a Poul Sander 2023-09-24 20:01 90
	j = json{ {"background_data", p.background_data}, {"decoration_data", p.decoration_data}, {"themes", p.themes} };
2660ccea Poul Sander 2023-09-22 19:25 91
}
2660ccea Poul Sander 2023-09-22 19:25 92
b9eda2d4 Poul Sander 2023-10-04 20:16 93
void from_json(const json& j, BackGroundData& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 94
	j.at("background_name").get_to(p.name);
b9eda2d4 Poul Sander 2023-10-04 20:16 95
	j.at("background_sprite").get_to(p.background_sprite);
b9eda2d4 Poul Sander 2023-10-04 20:16 96
	j.at("background_sprite_16x9").get_to(p.background_sprite_16x9);
b9eda2d4 Poul Sander 2023-10-04 20:16 97
	j.at("background_scale").get_to(p.background_scale);
b9eda2d4 Poul Sander 2023-10-04 20:16 98
	j.at("tileMoveSpeedX").get_to(p.tileMoveSpeedX);
b9eda2d4 Poul Sander 2023-10-04 20:16 99
	j.at("tileMoveSpeedY").get_to(p.tileMoveSpeedY);
b9eda2d4 Poul Sander 2023-10-04 20:16 100
}
b9eda2d4 Poul Sander 2023-10-04 20:16 101
b9eda2d4 Poul Sander 2023-10-04 20:16 102
void from_json(const json& j, Theme& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 103
	j.at("theme_name").get_to(p.theme_name);
b9eda2d4 Poul Sander 2023-10-04 20:16 104
	j.at("back_board").get_to(p.back_board);
b9eda2d4 Poul Sander 2023-10-04 20:16 105
	j.at("background_name").get_to(p.background.name);
b9eda2d4 Poul Sander 2023-10-04 20:16 106
	j.at("decoration_name").get_to(p.decoration.name);
b9eda2d4 Poul Sander 2023-10-04 20:16 107
}
b9eda2d4 Poul Sander 2023-10-04 20:16 108
b9eda2d4 Poul Sander 2023-10-04 20:16 109
void from_json(const json& j, DecorationData& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 110
	j.at("name").get_to(p.name);
b9eda2d4 Poul Sander 2023-10-04 20:16 111
	j.at("decoration_sprites").get_to(p.decoration_sprites);
b9eda2d4 Poul Sander 2023-10-04 20:16 112
}
b9eda2d4 Poul Sander 2023-10-04 20:16 113
b9eda2d4 Poul Sander 2023-10-04 20:16 114
void from_json(const json& j, ThemeFileData& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 115
	if (j.contains("background_data")) {
b9eda2d4 Poul Sander 2023-10-04 20:16 116
		j.at("background_data").get_to(p.background_data);
b9eda2d4 Poul Sander 2023-10-04 20:16 117
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 118
	if (j.contains("decoration_data")) {
b9eda2d4 Poul Sander 2023-10-04 20:16 119
		j.at("decoration_data").get_to(p.decoration_data);
b9eda2d4 Poul Sander 2023-10-04 20:16 120
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 121
	if (j.contains("themes")) {
b9eda2d4 Poul Sander 2023-10-04 20:16 122
		j.at("themes").get_to(p.themes);
b9eda2d4 Poul Sander 2023-10-04 20:16 123
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 124
}
2660ccea Poul Sander 2023-09-22 19:25 125
2660ccea Poul Sander 2023-09-22 19:25 126
aaba4f1e Poul Sander 2023-07-25 13:34 127
aaba4f1e Poul Sander 2023-07-25 13:34 128
static std::vector<Theme> themes(1);
00759e5a Poul Sander 2023-09-24 20:01 129
static std::map<std::string, BackGroundData> background_data;
00759e5a Poul Sander 2023-09-24 20:01 130
static std::map<std::string, DecorationData> decoration_data;
aaba4f1e Poul Sander 2023-07-25 13:34 131
static bool initialized = false;
aaba4f1e Poul Sander 2023-07-25 13:34 132
static size_t current_theme = 0;
aaba4f1e Poul Sander 2023-07-25 13:34 133
2660ccea Poul Sander 2023-09-22 19:25 134
b9eda2d4 Poul Sander 2023-10-04 20:16 135
static void FillMissingFields(Theme& theme) {
b9eda2d4 Poul Sander 2023-10-04 20:16 136
	if (theme.background.name.empty()) {
b9eda2d4 Poul Sander 2023-10-04 20:16 137
		//If the theme does not define a background then use the standard.
b9eda2d4 Poul Sander 2023-10-04 20:16 138
		theme.background.name = "standard";
b9eda2d4 Poul Sander 2023-10-04 20:16 139
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 140
	theme.background = background_data[theme.background.name];
b9eda2d4 Poul Sander 2023-10-04 20:16 141
}
b9eda2d4 Poul Sander 2023-10-04 20:16 142
7d88cb38 Poul Sander 2023-10-05 20:48 143
static void ReadThemeDataFromFile(const std::string& filename) {
b9eda2d4 Poul Sander 2023-10-04 20:16 144
	if (globalData.verboseLevel) {
b9eda2d4 Poul Sander 2023-10-04 20:16 145
		std::cout << "Reading theme data from " << filename << "\n";
b9eda2d4 Poul Sander 2023-10-04 20:16 146
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 147
	std::string s = sago::GetFileContent(filename);
68c75938 Poul Sander 2023-11-13 23:03 148
	if (s.empty()) {
68c75938 Poul Sander 2023-11-13 23:03 149
		std::cout << "File \"" << filename << "\" does not exit!\n";
68c75938 Poul Sander 2023-11-13 23:03 150
		return;
68c75938 Poul Sander 2023-11-13 23:03 151
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 152
	json j = json::parse(s);
b9eda2d4 Poul Sander 2023-10-04 20:16 153
	ThemeFileData tfd = j;
b9eda2d4 Poul Sander 2023-10-04 20:16 154
	for (auto& bg : tfd.background_data) {
b9eda2d4 Poul Sander 2023-10-04 20:16 155
		background_data[bg.name] = bg;
b9eda2d4 Poul Sander 2023-10-04 20:16 156
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 157
	for (auto& dec : tfd.decoration_data) {
b9eda2d4 Poul Sander 2023-10-04 20:16 158
		decoration_data[dec.name] = dec;
b9eda2d4 Poul Sander 2023-10-04 20:16 159
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 160
	for (const Theme& theme : tfd.themes) {
b9eda2d4 Poul Sander 2023-10-04 20:16 161
		if (globalData.verboseLevel) {
b9eda2d4 Poul Sander 2023-10-04 20:16 162
			std::cout << "Adding theme " << theme.theme_name << "\n";
b9eda2d4 Poul Sander 2023-10-04 20:16 163
		}
b9eda2d4 Poul Sander 2023-10-04 20:16 164
		themes.push_back(theme);
b9eda2d4 Poul Sander 2023-10-04 20:16 165
		FillMissingFields(themes.back());
b9eda2d4 Poul Sander 2023-10-04 20:16 166
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 167
}
b9eda2d4 Poul Sander 2023-10-04 20:16 168
b9eda2d4 Poul Sander 2023-10-04 20:16 169
081d847a Poul Sander 2023-07-29 19:59 170
static void InitBackGroundData() {
081d847a Poul Sander 2023-07-29 19:59 171
	BackGroundData standard;
173b1d55 Poul Sander 2023-09-23 21:29 172
	standard.name = "standard";
081d847a Poul Sander 2023-07-29 19:59 173
	standard.background_sprite = "background";
081d847a Poul Sander 2023-07-29 19:59 174
	standard.background_sprite_16x9 = "background_sixteen_nine";
081d847a Poul Sander 2023-07-29 19:59 175
	standard.background_scale = ImgScale::Stretch;
00759e5a Poul Sander 2023-09-24 20:01 176
	background_data[standard.name] = standard;
00759e5a Poul Sander 2023-09-24 20:01 177
	DecorationData smileys;
00759e5a Poul Sander 2023-09-24 20:01 178
	decoration_data[smileys.name] = smileys;
081d847a Poul Sander 2023-07-29 19:59 179
}
081d847a Poul Sander 2023-07-29 19:59 180
68c75938 Poul Sander 2023-11-13 23:03 181
static bool str_startswith(const std::string& s, const char* prefix) {
68c75938 Poul Sander 2023-11-13 23:03 182
	return (s.rfind(prefix, 0) == 0);
68c75938 Poul Sander 2023-11-13 23:03 183
}
2660ccea Poul Sander 2023-09-22 19:25 184
2660ccea Poul Sander 2023-09-22 19:25 185
void DumpThemeData() {
2660ccea Poul Sander 2023-09-22 19:25 186
	ThemeFileData tfd;
2660ccea Poul Sander 2023-09-22 19:25 187
	tfd.themes = themes;
6d46a5a3 Poul Sander 2023-09-23 14:42 188
	for (auto& pair : background_data) {
2660ccea Poul Sander 2023-09-22 19:25 189
		tfd.background_data.push_back(pair.second);
081d847a Poul Sander 2023-07-29 19:59 190
	}
00759e5a Poul Sander 2023-09-24 20:01 191
	for (auto& pair : decoration_data) {
00759e5a Poul Sander 2023-09-24 20:01 192
		tfd.decoration_data.push_back(pair.second);
00759e5a Poul Sander 2023-09-24 20:01 193
	}
2660ccea Poul Sander 2023-09-22 19:25 194
	json j = tfd;
2660ccea Poul Sander 2023-09-22 19:25 195
	std::string s = j.dump(4);
2660ccea Poul Sander 2023-09-22 19:25 196
	sago::WriteFileContent("themes_dump.json", s);
081d847a Poul Sander 2023-07-29 19:59 197
}
081d847a Poul Sander 2023-07-29 19:59 198
68c75938 Poul Sander 2023-11-13 23:03 199
void ThemesSaveCustomSlots() {
68c75938 Poul Sander 2023-11-13 23:03 200
	ThemeFileData tfd;
68c75938 Poul Sander 2023-11-13 23:03 201
	for (const Theme& theme : themes) {
68c75938 Poul Sander 2023-11-13 23:03 202
		if ( str_startswith(theme.theme_name,"custom_slot_")) {
68c75938 Poul Sander 2023-11-13 23:03 203
			tfd.themes.push_back(theme);
68c75938 Poul Sander 2023-11-13 23:03 204
		}
68c75938 Poul Sander 2023-11-13 23:03 205
	}
68c75938 Poul Sander 2023-11-13 23:03 206
	json j = tfd;
68c75938 Poul Sander 2023-11-13 23:03 207
	std::string s = j.dump(4);
68c75938 Poul Sander 2023-11-13 23:03 208
	sago::WriteFileContent("custom_themes.json", s);
68c75938 Poul Sander 2023-11-13 23:03 209
}
68c75938 Poul Sander 2023-11-13 23:03 210
68c75938 Poul Sander 2023-11-13 23:03 211
static void ThemesAddCustomSlot(std::vector<Theme>& themes, int slot_number) {
68c75938 Poul Sander 2023-11-13 23:03 212
	std::string slot_name = fmt::format("custom_slot_{}", slot_number);
68c75938 Poul Sander 2023-11-13 23:03 213
	for (const Theme& theme : themes) {
68c75938 Poul Sander 2023-11-13 23:03 214
		if (theme.theme_name == slot_name) {
68c75938 Poul Sander 2023-11-13 23:03 215
			//Custom slot already exists
68c75938 Poul Sander 2023-11-13 23:03 216
			return;
68c75938 Poul Sander 2023-11-13 23:03 217
		}
68c75938 Poul Sander 2023-11-13 23:03 218
	}
68c75938 Poul Sander 2023-11-13 23:03 219
	Theme new_theme;
68c75938 Poul Sander 2023-11-13 23:03 220
	new_theme.theme_name = slot_name;
68c75938 Poul Sander 2023-11-13 23:03 221
	themes.push_back(new_theme);
68c75938 Poul Sander 2023-11-13 23:03 222
	FillMissingFields(themes.back());
68c75938 Poul Sander 2023-11-13 23:03 223
}
68c75938 Poul Sander 2023-11-13 23:03 224
aaba4f1e Poul Sander 2023-07-25 13:34 225
void InitThemes() {
aaba4f1e Poul Sander 2023-07-25 13:34 226
	if (initialized) {
aaba4f1e Poul Sander 2023-07-25 13:34 227
		return;
aaba4f1e Poul Sander 2023-07-25 13:34 228
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 229
	initialized = true;
081d847a Poul Sander 2023-07-29 19:59 230
	InitBackGroundData();
aaba4f1e Poul Sander 2023-07-25 13:34 231
	themes.resize(1);  //Add the default theme
081d847a Poul Sander 2023-07-29 19:59 232
	FillMissingFields(themes[0]);
7d88cb38 Poul Sander 2023-10-05 20:48 233
	const std::vector<std::string>& theme_files = sago::GetFileList("themes");
7d88cb38 Poul Sander 2023-10-05 20:48 234
	for (const std::string& filename : theme_files) {
7d88cb38 Poul Sander 2023-10-05 20:48 235
		if (boost::algorithm::ends_with(filename,".json")) {
7d88cb38 Poul Sander 2023-10-05 20:48 236
			ReadThemeDataFromFile("themes/"+filename);
7d88cb38 Poul Sander 2023-10-05 20:48 237
		}
7d88cb38 Poul Sander 2023-10-05 20:48 238
	}
68c75938 Poul Sander 2023-11-13 23:03 239
	ReadThemeDataFromFile("custom_themes.json");
b3d74fc4 Poul Sander 2023-11-15 16:04 240
	for (int i=1; i <= 4; ++i) {
68c75938 Poul Sander 2023-11-13 23:03 241
		ThemesAddCustomSlot(themes, i);
68c75938 Poul Sander 2023-11-13 23:03 242
	}
2660ccea Poul Sander 2023-09-22 19:25 243
	DumpThemeData();
aaba4f1e Poul Sander 2023-07-25 13:34 244
}
aaba4f1e Poul Sander 2023-07-25 13:34 245
aaba4f1e Poul Sander 2023-07-25 13:34 246
Theme getNextTheme() {
aaba4f1e Poul Sander 2023-07-25 13:34 247
	InitThemes();
aaba4f1e Poul Sander 2023-07-25 13:34 248
	current_theme++;
aaba4f1e Poul Sander 2023-07-25 13:34 249
	current_theme = current_theme % themes.size();
aaba4f1e Poul Sander 2023-07-25 13:34 250
	return themes.at(current_theme);
aaba4f1e Poul Sander 2023-07-25 13:34 251
}
081d847a Poul Sander 2023-07-29 19:59 252
081d847a Poul Sander 2023-07-29 19:59 253
Theme getTheme(size_t theme_number) {
081d847a Poul Sander 2023-07-29 19:59 254
	InitThemes();
081d847a Poul Sander 2023-07-29 19:59 255
	return themes.at(theme_number % themes.size());
081d847a Poul Sander 2023-07-29 19:59 256
}
1970-01-01 00:00 257