more small adjustments from Tron
[supertux.git] / src / mainloop.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19 #include <config.h>
20
21 #include "mainloop.hpp"
22
23 #include <stdlib.h>
24 #include <SDL.h>
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 "main.hpp"
33 #include "resources.hpp"
34 #include "screen.hpp"
35 #include "screen_fade.hpp"
36 #include "timer.hpp"
37 #include "player_status.hpp"
38 #include "random_generator.hpp"
39
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"
42 // binary fraction...
43 static const float LOGICAL_FPS = 64.0;
44
45 MainLoop* main_loop = NULL;
46
47 MainLoop::MainLoop()
48   : speed(1.0), nextpop(false), nextpush(false)
49 {
50   using namespace Scripting;
51   TimeScheduler::instance = new TimeScheduler();
52 }
53
54 MainLoop::~MainLoop()
55 {
56   using namespace Scripting;
57   delete TimeScheduler::instance;
58   TimeScheduler::instance = NULL;
59
60   for(std::vector<Screen*>::iterator i = screen_stack.begin();
61       i != screen_stack.end(); ++i) {
62     delete *i;
63   }
64 }
65
66 void
67 MainLoop::push_screen(Screen* screen, ScreenFade* screen_fade)
68 {
69   this->next_screen.reset(screen);
70   this->screen_fade.reset(screen_fade);
71   nextpush = nextpop != NULL;
72   nextpop = false;
73   speed = 1.0;
74 }
75
76 void
77 MainLoop::exit_screen(ScreenFade* screen_fade)
78 {
79   next_screen.reset(NULL);
80   this->screen_fade.reset(screen_fade);
81   nextpop = true;
82   nextpush = false;
83 }
84
85 void
86 MainLoop::set_screen_fade(ScreenFade* screen_fade)
87 {
88   this->screen_fade.reset(screen_fade);
89 }
90
91 void
92 MainLoop::quit(ScreenFade* screen_fade)
93 {
94   for(std::vector<Screen*>::iterator i = screen_stack.begin();
95           i != screen_stack.end(); ++i)
96     delete *i;
97   screen_stack.clear();
98
99   exit_screen(screen_fade);
100 }
101
102 void
103 MainLoop::set_speed(float speed)
104 {
105   this->speed = speed;
106 }
107
108 void
109 MainLoop::draw_fps(DrawingContext& context, float fps_fps)
110 {
111   char str[60];
112   snprintf(str, sizeof(str), "%3.1f", fps_fps);
113   const char* fpstext = "FPS";
114   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_HUD);
115   context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), RIGHT_ALLIGN, LAYER_HUD);
116 }
117
118 void
119 MainLoop::run()
120 {
121   DrawingContext context;
122
123   unsigned int frame_count = 0;
124   float fps_fps = 0;
125   Uint32 fps_ticks = SDL_GetTicks();
126   Uint32 fps_nextframe_ticks = SDL_GetTicks();
127   Uint32 ticks;
128   bool skipdraw = false;
129
130   running = true;
131   while(running) {
132     while( (next_screen.get() != NULL || nextpop) &&
133             (screen_fade.get() == NULL || screen_fade->done())) {
134       if(current_screen.get() != NULL) {
135         current_screen->leave();
136       }
137
138       if(nextpop) {
139         if(screen_stack.empty()) {
140           running = false;
141           break;
142         }
143         next_screen.reset(screen_stack.back());
144         screen_stack.pop_back();
145       }
146       if(nextpush && current_screen.get() != NULL) {
147         screen_stack.push_back(current_screen.release());
148       }
149
150       nextpush = false;
151       nextpop = false;
152       speed = 1.0;
153       if(next_screen.get() != NULL)
154         next_screen->setup();
155       current_screen.reset(next_screen.release());
156       screen_fade.reset(NULL);
157
158       waiting_threads.wakeup();
159     }
160
161     if(!running || current_screen.get() == NULL)
162       break;
163
164     float elapsed_time = 1.0 / LOGICAL_FPS;
165     ticks = SDL_GetTicks();
166     if(ticks > fps_nextframe_ticks) {
167       if(skipdraw) {
168         // already skipped last frame? we have to slow down the game then...
169         skipdraw = false;
170         fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
171       } else {
172         // don't draw all frames when we're getting too slow
173         skipdraw = true;
174       }
175     } else {
176       skipdraw = false;
177       while(fps_nextframe_ticks > ticks) {
178         /* just wait */
179         // If we really have to wait long, then do an imprecise SDL_Delay()
180         Uint32 diff = fps_nextframe_ticks - ticks;
181         if(diff > 10) {
182           SDL_Delay(diff - 2);
183         }
184         ticks = SDL_GetTicks();
185       }
186     }
187     fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
188
189     if(!skipdraw) {
190       current_screen->draw(context);
191       if(Menu::current() != NULL)
192         Menu::current()->draw(context);
193       if(screen_fade.get() != NULL)
194         screen_fade->draw(context);
195       Console::instance->draw(context);
196
197       if(config->show_fps)
198         draw_fps(context, fps_fps);
199
200       context.do_drawing();
201
202       /* Calculate frames per second */
203       if(config->show_fps)
204       {
205         ++frame_count;
206
207         if(SDL_GetTicks() - fps_ticks >= 500)
208         {
209           fps_fps = (float) frame_count / .5;
210           frame_count = 0;
211           fps_ticks = SDL_GetTicks();
212         }
213       }
214     }
215
216     real_time += elapsed_time;
217     elapsed_time *= speed;
218     game_time += elapsed_time;
219
220     Scripting::update_debugger();
221     Scripting::TimeScheduler::instance->update(game_time);
222     current_screen->update(elapsed_time);
223     if(screen_fade.get() != NULL)
224       screen_fade->update(elapsed_time);
225     Console::instance->update(elapsed_time);
226
227     main_controller->update();
228     SDL_Event event;
229     while(SDL_PollEvent(&event)) {
230       main_controller->process_event(event);
231       if(Menu::current() != NULL)
232         Menu::current()->event(event);
233       if(event.type == SDL_QUIT)
234         quit();
235     }
236
237     sound_manager->update();
238
239     //log_info << "== periodic rand() = " << systemRandom.rand() << std::endl;
240   }
241 }