This repository has been archived by the owner on Jun 7, 2018. It is now read-only.
/
MainLoop.cpp
executable file
·292 lines (250 loc) · 6.68 KB
/
MainLoop.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
/*
* Copyright (C) 2006-2013 Christopho, Solarus - http://www.solarus-games.org
*
* Solarus is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Solarus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MainLoop.h"
#include "lowlevel/System.h"
#include "lowlevel/VideoManager.h"
#include "lowlevel/Color.h"
#include "lowlevel/Surface.h"
#include "lowlevel/Music.h"
#include "lowlevel/FileTools.h"
#include "lowlevel/Debug.h"
#include "lua/LuaContext.h"
#include "QuestProperties.h"
#include "Game.h"
#include "Savegame.h"
#include "StringResource.h"
#include "DebugKeys.h"
/**
* @brief Initializes the game engine.
* @param argc number of arguments of the command line
* @param argv command-line arguments
*/
MainLoop::MainLoop(int argc, char** argv):
root_surface(NULL),
debug_keys(NULL),
lua_context(NULL),
exiting(false),
game(NULL),
next_game(NULL) {
// Initialize low-level features (audio, video, files...).
System::initialize(argc, argv);
// Read the quest general properties.
QuestProperties quest_properties(*this);
quest_properties.load();
root_surface = new Surface(SOLARUS_SCREEN_WIDTH, SOLARUS_SCREEN_HEIGHT);
root_surface->increment_refcount();
debug_keys = new DebugKeys(*this);
lua_context = new LuaContext(*this);
lua_context->initialize();
}
/**
* @brief Cleans everything.
*/
MainLoop::~MainLoop() {
delete lua_context;
root_surface->decrement_refcount();
delete root_surface;
delete debug_keys;
System::quit();
}
/**
* @brief Returns the debugging keys object.
* @return the debugging keys object
*/
DebugKeys& MainLoop::get_debug_keys() {
return *debug_keys;
}
/**
* @brief Returns the shared Lua context.
* @return The Lua context where all scripts are run.
*/
LuaContext& MainLoop::get_lua_context() {
return *lua_context;
}
/**
* @brief Returns whether the user just closed the window.
*
* When this function returns true, you should stop immediately
* whatever you are doing, free your 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 The main function.
*
* 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() {
// main loop
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
while (!is_exiting()) {
// handle the input events
event = InputEvent::get_event();
if (event != NULL) {
notify_input(*event);
delete event;
}
// update the current screen
update();
// 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 the next drawing
if (delay <= 0) { // it's time to redraw
// see if the FPS number is too high
if (just_redrawn && frame_interval <= 30) {
frame_interval += 5; // redraw the screen less often
//std::cout << "\rFPS: " << (1000 / frame_interval) << std::flush;
}
next_frame_date = now + frame_interval;
just_redrawn = true;
draw();
}
else {
just_redrawn = false;
// if we have time, let's sleep to avoid using all the processor
System::sleep(1);
if (delay >= 15) {
// if we have much time, increase the FPS number
frame_interval--;
//std::cout << "\rFPS: " << (1000 / frame_interval) << std::flush;
}
}
}
}
if (game != NULL) {
game->stop();
delete game;
game = NULL;
}
}
/**
* @brief This function is called when there is an input event.
*
* It handles the events common to all screens:
* closing the window, pressing F5 or a debug key.
* The notify_input() method 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 defined(PANDORA)
// TODO make a clean flag
if (event.get_keyboard_key() == InputEvent::KEY_ESCAPE) {
exiting = true;
}
#endif
}
// 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 Updates the current screen.
*
* This function is called repeatedly by the main loop.
*/
void MainLoop::update() {
debug_keys->update();
if (game != NULL) {
game->update();
}
lua_context->update();
System::update();
}
/**
* @brief 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);
}