Skip to content

Instantly share code, notes, and snippets.

@adielfernandez
Created May 15, 2017 20:25
Show Gist options
  • Save adielfernandez/84cf3d9b3bc2df1eabcbee002e077d4c to your computer and use it in GitHub Desktop.
Save adielfernandez/84cf3d9b3bc2df1eabcbee002e077d4c to your computer and use it in GitHub Desktop.
#ifndef FrameCounter_h
#define FrameCounter_h
#endif /* FrameCounter_h */
#include "ofMain.h"
#pragma once
class FrameCounter{
public:
deque<float> frameRates;
float lastFrameTime;
float avgFrameRate;
int numFramesToAvg = 10;
long numFramesReceived = 0;
//log a new frame
void newFrame(){
numFramesReceived++;
//frame counting
float thisFrameRate = 1.0/( (ofGetElapsedTimef() - lastFrameTime) );
frameRates.push_back( thisFrameRate );
//delete oldest frame times
while( frameRates.size() > numFramesToAvg ){
frameRates.pop_front();
}
//get the avgFramerate from the frametimes we've kept
float total = 0;
for(int i = 0; i < frameRates.size(); i++){
total += frameRates[i];
}
avgFrameRate = total/(float)frameRates.size();
lastFrameTime = ofGetElapsedTimef();
}
//get that data back
float getAvgFrameRate(){
//if it's been more than a sec or two since we've gotten a frame
//return -1 instead of the last framerate we logged
if( ofGetElapsedTimef() - lastFrameTime > 2.0f ){
return -1.0f;
} else {
return avgFrameRate;
}
}
int getNumFrames(){
return numFramesReceived;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment