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