26 #ifndef SAGA2_GAMERATE_H 27 #define SAGA2_GAMERATE_H 32 kGRFramesPerSecond = 0
36 uint32 _ticksPerSecond;
41 float _instantFrameCount;
45 _ticksPerSecond = perSec;
48 _instantFrameCount = 0;
53 virtual void updateFrameCount() {
54 int32 frameTime = gameTime - _lastTime;
57 _instantFrameCount = frameTime ? _ticksPerSecond / frameTime : 100;
60 virtual float frameStat(
int statID = kGRFramesPerSecond) {
61 return _instantFrameCount;
67 kGRFramesPerKilosecond = 1,
68 kGRFPKS1SecAvgNewest = 2,
69 kGRFPKS1SecAvgNew = 3,
71 kGRFPKS1SecAvgOld = 5,
72 kGRFPKS1SecAvgOldest = 6,
74 kGRFPKSAvg1SecAvg = 8,
75 kGRFPKS1SecVarNewest = 9,
76 kGRFPKS1SecVarNew = 10,
78 kGRFPKS1SecVarOld = 12,
79 kGRFPKS1SecVarOldest = 13,
81 kGRFPKSVar1SecAvg = 15
97 float ksHistory(int32 i) {
98 return 1000.0 * _frameHistory[i];
101 void calculateAverages() {
103 for (
int i = 0; i < 5; i++)
109 for (uint i = 0; i < _historySize; i++)
110 _avg1Sec[i /
int(_desiredFPS)] += ksHistory(i);
113 for (uint i = 0; i < 5; i++) {
114 _avg5Sec += _avg1Sec[i];
115 _avg1Sec[i] /= _desiredFPS;
119 _secAvg = _avg5Sec / 5;
120 _avg5Sec /= (5 * _desiredFPS);
123 void calculateVariance() {
125 for (
int i = 0; i < 5; i++)
131 for (uint i = 0; i < _historySize; i++) {
132 _dif1Sec[i / int(_desiredFPS)] +=
ABS(ksHistory(i) - _avg1Sec[i /
int(_desiredFPS)]);
133 _dif5Sec +=
ABS(ksHistory(i) - _avg5Sec);
137 for (uint i = 0; i < 5; i++) {
138 _secDif += _avg1Sec[i] - _secAvg;
139 _dif1Sec[i] /= _desiredFPS;
143 _dif5Sec /= (5 * _desiredFPS);
153 delete[] _frameHistory;
155 _frameHistory =
nullptr;
158 virtual void updateFrameCount() {
159 frameCounter::updateFrameCount();
160 _frameHistory[_frames % _historySize] = _instantFrameCount;
161 if (0 == (_frames %
int(_desiredFPS))) {
167 virtual float frameStat(
int statID = kGRFramesPerSecond) {
168 int oldestOffset = (_frames % _historySize) / _desiredFPS;
170 case kGRFramesPerKilosecond :
171 return 1000 * _instantFrameCount;
172 case kGRFPKS1SecAvgNewest :
173 return _avg1Sec[4 + oldestOffset];
174 case kGRFPKS1SecAvgNew :
175 return _avg1Sec[3 + oldestOffset];
176 case kGRFPKS1SecAvg :
177 return _avg1Sec[2 + oldestOffset];
178 case kGRFPKS1SecAvgOld :
179 return _avg1Sec[1 + oldestOffset];
180 case kGRFPKS1SecAvgOldest :
181 return _avg1Sec[0 + oldestOffset];
182 case kGRFPKS5SecAvg :
184 case kGRFPKSAvg1SecAvg :
186 case kGRFPKS1SecVarNewest :
187 return _dif1Sec[4 + oldestOffset];
188 case kGRFPKS1SecVarNew :
189 return _dif1Sec[3 + oldestOffset];
190 case kGRFPKS1SecVar :
191 return _dif1Sec[2 + oldestOffset];
192 case kGRFPKS1SecVarOld :
193 return _dif1Sec[1 + oldestOffset];
194 case kGRFPKS1SecVarOldest :
195 return _dif1Sec[0 + oldestOffset];
196 case kGRFPKS5SecVar :
198 case kGRFPKSVar1SecAvg :
201 return frameCounter::frameStat(statID);
206 frameSmoother::frameSmoother(int32 fps, uint32 perSec, uint32 now)
210 _historySize = fps * 5;
211 _frameHistory =
new float[_historySize];
213 for (uint32 i = 0; i < _historySize; i++)
214 _frameHistory[i] = 0;
216 for (
int i = 0; i < 5; i++)
217 _dif1Sec[i] = _avg1Sec[i] = 0;
Definition: gamerate.h:84
Definition: gamerate.h:35
T ABS(T x)
Definition: util.h:56