Created
July 19, 2016 11:54
-
-
Save acidleaf/f2c0b216188a664de478fa8471d79e68 to your computer and use it in GitHub Desktop.
Simple FPS Counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __FPS_H__ | |
#define __FPS_H__ | |
// FPS Counter class | |
// Keep an instance in the main app context, and call update every frame | |
// Use the () operator to get the current FPS | |
class FPSCounter { | |
private: | |
unsigned long _frames = 0; | |
float _dt = 0.0f; | |
float _fps = 0.0f; | |
public: | |
// dt: time passed since last frame | |
// rate: how many times to update FPS per second | |
void update(double dt, float rate = 5.0f) { | |
++_frames; | |
_dt += (float)dt * 0.001f; | |
if (_dt > 1.0f / rate) { | |
_fps = _frames / _dt; | |
_dt -= 1.0f / rate; | |
_frames = 0; | |
} | |
} | |
float operator()() const { return _fps; } | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment