55bc630ea08f43be05fe920edc984843bb8b4fd8
[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 /** 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;
48
49 MainLoop* main_loop = NULL;
50
51 MainLoop::MainLoop()
52   : speed(1.0), nextpop(false), nextpush(false)
53 {
54   using namespace Scripting;
55   TimeScheduler::instance = new TimeScheduler();
56 }
57
58 MainLoop::~MainLoop()
59 {
60   using namespace Scripting;
61   delete TimeScheduler::instance;
62   TimeScheduler::instance = NULL;
63
64   for(std::vector<Screen*>::iterator i = screen_stack.begin();
65       i != screen_stack.end(); ++i) {
66     delete *i;
67   }
68 }
69
70 void
71 MainLoop::push_screen(Screen* screen, ScreenFade* screen_fade)
72 {
73   this->next_screen.reset(screen);
74   this->screen_fade.reset(screen_fade);
75   nextpush = !nextpop;
76   nextpop = false;
77   speed = 1.0;
78 }
79
80 void
81 MainLoop::exit_screen(ScreenFade* screen_fade)
82 {
83   next_screen.reset(NULL);
84   this->screen_fade.reset(screen_fade);
85   nextpop = true;
86   nextpush = false;
87 }
88
89 void
90 MainLoop::set_screen_fade(ScreenFade* screen_fade)
91 {
92   this->screen_fade.reset(screen_fade);
93 }
94
95 void
96 MainLoop::quit(ScreenFade* screen_fade)
97 {
98   for(std::vector<Screen*>::iterator i = screen_stack.begin();
99           i != screen_stack.end(); ++i)
100     delete *i;
101   screen_stack.clear();
102
103   exit_screen(screen_fade);
104 }
105
106 void
107 MainLoop::set_speed(float speed)
108 {
109   this->speed = speed;
110 }
111
112 void
113 MainLoop::draw_fps(DrawingContext& context, float fps_fps)
114 {
115   char str[60];
116   snprintf(str, sizeof(str), "%3.1f", fps_fps);
117   const char* fpstext = "FPS";
118   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);
119   context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), RIGHT_ALLIGN, LAYER_HUD);
120 }
121
122 void
123 MainLoop::draw(DrawingContext& context)
124 {
125   static Uint32 fps_ticks = SDL_GetTicks();
126   static int frame_count = 0;
127
128   current_screen->draw(context);
129   if(Menu::current() != NULL)
130     Menu::current()->draw(context);
131   if(screen_fade.get() != NULL)
132     screen_fade->draw(context);
133   Console::instance->draw(context);
134
135   if(config->show_fps)
136     draw_fps(context, fps);
137
138   context.do_drawing();
139
140   /* Calculate frames per second */
141   if(config->show_fps)
142   {
143     ++frame_count;
144
145     if(SDL_GetTicks() - fps_ticks >= 500)
146     {
147       fps = (float) frame_count / .5;
148       frame_count = 0;
149       fps_ticks = SDL_GetTicks();
150     }
151   }
152 }
153
154 void
155 MainLoop::update_gamelogic(float elapsed_time)
156 {
157   Scripting::update_debugger();
158   Scripting::TimeScheduler::instance->update(game_time);
159   current_screen->update(elapsed_time);
160   if(screen_fade.get() != NULL)
161     screen_fade->update(elapsed_time);
162   Console::instance->update(elapsed_time);
163 }
164
165 void
166 MainLoop::process_events()
167 {
168   main_controller->update();
169   SDL_Event event;
170   while(SDL_PollEvent(&event)) {
171     main_controller->process_event(event);
172     if(Menu::current() != NULL)
173       Menu::current()->event(event);
174     if(event.type == SDL_QUIT)
175       quit();
176   }
177 }
178
179 void
180 MainLoop::handle_screen_switch()
181 {
182   while( (next_screen.get() != NULL || nextpop) &&
183       (screen_fade.get() == NULL || screen_fade->done())) {
184     if(current_screen.get() != NULL) {
185       current_screen->leave();
186     }
187
188     if(nextpop) {
189       if(screen_stack.empty()) {
190         running = false;
191         break;
192       }
193       next_screen.reset(screen_stack.back());
194       screen_stack.pop_back();
195     }
196     if(nextpush && current_screen.get() != NULL) {
197       screen_stack.push_back(current_screen.release());
198     }
199
200     nextpush = false;
201     nextpop = false;
202     speed = 1.0;
203     if(next_screen.get() != NULL)
204       next_screen->setup();
205     current_screen.reset(next_screen.release());
206     screen_fade.reset(NULL);
207
208     waiting_threads.wakeup();
209   }
210 }
211
212 void
213 MainLoop::run()
214 {
215   DrawingContext context;
216
217   Uint32 last_ticks = 0;
218   Uint32 elapsed_ticks = 0;
219
220   running = true;
221   while(running) {
222
223     handle_screen_switch();
224     if(!running || current_screen.get() == NULL)
225       break;
226
227     Uint32 ticks = SDL_GetTicks();
228     elapsed_ticks += ticks - last_ticks;
229     last_ticks = ticks;
230
231     if (elapsed_ticks > TICKS_PER_FRAME*4) {
232       // when the game loads up or levels are switched the
233       // elapsed_ticks grows extremly large, so we just ignore those
234       // large time jumps
235       elapsed_ticks = 0;
236     }
237
238     int frames = 0;
239
240     if (elapsed_ticks > TICKS_PER_FRAME) { 
241       while(elapsed_ticks > TICKS_PER_FRAME && frames < MAX_FRAME_SKIP) {
242         elapsed_ticks -= TICKS_PER_FRAME;
243         float timestep = 1.0 / LOGICAL_FPS;
244         real_time += timestep;
245         timestep *= speed;
246         game_time += timestep;
247
248         process_events();
249         update_gamelogic(timestep);
250         frames += 1;
251       }
252
253       draw(context);
254     }
255
256     sound_manager->update();
257
258     SDL_Delay(0);
259   }
260 }