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"
33 #include "resources.hpp"
35 #include "screen_fade.hpp"
37 #include "player_status.hpp"
38 #include "video/renderer.hpp"
39 #include "random_generator.hpp"
41 // the engine will be run with a logical framerate of 64fps.
42 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
44 static const float LOGICAL_FPS = 64.0;
45 /** ticks (as returned from SDL_GetTicks) per frame */
46 static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS);
47 /** don't skip more than every 2nd frame */
48 static const int MAX_FRAME_SKIP = 2;
50 float game_speed = 1.0f;
52 MainLoop* main_loop = NULL;
55 : speed(1.0), nextpop(false), nextpush(false), fps(0), screenshot_requested(false)
57 using namespace Scripting;
58 TimeScheduler::instance = new TimeScheduler();
63 using namespace Scripting;
64 delete TimeScheduler::instance;
65 TimeScheduler::instance = NULL;
67 for(std::vector<Screen*>::iterator i = screen_stack.begin();
68 i != screen_stack.end(); ++i) {
74 MainLoop::push_screen(Screen* screen, ScreenFade* screen_fade)
76 this->next_screen.reset(screen);
77 this->screen_fade.reset(screen_fade);
84 MainLoop::exit_screen(ScreenFade* screen_fade)
86 next_screen.reset(NULL);
87 this->screen_fade.reset(screen_fade);
93 MainLoop::set_screen_fade(ScreenFade* screen_fade)
95 this->screen_fade.reset(screen_fade);
99 MainLoop::quit(ScreenFade* screen_fade)
101 for(std::vector<Screen*>::iterator i = screen_stack.begin();
102 i != screen_stack.end(); ++i)
104 screen_stack.clear();
106 exit_screen(screen_fade);
110 MainLoop::set_speed(float speed)
116 MainLoop::get_speed() const
122 MainLoop::draw_fps(DrawingContext& context, float fps_fps)
125 snprintf(str, sizeof(str), "%3.1f", fps_fps);
126 const char* fpstext = "FPS";
127 context.draw_text(white_text, fpstext, Vector(SCREEN_WIDTH - white_text->get_text_width(fpstext) - gold_text->get_text_width(" 99999") - BORDER_X, BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD);
128 context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
132 MainLoop::draw(DrawingContext& context)
134 static Uint32 fps_ticks = SDL_GetTicks();
135 static int frame_count = 0;
137 current_screen->draw(context);
138 if(Menu::current() != NULL)
139 Menu::current()->draw(context);
140 if(screen_fade.get() != NULL)
141 screen_fade->draw(context);
142 Console::instance->draw(context);
145 draw_fps(context, fps);
147 // if a screenshot was requested, pass request on to drawing_context
148 if (screenshot_requested) {
149 context.take_screenshot();
150 screenshot_requested = false;
152 context.do_drawing();
154 /* Calculate frames per second */
159 if(SDL_GetTicks() - fps_ticks >= 500)
161 fps = (float) frame_count / .5;
163 fps_ticks = SDL_GetTicks();
169 MainLoop::update_gamelogic(float elapsed_time)
171 Scripting::update_debugger();
172 Scripting::TimeScheduler::instance->update(game_time);
173 current_screen->update(elapsed_time);
174 if(screen_fade.get() != NULL)
175 screen_fade->update(elapsed_time);
176 Console::instance->update(elapsed_time);
180 MainLoop::process_events()
182 main_controller->update();
183 Uint8* keystate = SDL_GetKeyState(NULL);
185 while(SDL_PollEvent(&event))
187 main_controller->process_event(event);
189 if(Menu::current() != NULL)
190 Menu::current()->event(event);
198 case SDL_VIDEORESIZE:
199 Renderer::instance()->resize(event.resize.w, event.resize.h);
204 if (event.key.keysym.sym == SDLK_F11)
206 config->use_fullscreen = !config->use_fullscreen;
210 else if (event.key.keysym.sym == SDLK_PRINT ||
211 event.key.keysym.sym == SDLK_F12)
215 else if (event.key.keysym.sym == SDLK_F1 &&
216 (keystate[SDLK_LCTRL] || keystate[SDLK_RCTRL]) &&
219 Console::instance->toggle();
220 config->console_enabled = true;
229 MainLoop::handle_screen_switch()
231 while( (next_screen.get() != NULL || nextpop) &&
232 (screen_fade.get() == NULL || screen_fade->done())) {
233 if(current_screen.get() != NULL) {
234 current_screen->leave();
238 if(screen_stack.empty()) {
242 next_screen.reset(screen_stack.back());
243 screen_stack.pop_back();
245 if(nextpush && current_screen.get() != NULL) {
246 screen_stack.push_back(current_screen.release());
252 if(next_screen.get() != NULL)
253 next_screen->setup();
254 current_screen.reset(next_screen.release());
255 screen_fade.reset(NULL);
257 waiting_threads.wakeup();
262 MainLoop::run(DrawingContext &context)
264 Uint32 last_ticks = 0;
265 Uint32 elapsed_ticks = 0;
270 handle_screen_switch();
271 if(!running || current_screen.get() == NULL)
274 Uint32 ticks = SDL_GetTicks();
275 elapsed_ticks += ticks - last_ticks;
278 Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * game_speed);
280 if (elapsed_ticks > ticks_per_frame*4) {
281 // when the game loads up or levels are switched the
282 // elapsed_ticks grows extremely large, so we just ignore those
289 if (elapsed_ticks > ticks_per_frame)
291 while(elapsed_ticks > ticks_per_frame && frames < MAX_FRAME_SKIP)
293 elapsed_ticks -= ticks_per_frame;
294 float timestep = 1.0 / LOGICAL_FPS;
295 real_time += timestep;
297 game_time += timestep;
300 update_gamelogic(timestep);
307 sound_manager->update();
314 MainLoop::take_screenshot()
316 screenshot_requested = true;