- remove pointless leveltime from world1 levels
[supertux.git] / src / mainloop.cpp
1 //  $Id: worldmap.hpp 2800 2005-10-02 22:57:31Z matzebraun $
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 "gameconfig.hpp"
30 #include "main.hpp"
31 #include "resources.hpp"
32 #include "script_manager.hpp"
33 #include "screen.hpp"
34 #include "timer.hpp"
35
36 // the engine will be run with a logical framerate of 64fps.
37 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
38 // binary fraction...
39 static const float LOGICAL_FPS = 64.0;
40
41 MainLoop* main_loop = NULL;
42
43 MainLoop::MainLoop()
44   : speed(1.0)
45 {
46 }
47
48 MainLoop::~MainLoop()
49 {
50   for(std::vector<Screen*>::iterator i = screen_stack.begin();
51       i != screen_stack.end(); ++i) {
52     delete *i;
53   }
54 }
55
56 void
57 MainLoop::push_screen(Screen* screen)
58 {
59   this->next_screen.reset(screen);
60   nextpush = true;
61   speed = 1.0;
62 }
63
64 void
65 MainLoop::exit_screen()
66 {
67   next_screen.reset(screen_stack.back());
68   nextpush = false;
69   screen_stack.pop_back();
70   speed = 1.0;
71 }
72
73 void
74 MainLoop::quit()
75 {
76   running = false;
77 }
78
79 void
80 MainLoop::set_speed(float speed)
81 {
82   this->speed = speed;
83 }
84
85 void
86 MainLoop::run()
87 {
88   DrawingContext context; 
89   
90   unsigned int frame_count;
91   float fps_fps;
92   Uint32 fps_ticks = SDL_GetTicks();
93   Uint32 fps_nextframe_ticks = SDL_GetTicks();
94   Uint32 ticks;
95   bool skipdraw = false;
96   
97   running = true;
98   while(running) {
99     if(next_screen.get() != NULL) {
100       if(nextpush && current_screen.get() != NULL) {
101         current_screen->leave();
102         screen_stack.push_back(current_screen.release());
103       }
104       
105       next_screen->setup();
106       ScriptManager::instance->fire_wakeup_event(ScriptManager::SCREEN_SWITCHED);
107       current_screen.reset(next_screen.release());
108       next_screen.reset(NULL);
109       nextpush = false;
110     }
111
112     if(current_screen.get() == NULL)
113         break;
114       
115     float elapsed_time = 1.0 / LOGICAL_FPS;
116     ticks = SDL_GetTicks();
117     if(ticks > fps_nextframe_ticks) {
118       if(skipdraw == true) {
119         // already skipped last frame? we have to slow down the game then...
120         skipdraw = false;
121         fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
122       } else {
123         // don't draw all frames when we're getting too slow
124         skipdraw = true;
125       }
126     } else {
127       skipdraw = false;
128       while(fps_nextframe_ticks > ticks) {
129         /* just wait */
130         // If we really have to wait long, then do an imprecise SDL_Delay()
131         Uint32 diff = fps_nextframe_ticks - ticks;
132         if(diff > 15) {
133           SDL_Delay(diff - 10);
134         }
135         ticks = SDL_GetTicks();
136       }
137     }
138     fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
139
140     if(!skipdraw) {
141       current_screen->draw(context);
142       if(Menu::current() != NULL)
143           Menu::current()->draw(context);
144       Console::instance->draw(context);
145
146       context.do_drawing();
147
148       /* Calculate frames per second */
149       if(config->show_fps)
150       {
151         ++frame_count;
152         
153         if(SDL_GetTicks() - fps_ticks >= 500)
154         {
155           fps_fps = (float) frame_count / .5;
156           frame_count = 0;
157           fps_ticks = SDL_GetTicks();
158         }
159       }
160     }
161
162     elapsed_time *= speed;
163
164     game_time += elapsed_time;
165     ScriptManager::instance->update();
166     current_screen->update(elapsed_time);
167     Console::instance->update(elapsed_time);
168  
169     main_controller->update();
170     SDL_Event event;
171     while(SDL_PollEvent(&event)) {
172       main_controller->process_event(event);
173       if(Menu::current() != NULL)
174         Menu::current()->event(event);
175       if(event.type == SDL_QUIT)
176         quit();
177     }
178
179     sound_manager->update();
180   }
181 }
182