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