11#include " ofFpsCounter.h"
2+ #include < ofUtils.h>
23using namespace std ::chrono;
34
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+ }
513
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+ }
723
824void ofFpsCounter::newFrame (){
925 now = steady_clock::now ();
10- update (now);
11- timestamps.push (now);
26+ timestamps.push_back (now);
1227 lastFrameTime = now - then;
13-
28+ update (now);
1429 // 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+ }
1635 then = now;
1736 nFrameCount++;
1837}
@@ -24,18 +43,19 @@ void ofFpsCounter::update(){
2443
2544void ofFpsCounter::update (time_point<steady_clock> now){
2645 while (!timestamps.empty () && timestamps.front () + 2s < now){
27- timestamps.pop ();
46+ timestamps.pop_front ();
2847 }
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 ;
3351 }
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 {
3756 fps = timestamps.size ();
3857 }
58+
3959}
4060
4161double ofFpsCounter::getFps () const {
@@ -51,17 +71,28 @@ uint64_t ofFpsCounter::getLastFrameNanos() const{
5171}
5272
5373double ofFpsCounter::getLastFrameSecs () const {
54- return duration_cast<seconds >(lastFrameTime).count ();
74+ return std::chrono::duration< double >(lastFrameTime).count ();
5575}
5676
5777uint64_t ofFpsCounter::getLastFrameFilteredNanos () const {
5878 return duration_cast<nanoseconds>(lastFrameTime).count ();
5979}
6080
6181double ofFpsCounter::getLastFrameFilteredSecs () const {
62- return filteredTime;
82+ return std::chrono::duration< double >( filteredTime). count () ;
6383}
6484
6585void ofFpsCounter::setFilterAlpha (float alpha){
6686 filterAlpha = alpha;
6787}
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