Code Examples

Youcanaddyourcodeexamplehere

Remarks

The SDL_Event structure is the core of all event handling in SDL. SDL_Event is a union of all event structures used in SDL. Using it is a simple matter of knowing which event type corresponds to which union member. The table below lists these relationships.

SDL_PollEvent() removes the next event from the event queue. If there are no events on the queue it returns 0, otherwise it returns 1. We use a while loop to process each event in turn.

while(SDL_PollEvent(&test_event)){

The SDL_PollEvent() function takes a pointer to an SDL_Event structure that is to be filled with event information. We know that if SDL_PollEvent() removes an event from the queue then the event information will be placed in our test_event structure, but we also know that the type of event will be placed in the type member of test_event. So to handle each event type separately we use a switch statement.

switch(test_event.type){

We need to know what kind of events we're looking for and the event types of those events. So let's assume we want to detect where the user is moving the mouse pointer within our application. We look through our event types and notice that SDL_MOUSEMOTION is, more than likely, the event we're looking for. Looking at the table below tells us that SDL_MOUSEMOTION events are handled within the SDL_MouseMotionEvent structure which is the motion member of SDL_Event. We can check for the SDL_MOUSEMOTION event type within our switch statement like so:

caseSDL_MOUSEMOTION:

All we need do now is read the information out of the motion member of test_event.

It is also possible to push events onto the event queue and so use it as a two-way communication path. Both SDL_PushEvent() and SDL_PeepEvents() allow you to place events onto the event queue. This is usually used to place a SDL_USEREVENT on the event queue, however you could use it to post fake input events if you wished. Creating your own events is a simple matter of choosing the event type you want, setting the type member and filling the appropriate member structure with information.