diff --git a/include/settings.hpp b/include/settings.hpp index 767e0cd8..a2fbe86f 100644 --- a/include/settings.hpp +++ b/include/settings.hpp @@ -34,8 +34,7 @@ extern int threadCount; extern bool paramTooltip; extern bool cpuMeter; extern bool lockModules; -extern float frameRateLimit; -extern bool frameRateSync; +extern int frameSwapInterval; extern float autosavePeriod; extern bool skipLoadOnLaunch; extern std::string patchPath; diff --git a/src/app/Scene.cpp b/src/app/Scene.cpp index add47c34..25fc7d2e 100644 --- a/src/app/Scene.cpp +++ b/src/app/Scene.cpp @@ -56,6 +56,11 @@ void Scene::step() { void Scene::draw(const DrawArgs& args) { Widget::draw(args); + + // nvgBeginPath(args.vg); + // nvgRect(args.vg, 0, 0, box.size.x, box.size.y); + // nvgFillColor(args.vg, color::mult(color::WHITE, APP->window->frame % 2)); + // nvgFill(args.vg); } void Scene::onHoverKey(const event::HoverKey& e) { diff --git a/src/settings.cpp b/src/settings.cpp index 954a0952..eeb1ffc4 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -29,8 +29,7 @@ int threadCount = 1; bool paramTooltip = false; bool cpuMeter = false; bool lockModules = false; -float frameRateLimit = 70.0; -bool frameRateSync = true; +int frameSwapInterval = 1; float autosavePeriod = 15.0; bool skipLoadOnLaunch = false; std::string patchPath; @@ -75,9 +74,7 @@ json_t* toJson() { json_object_set_new(rootJ, "lockModules", json_boolean(lockModules)); - json_object_set_new(rootJ, "frameRateLimit", json_real(frameRateLimit)); - - json_object_set_new(rootJ, "frameRateSync", json_boolean(frameRateSync)); + json_object_set_new(rootJ, "frameSwapInterval", json_integer(frameSwapInterval)); json_object_set_new(rootJ, "autosavePeriod", json_real(autosavePeriod)); @@ -160,13 +157,9 @@ void fromJson(json_t* rootJ) { if (lockModulesJ) lockModules = json_boolean_value(lockModulesJ); - json_t* frameRateLimitJ = json_object_get(rootJ, "frameRateLimit"); - if (frameRateLimitJ) - frameRateLimit = json_number_value(frameRateLimitJ); - - json_t* frameRateSyncJ = json_object_get(rootJ, "frameRateSync"); - if (frameRateSyncJ) - frameRateSync = json_boolean_value(frameRateSyncJ); + json_t* frameSwapIntervalJ = json_object_get(rootJ, "frameSwapInterval"); + if (frameSwapIntervalJ) + frameSwapInterval = json_integer_value(frameSwapIntervalJ); json_t* autosavePeriodJ = json_object_get(rootJ, "autosavePeriod"); if (autosavePeriodJ) diff --git a/src/window.cpp b/src/window.cpp index 3cab776f..c40fa126 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -95,6 +95,7 @@ struct Window::Internal { int lastWindowHeight = 0; bool ignoreNextMouseDelta = false; + int frameSwapInterval = -1; }; @@ -248,8 +249,7 @@ Window::Window() { glfwSetInputMode(win, GLFW_LOCK_KEY_MODS, 1); glfwMakeContextCurrent(win); - // Enable v-sync - glfwSwapInterval(settings::frameRateSync ? 1 : 0); + glfwSwapInterval(1); // Set window callbacks glfwSetWindowSizeCallback(win, windowSizeCallback); @@ -333,8 +333,14 @@ void Window::run() { // Poll events glfwPollEvents(); - // In case glfwPollEvents() set another OpenGL context + + // In case glfwPollEvents() sets another OpenGL context glfwMakeContextCurrent(win); + if (settings::frameSwapInterval != internal->frameSwapInterval) { + internal->frameSwapInterval = settings::frameSwapInterval; + glfwSwapInterval(settings::frameSwapInterval); + } + // Call cursorPosCallback every frame, not just when the mouse moves { double xpos, ypos; @@ -396,23 +402,13 @@ void Window::run() { glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); nvgEndFrame(vg); - - glfwSwapBuffers(win); } - // Limit frame rate - double frameTimeEnd = glfwGetTime(); - if (settings::frameRateLimit > 0.0) { - double frameDuration = frameTimeEnd - frameTimeStart; - double waitDuration = 1.0 / settings::frameRateLimit - frameDuration; - if (waitDuration > 0.0) { - std::this_thread::sleep_for(std::chrono::duration(waitDuration)); - } - } + glfwSwapBuffers(win); // Compute actual frame rate - frameTimeEnd = glfwGetTime(); - // DEBUG("%g fps", 1 / (endTime - startTime)); + double frameTimeEnd = glfwGetTime(); + DEBUG("%g fps", 1 / (frameTimeEnd - frameTimeStart)); frame++; } } @@ -523,10 +519,12 @@ bool Window::isFullScreen() { } bool Window::isFrameOverdue() { - if (settings::frameRateLimit == 0.0) + if (settings::frameSwapInterval == 0) return false; double frameDuration = glfwGetTime() - frameTimeStart; - return frameDuration > 1.0 / settings::frameRateLimit; + // This is fudged a bit because it assumes the monitor refresh rate is 60 Hz. + double frameDeadline = settings::frameSwapInterval / 60.0; + return frameDuration > frameDeadline; } std::shared_ptr Window::loadFont(const std::string& filename) {