Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved SDL_PollEvent usage #4794

Merged
merged 7 commits into from Oct 15, 2021
4 changes: 2 additions & 2 deletions src/events/SDL_events.c
Expand Up @@ -887,9 +887,9 @@ SDL_WaitEventTimeout(SDL_Event * event, int timeout)
case 0:
break;
default:
/* Check whether we have reached the end of the poll cycle */
/* Check whether we have reached the end of the poll cycle, and no more events are left */
if (timeout == 0 && event && event->type == SDL_POLLSENTINEL) {
return 0;
return SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After searching through GitHub for uses of SDL_PollEvent, there is an alarming amount of strange/incorrect usage (mostly in beginner projects), such as:

  • Only calling it once per frame
  • Not checking the return value
  • Calling SDL_PollEvent(NULL) in a loop
  • Polling until a certain event (i.e a key press), and pushing the rest back into the queue

I'd say those first 3 issues are broken by design, but the last one (and similar uses of user-added events inside a poll cycle) could be an issue, so I decided it's better to err on the side of caution and ignore the sentinel if there are still more events in the queue.

}
/* Has existing events */
return 1;
Expand Down