Commit aad9f51b authored by Sam Lantinga's avatar Sam Lantinga

Fixed bug #684

Reworked Pierre's patch a little bit, which added SDL_WaitEventTimeout()

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403538
parent a0978cdf
...@@ -412,6 +412,14 @@ extern DECLSPEC int SDLCALL SDL_PollEvent(SDL_Event * event); ...@@ -412,6 +412,14 @@ extern DECLSPEC int SDLCALL SDL_PollEvent(SDL_Event * event);
*/ */
extern DECLSPEC int SDLCALL SDL_WaitEvent(SDL_Event * event); 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. /* Add an event to the event queue.
This function returns 1 on success, 0 if the event was filtered, 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. or -1 if the event queue was full or there was some other error.
......
...@@ -399,18 +399,24 @@ SDL_PumpEvents(void) ...@@ -399,18 +399,24 @@ SDL_PumpEvents(void)
int int
SDL_PollEvent(SDL_Event * event) SDL_PollEvent(SDL_Event * event)
{ {
SDL_PumpEvents(); return SDL_WaitEventTimeout(event, 0);
/* 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;
} }
int int
SDL_WaitEvent(SDL_Event * event) 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(); SDL_PumpEvents();
switch (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) { switch (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
case -1: case -1:
...@@ -418,7 +424,16 @@ SDL_WaitEvent(SDL_Event * event) ...@@ -418,7 +424,16 @@ SDL_WaitEvent(SDL_Event * event)
case 1: case 1:
return 1; return 1;
case 0: 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); SDL_Delay(10);
break;
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment