OpenFrames
FramerateLimiter.hpp
Go to the documentation of this file.
1 /***********************************
2  Copyright 2018 Ravishankar Mathur
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  ***********************************/
16 
21 #ifndef _OF_FRAMERATELIMITER_
22 #define _OF_FRAMERATELIMITER_
23 
24 #include <OpenFrames/Export.h>
25 #include <osg/Timer>
26 
27 namespace OpenFrames
28 {
40  class OF_EXPORT FramerateLimiter
41  {
42  public:
43  FramerateLimiter(double fps = 30.0);
44 
45  // Set/get the desired framerate in frames/second
46  void setDesiredFramerate(double fps);
47  double getDesiredFramerate()
48  {
49  if(_desired_spf == 0.0) return 0.0;
50  else return 1.0/_desired_spf;
51  }
52 
53  // Get the actual framerate for the previous set of frames
54  double getFramerate() { return 1.0/_curr_spf; }
55 
56  // Indicates the start of a frame
57  void frame();
58 
59  // Reset the timer
60  void reset();
61 
62  private:
63  double _desired_spf; // Desired seconds/frame (inverse of fps)
64  double _curr_spf; // Current measured seconds/frame
65 
66  unsigned int _max_frames; // Number of frames to use for averaging
67  double _max_frames_inv; // Inverse of _max_frames (for efficiency)
68  unsigned int _framecount; // Current number of elapsed frames
69 
70  int _sleeptime; // Time length to sleep for, in milli/microseconds (depending on system)
71  const osg::Timer &_timer; // Timer, duh....
72  osg::Timer_t _ref_time; // Time at start of framerate check
73  };
74 
75 } // !namespace OpenFrames
76 
77 #endif // !_OF_FRAMERATELIMITER_
This class implements a framerate-limiting algorithm.
Definition: FramerateLimiter.hpp:40
Definition: CoordinateAxes.hpp:29