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/mainloop.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "control/joystickkeyboardcontroller.hpp"
21 #include "gui/menu.hpp"
22 #include "scripting/squirrel_util.hpp"
23 #include "scripting/time_scheduler.hpp"
24 #include "supertux/constants.hpp"
25 #include "supertux/console.hpp"
26 #include "supertux/gameconfig.hpp"
27 #include "supertux/globals.hpp"
28 #include "supertux/main.hpp"
29 #include "supertux/player_status.hpp"
30 #include "supertux/resources.hpp"
31 #include "supertux/screen_fade.hpp"
32 #include "supertux/screen.hpp"
33 #include "supertux/timer.hpp"
34 #include "video/drawing_context.hpp"
35 #include "video/renderer.hpp"
37 /** ticks (as returned from SDL_GetTicks) per frame */
38 static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS);
39 /** don't skip more than every 2nd frame */
40 static const int MAX_FRAME_SKIP = 2;
42 float g_game_speed = 1.0f;
44 MainLoop* g_main_loop = NULL;
46 MainLoop::MainLoop() :
58 screenshot_requested(false)
60 using namespace Scripting;
61 TimeScheduler::instance = new TimeScheduler();
66 using namespace Scripting;
67 delete TimeScheduler::instance;
68 TimeScheduler::instance = NULL;
70 for(std::vector<Screen*>::iterator i = screen_stack.begin();
71 i != screen_stack.end(); ++i) {
77 MainLoop::push_screen(Screen* screen, ScreenFade* screen_fade)
79 this->next_screen.reset(screen);
80 this->screen_fade.reset(screen_fade);
87 MainLoop::exit_screen(ScreenFade* screen_fade)
89 next_screen.reset(NULL);
90 this->screen_fade.reset(screen_fade);
96 MainLoop::set_screen_fade(ScreenFade* screen_fade)
98 this->screen_fade.reset(screen_fade);
102 MainLoop::quit(ScreenFade* screen_fade)
104 for(std::vector<Screen*>::iterator i = screen_stack.begin();
105 i != screen_stack.end(); ++i)
107 screen_stack.clear();
109 exit_screen(screen_fade);
113 MainLoop::set_speed(float speed)
119 MainLoop::get_speed() const
125 MainLoop::has_no_pending_fadeout() const
127 return screen_fade.get() == NULL || screen_fade->done();
131 MainLoop::draw_fps(DrawingContext& context, float fps_fps)
134 snprintf(str, sizeof(str), "%3.1f", fps_fps);
135 const char* fpstext = "FPS";
136 context.draw_text(small_font, fpstext, Vector(SCREEN_WIDTH - small_font->get_text_width(fpstext) - small_font->get_text_width(" 99999") - BORDER_X, BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD);
137 context.draw_text(small_font, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
141 MainLoop::draw(DrawingContext& context)
143 static Uint32 fps_ticks = SDL_GetTicks();
144 static int frame_count = 0;
146 current_screen->draw(context);
147 if(Menu::current() != NULL)
148 Menu::current()->draw(context);
149 if(screen_fade.get() != NULL)
150 screen_fade->draw(context);
151 Console::instance->draw(context);
153 if(g_config->show_fps)
154 draw_fps(context, fps);
156 // if a screenshot was requested, pass request on to drawing_context
157 if (screenshot_requested) {
158 context.take_screenshot();
159 screenshot_requested = false;
161 context.do_drawing();
163 /* Calculate frames per second */
164 if(g_config->show_fps)
168 if(SDL_GetTicks() - fps_ticks >= 500)
170 fps = (float) frame_count / .5;
172 fps_ticks = SDL_GetTicks();
178 MainLoop::update_gamelogic(float elapsed_time)
180 Scripting::update_debugger();
181 Scripting::TimeScheduler::instance->update(game_time);
182 current_screen->update(elapsed_time);
183 if (Menu::current() != NULL)
184 Menu::current()->update();
185 if(screen_fade.get() != NULL)
186 screen_fade->update(elapsed_time);
187 Console::instance->update(elapsed_time);
191 MainLoop::process_events()
193 g_main_controller->update();
194 Uint8* keystate = SDL_GetKeyState(NULL);
196 while(SDL_PollEvent(&event))
198 g_main_controller->process_event(event);
200 if(Menu::current() != NULL)
201 Menu::current()->event(event);
209 case SDL_VIDEORESIZE:
210 Renderer::instance()->resize(event.resize.w, event.resize.h);
215 if (event.key.keysym.sym == SDLK_F10)
217 g_config->show_fps = !g_config->show_fps;
219 if (event.key.keysym.sym == SDLK_F11)
221 g_config->use_fullscreen = !g_config->use_fullscreen;
225 else if (event.key.keysym.sym == SDLK_PRINT ||
226 event.key.keysym.sym == SDLK_F12)
230 else if (event.key.keysym.sym == SDLK_F1 &&
231 (keystate[SDLK_LCTRL] || keystate[SDLK_RCTRL]) &&
234 Console::instance->toggle();
235 g_config->console_enabled = true;
244 MainLoop::handle_screen_switch()
246 while( (next_screen.get() != NULL || nextpop) &&
247 has_no_pending_fadeout()) {
248 if(current_screen.get() != NULL) {
249 current_screen->leave();
253 if(screen_stack.empty()) {
257 next_screen.reset(screen_stack.back());
258 screen_stack.pop_back();
260 if(nextpush && current_screen.get() != NULL) {
261 screen_stack.push_back(current_screen.release());
267 Screen* next_screen_ptr = next_screen.release();
268 next_screen.reset(0);
270 next_screen_ptr->setup();
271 current_screen.reset(next_screen_ptr);
272 screen_fade.reset(NULL);
274 waiting_threads.wakeup();
279 MainLoop::run(DrawingContext &context)
281 Uint32 last_ticks = 0;
282 Uint32 elapsed_ticks = 0;
287 handle_screen_switch();
288 if(!running || current_screen.get() == NULL)
291 Uint32 ticks = SDL_GetTicks();
292 elapsed_ticks += ticks - last_ticks;
295 Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * g_game_speed);
297 if (elapsed_ticks > ticks_per_frame*4) {
298 // when the game loads up or levels are switched the
299 // elapsed_ticks grows extremely large, so we just ignore those
304 if(elapsed_ticks < ticks_per_frame)
306 Uint32 delay_ticks = ticks_per_frame - elapsed_ticks;
307 SDL_Delay(delay_ticks);
308 last_ticks += delay_ticks;
309 elapsed_ticks += delay_ticks;
314 while(elapsed_ticks >= ticks_per_frame && frames < MAX_FRAME_SKIP)
316 elapsed_ticks -= ticks_per_frame;
317 float timestep = 1.0 / LOGICAL_FPS;
318 real_time += timestep;
320 game_time += timestep;
323 update_gamelogic(timestep);
329 sound_manager->update();
334 MainLoop::take_screenshot()
336 screenshot_requested = true;