[Supertux-Commit] r5415 - in trunk/supertux/src: . scripting

grumbel at millhouse.dreamhost.com grumbel at millhouse.dreamhost.com
Mon May 5 09:29:55 PDT 2008


Author: grumbel
Date: 2008-05-05 09:29:53 -0700 (Mon, 05 May 2008)
New Revision: 5415

Modified:
   trunk/supertux/src/mainloop.cpp
   trunk/supertux/src/scripting/functions.cpp
   trunk/supertux/src/scripting/functions.hpp
   trunk/supertux/src/scripting/wrapper.cpp
Log:
Crude hack to slow down the game, use set_game_speed(2.0) in console to run at half the speed

Modified: trunk/supertux/src/mainloop.cpp
===================================================================
--- trunk/supertux/src/mainloop.cpp	2008-05-05 15:36:13 UTC (rev 5414)
+++ trunk/supertux/src/mainloop.cpp	2008-05-05 16:29:53 UTC (rev 5415)
@@ -46,6 +46,8 @@
 /** 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 @@
     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 @@
 
     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;
+    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;
+            process_events();
+            update_gamelogic(timestep);
+            frames += 1;
+          }
+
+        draw(context);
       }
 
-      draw(context);
-    }
-
     sound_manager->update();
 
     SDL_Delay(0);

Modified: trunk/supertux/src/scripting/functions.cpp
===================================================================
--- trunk/supertux/src/scripting/functions.cpp	2008-05-05 15:36:13 UTC (rev 5414)
+++ trunk/supertux/src/scripting/functions.cpp	2008-05-05 16:29:53 UTC (rev 5415)
@@ -51,6 +51,8 @@
 #include "squirrel_util.hpp"
 #include "time_scheduler.hpp"
 
+extern float game_speed;
+
 namespace Scripting
 {
 
@@ -285,4 +287,9 @@
   return systemRandom.rand();
 }
 
+void set_game_speed(float speed)
+{
+  ::game_speed = speed;
 }
+
+}

Modified: trunk/supertux/src/scripting/functions.hpp
===================================================================
--- trunk/supertux/src/scripting/functions.hpp	2008-05-05 15:36:13 UTC (rev 5414)
+++ trunk/supertux/src/scripting/functions.hpp	2008-05-05 16:29:53 UTC (rev 5415)
@@ -141,6 +141,11 @@
 void play_sound(const std::string& soundfile);
 
 /**
+ *  Set the game_speed
+ */
+void set_game_speed(float speed);
+
+/**
  * speeds Tux up
  */
 void grease();

Modified: trunk/supertux/src/scripting/wrapper.cpp
===================================================================
--- trunk/supertux/src/scripting/wrapper.cpp	2008-05-05 15:36:13 UTC (rev 5414)
+++ trunk/supertux/src/scripting/wrapper.cpp	2008-05-05 16:29:53 UTC (rev 5415)
@@ -3536,6 +3536,29 @@
 
 }
 
+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;
@@ -4327,6 +4350,12 @@
     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))) {




More information about the Supertux-Commit mailing list