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