As I tried to understand how events work in SDL, I noticed there always were 3 events who occurred early in my test program. The following code prints each event's type.
int main(int argc, char **argv)
{
int on = 1;
SDL_Surface *screen = NULL;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WaitEvent(&event);
fprintf(stderr, "%d\n", event.type);
SDL_WaitEvent(&event);
fprintf(stderr, "%d\n", event.type);
SDL_WaitEvent(&event);
fprintf(stderr, "%d\n", event.type);
SDL_Quit();
}
Output:
17
16
1
SDL_VIDEOEXPOSE
.SDL_VIDEORESIZE
.SDL_ACTIVEVENT
.I understand that such things happen when a SDL program is launched: the window must be given a size and it must be given visibility. But what I don't get is why, after initializing the window, the functions SDL_WaitEvent
fill event
with these events. If you look at my code you can see that it does nothing more but wait for an event and print it's type in stderr
. I do not touch anything after launching the program, and yet 3 events are created and filled in event
. I have seen the notion of "queue" in the wiki and the doc and some sites, but it was never explained so I can only imagine that it may have something to do with it, since it looks like the 3 events are waiting to be put inside of a SDL_Event
.
I would like to know what is happening. Why do 3 events, always the same and in the same order, appear and fill event
?
A Queue is typically used with an asynchronous event interface. Queue is the name of an abstract data container type that can store a set of some other type (in this concrete scenario: a set of SDL_Event
s) with functions for adding an item and retrieving an item, and the retrieving function (typically called dequeue()
) always returns (and removes) the item that was first placed into the queue. This is also called a FIFO: First in first out.
SDL_WaitEvent()
is very similar to a normal dequeue()
operation (for retrieving an item from the Queue), the only difference is it will wait for an event if there is nothing in the Queue.
So the 3 Events you see are created during your initialization and are already waiting in the Queue by the time you call SDL_WaitEvent()
; they do not suddenly "appear". They are there because there might be a reason to react on the creation of a new window, or it being resized.