From a81690cf4b9c161f99c874144530d094af9a937d Mon Sep 17 00:00:00 2001 From: ceski <56656010+ceski-1@users.noreply.github.com> Date: Sun, 18 Feb 2024 21:00:24 -0800 Subject: [PATCH] Increase thread priority when window is visible and focused (#1505) --- src/i_video.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/i_video.c b/src/i_video.c index 13e59b1c..c5c2ec28 100644 --- a/src/i_video.c +++ b/src/i_video.c @@ -18,6 +18,11 @@ // //----------------------------------------------------------------------------- +#if defined(_WIN32) +#define WIN32_LEAN_AND_MEAN +#include +#endif + #include "SDL.h" // haleyjd #include "../miniz/miniz.h" @@ -200,6 +205,17 @@ void I_ResetRelativeMouseState(void) SDL_GetRelativeMouseState(NULL, NULL); } +static void UpdatePriority(void) +{ + const boolean active = (screenvisible && window_focused); +#if defined(_WIN32) + SetPriorityClass(GetCurrentProcess(), active ? ABOVE_NORMAL_PRIORITY_CLASS + : NORMAL_PRIORITY_CLASS); +#endif + SDL_SetThreadPriority(active ? SDL_THREAD_PRIORITY_HIGH + : SDL_THREAD_PRIORITY_NORMAL); +} + // [FG] window event handling from Chocolate Doom 3.0 static void HandleWindowEvent(SDL_WindowEvent *event) @@ -212,11 +228,13 @@ static void HandleWindowEvent(SDL_WindowEvent *event) case SDL_WINDOWEVENT_MINIMIZED: screenvisible = false; + UpdatePriority(); break; case SDL_WINDOWEVENT_MAXIMIZED: case SDL_WINDOWEVENT_RESTORED: screenvisible = true; + UpdatePriority(); break; // Update the value of window_focused when we get a focus event @@ -227,10 +245,12 @@ static void HandleWindowEvent(SDL_WindowEvent *event) case SDL_WINDOWEVENT_FOCUS_GAINED: window_focused = true; + UpdatePriority(); break; case SDL_WINDOWEVENT_FOCUS_LOST: window_focused = false; + UpdatePriority(); break; // We want to save the user's preferred monitor to use for running the