New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FPS timing with chrono #7867
base: master
Are you sure you want to change the base?
FPS timing with chrono #7867
Conversation
cc @artificiel |
I've made a proof of concept here and I'm attaching one image and the code. |
@dimitre - Awesome! |
Great! I'll be testing in the next days and report back here. |
@artificiel can you please test this PR with your tests mentioned here? I think this PR can be a slight improvement to actual FPS. #include <chrono>
using namespace std::chrono;
using namespace std::chrono_literals;
struct fpsCounter {
public:
int nAverages = 20;
using space = std::chrono::duration<long double, std::nano>;
time_point<steady_clock> lastTick;
steady_clock::duration onesec = 1s;
std::vector <space> intervals;
space interval;
space average;
bool firstTick = true;
int cursor = 0;
void tick() {
if (firstTick) {
firstTick = false;
lastTick = steady_clock::now();
return;
}
interval = steady_clock::now() - lastTick;
lastTick = steady_clock::now();
if (intervals.size() < nAverages) {
intervals.emplace_back(interval);
} else {
intervals[cursor] = interval;
cursor = (cursor+1)%nAverages;
}
}
double getFps() {
average = std::reduce(intervals.begin(), intervals.end());
return (double)intervals.size() * onesec / average;
}
float get() {
average = std::reduce(intervals.begin(), intervals.end());
return (float)intervals.size() * onesec / average;
}
}; |
This PR has the intent of modernize OF framerate managing.
The idea is using only std::chrono for two reasons : let std::chrono manage all time divisions in the best way possible, and unifying code across platforms.
it was tested in macOS and reduced FPS drift compared to ofTimer implementation