Throttle the program to run at max 60fps.

This commit is contained in:
2018-02-12 16:42:55 +01:00
parent 35ad3df4ca
commit 2192408ef9
3 changed files with 28 additions and 0 deletions

View File

@@ -4,6 +4,8 @@
#include <GL/glew.h>
#include <cstring>
#include <glog/logging.h>
#include <chrono>
#include <thread>
#include "glutils.hpp"
using namespace fmri;
@@ -86,3 +88,23 @@ void fmri::checkGLErrors()
}
}
}
void fmri::throttleIdleFunc()
{
using namespace std::chrono;
constexpr duration<double, ratio<1, 60>> refreshRate(1);
static auto lastCalled = steady_clock::now();
const auto now = steady_clock::now();
const auto diff = now - lastCalled;
if (diff < refreshRate) {
const auto remaining = refreshRate - diff;
this_thread::sleep_for(remaining);
}
lastCalled = now;
}