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"
34 #include "screen_fade.hpp"
36 #include "player_status.hpp"
38 // the engine will be run with a logical framerate of 64fps.
39 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
41 static const float LOGICAL_FPS = 64.0;
43 MainLoop* main_loop = NULL;
52 for(std::vector<Screen*>::iterator i = screen_stack.begin();
53 i != screen_stack.end(); ++i) {
59 MainLoop::push_screen(Screen* screen, ScreenFade* screen_fade)
61 this->next_screen.reset(screen);
62 this->screen_fade.reset(screen_fade);
68 MainLoop::exit_screen(ScreenFade* screen_fade)
70 next_screen.reset(NULL);
71 this->screen_fade.reset(screen_fade);
76 MainLoop::set_screen_fade(ScreenFade* screen_fade)
78 this->screen_fade.reset(screen_fade);
82 MainLoop::quit(ScreenFade* screen_fade)
84 for(std::vector<Screen*>::iterator i = screen_stack.begin();
85 i != screen_stack.end(); ++i)
88 exit_screen(screen_fade);
92 MainLoop::set_speed(float speed)
98 MainLoop::draw_fps(DrawingContext& context, float fps_fps)
101 snprintf(str, sizeof(str), "%3.1f", fps_fps);
102 const char* fpstext = "FPS";
103 context.draw_text(white_text, fpstext, Vector(SCREEN_WIDTH - white_text->get_text_width(fpstext) - gold_text->get_text_width(" 99999") - BORDER_X, BORDER_Y + 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
104 context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), RIGHT_ALLIGN, LAYER_FOREGROUND1);
110 DrawingContext context;
112 unsigned int frame_count;
114 Uint32 fps_ticks = SDL_GetTicks();
115 Uint32 fps_nextframe_ticks = SDL_GetTicks();
117 bool skipdraw = false;
121 if( (next_screen.get() != NULL || nextpop == true) &&
122 (screen_fade.get() == NULL || screen_fade->done())) {
123 if(current_screen.get() != NULL) {
124 current_screen->leave();
128 if(screen_stack.empty()) {
132 next_screen.reset(screen_stack.back());
133 screen_stack.pop_back();
136 } else if(current_screen.get() != NULL) {
137 screen_stack.push_back(current_screen.release());
140 next_screen->setup();
141 ScriptManager::instance->fire_wakeup_event(ScriptManager::SCREEN_SWITCHED);
142 current_screen.reset(next_screen.release());
143 next_screen.reset(NULL);
144 screen_fade.reset(NULL);
147 if(current_screen.get() == NULL)
150 float elapsed_time = 1.0 / LOGICAL_FPS;
151 ticks = SDL_GetTicks();
152 if(ticks > fps_nextframe_ticks) {
153 if(skipdraw == true) {
154 // already skipped last frame? we have to slow down the game then...
156 fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
158 // don't draw all frames when we're getting too slow
163 while(fps_nextframe_ticks > ticks) {
165 // If we really have to wait long, then do an imprecise SDL_Delay()
166 Uint32 diff = fps_nextframe_ticks - ticks;
170 ticks = SDL_GetTicks();
173 fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
176 current_screen->draw(context);
177 if(Menu::current() != NULL)
178 Menu::current()->draw(context);
179 if(screen_fade.get() != NULL)
180 screen_fade->draw(context);
181 Console::instance->draw(context);
184 draw_fps(context, fps_fps);
186 context.do_drawing();
188 /* Calculate frames per second */
193 if(SDL_GetTicks() - fps_ticks >= 500)
195 fps_fps = (float) frame_count / .5;
197 fps_ticks = SDL_GetTicks();
202 elapsed_time *= speed;
204 game_time += elapsed_time;
205 ScriptManager::instance->update();
206 current_screen->update(elapsed_time);
207 if(screen_fade.get() != NULL)
208 screen_fade->update(elapsed_time);
209 Console::instance->update(elapsed_time);
211 main_controller->update();
213 while(SDL_PollEvent(&event)) {
214 main_controller->process_event(event);
215 if(Menu::current() != NULL)
216 Menu::current()->event(event);
217 if(event.type == SDL_QUIT)
221 sound_manager->update();