diff --git a/include/settings.hpp b/include/settings.hpp index a0e92e77..1018cf08 100644 --- a/include/settings.hpp +++ b/include/settings.hpp @@ -18,6 +18,7 @@ extern bool powerMeter; extern bool lockModules; extern bool checkVersion; extern bool skipLoadOnLaunch; +extern float frameRateLimit; } // namespace settings diff --git a/src/settings.cpp b/src/settings.cpp index 6a17d206..14d938fa 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -77,6 +77,9 @@ static json_t *settingsToJson() { // paramTooltip json_object_set_new(rootJ, "paramTooltip", json_boolean(paramTooltip)); + // frameRateLimit + json_object_set_new(rootJ, "frameRateLimit", json_real(frameRateLimit)); + return rootJ; } @@ -163,6 +166,11 @@ static void settingsFromJson(json_t *rootJ) { json_t *paramTooltipJ = json_object_get(rootJ, "paramTooltip"); if (paramTooltipJ) paramTooltip = json_boolean_value(paramTooltipJ); + + // frameRateLimit + json_t *frameRateLimitJ = json_object_get(rootJ, "frameRateLimit"); + if (frameRateLimitJ) + frameRateLimit = json_number_value(frameRateLimitJ); } @@ -208,6 +216,7 @@ bool powerMeter = false; bool lockModules = false; bool checkVersion = true; bool skipLoadOnLaunch = false; +float frameRateLimit = 0.0; } // namespace settings diff --git a/src/window.cpp b/src/window.cpp index 03177c58..98e18158 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -6,6 +6,7 @@ #include "event.hpp" #include "app.hpp" #include "patch.hpp" +#include "settings.hpp" #include #include @@ -221,6 +222,7 @@ Window::Window() { glfwSetInputMode(win, GLFW_LOCK_KEY_MODS, 1); glfwMakeContextCurrent(win); + // Enable v-sync glfwSwapInterval(1); glfwSetWindowSizeCallback(win, windowSizeCallback); @@ -371,15 +373,18 @@ void Window::run() { glfwSwapBuffers(win); } - // Limit framerate manually if vsync isn't working + // Limit frame rate double endTime = glfwGetTime(); - double frameTime = endTime - startTime; - double minTime = 1 / 120.0; - if (frameTime < minTime) { - std::this_thread::sleep_for(std::chrono::duration(minTime - frameTime)); + if (settings::frameRateLimit > 0.0) { + double frameDuration = endTime - startTime; + double waitDuration = 1.0 / settings::frameRateLimit - frameDuration; + if (waitDuration > 0.0) { + std::this_thread::sleep_for(std::chrono::duration(waitDuration)); + } } + + // Compute actual frame rate endTime = glfwGetTime(); - // INFO("%lf fps", 1.0 / (endTime - startTime)); frame++; } }