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),
59 screenshot_requested(false)
61 using namespace scripting;
62 TimeScheduler::instance = new TimeScheduler();
65 ScreenManager::~ScreenManager()
67 using namespace scripting;
68 delete TimeScheduler::instance;
69 TimeScheduler::instance = NULL;
71 for(std::vector<Screen*>::iterator i = screen_stack.begin();
72 i != screen_stack.end(); ++i) {
78 ScreenManager::push_screen(Screen* screen, ScreenFade* screen_fade)
80 this->next_screen.reset(screen);
81 this->screen_fade.reset(screen_fade);
88 ScreenManager::exit_screen(ScreenFade* screen_fade)
90 next_screen.reset(NULL);
91 this->screen_fade.reset(screen_fade);
97 ScreenManager::set_screen_fade(ScreenFade* screen_fade)
99 this->screen_fade.reset(screen_fade);
103 ScreenManager::quit(ScreenFade* screen_fade)
105 for(std::vector<Screen*>::iterator i = screen_stack.begin();
106 i != screen_stack.end(); ++i)
108 screen_stack.clear();
110 exit_screen(screen_fade);
114 ScreenManager::set_speed(float speed)
120 ScreenManager::get_speed() const
126 ScreenManager::has_no_pending_fadeout() const
128 return screen_fade.get() == NULL || screen_fade->done();
132 ScreenManager::draw_fps(DrawingContext& context, float fps_fps)
135 snprintf(str, sizeof(str), "%3.1f", fps_fps);
136 const char* fpstext = "FPS";
137 context.draw_text(Resources::small_font, fpstext,
138 Vector(SCREEN_WIDTH - Resources::small_font->get_text_width(fpstext) - Resources::small_font->get_text_width(" 99999") - BORDER_X,
139 BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD);
140 context.draw_text(Resources::small_font, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
144 ScreenManager::draw(DrawingContext& context)
146 static Uint32 fps_ticks = SDL_GetTicks();
147 static int frame_count = 0;
149 current_screen->draw(context);
150 if(MenuManager::instance().current() != NULL)
151 MenuManager::instance().current()->draw(context);
152 if(screen_fade.get() != NULL)
153 screen_fade->draw(context);
154 Console::instance->draw(context);
156 if(g_config->show_fps)
157 draw_fps(context, fps);
159 // if a screenshot was requested, pass request on to drawing_context
160 if (screenshot_requested) {
161 context.take_screenshot();
162 screenshot_requested = false;
164 context.do_drawing();
166 /* Calculate frames per second */
167 if(g_config->show_fps)
171 if(SDL_GetTicks() - fps_ticks >= 500)
173 fps = (float) frame_count / .5;
175 fps_ticks = SDL_GetTicks();
181 ScreenManager::update_gamelogic(float elapsed_time)
183 scripting::update_debugger();
184 scripting::TimeScheduler::instance->update(game_time);
185 current_screen->update(elapsed_time);
186 if (MenuManager::instance().current() != NULL)
187 MenuManager::instance().current()->update();
188 if(screen_fade.get() != NULL)
189 screen_fade->update(elapsed_time);
190 Console::instance->update(elapsed_time);
194 ScreenManager::process_events()
196 g_input_manager->update();
198 while(SDL_PollEvent(&event))
200 g_input_manager->process_event(event);
202 if(MenuManager::instance().current() != NULL)
203 MenuManager::instance().current()->event(event);
211 case SDL_WINDOWEVENT:
212 switch(event.window.event)
214 case SDL_WINDOWEVENT_RESIZED:
215 Renderer::instance()->resize(event.window.data1,
217 MenuManager::instance().recalc_pos();
223 if (event.key.keysym.sym == SDLK_F10)
225 g_config->show_fps = !g_config->show_fps;
227 if (event.key.keysym.sym == SDLK_F11)
229 g_config->use_fullscreen = !g_config->use_fullscreen;
230 Renderer::instance()->apply_config();
231 MenuManager::instance().recalc_pos();
233 else if (event.key.keysym.sym == SDLK_PRINTSCREEN ||
234 event.key.keysym.sym == SDLK_F12)
238 else if (event.key.keysym.sym == SDLK_F1 &&
239 event.key.keysym.mod & KMOD_CTRL)
241 Console::instance->toggle();
242 g_config->console_enabled = true;
251 ScreenManager::handle_screen_switch()
253 while( (next_screen.get() != NULL || nextpop) &&
254 has_no_pending_fadeout()) {
255 if(current_screen.get() != NULL) {
256 current_screen->leave();
260 if(screen_stack.empty()) {
264 next_screen.reset(screen_stack.back());
265 screen_stack.pop_back();
267 if(nextpush && current_screen.get() != NULL) {
268 screen_stack.push_back(current_screen.release());
274 Screen* next_screen_ptr = next_screen.release();
275 next_screen.reset(0);
277 next_screen_ptr->setup();
278 current_screen.reset(next_screen_ptr);
279 screen_fade.reset(NULL);
281 waiting_threads.wakeup();
286 ScreenManager::run(DrawingContext &context)
288 Uint32 last_ticks = 0;
289 Uint32 elapsed_ticks = 0;
294 handle_screen_switch();
295 if(!running || current_screen.get() == NULL)
298 Uint32 ticks = SDL_GetTicks();
299 elapsed_ticks += ticks - last_ticks;
302 Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * g_game_speed);
304 if (elapsed_ticks > ticks_per_frame*4) {
305 // when the game loads up or levels are switched the
306 // elapsed_ticks grows extremely large, so we just ignore those
311 if(elapsed_ticks < ticks_per_frame)
313 Uint32 delay_ticks = ticks_per_frame - elapsed_ticks;
314 SDL_Delay(delay_ticks);
315 last_ticks += delay_ticks;
316 elapsed_ticks += delay_ticks;
321 while(elapsed_ticks >= ticks_per_frame && frames < MAX_FRAME_SKIP)
323 elapsed_ticks -= ticks_per_frame;
324 float timestep = 1.0 / LOGICAL_FPS;
325 real_time += timestep;
327 game_time += timestep;
330 update_gamelogic(timestep);
336 sound_manager->update();
341 ScreenManager::take_screenshot()
343 screenshot_requested = true;