-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFPSCounter.cpp
More file actions
34 lines (28 loc) · 806 Bytes
/
FPSCounter.cpp
File metadata and controls
34 lines (28 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "FPSCounter.h"
FPSCounter::FPSCounter() :
frameCount(0),
lastTime(0.0f)
{
}
float FPSCounter::getFPS(float newTime) {
// Use frameCount instead of NUM_FRAMES for the first few frames so that averaging works
int count = frameCount < NUM_FRAMES ? frameCount : NUM_FRAMES;
// Store frame time sample
frameTimes[frameCount % NUM_FRAMES] = newTime - lastTime;
// Calculate FPS
float averageFrameTime = 0;
for (int i = 0; i < count; ++i) {
averageFrameTime += frameTimes[i];
}
float fps;
if (averageFrameTime) { // Avoid div-by-zero
averageFrameTime /= count;
fps = 1.0f / averageFrameTime;
} else {
fps = 0.0f;
}
// Increment for next frame
frameCount++;
lastTime = newTime;
return fps;
}