1 // $Id: worldmap.hpp 2800 2005-10-02 22:57:31Z matzebraun $
4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "mainloop.hpp"
25 #include "video/drawing_context.hpp"
26 #include "control/joystickkeyboardcontroller.hpp"
27 #include "gui/menu.hpp"
28 #include "audio/sound_manager.hpp"
29 #include "script_manager.hpp"
30 #include "gameconfig.hpp"
32 #include "resources.hpp"
33 #include "script_manager.hpp"
37 // the engine will be run with a logical framerate of 64fps.
38 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
40 static const float LOGICAL_FPS = 64.0;
42 MainLoop* main_loop = NULL;
47 console.reset(new Console());
52 for(std::vector<Screen*>::iterator i = screen_stack.begin();
53 i != screen_stack.end(); ++i) {
59 MainLoop::push_screen(Screen* screen)
61 this->next_screen.reset(screen);
67 MainLoop::exit_screen()
69 next_screen.reset(screen_stack.back());
71 screen_stack.pop_back();
82 MainLoop::set_speed(float speed)
90 DrawingContext context;
92 unsigned int frame_count;
94 Uint32 fps_ticks = SDL_GetTicks();
95 Uint32 fps_nextframe_ticks = SDL_GetTicks();
97 bool skipdraw = false;
101 if(next_screen.get() != NULL) {
102 if(nextpush && current_screen.get() != NULL) {
103 current_screen->leave();
104 screen_stack.push_back(current_screen.release());
107 next_screen->setup();
108 script_manager->fire_wakeup_event(ScriptManager::SCREEN_SWITCHED);
109 current_screen.reset(next_screen.release());
110 next_screen.reset(NULL);
114 if(current_screen.get() == NULL)
117 float elapsed_time = 1.0 / LOGICAL_FPS;
118 ticks = SDL_GetTicks();
119 if(ticks > fps_nextframe_ticks) {
120 if(skipdraw == true) {
121 // already skipped last frame? we have to slow down the game then...
123 fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
125 // don't draw all frames when we're getting too slow
130 while(fps_nextframe_ticks > ticks) {
132 // If we really have to wait long, then do an imprecise SDL_Delay()
133 Uint32 diff = fps_nextframe_ticks - ticks;
135 SDL_Delay(diff - 10);
137 ticks = SDL_GetTicks();
140 fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
143 current_screen->draw(context);
144 if(Menu::current() != NULL)
145 Menu::current()->draw(context);
146 console->draw(context);
148 context.do_drawing();
150 /* Calculate frames per second */
155 if(SDL_GetTicks() - fps_ticks >= 500)
157 fps_fps = (float) frame_count / .5;
159 fps_ticks = SDL_GetTicks();
164 elapsed_time *= speed;
166 game_time += elapsed_time;
167 script_manager->update();
168 current_screen->update(elapsed_time);
170 main_controller->update();
172 while(SDL_PollEvent(&event)) {
173 main_controller->process_event(event);
174 if(Menu::current() != NULL)
175 Menu::current()->event(event);
176 if(event.type == SDL_QUIT)
180 sound_manager->update();