OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreFrameStats.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4  (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __FrameStats_H__
29 #define __FrameStats_H__
30 
32 
33 namespace Ogre
34 {
42 #define OGRE_FRAME_STATS_SAMPLES 60
43 
47  {
49  unsigned long mBestFrameTime;
50  unsigned long mWorstFrameTime;
51  unsigned long mLastTime;
52  unsigned long mFrameTimes[OGRE_FRAME_STATS_SAMPLES];
54 
55  public:
56  FrameStats(void)
57  {
58  reset( 0 );
59  }
60 
61  float getFps(void) const { return 1000.0f / getLastTime(); }
62  float getAvgFps(void) const { return 1000.0f / getAvgTime(); }
63 
64  float getBestTime(void) const { return mBestFrameTime * 0.001f; }
65  float getWorstTime(void) const { return mWorstFrameTime * 0.001f; }
66  float getLastTime(void) const
67  {
68  return mFrameTimes[(mNextFrame + OGRE_FRAME_STATS_SAMPLES - 1) %
69  OGRE_FRAME_STATS_SAMPLES] * 0.001f;
70  }
71 
72  float getAvgTime(void) const
73  {
74  if( !mFramesSampled )
75  return 0.0f;
76 
77  unsigned long avg = 0;
78 
79  for( size_t i=0; i<mFramesSampled; ++i )
80  {
81  //idx is first in the range [(mNextFrame - mFramesSampled); mNextFrame),
82  //but then warped around mNextFrame
83  size_t idx = (i + mNextFrame + (mFramesSampled * 2 - 1)) % mFramesSampled;
84  avg += mFrameTimes[idx];
85  }
86 
87  return avg / (float)mFramesSampled * 0.001f;
88  }
89 
91  void addSample( unsigned long timeMs )
92  {
93  unsigned long frameTimeMs = timeMs - mLastTime;
94  mFrameTimes[mNextFrame] = frameTimeMs;
95  mBestFrameTime = std::min( frameTimeMs, mBestFrameTime );
96  mWorstFrameTime = std::max( frameTimeMs, mWorstFrameTime );
97 
98  mFramesSampled = std::min<size_t>( (mFramesSampled + 1), OGRE_FRAME_STATS_SAMPLES );
99  mNextFrame = (mNextFrame + 1) % OGRE_FRAME_STATS_SAMPLES;
100 
101  mLastTime = timeMs;
102  }
103 
104  void reset( unsigned long timeMs )
105  {
106  mNextFrame = 0;
107  mBestFrameTime = std::numeric_limits<unsigned long>::max();
108  mWorstFrameTime = 0;
109  mLastTime = timeMs;
110  memset( mFrameTimes, 0, sizeof( unsigned long ) * OGRE_FRAME_STATS_SAMPLES );
111  mFramesSampled = 0;
112  }
113  };
114 
115 } // Namespace Ogre
116 #endif
#define _OgreExport
Definition: OgrePlatform.h:255
const float & max(const float &a, const float &b)
Definition: OgreCommon.h:851
#define OGRE_FRAME_STATS_SAMPLES
float getWorstTime(void) const
float getBestTime(void) const
float getAvgTime(void) const
float getFps(void) const
All return values are either in milliseconds or frames per second; but they're internally stored in m...
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
unsigned long mBestFrameTime
const float & min(const float &a, const float &b)
Definition: OgreCommon.h:846
void addSample(unsigned long timeMs)
Adds a new measured time, in microseconds
float getAvgFps(void) const
unsigned long mWorstFrameTime
This file configures Ogre's memory allocators.
float getLastTime(void) const
void reset(unsigned long timeMs)
unsigned long mLastTime