# HG changeset patch # User Sam Lantinga # Date 1234850380 0 # Node ID 9da8f57ab92c6b1e9ba1505d09a6625f982e8b92 # Parent 6f3308d4b6cddcfd17d7ace2a1a6b78eeb9f0b74 Fixed bug #684 Reworked Pierre's patch a little bit, which added SDL_WaitEventTimeout() diff -r 6f3308d4b6cd -r 9da8f57ab92c include/SDL_events.h --- a/include/SDL_events.h Tue Feb 17 05:57:54 2009 +0000 +++ b/include/SDL_events.h Tue Feb 17 05:59:40 2009 +0000 @@ -412,6 +412,14 @@ */ extern DECLSPEC int SDLCALL SDL_WaitEvent(SDL_Event * event); +/* Waits until the specified timeout (in milliseconds) for the next available + event, returning 1, or 0 if there was an error while waiting for events. If + 'event' is not NULL, the next event is removed from the queue and stored in + that area. + */ +extern DECLSPEC int SDLCALL SDL_WaitEventTimeout(SDL_Event * event, + int timeout); + /* Add an event to the event queue. This function returns 1 on success, 0 if the event was filtered, or -1 if the event queue was full or there was some other error. diff -r 6f3308d4b6cd -r 9da8f57ab92c src/events/SDL_events.c --- 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; } } }