source: rrlib_util/tFPSComputer.h @ 46:693922a1844d

Last change on this file since 46:693922a1844d was 46:693922a1844d, checked in by Bernd-Helge Schaefer <schaefer@…>, 9 years ago

tFPSComputer: Added support for incrementing the frame counter by more than 1 frame at a time

File size: 3.1 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public License
9// as published by the Free Software Foundation; either version 2
10// of the License, or (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program; if not, write to the Free Software
19// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20//
21//----------------------------------------------------------------------
22/*!\file    tFPSComputer.h
23 *
24 * \author  Bernd Helge Schaefer
25 * \date    2008-03-29
26 *
27 * \brief   Contains class tFPSComputer
28 *
29 */
30//----------------------------------------------------------------------
31// this is a -*- C++ -*- file
32//----------------------------------------------------------------------
33#ifndef _util_tFPSComputer_h_
34#define _util_tFPSComputer_h_
35
36#include "rrlib/util/tTime.h"
37
38namespace rrlib
39{
40namespace util
41{
42class tFPSComputer
43{
44public:
45  tFPSComputer(long long int check_interval_ms = 1000, float old_value_weight = 0.75);
46
47  tFPSComputer(const util::tTime &current_time, long long int check_interval_ms = 1000, float old_value_weight = 0.75);
48
49  inline void IncrementFrameCounter(unsigned int num_frames = 1)
50  {
51    this->frame_counter += num_frames;
52  }
53
54  inline bool IncrementFrameCounterAndCheckFPS()
55  {
56    return IncrementFrameCounterAndCheckFPS(util::tTime::Now());
57  }
58
59  inline bool IncrementFrameCounterAndCheckFPS(const util::tTime &current_time)
60  {
61    ++this->frame_counter;
62    return this->CheckFPS(current_time);
63  }
64
65  inline bool CheckFPS()
66  {
67    return CheckFPS(util::tTime::Now());
68  }
69
70  inline bool CheckFPS(const util::tTime &current_time)
71  {
72    current_fps_check = current_time;
73    long long int ms_passed = (current_fps_check - last_fps_check).ToMSec();
74    if (ms_passed < 0)
75    {
76      last_fps_check = current_time;
77      return false;
78    }
79    else if (ms_passed > this->check_interval_ms)
80    {
81      double number_of_frames = this->frame_counter - this->last_frame_counter;
82
83      fps = this->old_value_weight * fps + (1. - this->old_value_weight) * (number_of_frames * (1000. / (double) ms_passed));
84
85      if (this->fps < 0.1)
86      {
87        this->fps = 0.;
88      }
89
90      this->last_frame_counter = this->frame_counter;
91      this->last_fps_check = this->current_fps_check;
92      return true;
93    }
94    return false;
95  }
96
97  inline float FPS() const
98  {
99    return this->fps;
100  }
101
102  inline double FrameCounter() const
103  {
104    return this->frame_counter;
105  }
106
107private:
108  util::tTime current_fps_check;
109  util::tTime last_fps_check;
110  util::tTime time_diff_check;
111  double last_frame_counter;
112  double frame_counter;
113  double time_diff;
114  float fps;
115  float old_value_weight;
116  long long int check_interval_ms;
117};
118
119}
120}
121#endif
Note: See TracBrowser for help on using the repository browser.