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