Browse Source

Optimized SDL_HasEvent() and SDL_HasEvents()

Also document the counting behavior of SDL_PEEKEVENT.

Fixes https://github.com/libsdl-org/SDL/issues/6209
Sam Lantinga 11 months ago
parent
commit
d9a7da4fac
2 changed files with 19 additions and 3 deletions
  1. 1 1
      include/SDL3/SDL_events.h
  2. 18 2
      src/events/SDL_events.c

+ 1 - 1
include/SDL3/SDL_events.h

@@ -1011,7 +1011,7 @@ typedef enum SDL_EventAction
  *   event queue.
  * - `SDL_PEEKEVENT`: `numevents` events at the front of the event queue,
  *   within the specified minimum and maximum type, will be returned to the
- *   caller and will _not_ be removed from the queue.
+ *   caller and will _not_ be removed from the queue. If you pass NULL for `events`, then `numevents` is ignored and the total number of matching events will be returned.
  * - `SDL_GETEVENT`: up to `numevents` events at the front of the event queue,
  *   within the specified minimum and maximum type, will be returned to the
  *   caller and will be removed from the queue.

+ 18 - 2
src/events/SDL_events.c

@@ -1105,12 +1105,28 @@ int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_EventAction action,
 
 bool SDL_HasEvent(Uint32 type)
 {
-    return SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type) > 0;
+    return SDL_HasEvents(type, type);
 }
 
 bool SDL_HasEvents(Uint32 minType, Uint32 maxType)
 {
-    return SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, minType, maxType) > 0;
+    bool found = false;
+
+    SDL_LockMutex(SDL_EventQ.lock);
+    {
+        if (SDL_EventQ.active) {
+            for (SDL_EventEntry *entry = SDL_EventQ.head; entry; entry = entry->next) {
+                const Uint32 type = entry->event.type;
+                if (minType <= type && type <= maxType) {
+                    found = true;
+                    break;
+                }
+            }
+        }
+    }
+    SDL_UnlockMutex(SDL_EventQ.lock);
+
+    return found;
 }
 
 void SDL_FlushEvent(Uint32 type)