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* g_screen_manager = NULL;
46
47 ScreenManager::ScreenManager() :
48   waiting_threads(),
49   running(),
50   speed(1.0), 
51   nextpop(false), 
52   nextpush(false), 
53   fps(0), 
54   next_screen(),
55   current_screen(),
56   console(),
57   screen_fade(),
58   screen_stack(),
59   screenshot_requested(false)
60 {
61   using namespace Scripting;
62   TimeScheduler::instance = new TimeScheduler();
63 }
64
65 ScreenManager::~ScreenManager()
66 {
67   using namespace Scripting;
68   delete TimeScheduler::instance;
69   TimeScheduler::instance = NULL;
70
71   for(std::vector<Screen*>::iterator i = screen_stack.begin();
72       i != screen_stack.end(); ++i) {
73     delete *i;
74   }
75 }
76
77 void
78 ScreenManager::push_screen(Screen* screen, ScreenFade* screen_fade)
79 {
80   this->next_screen.reset(screen);
81   this->screen_fade.reset(screen_fade);
82   nextpush = !nextpop;
83   nextpop = false;
84   speed = 1.0f;
85 }
86
87 void
88 ScreenManager::exit_screen(ScreenFade* screen_fade)
89 {
90   next_screen.reset(NULL);
91   this->screen_fade.reset(screen_fade);
92   nextpop = true;
93   nextpush = false;
94 }
95
96 void
97 ScreenManager::set_screen_fade(ScreenFade* screen_fade)
98 {
99   this->screen_fade.reset(screen_fade);
100 }
101
102 void
103 ScreenManager::quit(ScreenFade* screen_fade)
104 {
105   for(std::vector<Screen*>::iterator i = screen_stack.begin();
106       i != screen_stack.end(); ++i)
107     delete *i;
108   screen_stack.clear();
109
110   exit_screen(screen_fade);
111 }
112
113 void
114 ScreenManager::set_speed(float speed)
115 {
116   this->speed = speed;
117 }
118
119 float
120 ScreenManager::get_speed() const
121 {
122   return speed;
123 }
124
125 bool
126 ScreenManager::has_no_pending_fadeout() const
127 {
128   return screen_fade.get() == NULL || screen_fade->done();
129 }
130
131 void
132 ScreenManager::draw_fps(DrawingContext& context, float fps_fps)
133 {
134   char str[60];
135   snprintf(str, sizeof(str), "%3.1f", fps_fps);
136   const char* fpstext = "FPS";
137   context.draw_text(Resources::small_font, fpstext, 
138                     Vector(SCREEN_WIDTH - Resources::small_font->get_text_width(fpstext) - Resources::small_font->get_text_width(" 99999") - BORDER_X, 
139                            BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD);
140   context.draw_text(Resources::small_font, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
141 }
142
143 void
144 ScreenManager::draw(DrawingContext& context)
145 {
146   static Uint32 fps_ticks = SDL_GetTicks();
147   static int frame_count = 0;
148
149   current_screen->draw(context);
150   if(MenuManager::current() != NULL)
151     MenuManager::current()->draw(context);
152   if(screen_fade.get() != NULL)
153     screen_fade->draw(context);
154   Console::instance->draw(context);
155
156   if(g_config->show_fps)
157     draw_fps(context, fps);
158
159   // if a screenshot was requested, pass request on to drawing_context
160   if (screenshot_requested) {
161     context.take_screenshot();
162     screenshot_requested = false;
163   }
164   context.do_drawing();
165
166   /* Calculate frames per second */
167   if(g_config->show_fps)
168   {
169     ++frame_count;
170
171     if(SDL_GetTicks() - fps_ticks >= 500)
172     {
173       fps = (float) frame_count / .5;
174       frame_count = 0;
175       fps_ticks = SDL_GetTicks();
176     }
177   }
178 }
179
180 void
181 ScreenManager::update_gamelogic(float elapsed_time)
182 {
183   Scripting::update_debugger();
184   Scripting::TimeScheduler::instance->update(game_time);
185   current_screen->update(elapsed_time);
186   if (MenuManager::current() != NULL)
187     MenuManager::current()->update();
188   if(screen_fade.get() != NULL)
189     screen_fade->update(elapsed_time);
190   Console::instance->update(elapsed_time);
191 }
192
193 void
194 ScreenManager::process_events()
195 {
196   g_main_controller->update();
197   Uint8* keystate = SDL_GetKeyState(NULL);
198   SDL_Event event;
199   while(SDL_PollEvent(&event)) 
200   {
201     g_main_controller->process_event(event);
202
203     if(MenuManager::current() != NULL)
204       MenuManager::current()->event(event);
205
206     switch(event.type)
207     {
208       case SDL_QUIT:
209         quit();
210         break;
211               
212       case SDL_VIDEORESIZE:
213         Renderer::instance()->resize(event.resize.w, event.resize.h);
214         MenuManager::recalc_pos();
215         break;
216             
217       case SDL_KEYDOWN:
218         if (event.key.keysym.sym == SDLK_F10)
219         {
220           g_config->show_fps = !g_config->show_fps;
221         }
222         if (event.key.keysym.sym == SDLK_F11) 
223         {
224           g_config->use_fullscreen = !g_config->use_fullscreen;
225           Renderer::instance()->apply_config();
226           MenuManager::recalc_pos();
227         }
228         else if (event.key.keysym.sym == SDLK_PRINT ||
229                  event.key.keysym.sym == SDLK_F12)
230         {
231           take_screenshot();
232         }
233         else if (event.key.keysym.sym == SDLK_F1 &&
234                  (keystate[SDLK_LCTRL] || keystate[SDLK_RCTRL]) &&
235                  keystate[SDLK_c])
236         {
237           Console::instance->toggle();
238           g_config->console_enabled = true;
239           g_config->save();
240         }
241         break;
242     }
243   }
244 }
245
246 void
247 ScreenManager::handle_screen_switch()
248 {
249   while( (next_screen.get() != NULL || nextpop) &&
250          has_no_pending_fadeout()) {
251     if(current_screen.get() != NULL) {
252       current_screen->leave();
253     }
254
255     if(nextpop) {
256       if(screen_stack.empty()) {
257         running = false;
258         break;
259       }
260       next_screen.reset(screen_stack.back());
261       screen_stack.pop_back();
262     }
263     if(nextpush && current_screen.get() != NULL) {
264       screen_stack.push_back(current_screen.release());
265     }
266
267     nextpush = false;
268     nextpop = false;
269     speed = 1.0;
270     Screen* next_screen_ptr = next_screen.release();
271     next_screen.reset(0);
272     if(next_screen_ptr)
273       next_screen_ptr->setup();
274     current_screen.reset(next_screen_ptr);
275     screen_fade.reset(NULL);
276
277     waiting_threads.wakeup();
278   }
279 }
280
281 void
282 ScreenManager::run(DrawingContext &context)
283 {
284   Uint32 last_ticks = 0;
285   Uint32 elapsed_ticks = 0;
286
287   running = true;
288   while(running) {
289
290     handle_screen_switch();
291     if(!running || current_screen.get() == NULL)
292       break;
293
294     Uint32 ticks = SDL_GetTicks();
295     elapsed_ticks += ticks - last_ticks;
296     last_ticks = ticks;
297
298     Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * g_game_speed);
299
300     if (elapsed_ticks > ticks_per_frame*4) {
301       // when the game loads up or levels are switched the
302       // elapsed_ticks grows extremely large, so we just ignore those
303       // large time jumps
304       elapsed_ticks = 0;
305     }
306
307     if(elapsed_ticks < ticks_per_frame)
308     {
309       Uint32 delay_ticks = ticks_per_frame - elapsed_ticks;
310       SDL_Delay(delay_ticks);
311       last_ticks += delay_ticks;
312       elapsed_ticks += delay_ticks;
313     }
314
315     int frames = 0;
316
317     while(elapsed_ticks >= ticks_per_frame && frames < MAX_FRAME_SKIP) 
318     {
319       elapsed_ticks -= ticks_per_frame;
320       float timestep = 1.0 / LOGICAL_FPS;
321       real_time += timestep;
322       timestep *= speed;
323       game_time += timestep;
324
325       process_events();
326       update_gamelogic(timestep);
327       frames += 1;
328     }
329
330     draw(context);
331
332     sound_manager->update();
333   }
334 }
335
336 void 
337 ScreenManager::take_screenshot()
338 {
339   screenshot_requested = true;
340 }
341
342 /* EOF */