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