X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fmainloop.cpp;h=444d3d2bdb67eeae847ebc1d67deb408c064b7ba;hb=3d0fc88a41d3774bbbcaee76efdb996bbacc6c45;hp=978d6bfede1b5360b06903ddcfceb84e9cbd59b2;hpb=08ccb17345a52f5ffd8a5dd6ecf675cad55f16a7;p=supertux.git diff --git a/src/mainloop.cpp b/src/mainloop.cpp index 978d6bfed..444d3d2bd 100644 --- a/src/mainloop.cpp +++ b/src/mainloop.cpp @@ -29,6 +29,7 @@ #include "scripting/time_scheduler.hpp" #include "scripting/squirrel_util.hpp" #include "gameconfig.hpp" +#include "constants.hpp" #include "main.hpp" #include "resources.hpp" #include "screen.hpp" @@ -38,10 +39,6 @@ #include "video/renderer.hpp" #include "random_generator.hpp" -// the engine will be run with a logical framerate of 64fps. -// We chose 64fps here because it is a power of 2, so 1/64 gives an "even" -// binary fraction... -static const float LOGICAL_FPS = 64.0; /** ticks (as returned from SDL_GetTicks) per frame */ static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS); /** don't skip more than every 2nd frame */ @@ -77,7 +74,7 @@ MainLoop::push_screen(Screen* screen, ScreenFade* screen_fade) this->screen_fade.reset(screen_fade); nextpush = !nextpop; nextpop = false; - speed = 1.0; + speed = 1.0f; } void @@ -118,14 +115,20 @@ MainLoop::get_speed() const return speed; } +bool +MainLoop::has_no_pending_fadeout() const +{ + return screen_fade.get() == NULL || screen_fade->done(); +} + void MainLoop::draw_fps(DrawingContext& context, float fps_fps) { char str[60]; snprintf(str, sizeof(str), "%3.1f", fps_fps); const char* fpstext = "FPS"; - 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), ALIGN_LEFT, LAYER_HUD); - context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD); + context.draw_text(small_font, fpstext, Vector(SCREEN_WIDTH - small_font->get_text_width(fpstext) - small_font->get_text_width(" 99999") - BORDER_X, BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD); + context.draw_text(small_font, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD); } void @@ -171,6 +174,8 @@ MainLoop::update_gamelogic(float elapsed_time) Scripting::update_debugger(); Scripting::TimeScheduler::instance->update(game_time); current_screen->update(elapsed_time); + if (Menu::current() != NULL) + Menu::current()->update(); if(screen_fade.get() != NULL) screen_fade->update(elapsed_time); Console::instance->update(elapsed_time); @@ -201,6 +206,10 @@ MainLoop::process_events() break; case SDL_KEYDOWN: + if (event.key.keysym.sym == SDLK_F10) + { + config->show_fps = !config->show_fps; + } if (event.key.keysym.sym == SDLK_F11) { config->use_fullscreen = !config->use_fullscreen; @@ -229,7 +238,7 @@ void MainLoop::handle_screen_switch() { while( (next_screen.get() != NULL || nextpop) && - (screen_fade.get() == NULL || screen_fade->done())) { + has_no_pending_fadeout()) { if(current_screen.get() != NULL) { current_screen->leave(); } @@ -249,9 +258,11 @@ MainLoop::handle_screen_switch() nextpush = false; nextpop = false; speed = 1.0; - if(next_screen.get() != NULL) - next_screen->setup(); - current_screen.reset(next_screen.release()); + Screen* next_screen_ptr = next_screen.release(); + next_screen.reset(0); + if(next_screen_ptr) + next_screen_ptr->setup(); + current_screen.reset(next_screen_ptr); screen_fade.reset(NULL); waiting_threads.wakeup(); @@ -284,29 +295,32 @@ MainLoop::run(DrawingContext &context) elapsed_ticks = 0; } + if(elapsed_ticks < ticks_per_frame) + { + Uint32 delay_ticks = ticks_per_frame - elapsed_ticks; + SDL_Delay(delay_ticks); + last_ticks += delay_ticks; + elapsed_ticks += delay_ticks; + } + int frames = 0; - if (elapsed_ticks > ticks_per_frame) + while(elapsed_ticks >= ticks_per_frame && frames < MAX_FRAME_SKIP) { - while(elapsed_ticks > ticks_per_frame && frames < MAX_FRAME_SKIP) - { - elapsed_ticks -= ticks_per_frame; - float timestep = 1.0 / LOGICAL_FPS; - real_time += timestep; - timestep *= speed; - game_time += timestep; - - process_events(); - update_gamelogic(timestep); - frames += 1; - } - - draw(context); + elapsed_ticks -= ticks_per_frame; + float timestep = 1.0 / LOGICAL_FPS; + real_time += timestep; + timestep *= speed; + game_time += timestep; + + process_events(); + update_gamelogic(timestep); + frames += 1; } - sound_manager->update(); + draw(context); - SDL_Delay(0); + sound_manager->update(); } }