diff --git a/src/glutils.cpp b/src/glutils.cpp index 536693f..b3ac430 100644 --- a/src/glutils.cpp +++ b/src/glutils.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "glutils.hpp" using namespace fmri; @@ -86,3 +88,23 @@ void fmri::checkGLErrors() } } } + +void fmri::throttleIdleFunc() +{ + using namespace std::chrono; + + constexpr duration> 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; +} diff --git a/src/glutils.hpp b/src/glutils.hpp index 5fd7fa0..fa28a00 100644 --- a/src/glutils.hpp +++ b/src/glutils.hpp @@ -38,4 +38,9 @@ namespace fmri { * Check if there are OpenGL errors and report them. */ void checkGLErrors(); + + /** + * Slow down until the idle func is being called a reasonable amount of times. + */ + void throttleIdleFunc(); } diff --git a/src/main.cpp b/src/main.cpp index 6e46145..cf11264 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -151,6 +151,7 @@ static void specialKeyFunc(int key, int, int) static void idleFunc() { checkGLErrors(); + throttleIdleFunc(); } int main(int argc, char *argv[])