fixed 'uninitialised' warning
[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
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"
41 // binary fraction...
42 static const float LOGICAL_FPS = 64.0;
43
44 MainLoop* main_loop = NULL;
45
46 MainLoop::MainLoop()
47   : speed(1.0), nextpop(false), nextpush(false)
48 {
49   using namespace Scripting;
50   TimeScheduler::instance = new TimeScheduler();
51 }
52
53 MainLoop::~MainLoop()
54 {
55   using namespace Scripting;
56   delete TimeScheduler::instance;
57   TimeScheduler::instance = NULL;
58
59   for(std::vector<Screen*>::iterator i = screen_stack.begin();
60       i != screen_stack.end(); ++i) {
61     delete *i;
62   }
63 }
64
65 void
66 MainLoop::push_screen(Screen* screen, ScreenFade* screen_fade)
67 {
68   this->next_screen.reset(screen);
69   this->screen_fade.reset(screen_fade);
70   if(nextpop)
71     nextpush = false;
72   else
73     nextpush = true;
74   nextpop = false;
75   speed = 1.0;
76 }
77
78 void
79 MainLoop::exit_screen(ScreenFade* screen_fade)
80 {
81   next_screen.reset(NULL);
82   this->screen_fade.reset(screen_fade);
83   nextpop = true;
84   nextpush = false;
85 }
86
87 void
88 MainLoop::set_screen_fade(ScreenFade* screen_fade)
89 {
90   this->screen_fade.reset(screen_fade);
91 }
92
93 void
94 MainLoop::quit(ScreenFade* screen_fade)
95 {
96   for(std::vector<Screen*>::iterator i = screen_stack.begin();
97           i != screen_stack.end(); ++i)
98     delete *i;
99   screen_stack.clear();
100
101   exit_screen(screen_fade);
102 }
103
104 void
105 MainLoop::set_speed(float speed)
106 {
107   this->speed = speed;
108 }
109
110 void
111 MainLoop::draw_fps(DrawingContext& context, float fps_fps)
112 {
113   char str[60];
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);                    
118 }
119
120 void
121 MainLoop::run()
122 {
123   DrawingContext context; 
124   
125   unsigned int frame_count = 0;
126   float fps_fps = 0;
127   Uint32 fps_ticks = SDL_GetTicks();
128   Uint32 fps_nextframe_ticks = SDL_GetTicks();
129   Uint32 ticks;
130   bool skipdraw = false;
131   
132   running = true;
133   while(running) {
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();
138       }
139
140       if(nextpop) {
141         if(screen_stack.empty()) {
142           running = false;
143           break;
144         }
145         next_screen.reset(screen_stack.back());
146         screen_stack.pop_back();
147       }
148       if(nextpush && current_screen.get() != NULL) {
149         screen_stack.push_back(current_screen.release());
150       }
151  
152       nextpush = false;
153       nextpop = false;
154       speed = 1.0;
155       if(next_screen.get() != NULL)
156         next_screen->setup();
157       current_screen.reset(next_screen.release());
158       screen_fade.reset(NULL);
159
160       waiting_threads.wakeup();
161     }
162
163     if(!running || current_screen.get() == NULL)
164       break;
165       
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...
171         skipdraw = false;
172         fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
173       } else {
174         // don't draw all frames when we're getting too slow
175         skipdraw = true;
176       }
177     } else {
178       skipdraw = false;
179       while(fps_nextframe_ticks > ticks) {
180         /* just wait */
181         // If we really have to wait long, then do an imprecise SDL_Delay()
182         Uint32 diff = fps_nextframe_ticks - ticks;
183         if(diff > 10) {
184           SDL_Delay(diff - 2);
185         }
186         ticks = SDL_GetTicks();
187       }
188     }
189     fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
190
191     if(!skipdraw) {
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);
198
199       if(config->show_fps)
200         draw_fps(context, fps_fps);
201
202       context.do_drawing();
203
204       /* Calculate frames per second */
205       if(config->show_fps)
206       {
207         ++frame_count;
208         
209         if(SDL_GetTicks() - fps_ticks >= 500)
210         {
211           fps_fps = (float) frame_count / .5;
212           frame_count = 0;
213           fps_ticks = SDL_GetTicks();
214         }
215       }
216     }
217
218     real_time += elapsed_time;
219     elapsed_time *= speed;
220     game_time += elapsed_time;
221     
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);
228  
229     main_controller->update();
230     SDL_Event event;
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)
236         quit();
237     }
238
239     sound_manager->update();
240   }
241 }
242