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 "random_generator.hpp"
40 // the engine will be run with a logical framerate of 64fps.
41 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
43 static const float LOGICAL_FPS = 64.0;
44 /** ticks (as returned from SDL_GetTicks) per frame */
45 static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS);
46 /** don't skip more than every 2nd frame */
47 static const int MAX_FRAME_SKIP = 2;
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::draw_fps(DrawingContext& context, float fps_fps)
122 snprintf(str, sizeof(str), "%3.1f", fps_fps);
123 const char* fpstext = "FPS";
124 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);
125 context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
129 MainLoop::draw(DrawingContext& context)
131 static Uint32 fps_ticks = SDL_GetTicks();
132 static int frame_count = 0;
134 current_screen->draw(context);
135 if(Menu::current() != NULL)
136 Menu::current()->draw(context);
137 if(screen_fade.get() != NULL)
138 screen_fade->draw(context);
139 Console::instance->draw(context);
142 draw_fps(context, fps);
144 // if a screenshot was requested, pass request on to drawing_context
145 if (screenshot_requested) {
146 context.take_screenshot();
147 screenshot_requested = false;
149 context.do_drawing();
151 /* Calculate frames per second */
156 if(SDL_GetTicks() - fps_ticks >= 500)
158 fps = (float) frame_count / .5;
160 fps_ticks = SDL_GetTicks();
166 MainLoop::update_gamelogic(float elapsed_time)
168 Scripting::update_debugger();
169 Scripting::TimeScheduler::instance->update(game_time);
170 current_screen->update(elapsed_time);
171 if(screen_fade.get() != NULL)
172 screen_fade->update(elapsed_time);
173 Console::instance->update(elapsed_time);
177 MainLoop::process_events()
179 main_controller->update();
181 while(SDL_PollEvent(&event)) {
182 main_controller->process_event(event);
183 if(Menu::current() != NULL)
184 Menu::current()->event(event);
185 if(event.type == SDL_QUIT)
187 else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F11) {
188 config->use_fullscreen = !config->use_fullscreen;
191 else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_PRINT) {
198 MainLoop::handle_screen_switch()
200 while( (next_screen.get() != NULL || nextpop) &&
201 (screen_fade.get() == NULL || screen_fade->done())) {
202 if(current_screen.get() != NULL) {
203 current_screen->leave();
207 if(screen_stack.empty()) {
211 next_screen.reset(screen_stack.back());
212 screen_stack.pop_back();
214 if(nextpush && current_screen.get() != NULL) {
215 screen_stack.push_back(current_screen.release());
221 if(next_screen.get() != NULL)
222 next_screen->setup();
223 current_screen.reset(next_screen.release());
224 screen_fade.reset(NULL);
226 waiting_threads.wakeup();
231 MainLoop::run(DrawingContext &context)
233 Uint32 last_ticks = 0;
234 Uint32 elapsed_ticks = 0;
239 handle_screen_switch();
240 if(!running || current_screen.get() == NULL)
243 Uint32 ticks = SDL_GetTicks();
244 elapsed_ticks += ticks - last_ticks;
247 if (elapsed_ticks > TICKS_PER_FRAME*4) {
248 // when the game loads up or levels are switched the
249 // elapsed_ticks grows extremly large, so we just ignore those
256 if (elapsed_ticks > TICKS_PER_FRAME) {
257 while(elapsed_ticks > TICKS_PER_FRAME && frames < MAX_FRAME_SKIP) {
258 elapsed_ticks -= TICKS_PER_FRAME;
259 float timestep = 1.0 / LOGICAL_FPS;
260 real_time += timestep;
262 game_time += timestep;
265 update_gamelogic(timestep);
272 sound_manager->update();
279 MainLoop::take_screenshot()
281 screenshot_requested = true;