/
screen_manager.cpp
337 lines (294 loc) · 8.3 KB
/
screen_manager.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
// SuperTux
// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
//
// This program 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.
//
// This program 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 "supertux/screen_manager.hpp"
#include "audio/sound_manager.hpp"
#include "control/input_manager.hpp"
#include "gui/menu.hpp"
#include "gui/menu_manager.hpp"
#include "scripting/squirrel_util.hpp"
#include "scripting/time_scheduler.hpp"
#include "supertux/console.hpp"
#include "supertux/constants.hpp"
#include "supertux/gameconfig.hpp"
#include "supertux/globals.hpp"
#include "supertux/main.hpp"
#include "supertux/menu/menu_storage.hpp"
#include "supertux/player_status.hpp"
#include "supertux/resources.hpp"
#include "supertux/screen.hpp"
#include "supertux/screen_fade.hpp"
#include "supertux/timer.hpp"
#include "video/drawing_context.hpp"
#include "video/renderer.hpp"
#include <stdio.h>
/** ticks (as returned from SDL_GetTicks) per frame */
static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS);
/** don't skip more than every 2nd frame */
static const int MAX_FRAME_SKIP = 2;
ScreenManager::ScreenManager() :
waiting_threads(),
m_menu_storage(new MenuStorage),
m_menu_manager(new MenuManager),
running(),
speed(1.0),
nextpop(false),
nextpush(false),
fps(0),
next_screen(),
current_screen(),
console(),
screen_fade(),
screen_stack(),
screenshot_requested(false)
{
using namespace scripting;
TimeScheduler::instance = new TimeScheduler();
}
ScreenManager::~ScreenManager()
{
using namespace scripting;
delete TimeScheduler::instance;
TimeScheduler::instance = NULL;
}
void
ScreenManager::push_screen(std::unique_ptr<Screen> screen, std::unique_ptr<ScreenFade> screen_fade)
{
assert(!this->next_screen);
this->next_screen = std::move(screen);
this->screen_fade = std::move(screen_fade);
nextpush = !nextpop;
nextpop = false;
speed = 1.0f;
}
void
ScreenManager::exit_screen(std::unique_ptr<ScreenFade> screen_fade)
{
next_screen.reset();
this->screen_fade = std::move(screen_fade);
nextpop = true;
nextpush = false;
}
void
ScreenManager::set_screen_fade(std::unique_ptr<ScreenFade> screen_fade)
{
this->screen_fade = std::move(screen_fade);
}
void
ScreenManager::quit(std::unique_ptr<ScreenFade> screen_fade)
{
screen_stack.clear();
exit_screen(std::move(screen_fade));
}
void
ScreenManager::set_speed(float speed)
{
this->speed = speed;
}
float
ScreenManager::get_speed() const
{
return speed;
}
bool
ScreenManager::has_no_pending_fadeout() const
{
return screen_fade.get() == NULL || screen_fade->done();
}
void
ScreenManager::draw_fps(DrawingContext& context, float fps_fps)
{
char str[60];
snprintf(str, sizeof(str), "%3.1f", fps_fps);
const char* fpstext = "FPS";
context.draw_text(Resources::small_font, fpstext,
Vector(SCREEN_WIDTH - Resources::small_font->get_text_width(fpstext) - Resources::small_font->get_text_width(" 99999") - BORDER_X,
BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD);
context.draw_text(Resources::small_font, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
}
void
ScreenManager::draw(DrawingContext& context)
{
static Uint32 fps_ticks = SDL_GetTicks();
static int frame_count = 0;
current_screen->draw(context);
if(MenuManager::instance().current() != NULL)
MenuManager::instance().current()->draw(context);
if(screen_fade.get() != NULL)
screen_fade->draw(context);
Console::instance->draw(context);
if(g_config->show_fps)
draw_fps(context, fps);
// if a screenshot was requested, pass request on to drawing_context
if (screenshot_requested) {
context.take_screenshot();
screenshot_requested = false;
}
context.do_drawing();
/* Calculate frames per second */
if(g_config->show_fps)
{
++frame_count;
if(SDL_GetTicks() - fps_ticks >= 500)
{
fps = (float) frame_count / .5;
frame_count = 0;
fps_ticks = SDL_GetTicks();
}
}
}
void
ScreenManager::update_gamelogic(float elapsed_time)
{
scripting::update_debugger();
scripting::TimeScheduler::instance->update(game_time);
current_screen->update(elapsed_time);
if (MenuManager::instance().current() != NULL)
MenuManager::instance().current()->update();
if(screen_fade.get() != NULL)
screen_fade->update(elapsed_time);
Console::instance->update(elapsed_time);
}
void
ScreenManager::process_events()
{
g_input_manager->update();
SDL_Event event;
while(SDL_PollEvent(&event))
{
g_input_manager->process_event(event);
if(MenuManager::instance().current() != NULL)
MenuManager::instance().current()->event(event);
switch(event.type)
{
case SDL_QUIT:
quit();
break;
case SDL_WINDOWEVENT:
switch(event.window.event)
{
case SDL_WINDOWEVENT_RESIZED:
Renderer::instance()->resize(event.window.data1,
event.window.data2);
MenuManager::instance().recalc_pos();
break;
}
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_F10)
{
g_config->show_fps = !g_config->show_fps;
}
if (event.key.keysym.sym == SDLK_F11)
{
g_config->use_fullscreen = !g_config->use_fullscreen;
Renderer::instance()->apply_config();
MenuManager::instance().recalc_pos();
}
else if (event.key.keysym.sym == SDLK_PRINTSCREEN ||
event.key.keysym.sym == SDLK_F12)
{
take_screenshot();
}
else if (event.key.keysym.sym == SDLK_F1 &&
event.key.keysym.mod & KMOD_CTRL)
{
Console::instance->toggle();
g_config->console_enabled = true;
g_config->save();
}
break;
}
}
}
void
ScreenManager::handle_screen_switch()
{
while((next_screen || nextpop) &&
has_no_pending_fadeout())
{
if(current_screen) {
current_screen->leave();
}
if(nextpop) {
if(screen_stack.empty()) {
running = false;
break;
}
next_screen = std::move(screen_stack.back());
screen_stack.pop_back();
}
if(nextpush && current_screen) {
screen_stack.push_back(std::move(current_screen));
}
nextpush = false;
nextpop = false;
speed = 1.0;
current_screen = std::move(next_screen);
if(current_screen)
current_screen->setup();
screen_fade.reset();
waiting_threads.wakeup();
}
}
void
ScreenManager::run(DrawingContext &context)
{
Uint32 last_ticks = 0;
Uint32 elapsed_ticks = 0;
running = true;
while(running) {
handle_screen_switch();
if(!running || !current_screen)
break;
Uint32 ticks = SDL_GetTicks();
elapsed_ticks += ticks - last_ticks;
last_ticks = ticks;
Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * g_game_speed);
if (elapsed_ticks > ticks_per_frame*4) {
// when the game loads up or levels are switched the
// elapsed_ticks grows extremely large, so we just ignore those
// large time jumps
elapsed_ticks = 0;
}
if(elapsed_ticks < ticks_per_frame)
{
Uint32 delay_ticks = ticks_per_frame - elapsed_ticks;
SDL_Delay(delay_ticks);
last_ticks += delay_ticks;
elapsed_ticks += delay_ticks;
}
int frames = 0;
while(elapsed_ticks >= ticks_per_frame && frames < MAX_FRAME_SKIP)
{
elapsed_ticks -= ticks_per_frame;
float timestep = 1.0 / LOGICAL_FPS;
real_time += timestep;
timestep *= speed;
game_time += timestep;
process_events();
update_gamelogic(timestep);
frames += 1;
}
draw(context);
sound_manager->update();
}
}
void
ScreenManager::take_screenshot()
{
screenshot_requested = true;
}
/* EOF */