Panda3D
clockObject.I
1 // Filename: clockObject.I
2 // Created by: drose (17Feb00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 ////////////////////////////////////////////////////////////////////
16 // Function: ClockObject::Destructor
17 // Access: Published
18 // Description:
19 ////////////////////////////////////////////////////////////////////
20 INLINE ClockObject::
21 ~ClockObject() {
22 }
23 
24 ////////////////////////////////////////////////////////////////////
25 // Function: ClockObject::get_mode
26 // Access: Published
27 // Description: Returns the current mode of the clock. See
28 // set_mode().
29 ////////////////////////////////////////////////////////////////////
30 INLINE ClockObject::Mode ClockObject::
31 get_mode() const {
32  return _mode;
33 }
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: ClockObject::get_frame_time
37 // Access: Published
38 // Description: Returns the time in seconds as of the last time
39 // tick() was called (typically, this will be as of the
40 // start of the current frame).
41 //
42 // This is generally the kind of time you want to ask
43 // for in most rendering and animation contexts, since
44 // it's important that all of the animation for a given
45 // frame remains in sync with each other.
46 ////////////////////////////////////////////////////////////////////
47 INLINE double ClockObject::
48 get_frame_time(Thread *current_frame) const {
49  CDReader cdata(_cycler, current_frame);
50  return cdata->_reported_frame_time;
51 }
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function: ClockObject::get_real_time
55 // Access: Published
56 // Description: Returns the actual number of seconds elapsed since
57 // the ClockObject was created, or since it was last
58 // reset. This is useful for doing real timing
59 // measurements, e.g. for performance statistics.
60 //
61 // This returns the most precise timer we have for short
62 // time intervals, but it may tend to drift over the
63 // long haul. If more accurate timekeeping is needed
64 // over a long period of time, use get_long_time()
65 // instead.
66 ////////////////////////////////////////////////////////////////////
67 INLINE double ClockObject::
68 get_real_time() const {
69  return (_true_clock->get_short_time() - _start_short_time);
70 }
71 
72 ////////////////////////////////////////////////////////////////////
73 // Function: ClockObject::get_long_time
74 // Access: Published
75 // Description: Returns the actual number of seconds elapsed since
76 // the ClockObject was created, or since it was last
77 // reset.
78 //
79 // This is similar to get_real_time(), except that it
80 // uses the most accurate counter we have over a long
81 // period of time, and so it is less likely to drift.
82 // However, it may not be very precise for measuring
83 // short intervals. On Windows, for instace, this is
84 // only accurate to within about 55 milliseconds.
85 ////////////////////////////////////////////////////////////////////
86 INLINE double ClockObject::
87 get_long_time() const {
88  return (_true_clock->get_long_time() - _start_long_time);
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function: ClockObject::reset
93 // Access: Published
94 // Description: Simultaneously resets both the time and the frame
95 // count to zero.
96 ////////////////////////////////////////////////////////////////////
97 INLINE void ClockObject::
98 reset() {
99  set_real_time(0.0);
100  set_frame_time(0.0);
101  set_frame_count(0);
102 }
103 
104 ////////////////////////////////////////////////////////////////////
105 // Function: ClockObject::get_frame_count
106 // Access: Published
107 // Description: Returns the number of times tick() has been called
108 // since the ClockObject was created, or since it was
109 // last reset. This is generally the number of frames
110 // that have been rendered.
111 ////////////////////////////////////////////////////////////////////
112 INLINE int ClockObject::
113 get_frame_count(Thread *current_thread) const {
114  CDReader cdata(_cycler, current_thread);
115  return cdata->_frame_count;
116 }
117 
118 ////////////////////////////////////////////////////////////////////
119 // Function: ClockObject::get_net_frame_rate
120 // Access: Published
121 // Description: Returns the average frame rate since the last reset.
122 // This is simply the total number of frames divided by
123 // the total elapsed time. This reports the virtual
124 // frame rate if the clock is in (or has been in)
125 // M_non_real_time mode.
126 ////////////////////////////////////////////////////////////////////
127 INLINE double ClockObject::
128 get_net_frame_rate(Thread *current_thread) const {
129  CDReader cdata(_cycler, current_thread);
130  return (double)cdata->_frame_count / cdata->_reported_frame_time;
131 }
132 
133 ////////////////////////////////////////////////////////////////////
134 // Function: ClockObject::get_dt
135 // Access: Published
136 // Description: Returns the elapsed time for the previous frame: the
137 // number of seconds elapsed between the last two calls
138 // to tick().
139 ////////////////////////////////////////////////////////////////////
140 INLINE double ClockObject::
141 get_dt(Thread *current_thread) const {
142  CDReader cdata(_cycler, current_thread);
143  if (_max_dt > 0.0) {
144  return min(_max_dt, cdata->_dt);
145  }
146  return cdata->_dt;
147 }
148 
149 ////////////////////////////////////////////////////////////////////
150 // Function: ClockObject::get_max_dt
151 // Access: Published
152 // Description: Returns the current maximum allowable time elapsed
153 // between any two frames. See set_max_dt().
154 ////////////////////////////////////////////////////////////////////
155 INLINE double ClockObject::
156 get_max_dt() const {
157  return _max_dt;
158 }
159 
160 ////////////////////////////////////////////////////////////////////
161 // Function: ClockObject::set_max_dt
162 // Access: Published
163 // Description: Sets a limit on the value returned by get_dt(). If
164 // this value is less than zero, no limit is imposed;
165 // otherwise, this is the maximum value that will ever
166 // be returned by get_dt(), regardless of how much time
167 // has actually elapsed between frames.
168 //
169 // This limit is only imposed in real-time mode; in
170 // non-real-time mode, the dt is fixed anyway and max_dt
171 // is ignored.
172 //
173 // This is generally used to guarantee reasonable
174 // behavior even in the presence of a very slow or
175 // chuggy frame rame.
176 ////////////////////////////////////////////////////////////////////
177 INLINE void ClockObject::
178 set_max_dt(double max_dt) {
179  _max_dt = max_dt;
180 }
181 
182 ////////////////////////////////////////////////////////////////////
183 // Function: ClockObject::get_degrade_factor
184 // Access: Published
185 // Description: In degrade mode, returns the ratio by which the
186 // performance is degraded. A value of 2.0 causes the
187 // clock to be slowed down by a factor of two (reducing
188 // performance to 1/2 what would be otherwise).
189 //
190 // This has no effect if mode is not M_degrade.
191 ////////////////////////////////////////////////////////////////////
192 INLINE double ClockObject::
194  return _degrade_factor;
195 }
196 
197 ////////////////////////////////////////////////////////////////////
198 // Function: ClockObject::set_degrade_factor
199 // Access: Published
200 // Description: In degrade mode, sets the ratio by which the
201 // performance is degraded. A value of 2.0 causes the
202 // clock to be slowed down by a factor of two (reducing
203 // performance to 1/2 what would be otherwise).
204 //
205 // This has no effect if mode is not M_degrade.
206 ////////////////////////////////////////////////////////////////////
207 INLINE void ClockObject::
208 set_degrade_factor(double degrade_factor) {
209  _degrade_factor = degrade_factor;
210 }
211 
212 ////////////////////////////////////////////////////////////////////
213 // Function: ClockObject::set_average_frame_rate_interval
214 // Access: Published
215 // Description: Specifies the interval of time (in seconds) over
216 // which get_average_frame_rate() averages the number of
217 // frames per second to compute the frame rate.
218 // Changing this does not necessarily immediately change
219 // the result of get_average_frame_rate(), until this
220 // interval of time has elapsed again.
221 //
222 // Setting this to zero disables the computation of
223 // get_average_frame_rate().
224 ////////////////////////////////////////////////////////////////////
225 INLINE void ClockObject::
227  _average_frame_rate_interval = time;
228  if (_average_frame_rate_interval == 0.0) {
229  _ticks.clear();
230  }
231 }
232 
233 ////////////////////////////////////////////////////////////////////
234 // Function: ClockObject::get_average_frame_rate_interval
235 // Access: Published
236 // Description: Returns the interval of time (in seconds) over
237 // which get_average_frame_rate() averages the number of frames
238 // per second to compute the frame rate.
239 ////////////////////////////////////////////////////////////////////
240 INLINE double ClockObject::
242  return _average_frame_rate_interval;
243 }
244 
245 ////////////////////////////////////////////////////////////////////
246 // Function: ClockObject::check_errors
247 // Access: Published
248 // Description: Returns true if a clock error was detected since the
249 // last time check_errors() was called. A clock error
250 // means that something happened, an OS or BIOS bug, for
251 // instance, that makes the current value of the clock
252 // somewhat suspect, and an application may wish to
253 // resynchronize with any external clocks.
254 ////////////////////////////////////////////////////////////////////
255 INLINE bool ClockObject::
256 check_errors(Thread *current_thread) {
257  CDReader cdata(_cycler, current_thread); // Just to hold a mutex.
258  int orig_error_count = _error_count;
259  _error_count = _true_clock->get_error_count();
260  return (_error_count != orig_error_count);
261 }
262 
263 ////////////////////////////////////////////////////////////////////
264 // Function: ClockObject::get_global_clock
265 // Access: Published
266 // Description: Returns a pointer to the global ClockObject. This is
267 // the ClockObject that most code should use for
268 // handling scene graph rendering and animation.
269 ////////////////////////////////////////////////////////////////////
272  if (_global_clock == (ClockObject *)NULL) {
273  make_global_clock();
274  }
275  return _global_clock;
276 }
277 
278 ////////////////////////////////////////////////////////////////////
279 // Function: ClockObject::CData::Copy Constructor
280 // Access: Public
281 // Description:
282 ////////////////////////////////////////////////////////////////////
283 INLINE ClockObject::CData::
284 CData(const ClockObject::CData &copy) :
285  _frame_count(copy._frame_count),
286  _reported_frame_time(copy._reported_frame_time),
287  _dt(copy._dt)
288 {
289 }
290 
291 ////////////////////////////////////////////////////////////////////
292 // Function: TimeVal::contructor
293 // Access: Published
294 // Description:
295 ////////////////////////////////////////////////////////////////////
296 INLINE TimeVal::
297 TimeVal() {
298 }
299 
300 ////////////////////////////////////////////////////////////////////
301 // Function: TimeVal::get_sec
302 // Access: Published
303 // Description:
304 ////////////////////////////////////////////////////////////////////
305 INLINE ulong TimeVal::
306 get_sec() const {
307  return tv[0];
308 }
309 
310 ////////////////////////////////////////////////////////////////////
311 // Function: TimeVal::get_usec
312 // Access: Published
313 // Description:
314 ////////////////////////////////////////////////////////////////////
315 INLINE ulong TimeVal::
316 get_usec() const {
317  return tv[1];
318 }
static ClockObject * get_global_clock()
Returns a pointer to the global ClockObject.
Definition: clockObject.I:271
void set_degrade_factor(double degrade_factor)
In degrade mode, sets the ratio by which the performance is degraded.
Definition: clockObject.I:208
void set_frame_time(double time, Thread *current_thread=Thread::get_current_thread())
Changes the time as reported for the current frame to the indicated time.
double get_degrade_factor() const
In degrade mode, returns the ratio by which the performance is degraded.
Definition: clockObject.I:193
void set_average_frame_rate_interval(double time)
Specifies the interval of time (in seconds) over which get_average_frame_rate() averages the number o...
Definition: clockObject.I:226
double get_net_frame_rate(Thread *current_thread=Thread::get_current_thread()) const
Returns the average frame rate since the last reset.
Definition: clockObject.I:128
double get_frame_time(Thread *current_thread=Thread::get_current_thread()) const
Returns the time in seconds as of the last time tick() was called (typically, this will be as of the ...
Definition: clockObject.I:48
double get_long_time() const
Returns the actual number of seconds elapsed since the ClockObject was created, or since it was last ...
Definition: clockObject.I:87
double get_average_frame_rate_interval() const
Returns the interval of time (in seconds) over which get_average_frame_rate() averages the number of ...
Definition: clockObject.I:241
This template class calls PipelineCycler::read_unlocked(), and then provides a transparent read-only ...
void set_max_dt(double max_dt)
Sets a limit on the value returned by get_dt().
Definition: clockObject.I:178
void set_real_time(double time)
Resets the clock to the indicated time.
int get_frame_count(Thread *current_thread=Thread::get_current_thread()) const
Returns the number of times tick() has been called since the ClockObject was created, or since it was last reset.
Definition: clockObject.I:113
A ClockObject keeps track of elapsed real time and discrete time.
Definition: clockObject.h:66
double get_real_time() const
Returns the actual number of seconds elapsed since the ClockObject was created, or since it was last ...
Definition: clockObject.I:68
Mode get_mode() const
Returns the current mode of the clock.
Definition: clockObject.I:31
double get_max_dt() const
Returns the current maximum allowable time elapsed between any two frames.
Definition: clockObject.I:156
void set_frame_count(int frame_count, Thread *current_thread=Thread::get_current_thread())
Resets the number of frames counted to the indicated number.
A thread; that is, a lightweight process.
Definition: thread.h:51
int get_error_count() const
Returns the number of clock errors that have been detected.
Definition: trueClock.I:70
bool check_errors(Thread *current_thread)
Returns true if a clock error was detected since the last time check_errors() was called...
Definition: clockObject.I:256
void reset()
Simultaneously resets both the time and the frame count to zero.
Definition: clockObject.I:98
double get_dt(Thread *current_thread=Thread::get_current_thread()) const
Returns the elapsed time for the previous frame: the number of seconds elapsed between the last two c...
Definition: clockObject.I:141