JNR
laTimer.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 //
31 // FILE: laTimer.cpp
32 // This class facilitates per-frame timing (delta, fps, etc)
33 //
34 // Copyright (C) 2007-2013 Atanas Laskov, <latanas@gmail.com>
35 //
36 
37 #include "stdafx.h"
38 #include "jnr.h"
39 
40 double laTimer::_dTimeMultiplier = 1; // NOTE: Replace this to enforce some delta and slow down / speed up for debugging
41 
42 laTimer::laTimer(void)
43 {
44  _dwTickCount = GetTickCount();
45  QueryPerformanceFrequency(&fr);
46 
47  _ttSampleFPS.parameters(1);
48  _ttSampleFPS.enable(M_TRUE);
49 
50  _bFirstFrame = M_TRUE;
51  _bStatisticsOn = M_FALSE;
52 
53  _dFPS = _dFPS_Min = _dFPS_Max = 60;
54  _dSPF = 1/_dFPS;
55  _dFPS_Sampled = _dFPS_Avg = _dFPS;
56  _nsamples = 1;
57 }
58 
59 laTimer::~laTimer(void)
60 {
61 }
62 
63 void laTimer::frameForce(double fps)
64 {
65  _dFPS = fps;
66  _dSPF = 1/_dFPS;
67 }
68 
69 void laTimer::frameStart()
70 {
71  QueryPerformanceCounter(&f_start);
72 }
73 
74 // Update timer
75 // NOTE: This is called only 1-ce per frame, and even if there are some additinal timers,
76 // it's probably not perfromance critical
77 //
78 //
79 void laTimer::frameEnd()
80 {
81  LARGE_INTEGER f_end;
82  QueryPerformanceCounter(&f_end);
83 
84  double delta = (f_end.QuadPart - f_start.QuadPart)/((double)fr.QuadPart);
85 
86  if( _bFirstFrame ) _bFirstFrame = M_FALSE;
87  else {
88 
89  _dSPF = delta * _dTimeMultiplier; // NOTE: Replace this to enforce some delta and slow down / speed up for debugging
90  _dFPS = (1.0/_dSPF) * _dTimeMultiplier; // NOTE: Comment multipler for FPS equivalence; Uncomment for real FPS
91 
92  if( _bStatisticsOn ) {
93  if( _dFPS < _dFPS_Min) _dFPS_Min = _dFPS;
94  if( _dFPS > _dFPS_Max) _dFPS_Max = _dFPS;
95 
96  _dFPS_Avg = ( _dFPS_Avg * _nsamples + _dFPS ) / ( _nsamples + 1.0 );
97  _nsamples += 1;
98  }
99 
100  }
101 
102  _ttSampleFPS.animate(*this);
103  if(_ttSampleFPS.isElapsed() /*&& _bStatisticsOn*/ )
104  {
105  _dFPS_Sampled = _dFPS;
106  _ttSampleFPS.reset();
107  }
108 }
109 
110 void laTimer::statistics_log()
111 {
112  MLOG("FPS Statistics: min( %.1lf FPS ), max( %.1lf FPS ), avg( %.1lf FPS, %.1lf samples )",
113  fps_min(), fps_max(), fps_avg(), _nsamples);
114 }