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"
35 #include "player_status.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;
51 for(std::vector<Screen*>::iterator i = screen_stack.begin();
52 i != screen_stack.end(); ++i) {
58 MainLoop::push_screen(Screen* screen)
60 this->next_screen.reset(screen);
66 MainLoop::exit_screen()
68 if (screen_stack.size() < 1) {
72 next_screen.reset(screen_stack.back());
74 screen_stack.pop_back();
85 MainLoop::set_speed(float speed)
91 MainLoop::draw_fps(DrawingContext& context, float fps_fps)
94 snprintf(str, sizeof(str), "%3.1f", fps_fps);
95 const char* fpstext = "FPS";
96 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);
97 context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), RIGHT_ALLIGN, LAYER_FOREGROUND1);
103 DrawingContext context;
105 unsigned int frame_count;
107 Uint32 fps_ticks = SDL_GetTicks();
108 Uint32 fps_nextframe_ticks = SDL_GetTicks();
110 bool skipdraw = false;
114 if(next_screen.get() != NULL) {
115 if(nextpush && current_screen.get() != NULL) {
116 current_screen->leave();
117 screen_stack.push_back(current_screen.release());
120 next_screen->setup();
121 ScriptManager::instance->fire_wakeup_event(ScriptManager::SCREEN_SWITCHED);
122 current_screen.reset(next_screen.release());
123 next_screen.reset(NULL);
127 if(current_screen.get() == NULL)
130 float elapsed_time = 1.0 / LOGICAL_FPS;
131 ticks = SDL_GetTicks();
132 if(ticks > fps_nextframe_ticks) {
133 if(skipdraw == true) {
134 // already skipped last frame? we have to slow down the game then...
136 fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
138 // don't draw all frames when we're getting too slow
143 while(fps_nextframe_ticks > ticks) {
145 // If we really have to wait long, then do an imprecise SDL_Delay()
146 Uint32 diff = fps_nextframe_ticks - ticks;
150 ticks = SDL_GetTicks();
153 fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
156 current_screen->draw(context);
157 if(Menu::current() != NULL)
158 Menu::current()->draw(context);
159 Console::instance->draw(context);
162 draw_fps(context, fps_fps);
164 context.do_drawing();
166 /* Calculate frames per second */
171 if(SDL_GetTicks() - fps_ticks >= 500)
173 fps_fps = (float) frame_count / .5;
175 fps_ticks = SDL_GetTicks();
180 elapsed_time *= speed;
182 game_time += elapsed_time;
183 ScriptManager::instance->update();
184 current_screen->update(elapsed_time);
185 Console::instance->update(elapsed_time);
187 main_controller->update();
189 while(SDL_PollEvent(&event)) {
190 main_controller->process_event(event);
191 if(Menu::current() != NULL)
192 Menu::current()->event(event);
193 if(event.type == SDL_QUIT)
197 sound_manager->update();