Throttle the program to run at max 60fps.
This commit is contained in:
@@ -4,6 +4,8 @@
|
|||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <glog/logging.h>
|
#include <glog/logging.h>
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
#include "glutils.hpp"
|
#include "glutils.hpp"
|
||||||
|
|
||||||
using namespace fmri;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -38,4 +38,9 @@ namespace fmri {
|
|||||||
* Check if there are OpenGL errors and report them.
|
* Check if there are OpenGL errors and report them.
|
||||||
*/
|
*/
|
||||||
void checkGLErrors();
|
void checkGLErrors();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Slow down until the idle func is being called a reasonable amount of times.
|
||||||
|
*/
|
||||||
|
void throttleIdleFunc();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -151,6 +151,7 @@ static void specialKeyFunc(int key, int, int)
|
|||||||
static void idleFunc()
|
static void idleFunc()
|
||||||
{
|
{
|
||||||
checkGLErrors();
|
checkGLErrors();
|
||||||
|
throttleIdleFunc();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
|||||||
Reference in New Issue
Block a user