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 "gameconfig.hpp"
31 #include "resources.hpp"
32 #include "script_manager.hpp"
36 // the engine will be run with a logical framerate of 64fps.
37 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
39 static const float LOGICAL_FPS = 64.0;
41 MainLoop* main_loop = NULL;
50 for(std::vector<Screen*>::iterator i = screen_stack.begin();
51 i != screen_stack.end(); ++i) {
57 MainLoop::push_screen(Screen* screen)
59 this->next_screen.reset(screen);
65 MainLoop::exit_screen()
67 if (screen_stack.size() < 1) {
71 next_screen.reset(screen_stack.back());
73 screen_stack.pop_back();
84 MainLoop::set_speed(float speed)
92 DrawingContext context;
94 unsigned int frame_count;
96 Uint32 fps_ticks = SDL_GetTicks();
97 Uint32 fps_nextframe_ticks = SDL_GetTicks();
99 bool skipdraw = false;
103 if(next_screen.get() != NULL) {
104 if(nextpush && current_screen.get() != NULL) {
105 current_screen->leave();
106 screen_stack.push_back(current_screen.release());
109 next_screen->setup();
110 ScriptManager::instance->fire_wakeup_event(ScriptManager::SCREEN_SWITCHED);
111 current_screen.reset(next_screen.release());
112 next_screen.reset(NULL);
116 if(current_screen.get() == NULL)
119 float elapsed_time = 1.0 / LOGICAL_FPS;
120 ticks = SDL_GetTicks();
121 if(ticks > fps_nextframe_ticks) {
122 if(skipdraw == true) {
123 // already skipped last frame? we have to slow down the game then...
125 fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
127 // don't draw all frames when we're getting too slow
132 while(fps_nextframe_ticks > ticks) {
134 // If we really have to wait long, then do an imprecise SDL_Delay()
135 Uint32 diff = fps_nextframe_ticks - ticks;
137 SDL_Delay(diff - 10);
139 ticks = SDL_GetTicks();
142 fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
145 current_screen->draw(context);
146 if(Menu::current() != NULL)
147 Menu::current()->draw(context);
148 Console::instance->draw(context);
150 context.do_drawing();
152 /* Calculate frames per second */
157 if(SDL_GetTicks() - fps_ticks >= 500)
159 fps_fps = (float) frame_count / .5;
161 fps_ticks = SDL_GetTicks();
166 elapsed_time *= speed;
168 game_time += elapsed_time;
169 ScriptManager::instance->update();
170 current_screen->update(elapsed_time);
171 Console::instance->update(elapsed_time);
173 main_controller->update();
175 while(SDL_PollEvent(&event)) {
176 main_controller->process_event(event);
177 if(Menu::current() != NULL)
178 Menu::current()->event(event);
179 if(event.type == SDL_QUIT)
183 sound_manager->update();