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/game_session.hpp"
31 #include "supertux/globals.hpp"
32 #include "supertux/main.hpp"
33 #include "supertux/menu/menu_storage.hpp"
34 #include "supertux/player_status.hpp"
35 #include "supertux/resources.hpp"
36 #include "supertux/screen.hpp"
37 #include "supertux/screen_fade.hpp"
38 #include "supertux/timer.hpp"
39 #include "video/drawing_context.hpp"
40 #include "video/renderer.hpp"
43 /** ticks (as returned from SDL_GetTicks) per frame */
44 static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS);
45 /** don't skip more than every 2nd frame */
46 static const int MAX_FRAME_SKIP = 2;
48 ScreenManager::ScreenManager() :
50 m_menu_storage(new MenuStorage),
51 m_menu_manager(new MenuManager),
57 m_screenshot_requested(false)
59 using namespace scripting;
60 TimeScheduler::instance = new TimeScheduler();
63 ScreenManager::~ScreenManager()
65 using namespace scripting;
66 delete TimeScheduler::instance;
67 TimeScheduler::instance = NULL;
71 ScreenManager::push_screen(std::unique_ptr<Screen> screen, std::unique_ptr<ScreenFade> screen_fade)
73 log_debug << "ScreenManager::push_screen(): " << screen.get() << std::endl;
76 m_screen_fade = std::move(screen_fade);
77 m_actions.push_back(Action(Action::PUSH_ACTION, std::move(screen)));
81 ScreenManager::pop_screen(std::unique_ptr<ScreenFade> screen_fade)
83 log_debug << "ScreenManager::pop_screen(): stack_size: " << m_screen_stack.size() << std::endl;
85 m_screen_fade = std::move(screen_fade);
86 m_actions.push_back(Action(Action::POP_ACTION));
90 ScreenManager::set_screen_fade(std::unique_ptr<ScreenFade> screen_fade)
92 m_screen_fade = std::move(screen_fade);
96 ScreenManager::quit(std::unique_ptr<ScreenFade> screen_fade)
98 m_screen_fade = std::move(screen_fade);
99 m_actions.push_back(Action(Action::QUIT_ACTION));
103 ScreenManager::set_speed(float speed)
109 ScreenManager::get_speed() const
115 ScreenManager::draw_fps(DrawingContext& context, float fps_fps)
118 snprintf(str, sizeof(str), "%3.1f", fps_fps);
119 const char* fpstext = "FPS";
120 context.draw_text(Resources::small_font, fpstext,
121 Vector(SCREEN_WIDTH - Resources::small_font->get_text_width(fpstext) - Resources::small_font->get_text_width(" 99999") - BORDER_X,
122 BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD);
123 context.draw_text(Resources::small_font, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
127 ScreenManager::draw(DrawingContext& context)
129 assert(!m_screen_stack.empty());
131 static Uint32 fps_ticks = SDL_GetTicks();
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)
159 static int frame_count = 0;
162 if (SDL_GetTicks() - fps_ticks >= 500)
164 m_fps = (float) frame_count / .5;
166 fps_ticks = SDL_GetTicks();
172 ScreenManager::update_gamelogic(float elapsed_time)
174 scripting::Scripting::current()->update_debugger();
175 scripting::TimeScheduler::instance->update(game_time);
177 if (!m_screen_stack.empty())
179 m_screen_stack.back()->update(elapsed_time);
182 m_menu_manager->process_input();
186 m_screen_fade->update(elapsed_time);
189 Console::current()->update(elapsed_time);
193 ScreenManager::process_events()
195 InputManager::current()->update();
197 while (SDL_PollEvent(&event))
199 InputManager::current()->process_event(event);
201 m_menu_manager->event(event);
209 case SDL_WINDOWEVENT:
210 switch(event.window.event)
212 case SDL_WINDOWEVENT_RESIZED:
213 VideoSystem::current()->resize(event.window.data1,
215 m_menu_manager->on_window_resize();
218 case SDL_WINDOWEVENT_FOCUS_LOST:
219 if(GameSession::current() != NULL) {
220 GameSession::current()->toggle_pause();
227 if (event.key.keysym.sym == SDLK_F10)
229 g_config->show_fps = !g_config->show_fps;
231 else if (event.key.keysym.sym == SDLK_F11)
233 g_config->use_fullscreen = !g_config->use_fullscreen;
234 VideoSystem::current()->apply_config();
235 m_menu_manager->on_window_resize();
237 else if (event.key.keysym.sym == SDLK_PRINTSCREEN ||
238 event.key.keysym.sym == SDLK_F12)
242 else if (event.key.keysym.sym == SDLK_F1 &&
243 event.key.keysym.mod & KMOD_CTRL)
245 Console::current()->toggle();
246 g_config->console_enabled = true;
249 else if (event.key.keysym.sym == SDLK_F2 &&
250 event.key.keysym.mod & KMOD_CTRL)
252 g_config->developer_mode = !g_config->developer_mode;
253 log_info << "developer mode: " << g_config->developer_mode << std::endl;
261 ScreenManager::has_pending_fadeout() const
263 return m_screen_fade && !m_screen_fade->done();
267 ScreenManager::handle_screen_switch()
269 if (has_pending_fadeout())
271 // wait till the fadeout is completed before switching screens
275 m_screen_fade.reset();
277 // keep track of the current screen, as only that needs a call to Screen::leave()
278 Screen* current_screen = m_screen_stack.empty() ? nullptr : m_screen_stack.back().get();
280 // Screen::setup() might push more screens, so loop till everything is done
281 while (!m_actions.empty())
283 // move actions to a new vector since setup() might modify it
284 auto actions = std::move(m_actions);
286 for(auto it = actions.begin(); it != actions.end(); ++it)
292 case Action::POP_ACTION:
293 assert(!m_screen_stack.empty());
294 if (current_screen == m_screen_stack.back().get())
296 m_screen_stack.back()->leave();
298 m_screen_stack.pop_back();
301 case Action::PUSH_ACTION:
302 assert(action.screen);
304 if (!m_screen_stack.empty())
306 if (current_screen == m_screen_stack.back().get())
308 m_screen_stack.back()->leave();
311 m_screen_stack.push_back(std::move(action.screen));
314 case Action::QUIT_ACTION:
315 m_screen_stack.clear();
320 if (!m_screen_stack.empty())
322 m_screen_stack.back()->setup();
324 m_waiting_threads.wakeup();
331 ScreenManager::run(DrawingContext &context)
333 Uint32 last_ticks = 0;
334 Uint32 elapsed_ticks = 0;
336 handle_screen_switch();
338 while (!m_screen_stack.empty())
340 Uint32 ticks = SDL_GetTicks();
341 elapsed_ticks += ticks - last_ticks;
344 Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * g_game_speed);
346 if (elapsed_ticks > ticks_per_frame*4)
348 // when the game loads up or levels are switched the
349 // elapsed_ticks grows extremely large, so we just ignore those
354 if (elapsed_ticks < ticks_per_frame)
356 Uint32 delay_ticks = ticks_per_frame - elapsed_ticks;
357 SDL_Delay(delay_ticks);
358 last_ticks += delay_ticks;
359 elapsed_ticks += delay_ticks;
364 while (elapsed_ticks >= ticks_per_frame && frames < MAX_FRAME_SKIP)
366 elapsed_ticks -= ticks_per_frame;
367 float timestep = 1.0 / LOGICAL_FPS;
368 real_time += timestep;
370 game_time += timestep;
373 update_gamelogic(timestep);
377 if (!m_screen_stack.empty())
382 SoundManager::current()->update();
384 handle_screen_switch();
389 ScreenManager::take_screenshot()
391 m_screenshot_requested = true;