SDL: Fixed multi-window test programs not quitting when windows are closed

From b37b94453c6d410d624a414e4aaa241f8825dc57 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 25 Jun 2024 10:15:36 -0700
Subject: [PATCH] Fixed multi-window test programs not quitting when windows
 are closed

---
 src/events/SDL_windowevents.c | 4 ++--
 src/test/SDL_test_common.c    | 8 ++++++++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/events/SDL_windowevents.c b/src/events/SDL_windowevents.c
index fb66714f93541..0273b2c61fbab 100644
--- a/src/events/SDL_windowevents.c
+++ b/src/events/SDL_windowevents.c
@@ -248,12 +248,12 @@ int SDL_SendWindowEvent(SDL_Window *window, SDL_EventType windowevent,
         int toplevel_count = 0;
         SDL_Window *n;
         for (n = SDL_GetVideoDevice()->windows; n; n = n->next) {
-            if (!n->parent) {
+            if (!n->parent && !(n->flags & SDL_WINDOW_HIDDEN)) {
                 ++toplevel_count;
             }
         }
 
-        if (toplevel_count == 1) {
+        if (toplevel_count <= 1) {
             if (SDL_GetHintBoolean(SDL_HINT_QUIT_ON_LAST_WINDOW_CLOSE, SDL_TRUE)) {
                 SDL_SendQuit(); /* This is the last toplevel window in the list so send the SDL_EVENT_QUIT event */
             }
diff --git a/src/test/SDL_test_common.c b/src/test/SDL_test_common.c
index 84f2a1c6cae48..791c5b4c386fd 100644
--- a/src/test/SDL_test_common.c
+++ b/src/test/SDL_test_common.c
@@ -2095,6 +2095,14 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
             }
         }
         break;
+    case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
+    {
+        SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
+        if (window) {
+            SDL_HideWindow(window);
+        }
+        break;
+    }
     case SDL_EVENT_KEY_DOWN:
     {
         SDL_bool withControl = !!(event->key.mod & SDL_KMOD_CTRL);