X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fmainloop.cpp;h=3d4ec4b51781e2e3e06ac363d3fde8136d593aa1;hb=b9a924741473410f99fee4e56aba6ebe9d181a8c;hp=07d85361964512cdc2f06c6b100243ce5c06f165;hpb=82895aabbae07b59a19e09a61ca94cd7b1603702;p=supertux.git diff --git a/src/mainloop.cpp b/src/mainloop.cpp index 07d853619..3d4ec4b51 100644 --- a/src/mainloop.cpp +++ b/src/mainloop.cpp @@ -46,10 +46,12 @@ static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS); /** don't skip more than every 2nd frame */ static const int MAX_FRAME_SKIP = 2; +float game_speed = 1.0f; + MainLoop* main_loop = NULL; MainLoop::MainLoop() - : speed(1.0), nextpop(false), nextpush(false), fps(0) + : speed(1.0), nextpop(false), nextpush(false), fps(0), screenshot_requested(false) { using namespace Scripting; TimeScheduler::instance = new TimeScheduler(); @@ -109,6 +111,12 @@ MainLoop::set_speed(float speed) this->speed = speed; } +float +MainLoop::get_speed() const +{ + return speed; +} + void MainLoop::draw_fps(DrawingContext& context, float fps_fps) { @@ -135,6 +143,11 @@ MainLoop::draw(DrawingContext& context) if(config->show_fps) draw_fps(context, fps); + // if a screenshot was requested, pass request on to drawing_context + if (screenshot_requested) { + context.take_screenshot(); + screenshot_requested = false; + } context.do_drawing(); /* Calculate frames per second */ @@ -177,6 +190,9 @@ MainLoop::process_events() config->use_fullscreen = !config->use_fullscreen; init_video(); } + else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_PRINT) { + take_screenshot(); + } } } @@ -214,10 +230,8 @@ MainLoop::handle_screen_switch() } void -MainLoop::run() +MainLoop::run(DrawingContext &context) { - DrawingContext context; - Uint32 last_ticks = 0; Uint32 elapsed_ticks = 0; @@ -232,7 +246,9 @@ MainLoop::run() elapsed_ticks += ticks - last_ticks; last_ticks = ticks; - if (elapsed_ticks > TICKS_PER_FRAME*4) { + Uint32 ticks_per_frame = TICKS_PER_FRAME * game_speed; + + if (elapsed_ticks > ticks_per_frame*4) { // when the game loads up or levels are switched the // elapsed_ticks grows extremly large, so we just ignore those // large time jumps @@ -241,24 +257,33 @@ MainLoop::run() int frames = 0; - if (elapsed_ticks > TICKS_PER_FRAME) { - 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; + if (elapsed_ticks > ticks_per_frame) + { + 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); } - draw(context); - } - sound_manager->update(); SDL_Delay(0); } } + +void +MainLoop::take_screenshot() +{ + screenshot_requested = true; +} +