diff --git a/panda/src/putil/clockObject.I b/panda/src/putil/clockObject.I index 0e108dfc15..005750d8e3 100644 --- a/panda/src/putil/clockObject.I +++ b/panda/src/putil/clockObject.I @@ -42,6 +42,12 @@ INLINE ClockObject:: // to tick(). You may set the value of dt with // 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 // the rate specified by set_dt(). If the application // would run faster than this rate, the clock will slow @@ -193,12 +199,13 @@ get_dt(Thread *current_thread) const { // Access: Published // Description: In non-real-time mode, sets the number of seconds // 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:: set_dt(double dt, Thread *current_thread) { nassertv(current_thread->get_pipeline_stage() == 0); + _set_dt = dt; CDWriter cdata(_cycler, current_thread); cdata->_dt = dt; } diff --git a/panda/src/putil/clockObject.cxx b/panda/src/putil/clockObject.cxx index bc6e6f4d0e..f6a101c957 100644 --- a/panda/src/putil/clockObject.cxx +++ b/panda/src/putil/clockObject.cxx @@ -46,6 +46,9 @@ ClockObject() { _average_frame_rate_interval = average_frame_rate_interval; _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: // Ignore real time. We always report the same interval having // 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; case M_forced: // If we are running faster than the desired interval, slow down. // If we are running slower than the desired interval, ignore that // 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; case M_degrade: @@ -291,6 +301,9 @@ operator << (ostream &out, ClockObject::Mode mode) { case ClockObject::M_non_real_time: return out << "non-real-time"; + case ClockObject::M_limited: + return out << "limited"; + case ClockObject::M_forced: return out << "forced"; @@ -317,6 +330,8 @@ operator >> (istream &in, ClockObject::Mode &mode) { mode = ClockObject::M_normal; } else if (word == "non-real-time") { mode = ClockObject::M_non_real_time; + } else if (word == "limited") { + mode = ClockObject::M_limited; } else if (word == "forced") { mode = ClockObject::M_forced; } else if (word == "degrade") { diff --git a/panda/src/putil/clockObject.h b/panda/src/putil/clockObject.h index 0e546aa40e..6accc8844f 100644 --- a/panda/src/putil/clockObject.h +++ b/panda/src/putil/clockObject.h @@ -71,6 +71,7 @@ PUBLISHED: M_forced, M_degrade, M_slave, + M_limited, }; ClockObject(); @@ -121,6 +122,7 @@ private: double _start_long_time; double _actual_frame_time; double _max_dt; + double _set_dt; double _degrade_factor; int _error_count;