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>
2660ccea Poul Sander 2023-09-22 19:25 33
2660ccea Poul Sander 2023-09-22 19:25 34
using json = nlohmann::json;
2660ccea Poul Sander 2023-09-22 19:25 35
2660ccea Poul Sander 2023-09-22 19:25 36
b9eda2d4 Poul Sander 2023-10-04 20:16 37
void to_json(json& j, const ImgScale& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 38
	switch (p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 39
	case ImgScale::Stretch:
b9eda2d4 Poul Sander 2023-10-04 20:16 40
		j = "stretch";
b9eda2d4 Poul Sander 2023-10-04 20:16 41
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 42
	case ImgScale::Tile:
b9eda2d4 Poul Sander 2023-10-04 20:16 43
		j = "tile";
b9eda2d4 Poul Sander 2023-10-04 20:16 44
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 45
	case ImgScale::Resize:
b9eda2d4 Poul Sander 2023-10-04 20:16 46
		j = "resize";
b9eda2d4 Poul Sander 2023-10-04 20:16 47
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 48
	case ImgScale::Cut:
b9eda2d4 Poul Sander 2023-10-04 20:16 49
		j = "cut";
b9eda2d4 Poul Sander 2023-10-04 20:16 50
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 51
	default:
b9eda2d4 Poul Sander 2023-10-04 20:16 52
		j = "stretch";
b9eda2d4 Poul Sander 2023-10-04 20:16 53
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 54
	}
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
void from_json(const json& j, ImgScale& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 58
	std::string s = j;
b9eda2d4 Poul Sander 2023-10-04 20:16 59
	if (s == "stretch") {
b9eda2d4 Poul Sander 2023-10-04 20:16 60
		p = ImgScale::Stretch;
b9eda2d4 Poul Sander 2023-10-04 20:16 61
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 62
	else if (s == "tile") {
b9eda2d4 Poul Sander 2023-10-04 20:16 63
		p = ImgScale::Tile;
b9eda2d4 Poul Sander 2023-10-04 20:16 64
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 65
	else if (s == "resize") {
b9eda2d4 Poul Sander 2023-10-04 20:16 66
		p = ImgScale::Resize;
b9eda2d4 Poul Sander 2023-10-04 20:16 67
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 68
	else if (s == "cut") {
b9eda2d4 Poul Sander 2023-10-04 20:16 69
		p = ImgScale::Cut;
b9eda2d4 Poul Sander 2023-10-04 20:16 70
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 71
	else {
b9eda2d4 Poul Sander 2023-10-04 20:16 72
		p = ImgScale::Stretch;
b9eda2d4 Poul Sander 2023-10-04 20:16 73
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 74
}
b9eda2d4 Poul Sander 2023-10-04 20:16 75
2660ccea Poul Sander 2023-09-22 19:25 76
void to_json(json& j, const BackGroundData& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 77
	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 78
}
2660ccea Poul Sander 2023-09-22 19:25 79
2660ccea Poul Sander 2023-09-22 19:25 80
void to_json(json& j, const Theme& p) {
173b1d55 Poul Sander 2023-09-23 21:29 81
	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 82
}
2660ccea Poul Sander 2023-09-22 19:25 83
00759e5a Poul Sander 2023-09-24 20:01 84
void to_json(json& j, const DecorationData& p) {
00759e5a Poul Sander 2023-09-24 20:01 85
	j = json{ {"name", p.name}, {"decoration_sprites", p.decoration_sprites} };
00759e5a Poul Sander 2023-09-24 20:01 86
}
00759e5a Poul Sander 2023-09-24 20:01 87
2660ccea Poul Sander 2023-09-22 19:25 88
void to_json(json& j, const ThemeFileData& p) {
00759e5a Poul Sander 2023-09-24 20:01 89
	j = json{ {"background_data", p.background_data}, {"decoration_data", p.decoration_data}, {"themes", p.themes} };
2660ccea Poul Sander 2023-09-22 19:25 90
}
2660ccea Poul Sander 2023-09-22 19:25 91
b9eda2d4 Poul Sander 2023-10-04 20:16 92
void from_json(const json& j, BackGroundData& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 93
	j.at("background_name").get_to(p.name);
b9eda2d4 Poul Sander 2023-10-04 20:16 94
	j.at("background_sprite").get_to(p.background_sprite);
b9eda2d4 Poul Sander 2023-10-04 20:16 95
	j.at("background_sprite_16x9").get_to(p.background_sprite_16x9);
b9eda2d4 Poul Sander 2023-10-04 20:16 96
	j.at("background_scale").get_to(p.background_scale);
b9eda2d4 Poul Sander 2023-10-04 20:16 97
	j.at("tileMoveSpeedX").get_to(p.tileMoveSpeedX);
b9eda2d4 Poul Sander 2023-10-04 20:16 98
	j.at("tileMoveSpeedY").get_to(p.tileMoveSpeedY);
b9eda2d4 Poul Sander 2023-10-04 20:16 99
}
b9eda2d4 Poul Sander 2023-10-04 20:16 100
b9eda2d4 Poul Sander 2023-10-04 20:16 101
void from_json(const json& j, Theme& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 102
	j.at("theme_name").get_to(p.theme_name);
b9eda2d4 Poul Sander 2023-10-04 20:16 103
	j.at("back_board").get_to(p.back_board);
b9eda2d4 Poul Sander 2023-10-04 20:16 104
	j.at("background_name").get_to(p.background.name);
b9eda2d4 Poul Sander 2023-10-04 20:16 105
	j.at("decoration_name").get_to(p.decoration.name);
b9eda2d4 Poul Sander 2023-10-04 20:16 106
}
b9eda2d4 Poul Sander 2023-10-04 20:16 107
b9eda2d4 Poul Sander 2023-10-04 20:16 108
void from_json(const json& j, DecorationData& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 109
	j.at("name").get_to(p.name);
b9eda2d4 Poul Sander 2023-10-04 20:16 110
	j.at("decoration_sprites").get_to(p.decoration_sprites);
b9eda2d4 Poul Sander 2023-10-04 20:16 111
}
b9eda2d4 Poul Sander 2023-10-04 20:16 112
b9eda2d4 Poul Sander 2023-10-04 20:16 113
void from_json(const json& j, ThemeFileData& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 114
	if (j.contains("background_data")) {
b9eda2d4 Poul Sander 2023-10-04 20:16 115
		j.at("background_data").get_to(p.background_data);
b9eda2d4 Poul Sander 2023-10-04 20:16 116
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 117
	if (j.contains("decoration_data")) {
b9eda2d4 Poul Sander 2023-10-04 20:16 118
		j.at("decoration_data").get_to(p.decoration_data);
b9eda2d4 Poul Sander 2023-10-04 20:16 119
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 120
	if (j.contains("themes")) {
b9eda2d4 Poul Sander 2023-10-04 20:16 121
		j.at("themes").get_to(p.themes);
b9eda2d4 Poul Sander 2023-10-04 20:16 122
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 123
}
2660ccea Poul Sander 2023-09-22 19:25 124
2660ccea Poul Sander 2023-09-22 19:25 125
aaba4f1e Poul Sander 2023-07-25 13:34 126
aaba4f1e Poul Sander 2023-07-25 13:34 127
static std::vector<Theme> themes(1);
00759e5a Poul Sander 2023-09-24 20:01 128
static std::map<std::string, BackGroundData> background_data;
00759e5a Poul Sander 2023-09-24 20:01 129
static std::map<std::string, DecorationData> decoration_data;
aaba4f1e Poul Sander 2023-07-25 13:34 130
static bool initialized = false;
aaba4f1e Poul Sander 2023-07-25 13:34 131
static size_t current_theme = 0;
aaba4f1e Poul Sander 2023-07-25 13:34 132
2660ccea Poul Sander 2023-09-22 19:25 133
b9eda2d4 Poul Sander 2023-10-04 20:16 134
static void FillMissingFields(Theme& theme) {
b9eda2d4 Poul Sander 2023-10-04 20:16 135
	if (theme.background.name.empty()) {
b9eda2d4 Poul Sander 2023-10-04 20:16 136
		//If the theme does not define a background then use the standard.
b9eda2d4 Poul Sander 2023-10-04 20:16 137
		theme.background.name = "standard";
b9eda2d4 Poul Sander 2023-10-04 20:16 138
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 139
	theme.background = background_data[theme.background.name];
b9eda2d4 Poul Sander 2023-10-04 20:16 140
}
b9eda2d4 Poul Sander 2023-10-04 20:16 141
7d88cb38 Poul Sander 2023-10-05 20:48 142
static void ReadThemeDataFromFile(const std::string& filename) {
b9eda2d4 Poul Sander 2023-10-04 20:16 143
	if (globalData.verboseLevel) {
b9eda2d4 Poul Sander 2023-10-04 20:16 144
		std::cout << "Reading theme data from " << filename << "\n";
b9eda2d4 Poul Sander 2023-10-04 20:16 145
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 146
	std::string s = sago::GetFileContent(filename);
b9eda2d4 Poul Sander 2023-10-04 20:16 147
	json j = json::parse(s);
b9eda2d4 Poul Sander 2023-10-04 20:16 148
	ThemeFileData tfd = j;
b9eda2d4 Poul Sander 2023-10-04 20:16 149
	for (auto& bg : tfd.background_data) {
b9eda2d4 Poul Sander 2023-10-04 20:16 150
		background_data[bg.name] = bg;
b9eda2d4 Poul Sander 2023-10-04 20:16 151
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 152
	for (auto& dec : tfd.decoration_data) {
b9eda2d4 Poul Sander 2023-10-04 20:16 153
		decoration_data[dec.name] = dec;
b9eda2d4 Poul Sander 2023-10-04 20:16 154
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 155
	for (const Theme& theme : tfd.themes) {
b9eda2d4 Poul Sander 2023-10-04 20:16 156
		if (globalData.verboseLevel) {
b9eda2d4 Poul Sander 2023-10-04 20:16 157
			std::cout << "Adding theme " << theme.theme_name << "\n";
b9eda2d4 Poul Sander 2023-10-04 20:16 158
		}
b9eda2d4 Poul Sander 2023-10-04 20:16 159
		themes.push_back(theme);
b9eda2d4 Poul Sander 2023-10-04 20:16 160
		FillMissingFields(themes.back());
b9eda2d4 Poul Sander 2023-10-04 20:16 161
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 162
}
b9eda2d4 Poul Sander 2023-10-04 20:16 163
b9eda2d4 Poul Sander 2023-10-04 20:16 164
081d847a Poul Sander 2023-07-29 19:59 165
static void InitBackGroundData() {
081d847a Poul Sander 2023-07-29 19:59 166
	BackGroundData standard;
173b1d55 Poul Sander 2023-09-23 21:29 167
	standard.name = "standard";
081d847a Poul Sander 2023-07-29 19:59 168
	standard.background_sprite = "background";
081d847a Poul Sander 2023-07-29 19:59 169
	standard.background_sprite_16x9 = "background_sixteen_nine";
081d847a Poul Sander 2023-07-29 19:59 170
	standard.background_scale = ImgScale::Stretch;
00759e5a Poul Sander 2023-09-24 20:01 171
	background_data[standard.name] = standard;
00759e5a Poul Sander 2023-09-24 20:01 172
	DecorationData smileys;
00759e5a Poul Sander 2023-09-24 20:01 173
	decoration_data[smileys.name] = smileys;
081d847a Poul Sander 2023-07-29 19:59 174
}
081d847a Poul Sander 2023-07-29 19:59 175
2660ccea Poul Sander 2023-09-22 19:25 176
2660ccea Poul Sander 2023-09-22 19:25 177
2660ccea Poul Sander 2023-09-22 19:25 178
void DumpThemeData() {
2660ccea Poul Sander 2023-09-22 19:25 179
	ThemeFileData tfd;
2660ccea Poul Sander 2023-09-22 19:25 180
	tfd.themes = themes;
6d46a5a3 Poul Sander 2023-09-23 14:42 181
	for (auto& pair : background_data) {
2660ccea Poul Sander 2023-09-22 19:25 182
		tfd.background_data.push_back(pair.second);
081d847a Poul Sander 2023-07-29 19:59 183
	}
00759e5a Poul Sander 2023-09-24 20:01 184
	for (auto& pair : decoration_data) {
00759e5a Poul Sander 2023-09-24 20:01 185
		tfd.decoration_data.push_back(pair.second);
00759e5a Poul Sander 2023-09-24 20:01 186
	}
2660ccea Poul Sander 2023-09-22 19:25 187
	json j = tfd;
2660ccea Poul Sander 2023-09-22 19:25 188
	std::string s = j.dump(4);
2660ccea Poul Sander 2023-09-22 19:25 189
	sago::WriteFileContent("themes_dump.json", s);
081d847a Poul Sander 2023-07-29 19:59 190
}
081d847a Poul Sander 2023-07-29 19:59 191
aaba4f1e Poul Sander 2023-07-25 13:34 192
void InitThemes() {
aaba4f1e Poul Sander 2023-07-25 13:34 193
	if (initialized) {
aaba4f1e Poul Sander 2023-07-25 13:34 194
		return;
aaba4f1e Poul Sander 2023-07-25 13:34 195
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 196
	initialized = true;
081d847a Poul Sander 2023-07-29 19:59 197
	InitBackGroundData();
aaba4f1e Poul Sander 2023-07-25 13:34 198
	themes.resize(1);  //Add the default theme
081d847a Poul Sander 2023-07-29 19:59 199
	FillMissingFields(themes[0]);
7d88cb38 Poul Sander 2023-10-05 20:48 200
	const std::vector<std::string>& theme_files = sago::GetFileList("themes");
7d88cb38 Poul Sander 2023-10-05 20:48 201
	for (const std::string& filename : theme_files) {
7d88cb38 Poul Sander 2023-10-05 20:48 202
		if (boost::algorithm::ends_with(filename,".json")) {
7d88cb38 Poul Sander 2023-10-05 20:48 203
			ReadThemeDataFromFile("themes/"+filename);
7d88cb38 Poul Sander 2023-10-05 20:48 204
		}
7d88cb38 Poul Sander 2023-10-05 20:48 205
	}
2660ccea Poul Sander 2023-09-22 19:25 206
	DumpThemeData();
aaba4f1e Poul Sander 2023-07-25 13:34 207
}
aaba4f1e Poul Sander 2023-07-25 13:34 208
aaba4f1e Poul Sander 2023-07-25 13:34 209
Theme getNextTheme() {
aaba4f1e Poul Sander 2023-07-25 13:34 210
	InitThemes();
aaba4f1e Poul Sander 2023-07-25 13:34 211
	current_theme++;
aaba4f1e Poul Sander 2023-07-25 13:34 212
	current_theme = current_theme % themes.size();
aaba4f1e Poul Sander 2023-07-25 13:34 213
	return themes.at(current_theme);
aaba4f1e Poul Sander 2023-07-25 13:34 214
}
081d847a Poul Sander 2023-07-29 19:59 215
081d847a Poul Sander 2023-07-29 19:59 216
Theme getTheme(size_t theme_number) {
081d847a Poul Sander 2023-07-29 19:59 217
	InitThemes();
081d847a Poul Sander 2023-07-29 19:59 218
	return themes.at(theme_number % themes.size());
081d847a Poul Sander 2023-07-29 19:59 219
}
1970-01-01 00:00 220