framework2
80 строк · 1.7 Кб
1#include "ofFpsCounter.h"
2
3ofFpsCounter::ofFpsCounter()
4:nFrameCount(0)
5,then(ofGetCurrentTime())
6,fps(0)
7,lastFrameTime(0)
8,filteredTime(0)
9,filterAlpha(0.9){}
10
11
12
13ofFpsCounter::ofFpsCounter(double targetFPS)
14:nFrameCount(0)
15,then(ofGetCurrentTime())
16,fps(targetFPS)
17,lastFrameTime(0)
18,filteredTime(0)
19,filterAlpha(0.9){}
20
21void ofFpsCounter::newFrame(){
22auto now = ofGetCurrentTime();
23update(now.getAsSeconds());
24timestamps.push(now.getAsSeconds());
25
26lastFrameTime = now - then;
27uint64_t filtered = filteredTime.count() * filterAlpha + lastFrameTime.count() * (1-filterAlpha);
28filteredTime = std::chrono::nanoseconds(filtered);
29then = now;
30nFrameCount++;
31}
32
33void ofFpsCounter::update(){
34auto now = ofGetCurrentTime();
35update(now.getAsSeconds());
36}
37
38void ofFpsCounter::update(double now){
39while(!timestamps.empty() && timestamps.front() + 2 < now){
40timestamps.pop();
41}
42
43auto diff = 0.0;
44if(!timestamps.empty() && timestamps.front() + 0.5 < now){
45diff = now - timestamps.front();
46}
47if(diff>0.0){
48fps = timestamps.size() / diff;
49}else{
50fps = timestamps.size();
51}
52}
53
54double ofFpsCounter::getFps() const{
55return fps;
56}
57
58uint64_t ofFpsCounter::getNumFrames() const{
59return nFrameCount;
60}
61
62uint64_t ofFpsCounter::getLastFrameNanos() const{
63return lastFrameTime.count();
64}
65
66double ofFpsCounter::getLastFrameSecs() const{
67return std::chrono::duration<double>(lastFrameTime).count();
68}
69
70uint64_t ofFpsCounter::getLastFrameFilteredNanos() const{
71return lastFrameTime.count();
72}
73
74double ofFpsCounter::getLastFrameFilteredSecs() const{
75return std::chrono::duration<double>(filteredTime).count();
76}
77
78void ofFpsCounter::setFilterAlpha(float alpha){
79filterAlpha = alpha;
80}
81