2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 // 2014 Ingo Ruhnke <grumbel@gmail.com>
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "supertux/screen_manager.hpp"
20 #include "audio/sound_manager.hpp"
21 #include "control/input_manager.hpp"
22 #include "gui/menu.hpp"
23 #include "gui/menu_manager.hpp"
24 #include "scripting/squirrel_util.hpp"
25 #include "scripting/time_scheduler.hpp"
26 #include "supertux/console.hpp"
27 #include "supertux/constants.hpp"
28 #include "supertux/gameconfig.hpp"
29 #include "supertux/globals.hpp"
30 #include "supertux/main.hpp"
31 #include "supertux/menu/menu_storage.hpp"
32 #include "supertux/player_status.hpp"
33 #include "supertux/resources.hpp"
34 #include "supertux/screen.hpp"
35 #include "supertux/screen_fade.hpp"
36 #include "supertux/timer.hpp"
37 #include "video/drawing_context.hpp"
38 #include "video/renderer.hpp"
41 /** ticks (as returned from SDL_GetTicks) per frame */
42 static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS);
43 /** don't skip more than every 2nd frame */
44 static const int MAX_FRAME_SKIP = 2;
46 ScreenManager::ScreenManager() :
48 m_menu_storage(new MenuStorage),
49 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 m_screen_fade = std::move(screen_fade);
75 m_actions.push_back(Action(Action::PUSH_ACTION, std::move(screen)));
79 ScreenManager::pop_screen(std::unique_ptr<ScreenFade> screen_fade)
81 log_debug << "ScreenManager::pop_screen(): stack_size: " << m_screen_stack.size() << std::endl;
83 m_screen_fade = std::move(screen_fade);
84 m_actions.push_back(Action(Action::POP_ACTION));
88 ScreenManager::set_screen_fade(std::unique_ptr<ScreenFade> screen_fade)
90 m_screen_fade = std::move(screen_fade);
94 ScreenManager::quit(std::unique_ptr<ScreenFade> screen_fade)
96 m_screen_fade = std::move(screen_fade);
97 m_actions.push_back(Action(Action::QUIT_ACTION));
101 ScreenManager::set_speed(float speed)
107 ScreenManager::get_speed() const
113 ScreenManager::draw_fps(DrawingContext& context, float fps_fps)
116 snprintf(str, sizeof(str), "%3.1f", fps_fps);
117 const char* fpstext = "FPS";
118 context.draw_text(Resources::small_font, fpstext,
119 Vector(SCREEN_WIDTH - Resources::small_font->get_text_width(fpstext) - Resources::small_font->get_text_width(" 99999") - BORDER_X,
120 BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD);
121 context.draw_text(Resources::small_font, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
125 ScreenManager::draw(DrawingContext& context)
127 assert(!m_screen_stack.empty());
129 static Uint32 fps_ticks = SDL_GetTicks();
130 static int frame_count = 0;
132 m_screen_stack.back()->draw(context);
133 m_menu_manager->draw(context);
137 m_screen_fade->draw(context);
140 Console::instance->draw(context);
142 if (g_config->show_fps)
144 draw_fps(context, m_fps);
147 // if a screenshot was requested, pass request on to drawing_context
148 if (m_screenshot_requested)
150 context.take_screenshot();
151 m_screenshot_requested = false;
153 context.do_drawing();
155 /* Calculate frames per second */
156 if (g_config->show_fps)
160 if (SDL_GetTicks() - fps_ticks >= 500)
162 m_fps = (float) frame_count / .5;
164 fps_ticks = SDL_GetTicks();
170 ScreenManager::update_gamelogic(float elapsed_time)
172 scripting::update_debugger();
173 scripting::TimeScheduler::instance->update(game_time);
175 if (!m_screen_stack.empty())
177 m_screen_stack.back()->update(elapsed_time);
180 m_menu_manager->process_input();
184 m_screen_fade->update(elapsed_time);
187 Console::instance->update(elapsed_time);
191 ScreenManager::process_events()
193 g_input_manager->update();
195 while (SDL_PollEvent(&event))
197 g_input_manager->process_event(event);
199 m_menu_manager->event(event);
207 case SDL_WINDOWEVENT:
208 switch(event.window.event)
210 case SDL_WINDOWEVENT_RESIZED:
211 Renderer::instance()->resize(event.window.data1,
213 m_menu_manager->on_window_resize();
219 if (event.key.keysym.sym == SDLK_F10)
221 g_config->show_fps = !g_config->show_fps;
223 else if (event.key.keysym.sym == SDLK_F11)
225 g_config->use_fullscreen = !g_config->use_fullscreen;
226 Renderer::instance()->apply_config();
227 m_menu_manager->on_window_resize();
229 else if (event.key.keysym.sym == SDLK_PRINTSCREEN ||
230 event.key.keysym.sym == SDLK_F12)
234 else if (event.key.keysym.sym == SDLK_F1 &&
235 event.key.keysym.mod & KMOD_CTRL)
237 Console::instance->toggle();
238 g_config->console_enabled = true;
247 ScreenManager::has_pending_fadeout() const
249 return m_screen_fade && !m_screen_fade->done();
253 ScreenManager::handle_screen_switch()
255 if (has_pending_fadeout())
257 // wait till the fadeout is completed before switching screens
261 m_screen_fade.reset();
263 // keep track of the current screen, as only that needs a call to Screen::leave()
264 Screen* current_screen = m_screen_stack.empty() ? nullptr : m_screen_stack.back().get();
266 // Screen::setup() might push more screens, so loop till everything is done
267 while (!m_actions.empty())
269 // move actions to a new vector since setup() might modify it
270 auto actions = std::move(m_actions);
272 for(auto it = actions.begin(); it != actions.end(); ++it)
278 case Action::POP_ACTION:
279 assert(!m_screen_stack.empty());
280 if (current_screen == m_screen_stack.back().get())
282 m_screen_stack.back()->leave();
284 m_screen_stack.pop_back();
287 case Action::PUSH_ACTION:
288 assert(action.screen);
290 if (!m_screen_stack.empty())
292 if (current_screen == m_screen_stack.back().get())
294 m_screen_stack.back()->leave();
297 m_screen_stack.push_back(std::move(action.screen));
300 case Action::QUIT_ACTION:
301 m_screen_stack.clear();
306 if (!m_screen_stack.empty())
308 m_screen_stack.back()->setup();
310 m_waiting_threads.wakeup();
317 ScreenManager::run(DrawingContext &context)
319 Uint32 last_ticks = 0;
320 Uint32 elapsed_ticks = 0;
322 handle_screen_switch();
324 while (!m_screen_stack.empty())
326 Uint32 ticks = SDL_GetTicks();
327 elapsed_ticks += ticks - last_ticks;
330 Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * g_game_speed);
332 if (elapsed_ticks > ticks_per_frame*4)
334 // when the game loads up or levels are switched the
335 // elapsed_ticks grows extremely large, so we just ignore those
340 if (elapsed_ticks < ticks_per_frame)
342 Uint32 delay_ticks = ticks_per_frame - elapsed_ticks;
343 SDL_Delay(delay_ticks);
344 last_ticks += delay_ticks;
345 elapsed_ticks += delay_ticks;
350 while (elapsed_ticks >= ticks_per_frame && frames < MAX_FRAME_SKIP)
352 elapsed_ticks -= ticks_per_frame;
353 float timestep = 1.0 / LOGICAL_FPS;
354 real_time += timestep;
356 game_time += timestep;
359 update_gamelogic(timestep);
363 if (!m_screen_stack.empty())
368 sound_manager->update();
370 handle_screen_switch();
375 ScreenManager::take_screenshot()
377 m_screenshot_requested = true;