From b9a924741473410f99fee4e56aba6ebe9d181a8c Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Mon, 5 May 2008 16:29:53 +0000 Subject: [PATCH] Crude hack to slow down the game, use set_game_speed(2.0) in console to run at half the speed SVN-Revision: 5415 --- src/mainloop.cpp | 36 +++++++++++++++++++++--------------- src/scripting/functions.cpp | 7 +++++++ src/scripting/functions.hpp | 5 +++++ src/scripting/wrapper.cpp | 29 +++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 15 deletions(-) diff --git a/src/mainloop.cpp b/src/mainloop.cpp index e5db0246a..3d4ec4b51 100644 --- a/src/mainloop.cpp +++ b/src/mainloop.cpp @@ -46,6 +46,8 @@ 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() @@ -244,7 +246,9 @@ MainLoop::run(DrawingContext &context) 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 @@ -253,22 +257,24 @@ MainLoop::run(DrawingContext &context) 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); diff --git a/src/scripting/functions.cpp b/src/scripting/functions.cpp index 36e470509..e53a83723 100644 --- a/src/scripting/functions.cpp +++ b/src/scripting/functions.cpp @@ -51,6 +51,8 @@ #include "squirrel_util.hpp" #include "time_scheduler.hpp" +extern float game_speed; + namespace Scripting { @@ -285,4 +287,9 @@ int rand() return systemRandom.rand(); } +void set_game_speed(float speed) +{ + ::game_speed = speed; +} + } diff --git a/src/scripting/functions.hpp b/src/scripting/functions.hpp index d56f6c2f7..afa649ab3 100644 --- a/src/scripting/functions.hpp +++ b/src/scripting/functions.hpp @@ -141,6 +141,11 @@ void play_music(const std::string& musicfile); void play_sound(const std::string& soundfile); /** + * Set the game_speed + */ +void set_game_speed(float speed); + +/** * speeds Tux up */ void grease(); diff --git a/src/scripting/wrapper.cpp b/src/scripting/wrapper.cpp index d001bbbc7..7a5ab3082 100644 --- a/src/scripting/wrapper.cpp +++ b/src/scripting/wrapper.cpp @@ -3536,6 +3536,29 @@ static SQInteger play_sound_wrapper(HSQUIRRELVM vm) } +static SQInteger set_game_speed_wrapper(HSQUIRRELVM vm) +{ + SQFloat arg0; + if(SQ_FAILED(sq_getfloat(vm, 2, &arg0))) { + sq_throwerror(vm, _SC("Argument 1 not a float")); + return SQ_ERROR; + } + + try { + Scripting::set_game_speed(static_cast (arg0)); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'set_game_speed'")); + return SQ_ERROR; + } + +} + static SQInteger grease_wrapper(HSQUIRRELVM vm) { (void) vm; @@ -4327,6 +4350,12 @@ void register_supertux_wrapper(HSQUIRRELVM v) throw SquirrelError(v, "Couldn't register function 'play_sound'"); } + sq_pushstring(v, "set_game_speed", -1); + sq_newclosure(v, &set_game_speed_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'set_game_speed'"); + } + sq_pushstring(v, "grease", -1); sq_newclosure(v, &grease_wrapper, 0); if(SQ_FAILED(sq_createslot(v, -3))) { -- 2.11.0