Browse Source

add get_average_frame_rate

David Rose 22 years ago
parent
commit
f4a0d85d02

+ 56 - 3
panda/src/express/clockObject.I

@@ -134,14 +134,16 @@ get_frame_count() const {
 }
 
 ////////////////////////////////////////////////////////////////////
-//     Function: ClockObject::get_frame_rate
+//     Function: ClockObject::get_net_frame_rate
 //       Access: Published
 //  Description: Returns the average frame rate since the last reset.
 //               This is simply the total number of frames divided by
-//               the total elapsed time.
+//               the total elapsed time.  This reports the virtual
+//               frame rate if the clock is in (or has been in)
+//               M_non_real_time mode.
 ////////////////////////////////////////////////////////////////////
 INLINE double ClockObject::
-get_frame_rate() const {
+get_net_frame_rate() const {
   return (double)get_frame_count() / get_frame_time();
 }
 
@@ -207,6 +209,57 @@ set_max_dt(double max_dt) {
   _max_dt = max_dt;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: ClockObject::set_average_frame_rate_interval
+//       Access: Published
+//  Description: Specifies the interval of time (in seconds) over
+//               which get_average_frame_rate() averages the number of
+//               frames per second to compute the frame rate.
+//               Changing this does not necessarily immediately change
+//               the result of get_average_frame_rate(), until this
+//               interval of time has elapsed again.
+//
+//               Setting this to zero disables the computation of
+//               get_average_frame_rate().
+////////////////////////////////////////////////////////////////////
+INLINE void ClockObject::
+set_average_frame_rate_interval(double time) {
+  _average_frame_rate_interval = time;
+  if (_average_frame_rate_interval == 0.0) {
+    _ticks.clear();
+  }
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: ClockObject::get_average_frame_rate_interval
+//       Access: Published
+//  Description: Returns the interval of time (in seconds) over
+//               which get_average_frame_rate() averages the number of frames
+//               per second to compute the frame rate.
+////////////////////////////////////////////////////////////////////
+INLINE double ClockObject::
+get_average_frame_rate_interval() const {
+  return _average_frame_rate_interval;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: ClockObject::get_average_frame_rate
+//       Access: Published
+//  Description: Returns the average frame rate in number of frames
+//               per second over the last
+//               get_average_frame_rate_interval() seconds.  This
+//               measures the virtual frame rate if the clock is in
+//               M_non_real_time mode.
+////////////////////////////////////////////////////////////////////
+INLINE double ClockObject::
+get_average_frame_rate() const {
+  if (_ticks.empty()) {
+    return 0.0;
+  } else {
+    return _ticks.size() / (_reported_frame_time - _ticks.front());
+  }
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: ClockObject::get_global_clock
 //       Access: Published

+ 11 - 0
panda/src/express/clockObject.cxx

@@ -39,6 +39,7 @@ ClockObject() {
   _reported_frame_time = 0.0;
   _dt = 0.0;
   _max_dt = -1.0;
+  _average_frame_rate_interval = average_frame_rate_interval;
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -116,6 +117,8 @@ set_frame_count(int frame_count) {
 ////////////////////////////////////////////////////////////////////
 void ClockObject::
 tick() {
+  double old_reported_time = _reported_frame_time;
+
   double old_time = _actual_frame_time;
   _actual_frame_time = get_real_time();
 
@@ -134,6 +137,14 @@ tick() {
   }
 
   _frame_count++;
+
+  if (_average_frame_rate_interval > 0.0) {
+    _ticks.push_back(old_reported_time);
+    while (!_ticks.empty() && 
+           _reported_frame_time - _ticks.front() > _average_frame_rate_interval) {
+      _ticks.pop_front();
+    }
+  }
 }
 
 ////////////////////////////////////////////////////////////////////

+ 12 - 1
panda/src/express/clockObject.h

@@ -23,6 +23,7 @@
 
 #include "trueClock.h"
 #include "config_express.h"
+#include "pdeque.h"
 
 class EXPCL_PANDAEXPRESS TimeVal {
 PUBLISHED:
@@ -86,7 +87,7 @@ PUBLISHED:
   void set_frame_count(int frame_count);
 
   INLINE int get_frame_count() const;
-  INLINE double get_frame_rate() const;
+  INLINE double get_net_frame_rate() const;
 
   INLINE double get_dt() const;
   INLINE void set_dt(double dt);
@@ -94,6 +95,10 @@ PUBLISHED:
   INLINE double get_max_dt() const;
   INLINE void set_max_dt(double max_dt);
 
+  INLINE void set_average_frame_rate_interval(double time);
+  INLINE double get_average_frame_rate_interval() const;
+  INLINE double get_average_frame_rate() const;
+
   void tick();
   void sync_frame_time();
 
@@ -110,6 +115,12 @@ private:
   double _dt;
   double _max_dt;
 
+  // For tracking the average frame rate over a certain interval of
+  // time.
+  double _average_frame_rate_interval;
+  typedef pdeque<double> Ticks;
+  Ticks _ticks;
+
   static ClockObject *_global_clock;
 };
 

+ 3 - 0
panda/src/express/config_express.cxx

@@ -188,6 +188,9 @@ const int patchfile_zone_size =
 const bool keep_temporary_files =
 config_express.GetBool("keep-temporary-files", false);
 
+extern const double average_frame_rate_interval = 
+config_express.GetDouble("average-frame-rate-interval", 1.0);
+
 // Set this true to use the VirtualFileSystem mechanism for loading
 // models, etc.  Since the VirtualFileSystem maps to the same as the
 // actual file system by default, there is probably no reason to set

+ 1 - 0
panda/src/express/config_express.h

@@ -47,6 +47,7 @@ extern const int patchfile_buffer_size;
 extern const int patchfile_zone_size;
 
 extern const bool keep_temporary_files;
+extern const double average_frame_rate_interval;
 
 extern EXPCL_PANDAEXPRESS const bool use_vfs;