-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
343 lines (316 loc) · 9.62 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "agent.h"
#include "agent_type.h"
#include "biology.h"
#include "game.h"
#include "sfml_title_screen.h"
#include "sfml_about_screen.h"
#include "sfml_gameover_screen.h"
#include "sfml_game.h"
#include "sfml_game_delegate.h"
#include "sfml_load_screen.h"
#include "sfml_resources.h"
#include "sfml_window_manager.h"
#include "tile.h"
#include "tile_id.h"
#include "sfml_text_input.h"
#include "sfml_scroll_box.h"
#include "sfml_load_screen.h"
#include "sound_type.h"
#include <QFile>
#include <typeinfo>
#include <SFML/Graphics.hpp>
#include <cassert>
/// Nature Zen
/// @param argc the number of arguments Nature Zen's executable is called
/// with by the operating system.
/// Arguments are:
/// * '--music': run with music
/// * '--short': only run for a couple of seconds
/// * '--short [n]': only run for [n] ticks
/// * '--title': show the title screen
/// * '--menu': show the menu screen
/// * '--about': show the about screen
/// * '--version': show the SFML version and quit
/// * '--spin': that's a secret...
/// * '--profiling': only run for a minute, for profiling
/// @param argv the arguments (as words) Nature Zen's executable is called
/// with by the operating system
/// All tests are called from here, only in debug mode
void test() {
test_biology();
test_agent();
test_tile_type();
test_tile();
test_agent_type();
test_sound_type();
test_tile_id();
//test_sfml_window_manager();
test_normal_char();
test_game();
test_game_state();
test_sfml_resources();
test_sfml_game();
test_sfml_game_delegate();
// test_sfml_load_screen();
}
///Start the game
///@param close_at_tick tick at which the game will be closed.
/// if close_at_tick equals minus one, the game runs indefinitely
///@param music will there be music?
///@param tiles starting tiles
///@param agents initial agents
///@param spawning will agents spawn?
///@param damage can agents damage one another?
///@param score is the game score being tracked? If not, the player will
/// never die
///@return closing status, which is zero if everything went OK
int start_sfml_game(
const int close_at_tick,
bool music,
bool sounds,
std::vector<tile> tiles,
std::vector<agent> agents,
bool spawning,
bool damage,
bool score
) {
//std::clog << "Create an sfml_game\n";
sfml_game g(sfml_game_delegate(close_at_tick, spawning, damage, score), tiles, agents);
if (!music) g.stop_music();
if (!sounds) g.stop_sounds();
//std::clog << "Execute an sfml_game\n";
g.exec();
//std::clog << "We're done with the sfml_game\n";
return 0;
}
int show_sfml_title_screen(int ca, bool music) {
sfml_title_screen ts(ca);
if (!music) ts.stop_music();
ts.exec();
return 0;
}
int show_sfml_menu_screen(int ca) {
sfml_menu_screen ms(ca);
ms.exec();
return 0;
}
int show_sfml_about_screen(int ca) {
sfml_about_screen as(ca);
as.exec();
return 0;
}
int show_sfml_gameover_screen(int ca) {
sfml_gameover_screen gos(ca);
gos.exec();
return 0;
}
int show_sfml_load_screen()
{
sfml_load_screen ls;
ls.exec();
return 0;
}
#include <functional>
void test_ref()
{
int a = 1;
std::vector<std::reference_wrapper<int>> v;
v.push_back(std::ref(a));
v.push_back(std::ref(a));
assert(v[0] == v[1]);
++v[0];
assert(v[0] == v[1]);
++v[1];
assert(v[0] == v[1]);
}
int main(int argc, char **argv) //!OCLINT main too long
{
//std::clog << "==========\n";
//std::clog << "Nature Zen\n";
//std::clog << "==========\n";
#ifndef NDEBUG
test_ref();
test();
#else
// In release mode, all asserts are removed from the code
assert(1 == 2);
#endif
const std::vector<std::string> args(argv, argv + argc);
//std::clog << "Get the user's name\n";
std::string user = "";
#ifdef WIN32
user = getenv("USERNAME");
#endif
#ifdef __linux__
user = system("whoami");
#endif
if (user != "") {
std::clog << "Current user: " << user << "\n" << std::endl;
}
//----------------------------------------------------------------------------
//Things with early exits
//----------------------------------------------------------------------------
//std::clog << "Processing CLI\n";
//Show the SFML version and quit
if (std::count(std::begin(args), std::end(args), "--version")) {
// Travis: 2.1
// RuG: 2.3.2
std::cout
<< "SFML version: " << SFML_VERSION_MAJOR
<< "." << SFML_VERSION_MINOR
#if(SFML_VERSION_MINOR > 1)
<< "." << SFML_VERSION_PATCH
#endif
<< std::endl
;
return 0; // 0: everything went OK
}
bool music = false;
bool sounds = false;
bool spawning = true;
bool damage = true;
bool score = true;
if (std::count(std::begin(args), std::end(args), "--no-music"))
{
music = true;
}
if (std::count(std::begin(args), std::end(args), "--sounds"))
{
sounds = true;
}
int close_at{-1};
if (std::count(std::begin(args), std::end(args), "--short"))
{
//std::clog << "Process '--short' CLI option\n";
close_at = 600;
assert(std::find(std::begin(args), std::end(args), "--short") != std::end(args));
if (std::find(std::begin(args), std::end(args), "--short") + 1 != std::end(args))
{
const std::string s{
*(std::find(std::begin(args), std::end(args), "--short") + 1)
};
if(s.at(0) != '-'){
close_at = std::atoi(s.c_str());
}
}
//The '--short' setting is used on Travis for debugging and
//should jump straight to the game
sfml_window_manager::get().set_state(game_state::playing);
}
else if (std::count(std::begin(args), std::end(args), "--profiling")){
//std::clog << "Process '--profiling' CLI option\n";
close_at = 10000;
sfml_window_manager::get().set_state(game_state::playing);
}
else if (std::count(std::begin(args), std::end(args), "--title"))
{
//std::clog << "Process '--title' CLI option\n";
sfml_window_manager::get().set_state(game_state::titlescreen);
}
else if (std::count(std::begin(args), std::end(args), "--menu"))
{
//std::clog << "Process '--menu' CLI option\n";
sfml_window_manager::get().set_state(game_state::menuscreen);
}
else if (std::count(std::begin(args), std::end(args), "--about"))
{
//std::clog << "Process '--about' CLI option\n";
sfml_window_manager::get().set_state(game_state::aboutscreen);
}
else if (std::count(std::begin(args), std::end(args), "--game-over") ||
std::count(std::begin(args), std::end(args), "--gameover")) {
//std::clog << "Process '--gameover' CLI option\n";
sfml_window_manager::get().set_state(game_state::gameover);
}
else if (std::count(std::begin(args), std::end(args), "--paused"))
{
//std::clog << "Process '--paused' CLI option\n";
sfml_window_manager::get().set_state(game_state::paused);
}
else if (std::count(std::begin(args), std::end(args), "--save"))
{
//std::clog << "Process '--save' CLI option\n";
sfml_window_manager::get().set_state(game_state::saving);
}
//std::clog << "Processed all CLI options\n";
//Not realy to show settings, but to use the variables
std::cout << "Settings\n"
<< "Close at : " << close_at << "\n"
<< "Music : " << music << "\n"
<< "Sounds : " << sounds << std::endl;
std::vector<tile> tiles;
std::vector<agent> agents;
// if (std::count(std::begin(args), std::end(args), "--spin"))
// {
// tiles.push_back(tile(0,-112,0,90,0,tile_type::grassland));
// agents.push_back(agent(agent_type::spider,50));
// }
if(std::count(std::begin(args), std::end(args), "--profiling")) {
int agents_size = 10;
int tiles_size = 10;
if (std::find(std::begin(args), std::end(args), "--profiling") + 1 != std::end(args))
{
const std::string s{
*(std::find(std::begin(args), std::end(args), "--profiling") + 1)
};
if(!s.empty() && s.at(0) != '-'){
agents_size = std::atoi(s.c_str());
}
}
if (std::find(std::begin(args), std::end(args), "--profiling") + 2 != std::end(args))
{
const std::string s{
*(std::find(std::begin(args), std::end(args), "--profiling") + 2)
};
if(!s.empty() && s.at(0) != '-'){
tiles_size = std::atoi(s.c_str());
}
}
for(int i = 0; i < agents_size; i++){
int type = random_int(1, 21);
agent a(random_agent_type(type), i, i);
agents.push_back(a);
}
for(int i = 0; i < tiles_size; i++){
tile t(i * 112, i * 112, 0, 90, 0, tile_type::grassland);
tiles.push_back(t);
}
spawning = false;
damage = false;
score = false;
}
else if(std::count(std::begin(args), std::end(args), "--god")) {
//std::clog << "Process '--god' CLI option\n";
score = false;
tiles = create_test_default_tiles();
agents = create_default_agents();
}
//A window should have opened up now
assert(sfml_window_manager::get().get_window().isOpen());
while (sfml_window_manager::get().get_window().isOpen()) {
std::clog << "State: " << sfml_window_manager::get().get_state() << '\n';
switch (sfml_window_manager::get().get_state()) {
case game_state::titlescreen:
show_sfml_title_screen(close_at, music);
break;
case game_state::menuscreen:
show_sfml_menu_screen(close_at);
break;
case game_state::aboutscreen:
show_sfml_about_screen(close_at);
break;
case game_state::saving:
case game_state::paused:
case game_state::shop:
case game_state::playing:
start_sfml_game(close_at, music, sounds, tiles, agents, spawning, damage, score);
break;
case game_state::gameover:
show_sfml_gameover_screen(-1);
break;
case game_state::loading:
show_sfml_load_screen();
break;
}
}
}