diff --git a/include/Window.hpp b/include/Window.hpp index 45032758..623fa5b9 100644 --- a/include/Window.hpp +++ b/include/Window.hpp @@ -84,7 +84,7 @@ struct Window { double getMonitorRefreshRate(); double getFrameTime(); double getLastFrameDuration(); - double getFrameTimeOverdue(); + double getFrameDurationRemaining(); std::shared_ptr loadFont(const std::string& filename); std::shared_ptr loadImage(const std::string& filename); diff --git a/src/Window.cpp b/src/Window.cpp index c772ad41..dccb5aa6 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -485,13 +485,9 @@ void Window::step() { glfwSwapBuffers(win); // On some platforms, glfwSwapBuffers() doesn't wait on monitor refresh, so we have to sleep as a fallback. - if (internal->frameSwapInterval > 0) { - double frameDurationExpected = internal->frameSwapInterval / internal->monitorRefreshRate * 0.90; - double swapBuffersTime = system::getTime(); - double frameDurationRemaining = frameDurationExpected - (swapBuffersTime - frameTime); - if (frameDurationRemaining > 0.0) { - std::this_thread::sleep_for(std::chrono::duration(frameDurationRemaining)); - } + double frameDurationRemaining = getFrameDurationRemaining(); + if (frameDurationRemaining > 0.0) { + std::this_thread::sleep_for(std::chrono::duration(frameDurationRemaining)); } t5 = system::getTime(); @@ -666,10 +662,9 @@ double Window::getLastFrameDuration() { } -double Window::getFrameTimeOverdue() { - double desiredFrameDuration = internal->frameSwapInterval / internal->monitorRefreshRate; - double frameDuration = system::getTime() - internal->frameTime; - return frameDuration - desiredFrameDuration; +double Window::getFrameDurationRemaining() { + double frameDurationDesired = internal->frameSwapInterval / internal->monitorRefreshRate; + return frameDurationDesired - (system::getTime() - internal->frameTime); } diff --git a/src/widget/FramebufferWidget.cpp b/src/widget/FramebufferWidget.cpp index 5c97f83f..413bd93c 100644 --- a/src/widget/FramebufferWidget.cpp +++ b/src/widget/FramebufferWidget.cpp @@ -114,7 +114,7 @@ void FramebufferWidget::draw(const DrawArgs& args) { } // It's more important to not lag the frame than to draw the framebuffer - if (dirty && APP->window->getFrameTimeOverdue() < 0.0) { + if (dirty && APP->window->getFrameDurationRemaining() > 0.0) { render(scale, offsetF); }