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"
39 // the engine will be run with a logical framerate of 64fps.
40 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
42 static const float LOGICAL_FPS = 64.0;
44 MainLoop* main_loop = NULL;
47 : speed(1.0), nextpop(false), nextpush(false)
49 using namespace Scripting;
50 TimeScheduler::instance = new TimeScheduler();
55 using namespace Scripting;
56 delete TimeScheduler::instance;
57 TimeScheduler::instance = NULL;
59 for(std::vector<Screen*>::iterator i = screen_stack.begin();
60 i != screen_stack.end(); ++i) {
66 MainLoop::push_screen(Screen* screen, ScreenFade* screen_fade)
68 this->next_screen.reset(screen);
69 this->screen_fade.reset(screen_fade);
79 MainLoop::exit_screen(ScreenFade* screen_fade)
81 next_screen.reset(NULL);
82 this->screen_fade.reset(screen_fade);
88 MainLoop::set_screen_fade(ScreenFade* screen_fade)
90 this->screen_fade.reset(screen_fade);
94 MainLoop::quit(ScreenFade* screen_fade)
96 for(std::vector<Screen*>::iterator i = screen_stack.begin();
97 i != screen_stack.end(); ++i)
101 exit_screen(screen_fade);
105 MainLoop::set_speed(float speed)
111 MainLoop::draw_fps(DrawingContext& context, float fps_fps)
114 snprintf(str, sizeof(str), "%3.1f", fps_fps);
115 const char* fpstext = "FPS";
116 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), LEFT_ALLIGN, LAYER_FOREGROUND1);
117 context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), RIGHT_ALLIGN, LAYER_FOREGROUND1);
123 DrawingContext context;
125 unsigned int frame_count = 0;
127 Uint32 fps_ticks = SDL_GetTicks();
128 Uint32 fps_nextframe_ticks = SDL_GetTicks();
130 bool skipdraw = false;
134 while( (next_screen.get() != NULL || nextpop == true) &&
135 (screen_fade.get() == NULL || screen_fade->done())) {
136 if(current_screen.get() != NULL) {
137 current_screen->leave();
141 if(screen_stack.empty()) {
145 next_screen.reset(screen_stack.back());
146 screen_stack.pop_back();
148 if(nextpush && current_screen.get() != NULL) {
149 screen_stack.push_back(current_screen.release());
155 if(next_screen.get() != NULL)
156 next_screen->setup();
157 current_screen.reset(next_screen.release());
158 screen_fade.reset(NULL);
160 waiting_threads.wakeup();
163 if(!running || current_screen.get() == NULL)
166 float elapsed_time = 1.0 / LOGICAL_FPS;
167 ticks = SDL_GetTicks();
168 if(ticks > fps_nextframe_ticks) {
169 if(skipdraw == true) {
170 // already skipped last frame? we have to slow down the game then...
172 fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
174 // don't draw all frames when we're getting too slow
179 while(fps_nextframe_ticks > ticks) {
181 // If we really have to wait long, then do an imprecise SDL_Delay()
182 Uint32 diff = fps_nextframe_ticks - ticks;
186 ticks = SDL_GetTicks();
189 fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
192 current_screen->draw(context);
193 if(Menu::current() != NULL)
194 Menu::current()->draw(context);
195 if(screen_fade.get() != NULL)
196 screen_fade->draw(context);
197 Console::instance->draw(context);
200 draw_fps(context, fps_fps);
202 context.do_drawing();
204 /* Calculate frames per second */
209 if(SDL_GetTicks() - fps_ticks >= 500)
211 fps_fps = (float) frame_count / .5;
213 fps_ticks = SDL_GetTicks();
218 real_time += elapsed_time;
219 elapsed_time *= speed;
220 game_time += elapsed_time;
222 Scripting::update_debugger();
223 Scripting::TimeScheduler::instance->update(game_time);
224 current_screen->update(elapsed_time);
225 if(screen_fade.get() != NULL)
226 screen_fade->update(elapsed_time);
227 Console::instance->update(elapsed_time);
229 main_controller->update();
231 while(SDL_PollEvent(&event)) {
232 main_controller->process_event(event);
233 if(Menu::current() != NULL)
234 Menu::current()->event(event);
235 if(event.type == SDL_QUIT)
239 sound_manager->update();