-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainLoop.cpp
More file actions
261 lines (229 loc) · 5.14 KB
/
MainLoop.cpp
File metadata and controls
261 lines (229 loc) · 5.14 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
/** @file MainLoop.cpp
*
* Still need to implement get_debug_keys(), get_lua_context(), is_resetting()
* set_resetting(), get_game(), set_game(), run(), notify_input(), update(), draw() */
#include "MainLoop.h"
#include "System.h"
#include "VideoManager.h"
#include "Color.h"
#include "Surface.h"
#include "LuaContext.h"
#include "QuestProperties.h"
#include "QuestResourceList.h"
/** @brief Missing debug_keys */
MainLoop::MainLoop(int argc, char** argv): root_surface(NULL), lua_context(NULL), exiting(false), game(NULL), next_game(NULL)
{
System::initialize(argc, argv);
//Read the general properties of the quest
QuestProperties quest_properties(*this);
quest_properties.load();
//Read the quest resource list from the file project_db.dat
QuestResourceList::initialize();
root_surface = new Surface(KQ_SCREEN_WIDTH, KQ_SCREEN_HEIGHT);
root_surface->increment_refcount();
lua_context = new LuaContext(*this);
lua_context->initialize();
}
/** @brief Destructor missing lua_context, debug_keys */
MainLoop::~MainLoop()
{
delete lua_context;
root_surface->decrement_refcount();
delete root_surface;
QuestResourceList::quit();
System::quit();
}
/** @brief Returns whether the user just closed the window
*
* When this function returns true, stop everything immediately
* free memory and let the program quit.
*
* @return true if the user wants to exit the program */
bool MainLoop::is_exiting()
{
return exiting;
}
/** @brief Sets whether the user wants to quit the program. */
void MainLoop::set_exiting()
{
// Stop the program.
exiting = true;
}
/**
* @brief Returns whether the program is being reset.
*/
bool MainLoop::is_resetting()
{
return game != NULL && next_game == NULL;
}
/**
* @brief Marks the current game as finished and sets the initial screen
* to be started at the next cycle.
*/
void MainLoop::set_resetting()
{
// Reset the program.
set_game(NULL);
}
/**
* @brief Returns the current game if any.
* @return The game currently running or NULL.
*/
Game* MainLoop::get_game()
{
return game;
}
/**
* @brief Changes the game.
* @param game The new game to start, or NULL to start no game.
*/
void MainLoop::set_game(Game* game)
{
if (this->game != NULL)
{
//this->game->stop();
}
this->next_game = game;
}
/** @brief Need LuaContext
*
* The main loop is executed here. The input events are forwarded
* to the current screen. The current screen is redrawn when necessary. */
void MainLoop::run()
{
InputEvent* event;
uint32_t now;
uint32_t next_frame_date = System::now();
uint32_t frame_interval = 25; //time interval between two drawings
int delay;
bool just_redrawn = false; //to detect when the FPS number needs to be decreased
//main loop
while(!is_exiting())
{
//handle input events
event = InputEvent::get_event();
if(event != NULL)
{
notify_input(*event);
delete event;
}
//update the current screen
update();
/*//Add code to check if user wants to go to another game
if(next_game != game)
{
if(game != NULL)
{
delete game;
}
game = next_game;
if(game != NULL)
{
game->start();
}
else
{
//lua_context->exit();
//lua_context->initialize();
//Music::play(Music::none);
}
}
else
{*/
now = System::now();
delay = next_frame_date - now;
//delay is the time remaining before next drawing
if(delay <= 0)
{
//time to redraw
if(just_redrawn && frame_interval <= 30)
{
//redraw less often
frame_interval += 5;
}
next_frame_date = now + frame_interval;
just_redrawn = true;
draw();
}
else
{
just_redrawn = false;
//sleep, if there's time
System::sleep(1);
if(delay >= 15)
{
//increase the FPS if there's a lot of time
frame_interval--;
}
}
//}
}
/*
if(game != NULL)
{
game->stop();
delete game;
game = NULL;
}*/
}
/** @brief Needs Game.
*
* It handles the events common to all screens:
* closing the window, pressing F5 or a debug key.
* The notify_input() mehtod of the current screen
* is then called. */
void MainLoop::notify_input(InputEvent& event)
{
if(event.is_window_closing())
{
exiting = true;
}
else if(event.is_keyboard_key_pressed())
{
//a key was pressed
if(event.get_keyboard_key() == InputEvent::KEY_ESCAPE)
{
exiting = true;
}
}
//send the event to Lua and to the current screen
bool handled = lua_context->notify_input(event);
/*if(!handled && game != NULL)
{
game->notify_input(event);
}*/
}
/** @brief Needs Game */
void MainLoop::update()
{
//'debug_keys->update();
if(game != NULL)
{
//game->update();
}
lua_context->update();
System::update();
}
/**
* @brief Needs Game. Redraws the current screen.
*
* This function is called repeatedly by the main loop.
*/
void MainLoop::draw()
{
root_surface->fill_with_color(Color::get_black());
if (game != NULL)
{
// game->draw(*root_surface);
}
lua_context->main_on_draw(*root_surface);
VideoManager::get_instance()->draw(*root_surface);
}
/**
* \brief Returns the shared Lua context.
* \return The Lua context where all scripts are run.
*/
LuaContext& MainLoop::get_lua_context()
{
return *lua_context;
}