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;
45 MainLoop* main_loop = NULL;
48 : speed(1.0), nextpop(false), nextpush(false)
50 using namespace Scripting;
51 TimeScheduler::instance = new TimeScheduler();
56 using namespace Scripting;
57 delete TimeScheduler::instance;
58 TimeScheduler::instance = NULL;
60 for(std::vector<Screen*>::iterator i = screen_stack.begin();
61 i != screen_stack.end(); ++i) {
67 MainLoop::push_screen(Screen* screen, ScreenFade* screen_fade)
69 this->next_screen.reset(screen);
70 this->screen_fade.reset(screen_fade);
80 MainLoop::exit_screen(ScreenFade* screen_fade)
82 next_screen.reset(NULL);
83 this->screen_fade.reset(screen_fade);
89 MainLoop::set_screen_fade(ScreenFade* screen_fade)
91 this->screen_fade.reset(screen_fade);
95 MainLoop::quit(ScreenFade* screen_fade)
97 for(std::vector<Screen*>::iterator i = screen_stack.begin();
98 i != screen_stack.end(); ++i)
100 screen_stack.clear();
102 exit_screen(screen_fade);
106 MainLoop::set_speed(float speed)
112 MainLoop::draw_fps(DrawingContext& context, float fps_fps)
115 snprintf(str, sizeof(str), "%3.1f", fps_fps);
116 const char* fpstext = "FPS";
117 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);
118 context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), RIGHT_ALLIGN, LAYER_FOREGROUND1);
124 DrawingContext context;
126 unsigned int frame_count = 0;
127 unsigned int rand_prints = 0;
129 Uint32 fps_ticks = SDL_GetTicks();
130 Uint32 fps_nextframe_ticks = SDL_GetTicks();
132 bool skipdraw = false;
136 while( (next_screen.get() != NULL || nextpop == true) &&
137 (screen_fade.get() == NULL || screen_fade->done())) {
138 if(current_screen.get() != NULL) {
139 current_screen->leave();
143 if(screen_stack.empty()) {
147 next_screen.reset(screen_stack.back());
148 screen_stack.pop_back();
150 if(nextpush && current_screen.get() != NULL) {
151 screen_stack.push_back(current_screen.release());
157 if(next_screen.get() != NULL)
158 next_screen->setup();
159 current_screen.reset(next_screen.release());
160 screen_fade.reset(NULL);
162 waiting_threads.wakeup();
165 if(!running || current_screen.get() == NULL)
168 float elapsed_time = 1.0 / LOGICAL_FPS;
169 ticks = SDL_GetTicks();
170 if(ticks > fps_nextframe_ticks) {
171 if(skipdraw == true) {
172 // already skipped last frame? we have to slow down the game then...
174 fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
176 // don't draw all frames when we're getting too slow
181 while(fps_nextframe_ticks > ticks) {
183 // If we really have to wait long, then do an imprecise SDL_Delay()
184 Uint32 diff = fps_nextframe_ticks - ticks;
188 ticks = SDL_GetTicks();
191 fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
194 current_screen->draw(context);
195 if(Menu::current() != NULL)
196 Menu::current()->draw(context);
197 if(screen_fade.get() != NULL)
198 screen_fade->draw(context);
199 Console::instance->draw(context);
202 draw_fps(context, fps_fps);
204 context.do_drawing();
206 /* Calculate frames per second */
211 if(SDL_GetTicks() - fps_ticks >= 500)
213 fps_fps = (float) frame_count / .5;
215 fps_ticks = SDL_GetTicks();
220 real_time += elapsed_time;
221 elapsed_time *= speed;
222 game_time += elapsed_time;
224 Scripting::update_debugger();
225 Scripting::TimeScheduler::instance->update(game_time);
226 current_screen->update(elapsed_time);
227 if(screen_fade.get() != NULL)
228 screen_fade->update(elapsed_time);
229 Console::instance->update(elapsed_time);
231 main_controller->update();
233 while(SDL_PollEvent(&event)) {
234 main_controller->process_event(event);
235 if(Menu::current() != NULL)
236 Menu::current()->event(event);
237 if(event.type == SDL_QUIT)
241 sound_manager->update();
243 // insert calls for debug (there are few rand calls otherwise)
244 if (0 && rand_prints++ % 20 == 0)
245 log_info << "== periodic rand() call " << systemRandom.rand() <<
246 " at frame " << rand_prints << "==" <<std::endl;