Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[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 "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   if (screen_stack.size() < 1) {
68     quit();
69     return;
70   }
71   next_screen.reset(screen_stack.back());
72   nextpush = false;
73   screen_stack.pop_back();
74   speed = 1.0;
75 }
76
77 void
78 MainLoop::quit()
79 {
80   running = false;
81 }
82
83 void
84 MainLoop::set_speed(float speed)
85 {
86   this->speed = speed;
87 }
88
89 void
90 MainLoop::run()
91 {
92   DrawingContext context; 
93   
94   unsigned int frame_count;
95   float fps_fps;
96   Uint32 fps_ticks = SDL_GetTicks();
97   Uint32 fps_nextframe_ticks = SDL_GetTicks();
98   Uint32 ticks;
99   bool skipdraw = false;
100   
101   running = true;
102   while(running) {
103     if(next_screen.get() != NULL) {
104       if(nextpush && current_screen.get() != NULL) {
105         current_screen->leave();
106         screen_stack.push_back(current_screen.release());
107       }
108       
109       next_screen->setup();
110       ScriptManager::instance->fire_wakeup_event(ScriptManager::SCREEN_SWITCHED);
111       current_screen.reset(next_screen.release());
112       next_screen.reset(NULL);
113       nextpush = false;
114     }
115
116     if(current_screen.get() == NULL)
117         break;
118       
119     float elapsed_time = 1.0 / LOGICAL_FPS;
120     ticks = SDL_GetTicks();
121     if(ticks > fps_nextframe_ticks) {
122       if(skipdraw == true) {
123         // already skipped last frame? we have to slow down the game then...
124         skipdraw = false;
125         fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
126       } else {
127         // don't draw all frames when we're getting too slow
128         skipdraw = true;
129       }
130     } else {
131       skipdraw = false;
132       while(fps_nextframe_ticks > ticks) {
133         /* just wait */
134         // If we really have to wait long, then do an imprecise SDL_Delay()
135         Uint32 diff = fps_nextframe_ticks - ticks;
136         if(diff > 15) {
137           SDL_Delay(diff - 10);
138         }
139         ticks = SDL_GetTicks();
140       }
141     }
142     fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
143
144     if(!skipdraw) {
145       current_screen->draw(context);
146       if(Menu::current() != NULL)
147           Menu::current()->draw(context);
148       Console::instance->draw(context);
149
150       context.do_drawing();
151
152       /* Calculate frames per second */
153       if(config->show_fps)
154       {
155         ++frame_count;
156         
157         if(SDL_GetTicks() - fps_ticks >= 500)
158         {
159           fps_fps = (float) frame_count / .5;
160           frame_count = 0;
161           fps_ticks = SDL_GetTicks();
162         }
163       }
164     }
165
166     elapsed_time *= speed;
167
168     game_time += elapsed_time;
169     ScriptManager::instance->update();
170     current_screen->update(elapsed_time);
171     Console::instance->update(elapsed_time);
172  
173     main_controller->update();
174     SDL_Event event;
175     while(SDL_PollEvent(&event)) {
176       main_controller->process_event(event);
177       if(Menu::current() != NULL)
178         Menu::current()->event(event);
179       if(event.type == SDL_QUIT)
180         quit();
181     }
182
183     sound_manager->update();
184   }
185 }
186