-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfpscounter.h
More file actions
60 lines (50 loc) · 1.15 KB
/
fpscounter.h
File metadata and controls
60 lines (50 loc) · 1.15 KB
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
#include <cstdint>
#include <chrono>
namespace ic4demoapp
{
struct FpsCounter
{
public:
FpsCounter(std::chrono::duration<uint64_t> update_interval = std::chrono::seconds(1))
: update_interval_(update_interval)
{
}
public:
void notify_frame()
{
if (count_ < 0)
{
prev_update_ = std::chrono::high_resolution_clock::now();
count_ = 0;
}
else
{
++count_;
update_if_required(update_interval_);
}
}
double current()
{
if (count_ < 0)
return 0;
update_if_required(update_interval_ * 3);
return current_;
}
private:
void update_if_required(std::chrono::duration<uint64_t> interval)
{
auto now = std::chrono::high_resolution_clock::now();
if (now > prev_update_ + interval)
{
auto dt_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(now - prev_update_).count();
current_ = 1e9 * static_cast<double>(count_) / static_cast<double>(dt_ns);
prev_update_ = now;
count_ = 0;
}
}
std::chrono::duration<uint64_t> update_interval_;
int64_t count_ = -1;
std::chrono::time_point<std::chrono::high_resolution_clock> prev_update_ = {};
double current_ = 0;
};
}