Merge branch 'feature/menu-cleanup'
[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/input_manager.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/console.hpp"
26 #include "supertux/constants.hpp"
27 #include "supertux/gameconfig.hpp"
28 #include "supertux/globals.hpp"
29 #include "supertux/main.hpp"
30 #include "supertux/menu/menu_storage.hpp"
31 #include "supertux/player_status.hpp"
32 #include "supertux/resources.hpp"
33 #include "supertux/screen.hpp"
34 #include "supertux/screen_fade.hpp"
35 #include "supertux/timer.hpp"
36 #include "video/drawing_context.hpp"
37 #include "video/renderer.hpp"
38
39 #include <stdio.h>
40 /** ticks (as returned from SDL_GetTicks) per frame */
41 static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS);
42 /** don't skip more than every 2nd frame */
43 static const int MAX_FRAME_SKIP = 2;
44
45 ScreenManager::ScreenManager() :
46   waiting_threads(),
47   m_menu_storage(new MenuStorage),
48   m_menu_manager(new MenuManager),
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
72 void
73 ScreenManager::push_screen(std::unique_ptr<Screen> screen, std::unique_ptr<ScreenFade> screen_fade)
74 {
75   assert(!this->next_screen);
76   this->next_screen = std::move(screen);
77   this->screen_fade = std::move(screen_fade);
78   nextpush = !nextpop;
79   nextpop = false;
80   speed = 1.0f;
81 }
82
83 void
84 ScreenManager::exit_screen(std::unique_ptr<ScreenFade> screen_fade)
85 {
86   next_screen.reset();
87   this->screen_fade = std::move(screen_fade);
88   nextpop = true;
89   nextpush = false;
90 }
91
92 void
93 ScreenManager::set_screen_fade(std::unique_ptr<ScreenFade> screen_fade)
94 {
95   this->screen_fade = std::move(screen_fade);
96 }
97
98 void
99 ScreenManager::quit(std::unique_ptr<ScreenFade> screen_fade)
100 {
101   screen_stack.clear();
102   exit_screen(std::move(screen_fade));
103 }
104
105 void
106 ScreenManager::set_speed(float speed)
107 {
108   this->speed = speed;
109 }
110
111 float
112 ScreenManager::get_speed() const
113 {
114   return speed;
115 }
116
117 bool
118 ScreenManager::has_no_pending_fadeout() const
119 {
120   return screen_fade.get() == NULL || screen_fade->done();
121 }
122
123 void
124 ScreenManager::draw_fps(DrawingContext& context, float fps_fps)
125 {
126   char str[60];
127   snprintf(str, sizeof(str), "%3.1f", fps_fps);
128   const char* fpstext = "FPS";
129   context.draw_text(Resources::small_font, fpstext, 
130                     Vector(SCREEN_WIDTH - Resources::small_font->get_text_width(fpstext) - Resources::small_font->get_text_width(" 99999") - BORDER_X, 
131                            BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD);
132   context.draw_text(Resources::small_font, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
133 }
134
135 void
136 ScreenManager::draw(DrawingContext& context)
137 {
138   static Uint32 fps_ticks = SDL_GetTicks();
139   static int frame_count = 0;
140
141   current_screen->draw(context);
142   if(MenuManager::instance().current() != NULL)
143     MenuManager::instance().current()->draw(context);
144   if(screen_fade.get() != NULL)
145     screen_fade->draw(context);
146   Console::instance->draw(context);
147
148   if(g_config->show_fps)
149     draw_fps(context, fps);
150
151   // if a screenshot was requested, pass request on to drawing_context
152   if (screenshot_requested) {
153     context.take_screenshot();
154     screenshot_requested = false;
155   }
156   context.do_drawing();
157
158   /* Calculate frames per second */
159   if(g_config->show_fps)
160   {
161     ++frame_count;
162
163     if(SDL_GetTicks() - fps_ticks >= 500)
164     {
165       fps = (float) frame_count / .5;
166       frame_count = 0;
167       fps_ticks = SDL_GetTicks();
168     }
169   }
170 }
171
172 void
173 ScreenManager::update_gamelogic(float elapsed_time)
174 {
175   scripting::update_debugger();
176   scripting::TimeScheduler::instance->update(game_time);
177   current_screen->update(elapsed_time);
178   if (MenuManager::instance().current() != NULL)
179     MenuManager::instance().current()->update();
180   if(screen_fade.get() != NULL)
181     screen_fade->update(elapsed_time);
182   Console::instance->update(elapsed_time);
183 }
184
185 void
186 ScreenManager::process_events()
187 {
188   g_input_manager->update();
189   SDL_Event event;
190   while(SDL_PollEvent(&event)) 
191   {
192     g_input_manager->process_event(event);
193
194     if(MenuManager::instance().current() != NULL)
195       MenuManager::instance().current()->event(event);
196
197     switch(event.type)
198     {
199       case SDL_QUIT:
200         quit();
201         break;
202               
203       case SDL_WINDOWEVENT:
204         switch(event.window.event)
205         {
206           case SDL_WINDOWEVENT_RESIZED:
207             Renderer::instance()->resize(event.window.data1,
208                                          event.window.data2);
209             MenuManager::instance().recalc_pos();
210             break;
211         }
212         break;
213             
214       case SDL_KEYDOWN:
215         if (event.key.keysym.sym == SDLK_F10)
216         {
217           g_config->show_fps = !g_config->show_fps;
218         }
219         if (event.key.keysym.sym == SDLK_F11) 
220         {
221           g_config->use_fullscreen = !g_config->use_fullscreen;
222           Renderer::instance()->apply_config();
223           MenuManager::instance().recalc_pos();
224         }
225         else if (event.key.keysym.sym == SDLK_PRINTSCREEN ||
226                  event.key.keysym.sym == SDLK_F12)
227         {
228           take_screenshot();
229         }
230         else if (event.key.keysym.sym == SDLK_F1 &&
231                  event.key.keysym.mod & KMOD_CTRL)
232         {
233           Console::instance->toggle();
234           g_config->console_enabled = true;
235           g_config->save();
236         }
237         break;
238     }
239   }
240 }
241
242 void
243 ScreenManager::handle_screen_switch()
244 {
245   while((next_screen || nextpop) &&
246         has_no_pending_fadeout())
247   {
248     if(current_screen) {
249       current_screen->leave();
250     }
251
252     if(nextpop) {
253       if(screen_stack.empty()) {
254         running = false;
255         break;
256       }
257       next_screen = std::move(screen_stack.back());
258       screen_stack.pop_back();
259     }
260     if(nextpush && current_screen) {
261       screen_stack.push_back(std::move(current_screen));
262     }
263
264     nextpush = false;
265     nextpop = false;
266     speed = 1.0;
267     current_screen = std::move(next_screen);
268     if(current_screen)
269       current_screen->setup();
270     screen_fade.reset();
271
272     waiting_threads.wakeup();
273   }
274 }
275
276 void
277 ScreenManager::run(DrawingContext &context)
278 {
279   Uint32 last_ticks = 0;
280   Uint32 elapsed_ticks = 0;
281
282   running = true;
283   while(running) {
284
285     handle_screen_switch();
286     if(!running || !current_screen)
287       break;
288
289     Uint32 ticks = SDL_GetTicks();
290     elapsed_ticks += ticks - last_ticks;
291     last_ticks = ticks;
292
293     Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * g_game_speed);
294
295     if (elapsed_ticks > ticks_per_frame*4) {
296       // when the game loads up or levels are switched the
297       // elapsed_ticks grows extremely large, so we just ignore those
298       // large time jumps
299       elapsed_ticks = 0;
300     }
301
302     if(elapsed_ticks < ticks_per_frame)
303     {
304       Uint32 delay_ticks = ticks_per_frame - elapsed_ticks;
305       SDL_Delay(delay_ticks);
306       last_ticks += delay_ticks;
307       elapsed_ticks += delay_ticks;
308     }
309
310     int frames = 0;
311
312     while(elapsed_ticks >= ticks_per_frame && frames < MAX_FRAME_SKIP) 
313     {
314       elapsed_ticks -= ticks_per_frame;
315       float timestep = 1.0 / LOGICAL_FPS;
316       real_time += timestep;
317       timestep *= speed;
318       game_time += timestep;
319
320       process_events();
321       update_gamelogic(timestep);
322       frames += 1;
323     }
324
325     draw(context);
326
327     sound_manager->update();
328   }
329 }
330
331 void 
332 ScreenManager::take_screenshot()
333 {
334   screenshot_requested = true;
335 }
336
337 /* EOF */