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/scripting.hpp"
25 #include "scripting/squirrel_util.hpp"
26 #include "scripting/time_scheduler.hpp"
27 #include "supertux/console.hpp"
28 #include "supertux/constants.hpp"
29 #include "supertux/gameconfig.hpp"
30 #include "supertux/globals.hpp"
31 #include "supertux/main.hpp"
32 #include "supertux/menu/menu_storage.hpp"
33 #include "supertux/player_status.hpp"
34 #include "supertux/resources.hpp"
35 #include "supertux/screen.hpp"
36 #include "supertux/screen_fade.hpp"
37 #include "supertux/timer.hpp"
38 #include "video/drawing_context.hpp"
39 #include "video/renderer.hpp"
42 /** ticks (as returned from SDL_GetTicks) per frame */
43 static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS);
44 /** don't skip more than every 2nd frame */
45 static const int MAX_FRAME_SKIP = 2;
47 ScreenManager::ScreenManager() :
49 m_menu_storage(new MenuStorage),
50 m_menu_manager(new MenuManager),
56 m_screenshot_requested(false)
58 using namespace scripting;
59 TimeScheduler::instance = new TimeScheduler();
62 ScreenManager::~ScreenManager()
64 using namespace scripting;
65 delete TimeScheduler::instance;
66 TimeScheduler::instance = NULL;
70 ScreenManager::push_screen(std::unique_ptr<Screen> screen, std::unique_ptr<ScreenFade> screen_fade)
72 log_debug << "ScreenManager::push_screen(): " << screen.get() << std::endl;
75 m_screen_fade = std::move(screen_fade);
76 m_actions.push_back(Action(Action::PUSH_ACTION, std::move(screen)));
80 ScreenManager::pop_screen(std::unique_ptr<ScreenFade> screen_fade)
82 log_debug << "ScreenManager::pop_screen(): stack_size: " << m_screen_stack.size() << std::endl;
84 m_screen_fade = std::move(screen_fade);
85 m_actions.push_back(Action(Action::POP_ACTION));
89 ScreenManager::set_screen_fade(std::unique_ptr<ScreenFade> screen_fade)
91 m_screen_fade = std::move(screen_fade);
95 ScreenManager::quit(std::unique_ptr<ScreenFade> screen_fade)
97 m_screen_fade = std::move(screen_fade);
98 m_actions.push_back(Action(Action::QUIT_ACTION));
102 ScreenManager::set_speed(float speed)
108 ScreenManager::get_speed() const
114 ScreenManager::draw_fps(DrawingContext& context, float fps_fps)
117 snprintf(str, sizeof(str), "%3.1f", fps_fps);
118 const char* fpstext = "FPS";
119 context.draw_text(Resources::small_font, fpstext,
120 Vector(SCREEN_WIDTH - Resources::small_font->get_text_width(fpstext) - Resources::small_font->get_text_width(" 99999") - BORDER_X,
121 BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD);
122 context.draw_text(Resources::small_font, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
126 ScreenManager::draw(DrawingContext& context)
128 assert(!m_screen_stack.empty());
130 static Uint32 fps_ticks = SDL_GetTicks();
131 static int frame_count = 0;
133 m_screen_stack.back()->draw(context);
134 m_menu_manager->draw(context);
138 m_screen_fade->draw(context);
141 Console::current()->draw(context);
143 if (g_config->show_fps)
145 draw_fps(context, m_fps);
148 // if a screenshot was requested, pass request on to drawing_context
149 if (m_screenshot_requested)
151 context.take_screenshot();
152 m_screenshot_requested = false;
154 context.do_drawing();
156 /* Calculate frames per second */
157 if (g_config->show_fps)
161 if (SDL_GetTicks() - fps_ticks >= 500)
163 m_fps = (float) frame_count / .5;
165 fps_ticks = SDL_GetTicks();
171 ScreenManager::update_gamelogic(float elapsed_time)
173 scripting::Scripting::current()->update_debugger();
174 scripting::TimeScheduler::instance->update(game_time);
176 if (!m_screen_stack.empty())
178 m_screen_stack.back()->update(elapsed_time);
181 m_menu_manager->process_input();
185 m_screen_fade->update(elapsed_time);
188 Console::current()->update(elapsed_time);
192 ScreenManager::process_events()
194 InputManager::current()->update();
196 while (SDL_PollEvent(&event))
198 InputManager::current()->process_event(event);
200 m_menu_manager->event(event);
208 case SDL_WINDOWEVENT:
209 switch(event.window.event)
211 case SDL_WINDOWEVENT_RESIZED:
212 VideoSystem::current()->get_renderer().resize(event.window.data1,
214 m_menu_manager->on_window_resize();
220 if (event.key.keysym.sym == SDLK_F10)
222 g_config->show_fps = !g_config->show_fps;
224 else if (event.key.keysym.sym == SDLK_F11)
226 g_config->use_fullscreen = !g_config->use_fullscreen;
227 VideoSystem::current()->get_renderer().apply_config();
228 m_menu_manager->on_window_resize();
230 else if (event.key.keysym.sym == SDLK_PRINTSCREEN ||
231 event.key.keysym.sym == SDLK_F12)
235 else if (event.key.keysym.sym == SDLK_F1 &&
236 event.key.keysym.mod & KMOD_CTRL)
238 Console::current()->toggle();
239 g_config->console_enabled = true;
242 else if (event.key.keysym.sym == SDLK_F2 &&
243 event.key.keysym.mod & KMOD_CTRL)
245 g_config->developer_mode = !g_config->developer_mode;
246 log_info << "developer mode: " << g_config->developer_mode << std::endl;
254 ScreenManager::has_pending_fadeout() const
256 return m_screen_fade && !m_screen_fade->done();
260 ScreenManager::handle_screen_switch()
262 if (has_pending_fadeout())
264 // wait till the fadeout is completed before switching screens
268 m_screen_fade.reset();
270 // keep track of the current screen, as only that needs a call to Screen::leave()
271 Screen* current_screen = m_screen_stack.empty() ? nullptr : m_screen_stack.back().get();
273 // Screen::setup() might push more screens, so loop till everything is done
274 while (!m_actions.empty())
276 // move actions to a new vector since setup() might modify it
277 auto actions = std::move(m_actions);
279 for(auto it = actions.begin(); it != actions.end(); ++it)
285 case Action::POP_ACTION:
286 assert(!m_screen_stack.empty());
287 if (current_screen == m_screen_stack.back().get())
289 m_screen_stack.back()->leave();
291 m_screen_stack.pop_back();
294 case Action::PUSH_ACTION:
295 assert(action.screen);
297 if (!m_screen_stack.empty())
299 if (current_screen == m_screen_stack.back().get())
301 m_screen_stack.back()->leave();
304 m_screen_stack.push_back(std::move(action.screen));
307 case Action::QUIT_ACTION:
308 m_screen_stack.clear();
313 if (!m_screen_stack.empty())
315 m_screen_stack.back()->setup();
317 m_waiting_threads.wakeup();
324 ScreenManager::run(DrawingContext &context)
326 Uint32 last_ticks = 0;
327 Uint32 elapsed_ticks = 0;
329 handle_screen_switch();
331 while (!m_screen_stack.empty())
333 Uint32 ticks = SDL_GetTicks();
334 elapsed_ticks += ticks - last_ticks;
337 Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * g_game_speed);
339 if (elapsed_ticks > ticks_per_frame*4)
341 // when the game loads up or levels are switched the
342 // elapsed_ticks grows extremely large, so we just ignore those
347 if (elapsed_ticks < ticks_per_frame)
349 Uint32 delay_ticks = ticks_per_frame - elapsed_ticks;
350 SDL_Delay(delay_ticks);
351 last_ticks += delay_ticks;
352 elapsed_ticks += delay_ticks;
357 while (elapsed_ticks >= ticks_per_frame && frames < MAX_FRAME_SKIP)
359 elapsed_ticks -= ticks_per_frame;
360 float timestep = 1.0 / LOGICAL_FPS;
361 real_time += timestep;
363 game_time += timestep;
366 update_gamelogic(timestep);
370 if (!m_screen_stack.empty())
375 SoundManager::current()->update();
377 handle_screen_switch();
382 ScreenManager::take_screenshot()
384 m_screenshot_requested = true;