SDL: Fixed getting different results for SDL_PollEvent(NULL) and SDL_PollEvent(&event)

From dca281e810263f1fbf9420c2988932b7700be1d4 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 8 Jan 2022 08:49:34 -0800
Subject: [PATCH] Fixed getting different results for SDL_PollEvent(NULL) and
 SDL_PollEvent(&event)

---
 src/events/SDL_events.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c
index ff079c50ea9..a69e5f53880 100644
--- a/src/events/SDL_events.c
+++ b/src/events/SDL_events.c
@@ -1004,9 +1004,22 @@ SDL_WaitEventTimeout(SDL_Event * event, int timeout)
         }
         break;
     default:
-        if (event && event->type == SDL_POLLSENTINEL) {
-            /* Reached the end of a poll cycle, and not willing to wait */
-            return 0;
+        if (include_sentinel) {
+            if (event) {
+                if (event->type == SDL_POLLSENTINEL) {
+                    /* Reached the end of a poll cycle, and not willing to wait */
+                    return 0;
+                }
+            } else {
+                /* Need to peek the next event to check for sentinel */
+                SDL_Event dummy;
+
+                if (SDL_PeepEventsInternal(&dummy, 1, SDL_PEEKEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT, SDL_TRUE) &&
+                    dummy.type == SDL_POLLSENTINEL) {
+                    /* Reached the end of a poll cycle, and not willing to wait */
+                    return 0;
+                }
+            }
         }
         /* Has existing events */
         return 1;