SDL: windows: OS version checks now cache results.

From 56fa22c63bfe92d99ed7479d87e80c72f9651b2a Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Thu, 25 Jan 2024 18:17:17 -0500
Subject: [PATCH] windows: OS version checks now cache results.

So these only have to talk to the Win32 API once per check, per process.
---
 src/core/windows/SDL_windows.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/src/core/windows/SDL_windows.c b/src/core/windows/SDL_windows.c
index 732b3e994628..ba5b6cbee48e 100644
--- a/src/core/windows/SDL_windows.c
+++ b/src/core/windows/SDL_windows.c
@@ -197,33 +197,38 @@ static BOOL IsWindowsVersionOrGreater(WORD wMajorVersion, WORD wMinorVersion, WO
 }
 #endif
 
-BOOL WIN_IsWindowsVistaOrGreater(void)
-{
+// apply some static variables so we only call into the Win32 API once per process for each check.
 #if defined(SDL_PLATFORM_WINRT) || defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
-    return TRUE;
+    #define CHECKWINVER(notdesktop_platform_result, test) return (notdesktop_platform_result);
 #else
-    return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 0);
+    #define CHECKWINVER(notdesktop_platform_result, test) \
+        static SDL_bool checked = SDL_FALSE; \
+        static BOOL retval = FALSE; \
+        if (!checked) { \
+            retval = (test); \
+            checked = SDL_TRUE; \
+        } \
+        return retval;
 #endif
+
+BOOL WIN_IsWindowsVistaOrGreater(void)
+{
+    CHECKWINVER(TRUE, IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 0));
 }
 
 BOOL WIN_IsWindows7OrGreater(void)
 {
-#if defined(SDL_PLATFORM_WINRT) || defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
-    return TRUE;
-#else
-    return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN7), LOBYTE(_WIN32_WINNT_WIN7), 0);
-#endif
+    CHECKWINVER(TRUE, IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN7), LOBYTE(_WIN32_WINNT_WIN7), 0));
 }
 
 BOOL WIN_IsWindows8OrGreater(void)
 {
-#if defined(SDL_PLATFORM_WINRT) || defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
-    return TRUE;
-#else
-    return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN8), LOBYTE(_WIN32_WINNT_WIN8), 0);
-#endif
+    CHECKWINVER(TRUE, IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN8), LOBYTE(_WIN32_WINNT_WIN8), 0));
 }
 
+#undef CHECKWINVER
+
+
 /*
 WAVExxxCAPS gives you 31 bytes for the device name, and just truncates if it's
 longer. However, since WinXP, you can use the WAVExxxCAPS2 structure, which