Browse Source

add ClockObject::M_limited

David Rose 19 years ago
parent
commit
a2df279a61
3 changed files with 29 additions and 5 deletions
  1. 9 2
      panda/src/putil/clockObject.I
  2. 18 3
      panda/src/putil/clockObject.cxx
  3. 2 0
      panda/src/putil/clockObject.h

+ 9 - 2
panda/src/putil/clockObject.I

@@ -42,6 +42,12 @@ INLINE ClockObject::
 //               to tick().  You may set the value of dt with
 //               to tick().  You may set the value of dt with
 //               set_dt().
 //               set_dt().
 //
 //
+//               M_limited - the clock will run as fast as it can, as
+//               in M_normal, but will not run faster than the rate
+//               specified by set_dt().  If the application would run
+//               faster than this rate, the clock will slow down the
+//               application.
+//
 //               M_forced - the clock forces the application to run at
 //               M_forced - the clock forces the application to run at
 //               the rate specified by set_dt().  If the application
 //               the rate specified by set_dt().  If the application
 //               would run faster than this rate, the clock will slow
 //               would run faster than this rate, the clock will slow
@@ -193,12 +199,13 @@ get_dt(Thread *current_thread) const {
 //       Access: Published
 //       Access: Published
 //  Description: In non-real-time mode, sets the number of seconds
 //  Description: In non-real-time mode, sets the number of seconds
 //               that should appear to elapse between frames.  In
 //               that should appear to elapse between frames.  In
-//               forced mode, sets our target dt.  In normal mode,
-//               this has no effect.
+//               forced mode or limited mode, sets our target dt.  In
+//               normal mode, this has no effect.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 INLINE void ClockObject::
 INLINE void ClockObject::
 set_dt(double dt, Thread *current_thread) {
 set_dt(double dt, Thread *current_thread) {
   nassertv(current_thread->get_pipeline_stage() == 0);
   nassertv(current_thread->get_pipeline_stage() == 0);
+  _set_dt = dt;
   CDWriter cdata(_cycler, current_thread);
   CDWriter cdata(_cycler, current_thread);
   cdata->_dt = dt;
   cdata->_dt = dt;
 }
 }

+ 18 - 3
panda/src/putil/clockObject.cxx

@@ -46,6 +46,9 @@ ClockObject() {
   _average_frame_rate_interval = average_frame_rate_interval;
   _average_frame_rate_interval = average_frame_rate_interval;
 
 
   _error_count = _true_clock->get_error_count();
   _error_count = _true_clock->get_error_count();
+
+  CDReader cdata(_cycler);
+  _set_dt = cdata->_dt;
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -145,15 +148,22 @@ tick(Thread *current_thread) {
     case M_non_real_time:
     case M_non_real_time:
       // Ignore real time.  We always report the same interval having
       // Ignore real time.  We always report the same interval having
       // elapsed each frame.
       // elapsed each frame.
-      cdata->_reported_frame_time += cdata->_dt;
+      cdata->_reported_frame_time += _set_dt;
+      break;
+      
+    case M_limited:
+      // If we are running faster than the desired interval, slow down.
+      wait_until(old_time + _set_dt);
+      cdata->_dt = _actual_frame_time - old_time;
+      cdata->_reported_frame_time = _actual_frame_time;
       break;
       break;
       
       
     case M_forced:
     case M_forced:
       // If we are running faster than the desired interval, slow down.
       // If we are running faster than the desired interval, slow down.
       // If we are running slower than the desired interval, ignore that
       // If we are running slower than the desired interval, ignore that
       // and pretend we're running at the specified rate.
       // and pretend we're running at the specified rate.
-      wait_until(old_time + cdata->_dt);
-      cdata->_reported_frame_time += cdata->_dt;
+      wait_until(old_time + _set_dt);
+      cdata->_reported_frame_time += _set_dt;
       break;
       break;
       
       
     case M_degrade:
     case M_degrade:
@@ -291,6 +301,9 @@ operator << (ostream &out, ClockObject::Mode mode) {
   case ClockObject::M_non_real_time:
   case ClockObject::M_non_real_time:
     return out << "non-real-time";
     return out << "non-real-time";
 
 
+  case ClockObject::M_limited:
+    return out << "limited";
+
   case ClockObject::M_forced:
   case ClockObject::M_forced:
     return out << "forced";
     return out << "forced";
 
 
@@ -317,6 +330,8 @@ operator >> (istream &in, ClockObject::Mode &mode) {
     mode = ClockObject::M_normal;
     mode = ClockObject::M_normal;
   } else if (word == "non-real-time") {
   } else if (word == "non-real-time") {
     mode = ClockObject::M_non_real_time;
     mode = ClockObject::M_non_real_time;
+  } else if (word == "limited") {
+    mode = ClockObject::M_limited;
   } else if (word == "forced") {
   } else if (word == "forced") {
     mode = ClockObject::M_forced;
     mode = ClockObject::M_forced;
   } else if (word == "degrade") {
   } else if (word == "degrade") {

+ 2 - 0
panda/src/putil/clockObject.h

@@ -71,6 +71,7 @@ PUBLISHED:
     M_forced,
     M_forced,
     M_degrade,
     M_degrade,
     M_slave,
     M_slave,
+    M_limited,
   };
   };
 
 
   ClockObject();
   ClockObject();
@@ -121,6 +122,7 @@ private:
   double _start_long_time;
   double _start_long_time;
   double _actual_frame_time;
   double _actual_frame_time;
   double _max_dt;
   double _max_dt;
+  double _set_dt;
   double _degrade_factor;
   double _degrade_factor;
   int _error_count;
   int _error_count;