refactored some supertux mainloops
[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 "screen.hpp"
33 #include "timer.hpp"
34
35 // the engine will be run with a logical framerate of 64fps.
36 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
37 // binary fraction...
38 static const float LOGICAL_FPS = 64.0;
39
40 MainLoop* main_loop = NULL;
41
42 MainLoop::MainLoop()
43   : speed(1.0)
44 {
45   console.reset(new Console());
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)
101         screen_stack.push_back(current_screen.release());
102       
103       next_screen->setup();
104       current_screen.reset(next_screen.release());
105       next_screen.reset(NULL);
106       nextpush = false;
107     }
108
109     if(current_screen.get() == NULL)
110         break;
111       
112     float elapsed_time = 1.0 / LOGICAL_FPS;
113     ticks = SDL_GetTicks();
114     if(ticks > fps_nextframe_ticks) {
115       if(skipdraw == true) {
116         // already skipped last frame? we have to slow down the game then...
117         skipdraw = false;
118         fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
119       } else {
120         // don't draw all frames when we're getting too slow
121         skipdraw = true;
122       }
123     } else {
124       skipdraw = false;
125       while(fps_nextframe_ticks > ticks) {
126         /* just wait */
127         // If we really have to wait long, then do an imprecise SDL_Delay()
128         Uint32 diff = fps_nextframe_ticks - ticks;
129         if(diff > 15) {
130           SDL_Delay(diff - 10);
131         }
132         ticks = SDL_GetTicks();
133       }
134     }
135     fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
136
137     if(!skipdraw) {
138       current_screen->draw(context);
139       if(Menu::current() != NULL)
140           Menu::current()->draw(context);
141       console->draw(context);
142
143       context.do_drawing();
144
145       /* Calculate frames per second */
146       if(config->show_fps)
147       {
148         ++frame_count;
149         
150         if(SDL_GetTicks() - fps_ticks >= 500)
151         {
152           fps_fps = (float) frame_count / .5;
153           frame_count = 0;
154           fps_ticks = SDL_GetTicks();
155         }
156       }
157     }
158
159     elapsed_time *= speed;
160
161     game_time += elapsed_time;
162     current_screen->update(elapsed_time);
163
164     main_controller->update();
165     SDL_Event event;
166     while(SDL_PollEvent(&event)) {
167       main_controller->process_event(event);
168       if(Menu::current() != NULL)
169         Menu::current()->event(event);
170       if(event.type == SDL_QUIT)
171         quit();
172     }
173
174     sound_manager->update();
175   }
176 }
177