Do SDL_WaitEvent and SDL_PollEvent get their events nullified after each call?

I was trying to implement SDL_WaitEvent, and I had trouble since it’s different then PollEvent in that it doesn’t run in a double loop, but a single loop, since it doesn’t return a flag if there is an event pending or not.

Anyway, in the wiki, it says that:

If event is not NULL, the next event is removed from the queue and stored in the SDL_Event structure pointed to by event.

And that got me thinking.

What happens if several events happen while I’m processing an old event?
They all get put into the event union? Makes sense.

But what happens if two events of the same type happen while I am processing an old event?
Does one of them go into some kind of queue? Does only the most recent or the oldest one count?

SDL has an internal queue (array, list, whatever) of events that you haven’t received yet.
When another event happens while you’re handling an old one, it’s just added to the queue.

SDL_Event ev;

Just copies the oldest event from that internal queue into the SDL_Event you passed to SDL_PollEvent(), ev in this example, and removes that event from the internal queue.

So what you usually do is something like

SDL_Event ev;
while( SDL_PollEvent(&ev) )
    // handle event ev
// ...  some other code after that loop, whatever you do
// after handling events, maybe rendering

This will fetch one event after the other from the internal queue and copy it into your ev so you can handle it.
When the internal queue is empty, SDL_PollEvent() returns 0, so the while-loop stops (and your other code, maybe rendering, is executed)

1 Like