1
1
#include " ofFpsCounter.h"
2
+ #include < ofUtils.h>
2
3
using namespace std ::chrono;
3
4
4
- ofFpsCounter::ofFpsCounter () {}
5
+ ofFpsCounter::ofFpsCounter ()
6
+ : lastFrameTime(std::chrono::duration<long long , std::nano>(0 ))
7
+ , diff(std::chrono::duration<long long , std::nano>(0 ))
8
+ , then(std::chrono::steady_clock::now())
9
+ , timeMode(0 ) {
10
+ timestamps.clear ();
11
+ timestamps.resize (targetFPS + 7 );
12
+ }
5
13
6
- ofFpsCounter::ofFpsCounter (double targetFPS) : fps(targetFPS) {}
14
+ ofFpsCounter::ofFpsCounter (double targetFPS, int mode)
15
+ : targetFPS(targetFPS)
16
+ , lastFrameTime(std::chrono::duration<long long , std::nano>(0 ))
17
+ , diff(std::chrono::duration<long long , std::nano>(0 ))
18
+ , then(std::chrono::steady_clock::now())
19
+ , timeMode(mode) {
20
+ timestamps.clear ();
21
+ timestamps.resize (targetFPS + 7 );
22
+ }
7
23
8
24
void ofFpsCounter::newFrame (){
9
25
now = steady_clock::now ();
10
- update (now);
11
- timestamps.push (now);
26
+ timestamps.push_back (now);
12
27
lastFrameTime = now - then;
13
-
28
+ update (now);
14
29
// std::lerp from c++20 on
15
- filteredTime = filteredTime * filterAlpha + getLastFrameSecs () * (1 -filterAlpha);
30
+ if (timeMode == 2 ) { // Filtered
31
+ filterAlpha = std::clamp (filterAlpha, 0.0 , 1.0 );
32
+ filteredTime = filteredTime * filterAlpha + getLastFrameSecs () * (1.0 - filterAlpha);
33
+ filteredTime = std::clamp (filteredTime, 0.0 , 1.0 / targetFPS);
34
+ }
16
35
then = now;
17
36
nFrameCount++;
18
37
}
@@ -24,18 +43,19 @@ void ofFpsCounter::update(){
24
43
25
44
void ofFpsCounter::update (time_point<steady_clock> now){
26
45
while (!timestamps.empty () && timestamps.front () + 2s < now){
27
- timestamps.pop ();
46
+ timestamps.pop_front ();
28
47
}
29
-
30
- space diff;
31
- if (!timestamps.empty () && timestamps.front () + 0 .5s < now){
32
- diff = now - timestamps.front ();
48
+ if (timestamps.size () < 2 ) {
49
+ fps = targetFPS; // if no sample size then set fps to target until can sample
50
+ return ;
33
51
}
34
- if (diff > 0 .0s){
35
- fps = (double )timestamps.size () / std::chrono::duration<double >(diff).count ();
36
- }else {
52
+ diff = now - timestamps.front ();
53
+ if (diff > std::chrono::duration<double >(0 )) {
54
+ fps = static_cast <double >(timestamps.size ()) / std::chrono::duration<double >(diff).count ();
55
+ } else {
37
56
fps = timestamps.size ();
38
57
}
58
+
39
59
}
40
60
41
61
double ofFpsCounter::getFps () const {
@@ -51,17 +71,28 @@ uint64_t ofFpsCounter::getLastFrameNanos() const{
51
71
}
52
72
53
73
double ofFpsCounter::getLastFrameSecs () const {
54
- return duration_cast<seconds >(lastFrameTime).count ();
74
+ return std::chrono::duration< double >(lastFrameTime).count ();
55
75
}
56
76
57
77
uint64_t ofFpsCounter::getLastFrameFilteredNanos () const {
58
78
return duration_cast<nanoseconds>(lastFrameTime).count ();
59
79
}
60
80
61
81
double ofFpsCounter::getLastFrameFilteredSecs () const {
62
- return filteredTime;
82
+ return std::chrono::duration< double >( filteredTime). count () ;
63
83
}
64
84
65
85
void ofFpsCounter::setFilterAlpha (float alpha){
66
86
filterAlpha = alpha;
67
87
}
88
+
89
+ void ofFpsCounter::setTimeMode (int mode) {
90
+ timeMode = mode;
91
+ }
92
+
93
+ void ofFpsCounter::setTargetFPS (double fps) {
94
+ targetFPS = fps;
95
+ if (fps > timestamps.max_size ()) {
96
+ timestamps.resize (fps);
97
+ }
98
+ }
0 commit comments