4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "mainloop.hpp"
25 #include "video/drawing_context.hpp"
26 #include "control/joystickkeyboardcontroller.hpp"
27 #include "gui/menu.hpp"
28 #include "audio/sound_manager.hpp"
29 #include "scripting/time_scheduler.hpp"
30 #include "scripting/squirrel_util.hpp"
31 #include "gameconfig.hpp"
32 #include "constants.hpp"
34 #include "resources.hpp"
36 #include "screen_fade.hpp"
38 #include "player_status.hpp"
39 #include "video/renderer.hpp"
40 #include "random_generator.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 float game_speed = 1.0f;
49 MainLoop* main_loop = NULL;
52 : speed(1.0), nextpop(false), nextpush(false), fps(0), screenshot_requested(false)
54 using namespace Scripting;
55 TimeScheduler::instance = new TimeScheduler();
60 using namespace Scripting;
61 delete TimeScheduler::instance;
62 TimeScheduler::instance = NULL;
64 for(std::vector<Screen*>::iterator i = screen_stack.begin();
65 i != screen_stack.end(); ++i) {
71 MainLoop::push_screen(Screen* screen, ScreenFade* screen_fade)
73 this->next_screen.reset(screen);
74 this->screen_fade.reset(screen_fade);
81 MainLoop::exit_screen(ScreenFade* screen_fade)
83 next_screen.reset(NULL);
84 this->screen_fade.reset(screen_fade);
90 MainLoop::set_screen_fade(ScreenFade* screen_fade)
92 this->screen_fade.reset(screen_fade);
96 MainLoop::quit(ScreenFade* screen_fade)
98 for(std::vector<Screen*>::iterator i = screen_stack.begin();
99 i != screen_stack.end(); ++i)
101 screen_stack.clear();
103 exit_screen(screen_fade);
107 MainLoop::set_speed(float speed)
113 MainLoop::get_speed() const
119 MainLoop::has_no_pending_fadeout() const
121 return screen_fade.get() == NULL || screen_fade->done();
125 MainLoop::draw_fps(DrawingContext& context, float fps_fps)
128 snprintf(str, sizeof(str), "%3.1f", fps_fps);
129 const char* fpstext = "FPS";
130 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);
131 context.draw_text(small_font, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
135 MainLoop::draw(DrawingContext& context)
137 static Uint32 fps_ticks = SDL_GetTicks();
138 static int frame_count = 0;
140 current_screen->draw(context);
141 if(Menu::current() != NULL)
142 Menu::current()->draw(context);
143 if(screen_fade.get() != NULL)
144 screen_fade->draw(context);
145 Console::instance->draw(context);
148 draw_fps(context, fps);
150 // if a screenshot was requested, pass request on to drawing_context
151 if (screenshot_requested) {
152 context.take_screenshot();
153 screenshot_requested = false;
155 context.do_drawing();
157 /* Calculate frames per second */
162 if(SDL_GetTicks() - fps_ticks >= 500)
164 fps = (float) frame_count / .5;
166 fps_ticks = SDL_GetTicks();
172 MainLoop::update_gamelogic(float elapsed_time)
174 Scripting::update_debugger();
175 Scripting::TimeScheduler::instance->update(game_time);
176 current_screen->update(elapsed_time);
177 if (Menu::current() != NULL)
178 Menu::current()->update();
179 if(screen_fade.get() != NULL)
180 screen_fade->update(elapsed_time);
181 Console::instance->update(elapsed_time);
185 MainLoop::process_events()
187 main_controller->update();
188 Uint8* keystate = SDL_GetKeyState(NULL);
190 while(SDL_PollEvent(&event))
192 main_controller->process_event(event);
194 if(Menu::current() != NULL)
195 Menu::current()->event(event);
203 case SDL_VIDEORESIZE:
204 Renderer::instance()->resize(event.resize.w, event.resize.h);
209 if (event.key.keysym.sym == SDLK_F10)
211 config->show_fps = !config->show_fps;
213 if (event.key.keysym.sym == SDLK_F11)
215 config->use_fullscreen = !config->use_fullscreen;
219 else if (event.key.keysym.sym == SDLK_PRINT ||
220 event.key.keysym.sym == SDLK_F12)
224 else if (event.key.keysym.sym == SDLK_F1 &&
225 (keystate[SDLK_LCTRL] || keystate[SDLK_RCTRL]) &&
228 Console::instance->toggle();
229 config->console_enabled = true;
238 MainLoop::handle_screen_switch()
240 while( (next_screen.get() != NULL || nextpop) &&
241 has_no_pending_fadeout()) {
242 if(current_screen.get() != NULL) {
243 current_screen->leave();
247 if(screen_stack.empty()) {
251 next_screen.reset(screen_stack.back());
252 screen_stack.pop_back();
254 if(nextpush && current_screen.get() != NULL) {
255 screen_stack.push_back(current_screen.release());
261 Screen* next_screen_ptr = next_screen.release();
262 next_screen.reset(0);
264 next_screen_ptr->setup();
265 current_screen.reset(next_screen_ptr);
266 screen_fade.reset(NULL);
268 waiting_threads.wakeup();
273 MainLoop::run(DrawingContext &context)
275 Uint32 last_ticks = 0;
276 Uint32 elapsed_ticks = 0;
281 handle_screen_switch();
282 if(!running || current_screen.get() == NULL)
285 Uint32 ticks = SDL_GetTicks();
286 elapsed_ticks += ticks - last_ticks;
289 Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * game_speed);
291 if (elapsed_ticks > ticks_per_frame*4) {
292 // when the game loads up or levels are switched the
293 // elapsed_ticks grows extremely large, so we just ignore those
300 if (elapsed_ticks > ticks_per_frame)
302 while(elapsed_ticks > ticks_per_frame && frames < MAX_FRAME_SKIP)
304 elapsed_ticks -= ticks_per_frame;
305 float timestep = 1.0 / LOGICAL_FPS;
306 real_time += timestep;
308 game_time += timestep;
311 update_gamelogic(timestep);
318 sound_manager->update();
325 MainLoop::take_screenshot()
327 screenshot_requested = true;