Skip to content

Commit 5f463a4

Browse files
committed
update ofFpsCounter
1 parent 3498bfd commit 5f463a4

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

libs/openFrameworks/utils/ofFpsCounter.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
ofFpsCounter::ofFpsCounter()
44
:nFrameCount(0)
5-
,then(ofGetCurrentTime())
5+
,then(steady_clock::now())
66
,fps(0)
77
,lastFrameTime(0)
88
,filteredTime(0)
@@ -12,40 +12,43 @@ ofFpsCounter::ofFpsCounter()
1212

1313
ofFpsCounter::ofFpsCounter(double targetFPS)
1414
:nFrameCount(0)
15-
,then(ofGetCurrentTime())
15+
,then(steady_clock::now())
1616
,fps(targetFPS)
1717
,lastFrameTime(0)
1818
,filteredTime(0)
1919
,filterAlpha(0.9){}
2020

2121
void ofFpsCounter::newFrame(){
22-
auto now = ofGetCurrentTime();
23-
update(now.getAsSeconds());
24-
timestamps.push(now.getAsSeconds());
22+
now = steady_clock::now();
23+
24+
// auto now = ofGetCurrentTime();
25+
update(now);
26+
timestamps.push(now);
2527

2628
lastFrameTime = now - then;
27-
uint64_t filtered = filteredTime.count() * filterAlpha + lastFrameTime.count() * (1-filterAlpha);
28-
filteredTime = std::chrono::nanoseconds(filtered);
29+
// auto filtered =
30+
// filteredTime * filterAlpha + lastFrameTime * (1-filterAlpha);
31+
// filteredTime = filteredTime * filterAlpha + lastFrameTime * (1-filterAlpha);
2932
then = now;
3033
nFrameCount++;
3134
}
3235

3336
void ofFpsCounter::update(){
34-
auto now = ofGetCurrentTime();
35-
update(now.getAsSeconds());
37+
now = steady_clock::now();
38+
update(now);
3639
}
3740

38-
void ofFpsCounter::update(double now){
39-
while(!timestamps.empty() && timestamps.front() + 2 < now){
41+
void ofFpsCounter::update(time_point<steady_clock> now){
42+
while(!timestamps.empty() && timestamps.front() + 2s < now){
4043
timestamps.pop();
4144
}
4245

43-
auto diff = 0.0;
44-
if(!timestamps.empty() && timestamps.front() + 0.5 < now){
46+
space diff;
47+
if(!timestamps.empty() && timestamps.front() + 0.5s < now){
4548
diff = now - timestamps.front();
4649
}
47-
if(diff>0.0){
48-
fps = timestamps.size() / diff;
50+
if(diff > 0.0s){
51+
fps = (double)timestamps.size() / std::chrono::duration<double>(diff).count();
4952
}else{
5053
fps = timestamps.size();
5154
}

libs/openFrameworks/utils/ofFpsCounter.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
#include "ofUtils.h"
55
#include <queue>
66

7+
#include <chrono>
8+
#include <ctime>
9+
#include <iostream>
10+
#include <thread>
11+
12+
using namespace std::chrono;
13+
using namespace std::chrono_literals;
14+
15+
using std::cout;
16+
using std::endl;
17+
718
class ofFpsCounter {
819
public:
920
ofFpsCounter();
@@ -25,12 +36,17 @@ class ofFpsCounter {
2536
void setFilterAlpha(float alpha);
2637

2738
private:
28-
void update(double now);
39+
void update(time_point<steady_clock> now);
2940
uint64_t nFrameCount;
30-
ofTime then;
41+
// ofTime then;
42+
43+
time_point<steady_clock> now;
44+
time_point<steady_clock> then;
3145
double fps;
32-
std::chrono::nanoseconds lastFrameTime;
33-
std::chrono::nanoseconds filteredTime;
46+
47+
using space = std::chrono::duration<long long, std::nano>;
48+
space lastFrameTime;
49+
space filteredTime;
3450
double filterAlpha;
35-
std::queue<double> timestamps;
51+
std::queue<time_point<steady_clock>> timestamps;
3652
};

libs/openFrameworks/utils/ofTimerFps.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ ofTimerFps::ofTimerFps(){
99
};
1010

1111
void ofTimerFps::setFps(int fps) {
12+
// interval = std::ratio<1s, fps>;
1213
interval = duration_cast<microseconds>(1s) / fps;
1314
}
1415

0 commit comments

Comments
 (0)