-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.cpp
More file actions
375 lines (300 loc) · 7.23 KB
/
Application.cpp
File metadata and controls
375 lines (300 loc) · 7.23 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "Application.h"
Application::Application()
{
file_system = new ModuleFileSystem(this);
window = new ModuleWindow(this);
input = new ModuleInput(this);
audio = new ModuleAudio(this, true);
scene = new ModuleScene(this);
renderer3D = new ModuleRenderer3D(this);
camera = new ModuleCamera3D(this);
physics = new ModulePhysics3D(this);
geometry_loader = new ModuleGeometryLoader(this);
editor = new ModuleEditor(this);
// The order of calls is very important!
// Modules will Init() Start() and Update in this order
// They will CleanUp() in reverse order
// Main Modules
AddModule(file_system);
AddModule(window);
AddModule(camera);
AddModule(input);
AddModule(audio);
AddModule(physics);
AddModule(geometry_loader);
// Scenes
AddModule(scene);
// Renderer last!
AddModule(renderer3D);
AddModule(editor);
}
Application::~Application()
{
std::list<Module*>::reverse_iterator item = list_modules.rbegin();
while(item != list_modules.rend())
{
delete *item;
item++;
}
}
bool Application::Awake()
{
bool ret = false;
pugi::xml_document config_file;
pugi::xml_node config;
pugi::xml_node app_config;
config = LoadConfig(config_file);
if (config.empty() == false)
{
ret = true;
app_config = config.child("app");
title = app_config.child("title").child_value(DEFAULT_TITLE);
organization = app_config.child("organization").child_value("CITM");
max_fps = app_config.attribute("max_fps").as_int(120);
max_ms_per_frame = 1000.0f / max_fps;
std::list<Module*>::iterator item = list_modules.begin();
while (item != list_modules.end() && ret == true)
{
ret = (*item)->Awake(config.child((*item)->GetName().c_str()));
item++;
}
}
else
{
// self-config
ret = true;
title = DEFAULT_TITLE;
organization = "CITM";
max_fps = 120;
max_ms_per_frame = 1000.0f / max_fps;
}
return ret;
}
bool Application::Init()
{
bool ret = true;
want_to_save_config = false;
want_to_save_game = false;
game_save_file = nullptr;
frames = 0.0f;
ms_in_last_frame = 0.0f;
// Call Init() in all modules
std::list<Module*>::iterator item = list_modules.begin();
while(item != list_modules.end() && ret == true)
{
ret = (*item)->Init();
item++;
}
// After all Init calls we call Start() in all modules
LOG("Application Start --------------");
item = list_modules.begin();
while(item != list_modules.end() && ret == true)
{
ret = (*item)->Start();
item++;
}
ms_timer.Start();
fps_timer.Start();
return ret;
}
pugi::xml_node Application::LoadConfig(pugi::xml_document& config_file) const
{
pugi::xml_node ret;
char* buf;
int size = file_system->Load("config.xml", &buf);
if (size > 0)
{
pugi::xml_parse_result result = config_file.load_buffer(buf, size);
RELEASE(buf);
if (result == NULL)
{
LOG("Could not load map xml file config.xml. pugi error: %s", result.description());
}
else
ret = config_file.child("config");
}
return ret;
}
// ---------------------------------------------
void Application::PrepareUpdate()
{
dt = (float)ms_timer.Read() / 1000.0f;
ms_timer.Start();
}
// ---------------------------------------------
void Application::FinishUpdate()
{
if (want_to_save_config)
{
SaveConfigNow();
want_to_save_config = false;
}
if (want_to_save_game)
{
SaveGameNow();
want_to_save_game = false;
game_save_file = nullptr;
}
frames++;
if (fps_timer.Read() >= 1000)
{
editor->UpdateFpsLog(frames);
frames = 0;
fps_timer.Start();
}
ms_in_last_frame = ms_timer.Read();
if (ms_in_last_frame < max_ms_per_frame && max_ms_per_frame > 0)
SDL_Delay(max_ms_per_frame - ms_in_last_frame);
editor->UpdateMsLog(ms_in_last_frame);
}
// Call PreUpdate, Update and PostUpdate on all modules
update_status Application::Update()
{
update_status ret = UPDATE_CONTINUE;
PrepareUpdate();
std::list<Module*>::iterator item = list_modules.begin();
while(item != list_modules.end() && ret == UPDATE_CONTINUE)
{
ret = (*item)->PreUpdate(dt);
item++;
}
item = list_modules.begin();
while(item != list_modules.end() && ret == UPDATE_CONTINUE)
{
ret = (*item)->Update(dt);
item++;
}
item = list_modules.begin();
while(item != list_modules.end() && ret == UPDATE_CONTINUE)
{
ret = (*item)->PostUpdate(dt);
item++;
}
FinishUpdate();
return ret;
}
void Application::SetTitle(const char* _title)
{
SDL_SetWindowTitle(window->window, _title);
title = _title;
}
const char* Application::GetTitle() const
{
return (char*)title;
}
void Application::SetOrganization(const char* _organization)
{
SDL_SetWindowTitle(window->window, _organization);
organization = _organization;
}
const char* Application::GetOrganization() const
{
return (char*)organization;
}
int Application::GetMaxFps()
{
return max_fps;
}
void Application::SetMaxFps(int x)
{
if (x > 0 && x < 120)
max_fps = x;
else
max_fps = 120;
max_ms_per_frame = 1000 / (float)max_fps;
}
bool Application::CleanUp()
{
bool ret = true;
std::list<Module*>::reverse_iterator item = list_modules.rbegin();
while(item != list_modules.rend() && ret == true)
{
ret = (*item)->CleanUp();
item++;
}
return ret;
}
void Application::AddModule(Module* mod)
{
list_modules.push_back(mod);
}
void Application::OpenLink(char* path)
{
ShellExecuteA(0, "Open", path, 0, "", 3);
}
void Application::DebugDraw()
{
std::list<Module*>::iterator item = list_modules.begin();
while (item != list_modules.end())
{
(*item)->DebugDraw();
item++;
}
}
void Application::SaveConfig()
{
want_to_save_config = true;
}
void Application::SaveGame(const char* file)
{
want_to_save_game = true;
game_save_file = file;
}
bool Application::SaveConfigNow() const
{
bool ret = true;
LOG("Saving Config");
// xml object were we will store all data
pugi::xml_document data;
pugi::xml_node root;
root = data.append_child("config");
//save Application
pugi::xml_node cam = data.append_child("application");
cam.append_attribute("title") = title;
cam.append_attribute("organization") = organization;
cam.append_attribute("max_fps") = max_fps;
//save Modules
std::list<Module*>::const_iterator item = list_modules.begin();
for(item; item != list_modules.end(); item++)
{
ret = (*item)->SaveConfig(root.append_child((*item)->GetName().c_str()));
}
if (ret == true)
{
std::stringstream stream;
data.save(stream);
// we are done, so write data to disk
file_system->Save("config.xml", stream.str().c_str(), stream.str().length());
LOG("... finished saving config.xml");
}
else
LOG("Save config process halted from an error");
data.reset();
return ret;
}
bool Application::SaveGameNow() const
{
bool ret = true;
LOG("Saving Game State to %s...", game_save_file);
// xml object were we will store all data
pugi::xml_document data;
pugi::xml_node root;
root = data.append_child("game_state");
std::list<Module*>::const_iterator item = list_modules.begin();
for (item; item != list_modules.end(); item++)
{
ret = (*item)->SaveConfig(root.append_child((*item)->GetName().c_str()));
}
if (ret == true)
{
std::stringstream stream;
data.save(stream);
// we are done, so write data to disk
file_system->Save(game_save_file, stream.str().c_str(), stream.str().length());
LOG("... finished saving ", game_save_file);
}
else
LOG("Save game process halted from an error");
data.reset();
return ret;
}