-
Notifications
You must be signed in to change notification settings - Fork 0
/
fps.cpp
63 lines (51 loc) · 1 KB
/
fps.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "fps.h"
#include <chrono>
class Timer
{
public:
Timer() noexcept
: start{ std::chrono::steady_clock::now() }
{
}
[[nodiscard]] auto value() const noexcept
{
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count();
}
void reset() noexcept
{
start = std::chrono::steady_clock::now();
}
private:
std::chrono::steady_clock::time_point start;
};
FpsCounter::FpsCounter()
: fps{ 60 }
, averageFps{ 60 }
, frameCount{ 0 }
, timer{ new Timer }
{
}
void FpsCounter::tick() noexcept
{
frameCount++;
if (timer->value() >= 1000000)
{
fps = frameCount;
constexpr auto alpha = 0.25; // TODO: consider making this configurable
averageFps = alpha * averageFps + (1.0 - alpha) * frameCount;
frameCount = 0;
timer->reset();
}
}
double FpsCounter::getFps() const noexcept
{
return fps;
}
double FpsCounter::getAverageFps() const noexcept
{
return fps;
}
unsigned int FpsCounter::getFrameCount() const noexcept
{
return frameCount;
}