From 1c18cd544599c330e86349368b3f649343771b97 Mon Sep 17 00:00:00 2001 From: TCL987 Date: Tue, 10 Oct 2023 23:36:04 -0700 Subject: [PATCH] Limit the frame rate when minimized This avoids high CPU usage when minimized. --- .../OpenVR-SpaceCalibrator.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/OpenVR-SpaceCalibrator/OpenVR-SpaceCalibrator.cpp b/OpenVR-SpaceCalibrator/OpenVR-SpaceCalibrator.cpp index 972c76e..d46c5d4 100644 --- a/OpenVR-SpaceCalibrator/OpenVR-SpaceCalibrator.cpp +++ b/OpenVR-SpaceCalibrator/OpenVR-SpaceCalibrator.cpp @@ -13,6 +13,9 @@ #include #include #include +#include +#include + #pragma comment(linker,"\"/manifestdependency:type='win32' \ name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ @@ -57,6 +60,7 @@ static GLuint fboHandle = 0, fboTextureHandle = 0; static int fboTextureWidth = 0, fboTextureHeight = 0; static char cwd[MAX_PATH]; +const float MINIMIZED_MAX_FPS = 60.0f; void CreateGLFWWindow() { @@ -212,6 +216,7 @@ void RequestImmediateRedraw() { immediateRedraw = true; } +double lastFrameStartTime = glfwGetTime(); void RunLoop() { while (!glfwWindowShouldClose(glfwWindow)) @@ -369,6 +374,19 @@ void RunLoop() } glfwWaitEventsTimeout(waitEventsTimeout); + + // If we're minimized rendering won't limit our frame rate so we need to do it ourselves. + if (glfwGetWindowAttrib(glfwWindow, GLFW_ICONIFIED)) + { + double targetFrameTime = 1 / MINIMIZED_MAX_FPS; + double waitTime = targetFrameTime - (glfwGetTime() - lastFrameStartTime); + if (waitTime > 0) + { + std::this_thread::sleep_for(std::chrono::duration(waitTime)); + } + + lastFrameStartTime += targetFrameTime; + } } }