2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "supertux/screen_manager.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "control/input_manager.hpp"
21 #include "gui/menu.hpp"
22 #include "gui/menu_manager.hpp"
23 #include "scripting/squirrel_util.hpp"
24 #include "scripting/time_scheduler.hpp"
25 #include "supertux/console.hpp"
26 #include "supertux/constants.hpp"
27 #include "supertux/gameconfig.hpp"
28 #include "supertux/globals.hpp"
29 #include "supertux/main.hpp"
30 #include "supertux/menu/menu_storage.hpp"
31 #include "supertux/player_status.hpp"
32 #include "supertux/resources.hpp"
33 #include "supertux/screen.hpp"
34 #include "supertux/screen_fade.hpp"
35 #include "supertux/timer.hpp"
36 #include "video/drawing_context.hpp"
37 #include "video/renderer.hpp"
40 /** ticks (as returned from SDL_GetTicks) per frame */
41 static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS);
42 /** don't skip more than every 2nd frame */
43 static const int MAX_FRAME_SKIP = 2;
45 ScreenManager::ScreenManager() :
47 m_menu_storage(new MenuStorage),
48 m_menu_manager(new MenuManager),
55 m_screenshot_requested(false)
57 using namespace scripting;
58 TimeScheduler::instance = new TimeScheduler();
61 ScreenManager::~ScreenManager()
63 using namespace scripting;
64 delete TimeScheduler::instance;
65 TimeScheduler::instance = NULL;
69 ScreenManager::push_screen(std::unique_ptr<Screen> screen, std::unique_ptr<ScreenFade> screen_fade)
71 log_debug << "ScreenManager::push_screen(): " << screen.get() << std::endl;
74 if (m_action == PUSH_ACTION)
76 assert(m_next_screen);
77 // this happens for example when a Screen::setup() calls
78 // push_screen() itself, i.e. GameSessions/LevelIntro, in that
79 // case just commit the last action directly to the stack
80 m_screen_stack.push_back(std::move(m_next_screen));
84 assert(m_action == NO_ACTION || m_action == POP_ACTION);
86 m_next_screen = std::move(screen);
87 m_screen_fade = std::move(screen_fade);
89 if (m_action == POP_ACTION)
91 m_action = REPLACE_ACTION;
95 m_action = PUSH_ACTION;
101 ScreenManager::pop_screen(std::unique_ptr<ScreenFade> screen_fade)
103 log_debug << "ScreenManager::pop_screen(): stack_size: " << m_screen_stack.size() << std::endl;
104 assert(m_action == NO_ACTION);
106 m_next_screen.reset();
107 m_screen_fade = std::move(screen_fade);
108 m_action = POP_ACTION;
112 ScreenManager::set_screen_fade(std::unique_ptr<ScreenFade> screen_fade)
114 m_screen_fade = std::move(screen_fade);
118 ScreenManager::quit(std::unique_ptr<ScreenFade> screen_fade)
120 m_screen_fade = std::move(screen_fade);
121 m_action = QUIT_ACTION;
125 ScreenManager::set_speed(float speed)
131 ScreenManager::get_speed() const
137 ScreenManager::draw_fps(DrawingContext& context, float fps_fps)
140 snprintf(str, sizeof(str), "%3.1f", fps_fps);
141 const char* fpstext = "FPS";
142 context.draw_text(Resources::small_font, fpstext,
143 Vector(SCREEN_WIDTH - Resources::small_font->get_text_width(fpstext) - Resources::small_font->get_text_width(" 99999") - BORDER_X,
144 BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD);
145 context.draw_text(Resources::small_font, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
149 ScreenManager::draw(DrawingContext& context)
151 assert(!m_screen_stack.empty());
153 static Uint32 fps_ticks = SDL_GetTicks();
154 static int frame_count = 0;
156 m_screen_stack.back()->draw(context);
157 m_menu_manager->draw(context);
161 m_screen_fade->draw(context);
164 Console::instance->draw(context);
166 if (g_config->show_fps)
168 draw_fps(context, m_fps);
171 // if a screenshot was requested, pass request on to drawing_context
172 if (m_screenshot_requested)
174 context.take_screenshot();
175 m_screenshot_requested = false;
177 context.do_drawing();
179 /* Calculate frames per second */
180 if (g_config->show_fps)
184 if (SDL_GetTicks() - fps_ticks >= 500)
186 m_fps = (float) frame_count / .5;
188 fps_ticks = SDL_GetTicks();
194 ScreenManager::update_gamelogic(float elapsed_time)
196 scripting::update_debugger();
197 scripting::TimeScheduler::instance->update(game_time);
199 if (!m_screen_stack.empty())
201 m_screen_stack.back()->update(elapsed_time);
204 m_menu_manager->process_input();
208 m_screen_fade->update(elapsed_time);
211 Console::instance->update(elapsed_time);
215 ScreenManager::process_events()
217 g_input_manager->update();
219 while (SDL_PollEvent(&event))
221 g_input_manager->process_event(event);
223 m_menu_manager->event(event);
231 case SDL_WINDOWEVENT:
232 switch(event.window.event)
234 case SDL_WINDOWEVENT_RESIZED:
235 Renderer::instance()->resize(event.window.data1,
237 m_menu_manager->on_window_resize();
243 if (event.key.keysym.sym == SDLK_F10)
245 g_config->show_fps = !g_config->show_fps;
247 else if (event.key.keysym.sym == SDLK_F11)
249 g_config->use_fullscreen = !g_config->use_fullscreen;
250 Renderer::instance()->apply_config();
251 m_menu_manager->on_window_resize();
253 else if (event.key.keysym.sym == SDLK_PRINTSCREEN ||
254 event.key.keysym.sym == SDLK_F12)
258 else if (event.key.keysym.sym == SDLK_F1 &&
259 event.key.keysym.mod & KMOD_CTRL)
261 Console::instance->toggle();
262 g_config->console_enabled = true;
271 ScreenManager::has_pending_fadeout() const
273 return m_screen_fade && !m_screen_fade->done();
277 ScreenManager::handle_screen_switch()
279 if (m_action != NO_ACTION && !has_pending_fadeout())
281 if (m_action == POP_ACTION)
283 assert(!m_screen_stack.empty());
284 m_action = NO_ACTION;
286 m_screen_stack.back()->leave();
287 m_screen_stack.pop_back();
289 if (!m_screen_stack.empty())
291 m_screen_stack.back()->setup();
294 else if (m_action == PUSH_ACTION)
296 assert(m_next_screen);
297 m_action = NO_ACTION;
299 if (!m_screen_stack.empty())
301 m_screen_stack.back()->leave();
304 m_screen_stack.push_back(std::move(m_next_screen));
305 m_screen_stack.back()->setup();
307 else if (m_action == REPLACE_ACTION)
309 assert(!m_screen_stack.empty());
310 assert(!m_next_screen);
311 m_action = NO_ACTION;
313 m_screen_stack.back()->leave();
314 m_screen_stack.pop_back();
316 m_screen_stack.push_back(std::move(m_next_screen));
317 m_screen_stack.back()->setup();
319 else if (m_action == QUIT_ACTION)
321 m_screen_stack.clear();
326 m_screen_fade.reset();
328 m_waiting_threads.wakeup();
333 ScreenManager::run(DrawingContext &context)
335 Uint32 last_ticks = 0;
336 Uint32 elapsed_ticks = 0;
338 assert(m_action == PUSH_ACTION);
339 m_screen_stack.push_back(std::move(m_next_screen));
340 m_action = NO_ACTION;
342 while (!m_screen_stack.empty())
344 Uint32 ticks = SDL_GetTicks();
345 elapsed_ticks += ticks - last_ticks;
348 Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * g_game_speed);
350 if (elapsed_ticks > ticks_per_frame*4)
352 // when the game loads up or levels are switched the
353 // elapsed_ticks grows extremely large, so we just ignore those
358 if (elapsed_ticks < ticks_per_frame)
360 Uint32 delay_ticks = ticks_per_frame - elapsed_ticks;
361 SDL_Delay(delay_ticks);
362 last_ticks += delay_ticks;
363 elapsed_ticks += delay_ticks;
368 while (elapsed_ticks >= ticks_per_frame && frames < MAX_FRAME_SKIP)
370 elapsed_ticks -= ticks_per_frame;
371 float timestep = 1.0 / LOGICAL_FPS;
372 real_time += timestep;
374 game_time += timestep;
377 update_gamelogic(timestep);
381 if (!m_screen_stack.empty())
386 sound_manager->update();
388 handle_screen_switch();
393 ScreenManager::take_screenshot()
395 m_screenshot_requested = true;