the boat works now
[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 #include "player_status.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 }
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   if (screen_stack.size() < 1) {
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::draw_fps(DrawingContext& context, float fps_fps)
92 {
93   char str[60];
94   snprintf(str, sizeof(str), "%3.1f", fps_fps);
95   const char* fpstext = "FPS";
96   context.draw_text(white_text, fpstext, Vector(SCREEN_WIDTH - white_text->get_text_width(fpstext) - gold_text->get_text_width(" 99999") - BORDER_X, BORDER_Y + 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
97   context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), RIGHT_ALLIGN, LAYER_FOREGROUND1);                    
98 }
99
100 void
101 MainLoop::run()
102 {
103   DrawingContext context; 
104   
105   unsigned int frame_count;
106   float fps_fps = 0;
107   Uint32 fps_ticks = SDL_GetTicks();
108   Uint32 fps_nextframe_ticks = SDL_GetTicks();
109   Uint32 ticks;
110   bool skipdraw = false;
111   
112   running = true;
113   while(running) {
114     if(next_screen.get() != NULL) {
115       if(nextpush && current_screen.get() != NULL) {
116         current_screen->leave();
117         screen_stack.push_back(current_screen.release());
118       }
119       
120       next_screen->setup();
121       ScriptManager::instance->fire_wakeup_event(ScriptManager::SCREEN_SWITCHED);
122       current_screen.reset(next_screen.release());
123       next_screen.reset(NULL);
124       nextpush = false;
125     }
126
127     if(current_screen.get() == NULL)
128         break;
129       
130     float elapsed_time = 1.0 / LOGICAL_FPS;
131     ticks = SDL_GetTicks();
132     if(ticks > fps_nextframe_ticks) {
133       if(skipdraw == true) {
134         // already skipped last frame? we have to slow down the game then...
135         skipdraw = false;
136         fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
137       } else {
138         // don't draw all frames when we're getting too slow
139         skipdraw = true;
140       }
141     } else {
142       skipdraw = false;
143       while(fps_nextframe_ticks > ticks) {
144         /* just wait */
145         // If we really have to wait long, then do an imprecise SDL_Delay()
146         Uint32 diff = fps_nextframe_ticks - ticks;
147         if(diff > 10) {
148           SDL_Delay(diff - 2);
149         }
150         ticks = SDL_GetTicks();
151       }
152     }
153     fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
154
155     if(!skipdraw) {
156       current_screen->draw(context);
157       if(Menu::current() != NULL)
158           Menu::current()->draw(context);
159       Console::instance->draw(context);
160
161       if(config->show_fps)
162         draw_fps(context, fps_fps);
163
164       context.do_drawing();
165
166       /* Calculate frames per second */
167       if(config->show_fps)
168       {
169         ++frame_count;
170         
171         if(SDL_GetTicks() - fps_ticks >= 500)
172         {
173           fps_fps = (float) frame_count / .5;
174           frame_count = 0;
175           fps_ticks = SDL_GetTicks();
176         }
177       }
178     }
179
180     elapsed_time *= speed;
181
182     game_time += elapsed_time;
183     ScriptManager::instance->update();
184     current_screen->update(elapsed_time);
185     Console::instance->update(elapsed_time);
186  
187     main_controller->update();
188     SDL_Event event;
189     while(SDL_PollEvent(&event)) {
190       main_controller->process_event(event);
191       if(Menu::current() != NULL)
192         Menu::current()->event(event);
193       if(event.type == SDL_QUIT)
194         quit();
195     }
196
197     sound_manager->update();
198   }
199 }
200