Scripting scheduler is now paused while pause menu is shown
authorChristoph Sommer <mail@christoph-sommer.de>
Mon, 16 Apr 2007 17:17:53 +0000 (17:17 +0000)
committerChristoph Sommer <mail@christoph-sommer.de>
Mon, 16 Apr 2007 17:17:53 +0000 (17:17 +0000)
SVN-Revision: 4985

src/game_session.cpp
src/scripting/time_scheduler.cpp
src/scripting/time_scheduler.hpp

index 9a6565e..b9b7510 100644 (file)
@@ -73,6 +73,7 @@
 #include "object/endsequence_walkleft.hpp"
 #include "object/endsequence_fireworks.hpp"
 #include "direction.hpp"
+#include "scripting/time_scheduler.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"
@@ -97,6 +98,7 @@ GameSession::GameSession(const std::string& levelfile_, Statistics* statistics)
   currentsector = NULL;
 
   game_pause = false;
+  Scripting::TimeScheduler::instance->set_pause(game_pause);
 
   statistics_backdrop.reset(new Surface("images/engine/menu/score-backdrop.png"));
 
@@ -115,6 +117,7 @@ void
 GameSession::restart_level()
 {
   game_pause   = false;
+  Scripting::TimeScheduler::instance->set_pause(game_pause);
   end_sequence = 0;
 
   main_controller->reset();
@@ -165,6 +168,11 @@ GameSession::~GameSession()
   delete playback_demo_stream;
   delete demo_controller;
 
+  if (game_pause) {
+    game_pause = false;
+    Scripting::TimeScheduler::instance->set_pause(game_pause);
+  }
+
   current_ = NULL;
 }
 
@@ -297,9 +305,11 @@ GameSession::toggle_pause()
     Menu::set_current(game_menu.get());
     game_menu->set_active_item(MNID_CONTINUE);
     game_pause = true;
+    Scripting::TimeScheduler::instance->set_pause(game_pause);
   } else {
     Menu::set_current(NULL);
     game_pause = false;
+    Scripting::TimeScheduler::instance->set_pause(game_pause);
   }
 }
 
@@ -339,6 +349,7 @@ GameSession::process_events()
   // end of pause mode?
   if(!Menu::current() && game_pause) {
     game_pause = false;
+    Scripting::TimeScheduler::instance->set_pause(game_pause);
   }
 
   // playback a demo?
index d28308b..41a4d6f 100644 (file)
@@ -31,7 +31,7 @@ namespace Scripting
 
 TimeScheduler* TimeScheduler::instance = NULL;
 
-TimeScheduler::TimeScheduler()
+TimeScheduler::TimeScheduler() : paused(false), last_update(0), internal_time(0)
 {
 }
 
@@ -42,7 +42,10 @@ TimeScheduler::~TimeScheduler()
 void
 TimeScheduler::update(float time)
 {
-  while(!schedule.empty() && schedule.front().wakeup_time < time) {
+  if (!paused) internal_time+=(time - last_update); 
+  last_update = time;
+
+  while(!schedule.empty() && schedule.front().wakeup_time < internal_time) {
     HSQOBJECT thread_ref = schedule.front().thread_ref;
 
     sq_pushobject(global_vm, thread_ref);
@@ -88,7 +91,7 @@ TimeScheduler::schedule_thread(HSQUIRRELVM scheduled_vm, float time)
     sq_pop(global_vm, 2);
     throw SquirrelError(global_vm, "Couldn't get thread weakref from vm");
   }
-  entry.wakeup_time = time;
+  entry.wakeup_time = time - (last_update - internal_time);
 
   sq_addref(global_vm, & entry.thread_ref);
   sq_pop(global_vm, 2);
@@ -97,4 +100,10 @@ TimeScheduler::schedule_thread(HSQUIRRELVM scheduled_vm, float time)
   std::push_heap(schedule.begin(), schedule.end());
 }
 
+void
+TimeScheduler::set_pause(bool paused)
+{
+  this->paused = paused;
+}
+
 }
index d26cb76..c431f14 100644 (file)
@@ -41,6 +41,8 @@ public:
 
   static TimeScheduler* instance;
 
+  void set_pause(bool paused);
+
 private:
   struct ScheduleEntry {
     /// weak reference to the squirrel vm object
@@ -57,6 +59,9 @@ private:
 
   typedef std::vector<ScheduleEntry> ScheduleHeap;
   ScheduleHeap schedule;
+  bool paused;
+  float last_update; 
+  float internal_time;
 };
 
 }