@@ -230,53 +230,72 @@ get_latest_frame() const {
230230}
231231
232232// //////////////////////////////////////////////////////////////////
233- // Function: PStatThreadData::get_frame_rate
233+ // Function: PStatThreadData::get_elapsed_frames
234234// Access: Public
235- // Description: Computes the average frame rate over the past number
236- // of seconds, by counting up the number of frames
237- // elapsed in that time interval.
235+ // Description: Computes the oldest frame number not older than time
236+ // seconds, and the newest frame number. Handy for
237+ // computing average frame rate over a time. Returns
238+ // true if there is any data in that range, false
239+ // otherwise.
238240// //////////////////////////////////////////////////////////////////
239- float PStatThreadData::
240- get_frame_rate ( float time) const {
241+ bool PStatThreadData::
242+ get_elapsed_frames ( int &then_i, int &now_i, float time) const {
241243 if (_frames.empty ()) {
242- // No frames in the data at all; nothing to base the frame rate
243- // on.
244- return 0.0 ;
244+ // No frames in the data at all.
245+ return false ;
245246 }
246247
247- int now_i = _frames.size () - 1 ;
248+ now_i = _frames.size () - 1 ;
248249 while (now_i > 0 && _frames[now_i] == (PStatFrameData *)NULL ) {
249250 now_i--;
250251 }
251252 if (now_i < 0 ) {
252253 // No frames have any real data.
253- return 0.0 ;
254+ return false ;
254255 }
255- nassertr (_frames[now_i] != (PStatFrameData *)NULL , 0.0 );
256+ nassertr (_frames[now_i] != (PStatFrameData *)NULL , false );
256257
257258 float now = _frames[now_i]->get_end ();
258259 float then = now - time;
259260
260- int then_i = now_i;
261- int last_good_i = now_i;
261+ int old_i = now_i;
262+ then_i = now_i;
262263
263- while (then_i >= 0 ) {
264- const PStatFrameData *frame = _frames[then_i ];
264+ while (old_i >= 0 ) {
265+ const PStatFrameData *frame = _frames[old_i ];
265266 if (frame != (PStatFrameData *)NULL ) {
266267 if (frame->get_start () > then) {
267- last_good_i = then_i ;
268+ then_i = old_i ;
268269 } else {
269270 break ;
270271 }
271272 }
272- then_i --;
273+ old_i --;
273274 }
274275
275- nassertr (last_good_i >= 0 , 0.0 );
276- nassertr (_frames[last_good_i] != (PStatFrameData *)NULL , 0.0 );
276+ nassertr (then_i >= 0 , false );
277+ nassertr (_frames[then_i] != (PStatFrameData *)NULL , false );
278+
279+ return true ;
280+ }
277281
278- int num_frames = now_i - last_good_i + 1 ;
279- return (float )num_frames / (now - _frames[last_good_i]->get_start ());
282+ // //////////////////////////////////////////////////////////////////
283+ // Function: PStatThreadData::get_frame_rate
284+ // Access: Public
285+ // Description: Computes the average frame rate over the past number
286+ // of seconds, by counting up the number of frames
287+ // elapsed in that time interval.
288+ // //////////////////////////////////////////////////////////////////
289+ float PStatThreadData::
290+ get_frame_rate (float time) const {
291+ int then_i, now_i;
292+ if (!get_elapsed_frames (then_i, now_i, time)) {
293+ return 0 .0f ;
294+ }
295+
296+ int num_frames = now_i - then_i + 1 ;
297+ float now = _frames[now_i]->get_end ();
298+ return (float )num_frames / (now - _frames[then_i]->get_start ());
280299}
281300
282301
0 commit comments