Panda3D
|
00001 // Filename: clockObject.I 00002 // Created by: drose (17Feb00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 //////////////////////////////////////////////////////////////////// 00016 // Function: ClockObject::Destructor 00017 // Access: Published 00018 // Description: 00019 //////////////////////////////////////////////////////////////////// 00020 INLINE ClockObject:: 00021 ~ClockObject() { 00022 } 00023 00024 //////////////////////////////////////////////////////////////////// 00025 // Function: ClockObject::get_mode 00026 // Access: Published 00027 // Description: Returns the current mode of the clock. See 00028 // set_mode(). 00029 //////////////////////////////////////////////////////////////////// 00030 INLINE ClockObject::Mode ClockObject:: 00031 get_mode() const { 00032 return _mode; 00033 } 00034 00035 //////////////////////////////////////////////////////////////////// 00036 // Function: ClockObject::get_frame_time 00037 // Access: Published 00038 // Description: Returns the time in seconds as of the last time 00039 // tick() was called (typically, this will be as of the 00040 // start of the current frame). 00041 // 00042 // This is generally the kind of time you want to ask 00043 // for in most rendering and animation contexts, since 00044 // it's important that all of the animation for a given 00045 // frame remains in sync with each other. 00046 //////////////////////////////////////////////////////////////////// 00047 INLINE double ClockObject:: 00048 get_frame_time(Thread *current_frame) const { 00049 CDReader cdata(_cycler, current_frame); 00050 return cdata->_reported_frame_time; 00051 } 00052 00053 //////////////////////////////////////////////////////////////////// 00054 // Function: ClockObject::get_real_time 00055 // Access: Published 00056 // Description: Returns the actual number of seconds elapsed since 00057 // the ClockObject was created, or since it was last 00058 // reset. This is useful for doing real timing 00059 // measurements, e.g. for performance statistics. 00060 // 00061 // This returns the most precise timer we have for short 00062 // time intervals, but it may tend to drift over the 00063 // long haul. If more accurate timekeeping is needed 00064 // over a long period of time, use get_long_time() 00065 // instead. 00066 //////////////////////////////////////////////////////////////////// 00067 INLINE double ClockObject:: 00068 get_real_time() const { 00069 return (_true_clock->get_short_time() - _start_short_time); 00070 } 00071 00072 //////////////////////////////////////////////////////////////////// 00073 // Function: ClockObject::get_long_time 00074 // Access: Published 00075 // Description: Returns the actual number of seconds elapsed since 00076 // the ClockObject was created, or since it was last 00077 // reset. 00078 // 00079 // This is similar to get_real_time(), except that it 00080 // uses the most accurate counter we have over a long 00081 // period of time, and so it is less likely to drift. 00082 // However, it may not be very precise for measuring 00083 // short intervals. On Windows, for instace, this is 00084 // only accurate to within about 55 milliseconds. 00085 //////////////////////////////////////////////////////////////////// 00086 INLINE double ClockObject:: 00087 get_long_time() const { 00088 return (_true_clock->get_long_time() - _start_long_time); 00089 } 00090 00091 //////////////////////////////////////////////////////////////////// 00092 // Function: ClockObject::reset 00093 // Access: Published 00094 // Description: Simultaneously resets both the time and the frame 00095 // count to zero. 00096 //////////////////////////////////////////////////////////////////// 00097 INLINE void ClockObject:: 00098 reset() { 00099 set_real_time(0.0); 00100 set_frame_time(0.0); 00101 set_frame_count(0); 00102 } 00103 00104 //////////////////////////////////////////////////////////////////// 00105 // Function: ClockObject::get_frame_count 00106 // Access: Published 00107 // Description: Returns the number of times tick() has been called 00108 // since the ClockObject was created, or since it was 00109 // last reset. This is generally the number of frames 00110 // that have been rendered. 00111 //////////////////////////////////////////////////////////////////// 00112 INLINE int ClockObject:: 00113 get_frame_count(Thread *current_thread) const { 00114 CDReader cdata(_cycler, current_thread); 00115 return cdata->_frame_count; 00116 } 00117 00118 //////////////////////////////////////////////////////////////////// 00119 // Function: ClockObject::get_net_frame_rate 00120 // Access: Published 00121 // Description: Returns the average frame rate since the last reset. 00122 // This is simply the total number of frames divided by 00123 // the total elapsed time. This reports the virtual 00124 // frame rate if the clock is in (or has been in) 00125 // M_non_real_time mode. 00126 //////////////////////////////////////////////////////////////////// 00127 INLINE double ClockObject:: 00128 get_net_frame_rate(Thread *current_thread) const { 00129 CDReader cdata(_cycler, current_thread); 00130 return (double)cdata->_frame_count / cdata->_reported_frame_time; 00131 } 00132 00133 //////////////////////////////////////////////////////////////////// 00134 // Function: ClockObject::get_dt 00135 // Access: Published 00136 // Description: Returns the elapsed time for the previous frame: the 00137 // number of seconds elapsed between the last two calls 00138 // to tick(). 00139 //////////////////////////////////////////////////////////////////// 00140 INLINE double ClockObject:: 00141 get_dt(Thread *current_thread) const { 00142 CDReader cdata(_cycler, current_thread); 00143 if (_max_dt > 0.0) { 00144 return min(_max_dt, cdata->_dt); 00145 } 00146 return cdata->_dt; 00147 } 00148 00149 //////////////////////////////////////////////////////////////////// 00150 // Function: ClockObject::get_max_dt 00151 // Access: Published 00152 // Description: Returns the current maximum allowable time elapsed 00153 // between any two frames. See set_max_dt(). 00154 //////////////////////////////////////////////////////////////////// 00155 INLINE double ClockObject:: 00156 get_max_dt() const { 00157 return _max_dt; 00158 } 00159 00160 //////////////////////////////////////////////////////////////////// 00161 // Function: ClockObject::set_max_dt 00162 // Access: Published 00163 // Description: Sets a limit on the value returned by get_dt(). If 00164 // this value is less than zero, no limit is imposed; 00165 // otherwise, this is the maximum value that will ever 00166 // be returned by get_dt(), regardless of how much time 00167 // has actually elapsed between frames. 00168 // 00169 // This limit is only imposed in real-time mode; in 00170 // non-real-time mode, the dt is fixed anyway and max_dt 00171 // is ignored. 00172 // 00173 // This is generally used to guarantee reasonable 00174 // behavior even in the presence of a very slow or 00175 // chuggy frame rame. 00176 //////////////////////////////////////////////////////////////////// 00177 INLINE void ClockObject:: 00178 set_max_dt(double max_dt) { 00179 _max_dt = max_dt; 00180 } 00181 00182 //////////////////////////////////////////////////////////////////// 00183 // Function: ClockObject::get_degrade_factor 00184 // Access: Published 00185 // Description: In degrade mode, returns the ratio by which the 00186 // performance is degraded. A value of 2.0 causes the 00187 // clock to be slowed down by a factor of two (reducing 00188 // performance to 1/2 what would be otherwise). 00189 // 00190 // This has no effect if mode is not M_degrade. 00191 //////////////////////////////////////////////////////////////////// 00192 INLINE double ClockObject:: 00193 get_degrade_factor() const { 00194 return _degrade_factor; 00195 } 00196 00197 //////////////////////////////////////////////////////////////////// 00198 // Function: ClockObject::set_degrade_factor 00199 // Access: Published 00200 // Description: In degrade mode, sets the ratio by which the 00201 // performance is degraded. A value of 2.0 causes the 00202 // clock to be slowed down by a factor of two (reducing 00203 // performance to 1/2 what would be otherwise). 00204 // 00205 // This has no effect if mode is not M_degrade. 00206 //////////////////////////////////////////////////////////////////// 00207 INLINE void ClockObject:: 00208 set_degrade_factor(double degrade_factor) { 00209 _degrade_factor = degrade_factor; 00210 } 00211 00212 //////////////////////////////////////////////////////////////////// 00213 // Function: ClockObject::set_average_frame_rate_interval 00214 // Access: Published 00215 // Description: Specifies the interval of time (in seconds) over 00216 // which get_average_frame_rate() averages the number of 00217 // frames per second to compute the frame rate. 00218 // Changing this does not necessarily immediately change 00219 // the result of get_average_frame_rate(), until this 00220 // interval of time has elapsed again. 00221 // 00222 // Setting this to zero disables the computation of 00223 // get_average_frame_rate(). 00224 //////////////////////////////////////////////////////////////////// 00225 INLINE void ClockObject:: 00226 set_average_frame_rate_interval(double time) { 00227 _average_frame_rate_interval = time; 00228 if (_average_frame_rate_interval == 0.0) { 00229 _ticks.clear(); 00230 } 00231 } 00232 00233 //////////////////////////////////////////////////////////////////// 00234 // Function: ClockObject::get_average_frame_rate_interval 00235 // Access: Published 00236 // Description: Returns the interval of time (in seconds) over 00237 // which get_average_frame_rate() averages the number of frames 00238 // per second to compute the frame rate. 00239 //////////////////////////////////////////////////////////////////// 00240 INLINE double ClockObject:: 00241 get_average_frame_rate_interval() const { 00242 return _average_frame_rate_interval; 00243 } 00244 00245 //////////////////////////////////////////////////////////////////// 00246 // Function: ClockObject::check_errors 00247 // Access: Published 00248 // Description: Returns true if a clock error was detected since the 00249 // last time check_errors() was called. A clock error 00250 // means that something happened, an OS or BIOS bug, for 00251 // instance, that makes the current value of the clock 00252 // somewhat suspect, and an application may wish to 00253 // resynchronize with any external clocks. 00254 //////////////////////////////////////////////////////////////////// 00255 INLINE bool ClockObject:: 00256 check_errors(Thread *current_thread) { 00257 CDReader cdata(_cycler, current_thread); // Just to hold a mutex. 00258 int orig_error_count = _error_count; 00259 _error_count = _true_clock->get_error_count(); 00260 return (_error_count != orig_error_count); 00261 } 00262 00263 //////////////////////////////////////////////////////////////////// 00264 // Function: ClockObject::get_global_clock 00265 // Access: Published 00266 // Description: Returns a pointer to the global ClockObject. This is 00267 // the ClockObject that most code should use for 00268 // handling scene graph rendering and animation. 00269 //////////////////////////////////////////////////////////////////// 00270 INLINE ClockObject *ClockObject:: 00271 get_global_clock() { 00272 if (_global_clock == (ClockObject *)NULL) { 00273 make_global_clock(); 00274 } 00275 return _global_clock; 00276 } 00277 00278 //////////////////////////////////////////////////////////////////// 00279 // Function: ClockObject::CData::Copy Constructor 00280 // Access: Public 00281 // Description: 00282 //////////////////////////////////////////////////////////////////// 00283 INLINE ClockObject::CData:: 00284 CData(const ClockObject::CData ©) : 00285 _frame_count(copy._frame_count), 00286 _reported_frame_time(copy._reported_frame_time), 00287 _dt(copy._dt) 00288 { 00289 } 00290 00291 //////////////////////////////////////////////////////////////////// 00292 // Function: TimeVal::contructor 00293 // Access: Published 00294 // Description: 00295 //////////////////////////////////////////////////////////////////// 00296 INLINE TimeVal:: 00297 TimeVal() { 00298 } 00299 00300 //////////////////////////////////////////////////////////////////// 00301 // Function: TimeVal::get_sec 00302 // Access: Published 00303 // Description: 00304 //////////////////////////////////////////////////////////////////// 00305 INLINE ulong TimeVal:: 00306 get_sec() const { 00307 return tv[0]; 00308 } 00309 00310 //////////////////////////////////////////////////////////////////// 00311 // Function: TimeVal::get_usec 00312 // Access: Published 00313 // Description: 00314 //////////////////////////////////////////////////////////////////// 00315 INLINE ulong TimeVal:: 00316 get_usec() const { 00317 return tv[1]; 00318 }