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