fix cr/lfs and remove trailing whitespaces...
[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   if(nextpop)
72     nextpush = false;
73   else
74     nextpush = true;
75   nextpop = false;
76   speed = 1.0;
77 }
78
79 void
80 MainLoop::exit_screen(ScreenFade* screen_fade)
81 {
82   next_screen.reset(NULL);
83   this->screen_fade.reset(screen_fade);
84   nextpop = true;
85   nextpush = false;
86 }
87
88 void
89 MainLoop::set_screen_fade(ScreenFade* screen_fade)
90 {
91   this->screen_fade.reset(screen_fade);
92 }
93
94 void
95 MainLoop::quit(ScreenFade* screen_fade)
96 {
97   for(std::vector<Screen*>::iterator i = screen_stack.begin();
98           i != screen_stack.end(); ++i)
99     delete *i;
100   screen_stack.clear();
101
102   exit_screen(screen_fade);
103 }
104
105 void
106 MainLoop::set_speed(float speed)
107 {
108   this->speed = speed;
109 }
110
111 void
112 MainLoop::draw_fps(DrawingContext& context, float fps_fps)
113 {
114   char str[60];
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);
119 }
120
121 void
122 MainLoop::run()
123 {
124   DrawingContext context;
125
126   unsigned int frame_count = 0;
127   float fps_fps = 0;
128   Uint32 fps_ticks = SDL_GetTicks();
129   Uint32 fps_nextframe_ticks = SDL_GetTicks();
130   Uint32 ticks;
131   bool skipdraw = false;
132
133   running = true;
134   while(running) {
135     while( (next_screen.get() != NULL || nextpop == true) &&
136             (screen_fade.get() == NULL || screen_fade->done())) {
137       if(current_screen.get() != NULL) {
138         current_screen->leave();
139       }
140
141       if(nextpop) {
142         if(screen_stack.empty()) {
143           running = false;
144           break;
145         }
146         next_screen.reset(screen_stack.back());
147         screen_stack.pop_back();
148       }
149       if(nextpush && current_screen.get() != NULL) {
150         screen_stack.push_back(current_screen.release());
151       }
152
153       nextpush = false;
154       nextpop = false;
155       speed = 1.0;
156       if(next_screen.get() != NULL)
157         next_screen->setup();
158       current_screen.reset(next_screen.release());
159       screen_fade.reset(NULL);
160
161       waiting_threads.wakeup();
162     }
163
164     if(!running || current_screen.get() == NULL)
165       break;
166
167     float elapsed_time = 1.0 / LOGICAL_FPS;
168     ticks = SDL_GetTicks();
169     if(ticks > fps_nextframe_ticks) {
170       if(skipdraw == true) {
171         // already skipped last frame? we have to slow down the game then...
172         skipdraw = false;
173         fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
174       } else {
175         // don't draw all frames when we're getting too slow
176         skipdraw = true;
177       }
178     } else {
179       skipdraw = false;
180       while(fps_nextframe_ticks > ticks) {
181         /* just wait */
182         // If we really have to wait long, then do an imprecise SDL_Delay()
183         Uint32 diff = fps_nextframe_ticks - ticks;
184         if(diff > 10) {
185           SDL_Delay(diff - 2);
186         }
187         ticks = SDL_GetTicks();
188       }
189     }
190     fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
191
192     if(!skipdraw) {
193       current_screen->draw(context);
194       if(Menu::current() != NULL)
195         Menu::current()->draw(context);
196       if(screen_fade.get() != NULL)
197         screen_fade->draw(context);
198       Console::instance->draw(context);
199
200       if(config->show_fps)
201         draw_fps(context, fps_fps);
202
203       context.do_drawing();
204
205       /* Calculate frames per second */
206       if(config->show_fps)
207       {
208         ++frame_count;
209
210         if(SDL_GetTicks() - fps_ticks >= 500)
211         {
212           fps_fps = (float) frame_count / .5;
213           frame_count = 0;
214           fps_ticks = SDL_GetTicks();
215         }
216       }
217     }
218
219     real_time += elapsed_time;
220     elapsed_time *= speed;
221     game_time += elapsed_time;
222
223     Scripting::update_debugger();
224     Scripting::TimeScheduler::instance->update(game_time);
225     current_screen->update(elapsed_time);
226     if(screen_fade.get() != NULL)
227       screen_fade->update(elapsed_time);
228     Console::instance->update(elapsed_time);
229
230     main_controller->update();
231     SDL_Event event;
232     while(SDL_PollEvent(&event)) {
233       main_controller->process_event(event);
234       if(Menu::current() != NULL)
235         Menu::current()->event(event);
236       if(event.type == SDL_QUIT)
237         quit();
238     }
239
240     sound_manager->update();
241
242     //log_info << "== periodic rand() = " << systemRandom.rand() << std::endl;
243   }
244 }