/** 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()
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
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);
#include "squirrel_util.hpp"
#include "time_scheduler.hpp"
+extern float game_speed;
+
namespace Scripting
{
return systemRandom.rand();
}
+void set_game_speed(float speed)
+{
+ ::game_speed = speed;
+}
+
}
void play_sound(const std::string& soundfile);
/**
+ * Set the game_speed
+ */
+void set_game_speed(float speed);
+
+/**
* speeds Tux up
*/
void grease();
}
+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<float> (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;
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))) {