hopefully fixed the crash on exit, keep sectors script bundled in the sector and...
[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   console.reset(new Console());
47 }
48
49 MainLoop::~MainLoop()
50 {
51   for(std::vector<Screen*>::iterator i = screen_stack.begin();
52       i != screen_stack.end(); ++i) {
53     delete *i;
54   }
55 }
56
57 void
58 MainLoop::push_screen(Screen* screen)
59 {
60   this->next_screen.reset(screen);
61   nextpush = true;
62   speed = 1.0;
63 }
64
65 void
66 MainLoop::exit_screen()
67 {
68   next_screen.reset(screen_stack.back());
69   nextpush = false;
70   screen_stack.pop_back();
71   speed = 1.0;
72 }
73
74 void
75 MainLoop::quit()
76 {
77   running = false;
78 }
79
80 void
81 MainLoop::set_speed(float speed)
82 {
83   this->speed = speed;
84 }
85
86 void
87 MainLoop::run()
88 {
89   DrawingContext context; 
90   
91   unsigned int frame_count;
92   float fps_fps;
93   Uint32 fps_ticks = SDL_GetTicks();
94   Uint32 fps_nextframe_ticks = SDL_GetTicks();
95   Uint32 ticks;
96   bool skipdraw = false;
97   
98   running = true;
99   while(running) {
100     if(next_screen.get() != NULL) {
101       if(nextpush && current_screen.get() != NULL) {
102         current_screen->leave();
103         screen_stack.push_back(current_screen.release());
104       }
105       
106       next_screen->setup();
107       ScriptManager::instance->fire_wakeup_event(ScriptManager::SCREEN_SWITCHED);
108       current_screen.reset(next_screen.release());
109       next_screen.reset(NULL);
110       nextpush = false;
111     }
112
113     if(current_screen.get() == NULL)
114         break;
115       
116     float elapsed_time = 1.0 / LOGICAL_FPS;
117     ticks = SDL_GetTicks();
118     if(ticks > fps_nextframe_ticks) {
119       if(skipdraw == true) {
120         // already skipped last frame? we have to slow down the game then...
121         skipdraw = false;
122         fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
123       } else {
124         // don't draw all frames when we're getting too slow
125         skipdraw = true;
126       }
127     } else {
128       skipdraw = false;
129       while(fps_nextframe_ticks > ticks) {
130         /* just wait */
131         // If we really have to wait long, then do an imprecise SDL_Delay()
132         Uint32 diff = fps_nextframe_ticks - ticks;
133         if(diff > 15) {
134           SDL_Delay(diff - 10);
135         }
136         ticks = SDL_GetTicks();
137       }
138     }
139     fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
140
141     if(!skipdraw) {
142       current_screen->draw(context);
143       if(Menu::current() != NULL)
144           Menu::current()->draw(context);
145       console->draw(context);
146
147       context.do_drawing();
148
149       /* Calculate frames per second */
150       if(config->show_fps)
151       {
152         ++frame_count;
153         
154         if(SDL_GetTicks() - fps_ticks >= 500)
155         {
156           fps_fps = (float) frame_count / .5;
157           frame_count = 0;
158           fps_ticks = SDL_GetTicks();
159         }
160       }
161     }
162
163     elapsed_time *= speed;
164
165     game_time += elapsed_time;
166     ScriptManager::instance->update();
167     current_screen->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