Renamed MainLoop to ScreenManager
[supertux.git] / src / supertux / screen_manager.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "supertux/screen_manager.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "control/joystickkeyboardcontroller.hpp"
21 #include "gui/menu.hpp"
22 #include "gui/menu_manager.hpp"
23 #include "scripting/squirrel_util.hpp"
24 #include "scripting/time_scheduler.hpp"
25 #include "supertux/constants.hpp"
26 #include "supertux/console.hpp"
27 #include "supertux/gameconfig.hpp"
28 #include "supertux/globals.hpp"
29 #include "supertux/main.hpp"
30 #include "supertux/player_status.hpp"
31 #include "supertux/resources.hpp"
32 #include "supertux/screen_fade.hpp"
33 #include "supertux/screen.hpp"
34 #include "supertux/timer.hpp"
35 #include "video/drawing_context.hpp"
36 #include "video/renderer.hpp"
37
38 /** ticks (as returned from SDL_GetTicks) per frame */
39 static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS);
40 /** don't skip more than every 2nd frame */
41 static const int MAX_FRAME_SKIP = 2;
42
43 float g_game_speed = 1.0f;
44
45 ScreenManager::ScreenManager() :
46   waiting_threads(),
47   running(),
48   speed(1.0), 
49   nextpop(false), 
50   nextpush(false), 
51   fps(0), 
52   next_screen(),
53   current_screen(),
54   console(),
55   screen_fade(),
56   screen_stack(),
57   screenshot_requested(false)
58 {
59   using namespace Scripting;
60   TimeScheduler::instance = new TimeScheduler();
61 }
62
63 ScreenManager::~ScreenManager()
64 {
65   using namespace Scripting;
66   delete TimeScheduler::instance;
67   TimeScheduler::instance = NULL;
68
69   for(std::vector<Screen*>::iterator i = screen_stack.begin();
70       i != screen_stack.end(); ++i) {
71     delete *i;
72   }
73 }
74
75 void
76 ScreenManager::push_screen(Screen* screen, ScreenFade* screen_fade)
77 {
78   this->next_screen.reset(screen);
79   this->screen_fade.reset(screen_fade);
80   nextpush = !nextpop;
81   nextpop = false;
82   speed = 1.0f;
83 }
84
85 void
86 ScreenManager::exit_screen(ScreenFade* screen_fade)
87 {
88   next_screen.reset(NULL);
89   this->screen_fade.reset(screen_fade);
90   nextpop = true;
91   nextpush = false;
92 }
93
94 void
95 ScreenManager::set_screen_fade(ScreenFade* screen_fade)
96 {
97   this->screen_fade.reset(screen_fade);
98 }
99
100 void
101 ScreenManager::quit(ScreenFade* screen_fade)
102 {
103   for(std::vector<Screen*>::iterator i = screen_stack.begin();
104       i != screen_stack.end(); ++i)
105     delete *i;
106   screen_stack.clear();
107
108   exit_screen(screen_fade);
109 }
110
111 void
112 ScreenManager::set_speed(float speed)
113 {
114   this->speed = speed;
115 }
116
117 float
118 ScreenManager::get_speed() const
119 {
120   return speed;
121 }
122
123 bool
124 ScreenManager::has_no_pending_fadeout() const
125 {
126   return screen_fade.get() == NULL || screen_fade->done();
127 }
128
129 void
130 ScreenManager::draw_fps(DrawingContext& context, float fps_fps)
131 {
132   char str[60];
133   snprintf(str, sizeof(str), "%3.1f", fps_fps);
134   const char* fpstext = "FPS";
135   context.draw_text(Resources::small_font, fpstext, 
136                     Vector(SCREEN_WIDTH - Resources::small_font->get_text_width(fpstext) - Resources::small_font->get_text_width(" 99999") - BORDER_X, 
137                            BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD);
138   context.draw_text(Resources::small_font, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
139 }
140
141 void
142 ScreenManager::draw(DrawingContext& context)
143 {
144   static Uint32 fps_ticks = SDL_GetTicks();
145   static int frame_count = 0;
146
147   current_screen->draw(context);
148   if(MenuManager::current() != NULL)
149     MenuManager::current()->draw(context);
150   if(screen_fade.get() != NULL)
151     screen_fade->draw(context);
152   Console::instance->draw(context);
153
154   if(g_config->show_fps)
155     draw_fps(context, fps);
156
157   // if a screenshot was requested, pass request on to drawing_context
158   if (screenshot_requested) {
159     context.take_screenshot();
160     screenshot_requested = false;
161   }
162   context.do_drawing();
163
164   /* Calculate frames per second */
165   if(g_config->show_fps)
166   {
167     ++frame_count;
168
169     if(SDL_GetTicks() - fps_ticks >= 500)
170     {
171       fps = (float) frame_count / .5;
172       frame_count = 0;
173       fps_ticks = SDL_GetTicks();
174     }
175   }
176 }
177
178 void
179 ScreenManager::update_gamelogic(float elapsed_time)
180 {
181   Scripting::update_debugger();
182   Scripting::TimeScheduler::instance->update(game_time);
183   current_screen->update(elapsed_time);
184   if (MenuManager::current() != NULL)
185     MenuManager::current()->update();
186   if(screen_fade.get() != NULL)
187     screen_fade->update(elapsed_time);
188   Console::instance->update(elapsed_time);
189 }
190
191 void
192 ScreenManager::process_events()
193 {
194   g_main_controller->update();
195   Uint8* keystate = SDL_GetKeyState(NULL);
196   SDL_Event event;
197   while(SDL_PollEvent(&event)) 
198   {
199     g_main_controller->process_event(event);
200
201     if(MenuManager::current() != NULL)
202       MenuManager::current()->event(event);
203
204     switch(event.type)
205     {
206       case SDL_QUIT:
207         quit();
208         break;
209               
210       case SDL_VIDEORESIZE:
211         Renderer::instance()->resize(event.resize.w, event.resize.h);
212         MenuManager::recalc_pos();
213         break;
214             
215       case SDL_KEYDOWN:
216         if (event.key.keysym.sym == SDLK_F10)
217         {
218           g_config->show_fps = !g_config->show_fps;
219         }
220         if (event.key.keysym.sym == SDLK_F11) 
221         {
222           g_config->use_fullscreen = !g_config->use_fullscreen;
223           Renderer::instance()->apply_config();
224           MenuManager::recalc_pos();
225         }
226         else if (event.key.keysym.sym == SDLK_PRINT ||
227                  event.key.keysym.sym == SDLK_F12)
228         {
229           take_screenshot();
230         }
231         else if (event.key.keysym.sym == SDLK_F1 &&
232                  (keystate[SDLK_LCTRL] || keystate[SDLK_RCTRL]) &&
233                  keystate[SDLK_c])
234         {
235           Console::instance->toggle();
236           g_config->console_enabled = true;
237           g_config->save();
238         }
239         break;
240     }
241   }
242 }
243
244 void
245 ScreenManager::handle_screen_switch()
246 {
247   while( (next_screen.get() != NULL || nextpop) &&
248          has_no_pending_fadeout()) {
249     if(current_screen.get() != NULL) {
250       current_screen->leave();
251     }
252
253     if(nextpop) {
254       if(screen_stack.empty()) {
255         running = false;
256         break;
257       }
258       next_screen.reset(screen_stack.back());
259       screen_stack.pop_back();
260     }
261     if(nextpush && current_screen.get() != NULL) {
262       screen_stack.push_back(current_screen.release());
263     }
264
265     nextpush = false;
266     nextpop = false;
267     speed = 1.0;
268     Screen* next_screen_ptr = next_screen.release();
269     next_screen.reset(0);
270     if(next_screen_ptr)
271       next_screen_ptr->setup();
272     current_screen.reset(next_screen_ptr);
273     screen_fade.reset(NULL);
274
275     waiting_threads.wakeup();
276   }
277 }
278
279 void
280 ScreenManager::run(DrawingContext &context)
281 {
282   Uint32 last_ticks = 0;
283   Uint32 elapsed_ticks = 0;
284
285   running = true;
286   while(running) {
287
288     handle_screen_switch();
289     if(!running || current_screen.get() == NULL)
290       break;
291
292     Uint32 ticks = SDL_GetTicks();
293     elapsed_ticks += ticks - last_ticks;
294     last_ticks = ticks;
295
296     Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * g_game_speed);
297
298     if (elapsed_ticks > ticks_per_frame*4) {
299       // when the game loads up or levels are switched the
300       // elapsed_ticks grows extremely large, so we just ignore those
301       // large time jumps
302       elapsed_ticks = 0;
303     }
304
305     if(elapsed_ticks < ticks_per_frame)
306     {
307       Uint32 delay_ticks = ticks_per_frame - elapsed_ticks;
308       SDL_Delay(delay_ticks);
309       last_ticks += delay_ticks;
310       elapsed_ticks += delay_ticks;
311     }
312
313     int frames = 0;
314
315     while(elapsed_ticks >= ticks_per_frame && frames < MAX_FRAME_SKIP) 
316     {
317       elapsed_ticks -= ticks_per_frame;
318       float timestep = 1.0 / LOGICAL_FPS;
319       real_time += timestep;
320       timestep *= speed;
321       game_time += timestep;
322
323       process_events();
324       update_gamelogic(timestep);
325       frames += 1;
326     }
327
328     draw(context);
329
330     sound_manager->update();
331   }
332 }
333
334 void 
335 ScreenManager::take_screenshot()
336 {
337   screenshot_requested = true;
338 }
339
340 /* EOF */