Added F12 as shortcut for screenshots since Print key is captured by some window...
[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 "scripting/time_scheduler.hpp"
30 #include "scripting/squirrel_util.hpp"
31 #include "gameconfig.hpp"
32 #include "main.hpp"
33 #include "resources.hpp"
34 #include "screen.hpp"
35 #include "screen_fade.hpp"
36 #include "timer.hpp"
37 #include "player_status.hpp"
38 #include "video/renderer.hpp"
39 #include "random_generator.hpp"
40
41 // the engine will be run with a logical framerate of 64fps.
42 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
43 // binary fraction...
44 static const float LOGICAL_FPS = 64.0;
45 /** ticks (as returned from SDL_GetTicks) per frame */
46 static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS);
47 /** don't skip more than every 2nd frame */
48 static const int MAX_FRAME_SKIP = 2;
49
50 float game_speed = 1.0f;
51
52 MainLoop* main_loop = NULL;
53
54 MainLoop::MainLoop()
55   : speed(1.0), nextpop(false), nextpush(false), fps(0), screenshot_requested(false)
56 {
57   using namespace Scripting;
58   TimeScheduler::instance = new TimeScheduler();
59 }
60
61 MainLoop::~MainLoop()
62 {
63   using namespace Scripting;
64   delete TimeScheduler::instance;
65   TimeScheduler::instance = NULL;
66
67   for(std::vector<Screen*>::iterator i = screen_stack.begin();
68       i != screen_stack.end(); ++i) {
69     delete *i;
70   }
71 }
72
73 void
74 MainLoop::push_screen(Screen* screen, ScreenFade* screen_fade)
75 {
76   this->next_screen.reset(screen);
77   this->screen_fade.reset(screen_fade);
78   nextpush = !nextpop;
79   nextpop = false;
80   speed = 1.0;
81 }
82
83 void
84 MainLoop::exit_screen(ScreenFade* screen_fade)
85 {
86   next_screen.reset(NULL);
87   this->screen_fade.reset(screen_fade);
88   nextpop = true;
89   nextpush = false;
90 }
91
92 void
93 MainLoop::set_screen_fade(ScreenFade* screen_fade)
94 {
95   this->screen_fade.reset(screen_fade);
96 }
97
98 void
99 MainLoop::quit(ScreenFade* screen_fade)
100 {
101   for(std::vector<Screen*>::iterator i = screen_stack.begin();
102           i != screen_stack.end(); ++i)
103     delete *i;
104   screen_stack.clear();
105
106   exit_screen(screen_fade);
107 }
108
109 void
110 MainLoop::set_speed(float speed)
111 {
112   this->speed = speed;
113 }
114
115 float
116 MainLoop::get_speed() const
117 {
118   return speed;
119 }
120
121 void
122 MainLoop::draw_fps(DrawingContext& context, float fps_fps)
123 {
124   char str[60];
125   snprintf(str, sizeof(str), "%3.1f", fps_fps);
126   const char* fpstext = "FPS";
127   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), ALIGN_LEFT, LAYER_HUD);
128   context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
129 }
130
131 void
132 MainLoop::draw(DrawingContext& context)
133 {
134   static Uint32 fps_ticks = SDL_GetTicks();
135   static int frame_count = 0;
136
137   current_screen->draw(context);
138   if(Menu::current() != NULL)
139     Menu::current()->draw(context);
140   if(screen_fade.get() != NULL)
141     screen_fade->draw(context);
142   Console::instance->draw(context);
143
144   if(config->show_fps)
145     draw_fps(context, fps);
146
147   // if a screenshot was requested, pass request on to drawing_context
148   if (screenshot_requested) {
149     context.take_screenshot();
150     screenshot_requested = false;
151   }
152   context.do_drawing();
153
154   /* Calculate frames per second */
155   if(config->show_fps)
156   {
157     ++frame_count;
158
159     if(SDL_GetTicks() - fps_ticks >= 500)
160     {
161       fps = (float) frame_count / .5;
162       frame_count = 0;
163       fps_ticks = SDL_GetTicks();
164     }
165   }
166 }
167
168 void
169 MainLoop::update_gamelogic(float elapsed_time)
170 {
171   Scripting::update_debugger();
172   Scripting::TimeScheduler::instance->update(game_time);
173   current_screen->update(elapsed_time);
174   if(screen_fade.get() != NULL)
175     screen_fade->update(elapsed_time);
176   Console::instance->update(elapsed_time);
177 }
178
179 void
180 MainLoop::process_events()
181 {
182   main_controller->update();
183   Uint8* keystate = SDL_GetKeyState(NULL);
184   SDL_Event event;
185   while(SDL_PollEvent(&event)) 
186     {
187       main_controller->process_event(event);
188
189       if(Menu::current() != NULL)
190         Menu::current()->event(event);
191
192       switch(event.type)
193         {
194           case SDL_QUIT:
195             quit();
196             break;
197               
198           case SDL_VIDEORESIZE:
199             Renderer::instance()->resize(event.resize.w, event.resize.h);
200             Menu::recalc_pos();
201             break;
202             
203           case SDL_KEYDOWN:
204             if (event.key.keysym.sym == SDLK_F11) 
205               {
206                 config->use_fullscreen = !config->use_fullscreen;
207                 init_video();
208               }
209             else if (event.key.keysym.sym == SDLK_PRINT ||
210                      event.key.keysym.sym == SDLK_F12)
211               {
212                 take_screenshot();
213               }
214             else if (event.key.keysym.sym == SDLK_F1 &&
215                      (keystate[SDLK_LCTRL] || keystate[SDLK_RCTRL]) &&
216                      keystate[SDLK_c])
217               {
218                 Console::instance->toggle();
219                 config->console_enabled = true;
220                 config->save();
221               }
222             break;
223         }
224     }
225 }
226
227 void
228 MainLoop::handle_screen_switch()
229 {
230   while( (next_screen.get() != NULL || nextpop) &&
231       (screen_fade.get() == NULL || screen_fade->done())) {
232     if(current_screen.get() != NULL) {
233       current_screen->leave();
234     }
235
236     if(nextpop) {
237       if(screen_stack.empty()) {
238         running = false;
239         break;
240       }
241       next_screen.reset(screen_stack.back());
242       screen_stack.pop_back();
243     }
244     if(nextpush && current_screen.get() != NULL) {
245       screen_stack.push_back(current_screen.release());
246     }
247
248     nextpush = false;
249     nextpop = false;
250     speed = 1.0;
251     if(next_screen.get() != NULL)
252       next_screen->setup();
253     current_screen.reset(next_screen.release());
254     screen_fade.reset(NULL);
255
256     waiting_threads.wakeup();
257   }
258 }
259
260 void
261 MainLoop::run(DrawingContext &context)
262 {
263   Uint32 last_ticks = 0;
264   Uint32 elapsed_ticks = 0;
265
266   running = true;
267   while(running) {
268
269     handle_screen_switch();
270     if(!running || current_screen.get() == NULL)
271       break;
272
273     Uint32 ticks = SDL_GetTicks();
274     elapsed_ticks += ticks - last_ticks;
275     last_ticks = ticks;
276
277     Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * game_speed);
278
279     if (elapsed_ticks > ticks_per_frame*4) {
280       // when the game loads up or levels are switched the
281       // elapsed_ticks grows extremly large, so we just ignore those
282       // large time jumps
283       elapsed_ticks = 0;
284     }
285
286     int frames = 0;
287
288     if (elapsed_ticks > ticks_per_frame) 
289       {
290         while(elapsed_ticks > ticks_per_frame && frames < MAX_FRAME_SKIP) 
291           {
292             elapsed_ticks -= ticks_per_frame;
293             float timestep = 1.0 / LOGICAL_FPS;
294             real_time += timestep;
295             timestep *= speed;
296             game_time += timestep;
297
298             process_events();
299             update_gamelogic(timestep);
300             frames += 1;
301           }
302
303         draw(context);
304       }
305
306     sound_manager->update();
307
308     SDL_Delay(0);
309   }
310 }
311
312 void 
313 MainLoop::take_screenshot()
314 {
315   screenshot_requested = true;
316 }
317