src/events/SDL_events.c
changeset 3072 9da8f57ab92c
parent 2984 0b160c970b7e
child 3259 22ac66da0765
--- a/src/events/SDL_events.c	Tue Feb 17 05:57:54 2009 +0000
+++ b/src/events/SDL_events.c	Tue Feb 17 05:59:40 2009 +0000
@@ -399,18 +399,24 @@
 int
 SDL_PollEvent(SDL_Event * event)
 {
-    SDL_PumpEvents();
-
-    /* We can't return -1, just return 0 (no event) on error */
-    if (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS) <= 0)
-        return 0;
-    return 1;
+    return SDL_WaitEventTimeout(event, 0);
 }
 
 int
 SDL_WaitEvent(SDL_Event * event)
 {
-    while (1) {
+    return SDL_WaitEventTimeout(event, -1);
+}
+
+int
+SDL_WaitEventTimeout(SDL_Event * event, int timeout)
+{
+    Uint32 expiration = 0;
+
+    if (timeout > 0)
+        expiration = SDL_GetTicks() + timeout;
+
+    for (;;) {
         SDL_PumpEvents();
         switch (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
         case -1:
@@ -418,7 +424,16 @@
         case 1:
             return 1;
         case 0:
+            if (timeout == 0) {
+                /* Polling and no events, just return */
+                return 0;
+            }
+            if (timeout > 0 && ((int) (SDL_GetTicks() - expiration) >= 0)) {
+                /* Timeout expired and no events */
+                return 0;
+            }
             SDL_Delay(10);
+            break;
         }
     }
 }