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"
2660ccea Poul Sander 2023-09-22 19:25 30
2660ccea Poul Sander 2023-09-22 19:25 31
using json = nlohmann::json;
2660ccea Poul Sander 2023-09-22 19:25 32
2660ccea Poul Sander 2023-09-22 19:25 33
2660ccea Poul Sander 2023-09-22 19:25 34
void to_json(json& j, const BackGroundData& p) {
173b1d55 Poul Sander 2023-09-23 21:29 35
	j = json{ {"background_name", p.name}, {"background_sprite", p.background_sprite} };
2660ccea Poul Sander 2023-09-22 19:25 36
}
2660ccea Poul Sander 2023-09-22 19:25 37
2660ccea Poul Sander 2023-09-22 19:25 38
void to_json(json& j, const Theme& p) {
173b1d55 Poul Sander 2023-09-23 21:29 39
	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 40
}
2660ccea Poul Sander 2023-09-22 19:25 41
00759e5a Poul Sander 2023-09-24 20:01 42
void to_json(json& j, const DecorationData& p) {
00759e5a Poul Sander 2023-09-24 20:01 43
	j = json{ {"name", p.name}, {"decoration_sprites", p.decoration_sprites} };
00759e5a Poul Sander 2023-09-24 20:01 44
}
00759e5a Poul Sander 2023-09-24 20:01 45
2660ccea Poul Sander 2023-09-22 19:25 46
void to_json(json& j, const ThemeFileData& p) {
00759e5a Poul Sander 2023-09-24 20:01 47
	j = json{ {"background_data", p.background_data}, {"decoration_data", p.decoration_data}, {"themes", p.themes} };
2660ccea Poul Sander 2023-09-22 19:25 48
}
2660ccea Poul Sander 2023-09-22 19:25 49
2660ccea Poul Sander 2023-09-22 19:25 50
2660ccea Poul Sander 2023-09-22 19:25 51
aaba4f1e Poul Sander 2023-07-25 13:34 52
aaba4f1e Poul Sander 2023-07-25 13:34 53
static std::vector<Theme> themes(1);
00759e5a Poul Sander 2023-09-24 20:01 54
static std::map<std::string, BackGroundData> background_data;
00759e5a Poul Sander 2023-09-24 20:01 55
static std::map<std::string, DecorationData> decoration_data;
aaba4f1e Poul Sander 2023-07-25 13:34 56
static bool initialized = false;
aaba4f1e Poul Sander 2023-07-25 13:34 57
static size_t current_theme = 0;
aaba4f1e Poul Sander 2023-07-25 13:34 58
2660ccea Poul Sander 2023-09-22 19:25 59
081d847a Poul Sander 2023-07-29 19:59 60
static void InitBackGroundData() {
081d847a Poul Sander 2023-07-29 19:59 61
	BackGroundData standard;
173b1d55 Poul Sander 2023-09-23 21:29 62
	standard.name = "standard";
081d847a Poul Sander 2023-07-29 19:59 63
	standard.background_sprite = "background";
081d847a Poul Sander 2023-07-29 19:59 64
	standard.background_sprite_16x9 = "background_sixteen_nine";
081d847a Poul Sander 2023-07-29 19:59 65
	standard.background_scale = ImgScale::Stretch;
00759e5a Poul Sander 2023-09-24 20:01 66
	background_data[standard.name] = standard;
081d847a Poul Sander 2023-07-29 19:59 67
	BackGroundData alt_background;
173b1d55 Poul Sander 2023-09-23 21:29 68
	alt_background.name = "alt_background";
081d847a Poul Sander 2023-07-29 19:59 69
	alt_background.background_sprite = "background_sample";
081d847a Poul Sander 2023-07-29 19:59 70
	alt_background.background_sprite_16x9 = "";
081d847a Poul Sander 2023-07-29 19:59 71
	alt_background.background_scale = ImgScale::Tile;
6444412c Poul Sander 2023-07-30 22:07 72
	alt_background.tileMoveSpeedX = 50;
6444412c Poul Sander 2023-07-30 22:07 73
	alt_background.tileMoveSpeedY = 100;
00759e5a Poul Sander 2023-09-24 20:01 74
	background_data[alt_background.name] = alt_background;
00759e5a Poul Sander 2023-09-24 20:01 75
	DecorationData smileys;
00759e5a Poul Sander 2023-09-24 20:01 76
	decoration_data[smileys.name] = smileys;
081d847a Poul Sander 2023-07-29 19:59 77
}
081d847a Poul Sander 2023-07-29 19:59 78
6d46a5a3 Poul Sander 2023-09-23 14:42 79
static void FillMissingFields(Theme& theme) {
173b1d55 Poul Sander 2023-09-23 21:29 80
	if (theme.background.name.empty()) {
081d847a Poul Sander 2023-07-29 19:59 81
		//If the theme does not define a background then use the standard.
173b1d55 Poul Sander 2023-09-23 21:29 82
		theme.background.name = "standard";
2660ccea Poul Sander 2023-09-22 19:25 83
	}
173b1d55 Poul Sander 2023-09-23 21:29 84
	theme.background = background_data[theme.background.name];
2660ccea Poul Sander 2023-09-22 19:25 85
}
2660ccea Poul Sander 2023-09-22 19:25 86
2660ccea Poul Sander 2023-09-22 19:25 87
2660ccea Poul Sander 2023-09-22 19:25 88
void DumpThemeData() {
2660ccea Poul Sander 2023-09-22 19:25 89
	ThemeFileData tfd;
2660ccea Poul Sander 2023-09-22 19:25 90
	tfd.themes = themes;
6d46a5a3 Poul Sander 2023-09-23 14:42 91
	for (auto& pair : background_data) {
2660ccea Poul Sander 2023-09-22 19:25 92
		tfd.background_data.push_back(pair.second);
081d847a Poul Sander 2023-07-29 19:59 93
	}
00759e5a Poul Sander 2023-09-24 20:01 94
	for (auto& pair : decoration_data) {
00759e5a Poul Sander 2023-09-24 20:01 95
		tfd.decoration_data.push_back(pair.second);
00759e5a Poul Sander 2023-09-24 20:01 96
	}
2660ccea Poul Sander 2023-09-22 19:25 97
	json j = tfd;
2660ccea Poul Sander 2023-09-22 19:25 98
	std::string s = j.dump(4);
2660ccea Poul Sander 2023-09-22 19:25 99
	sago::WriteFileContent("themes_dump.json", s);
081d847a Poul Sander 2023-07-29 19:59 100
}
081d847a Poul Sander 2023-07-29 19:59 101
aaba4f1e Poul Sander 2023-07-25 13:34 102
void InitThemes() {
aaba4f1e Poul Sander 2023-07-25 13:34 103
	if (initialized) {
aaba4f1e Poul Sander 2023-07-25 13:34 104
		return;
aaba4f1e Poul Sander 2023-07-25 13:34 105
	}
081d847a Poul Sander 2023-07-29 19:59 106
	InitBackGroundData();
aaba4f1e Poul Sander 2023-07-25 13:34 107
	themes.resize(1);  //Add the default theme
081d847a Poul Sander 2023-07-29 19:59 108
	FillMissingFields(themes[0]);
aaba4f1e Poul Sander 2023-07-25 13:34 109
	Theme snow;
aaba4f1e Poul Sander 2023-07-25 13:34 110
	snow.theme_name = "snow";
aaba4f1e Poul Sander 2023-07-25 13:34 111
	snow.back_board = "back_board_sample_snow";
173b1d55 Poul Sander 2023-09-23 21:29 112
	snow.background.name = "alt_background";
081d847a Poul Sander 2023-07-29 19:59 113
	FillMissingFields(snow);
aaba4f1e Poul Sander 2023-07-25 13:34 114
	themes.push_back(snow);
2660ccea Poul Sander 2023-09-22 19:25 115
	DumpThemeData();
aaba4f1e Poul Sander 2023-07-25 13:34 116
}
aaba4f1e Poul Sander 2023-07-25 13:34 117
aaba4f1e Poul Sander 2023-07-25 13:34 118
Theme getNextTheme() {
aaba4f1e Poul Sander 2023-07-25 13:34 119
	InitThemes();
aaba4f1e Poul Sander 2023-07-25 13:34 120
	current_theme++;
aaba4f1e Poul Sander 2023-07-25 13:34 121
	current_theme = current_theme % themes.size();
aaba4f1e Poul Sander 2023-07-25 13:34 122
	return themes.at(current_theme);
aaba4f1e Poul Sander 2023-07-25 13:34 123
}
081d847a Poul Sander 2023-07-29 19:59 124
081d847a Poul Sander 2023-07-29 19:59 125
Theme getTheme(size_t theme_number) {
081d847a Poul Sander 2023-07-29 19:59 126
	InitThemes();
081d847a Poul Sander 2023-07-29 19:59 127
	return themes.at(theme_number % themes.size());
081d847a Poul Sander 2023-07-29 19:59 128
}
1970-01-01 00:00 129