NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.64
REL="HOME"
TITLE="SDL Library Documentation"
REL="UP"
TITLE="SDL Event Structures."
HREF="eventstructures.html"><
LINK
REL="PREVIOUS"
TITLE="SDL Event Structures."
HREF="eventstructures.html"><
LINK
REL="NEXT"
TITLE="SDL_ActiveEvent"
HREF="sdlactiveevent.html"><
/HEAD
CLASS="REFENTRY"
BGCOLOR="#FFF8DC"
TEXT="#000000"
LINK="#0000ee"
VLINK="#551a8b"
ALINK="#ff0000"
CLASS="NAVHEADER"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
COLSPAN="3"
ALIGN="center"
>SDL Library Documentation<
/TH
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
HREF="eventstructures.html"
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
HREF="sdlactiveevent.html"
ALIGN="LEFT"
NAME="SDLEVENT"
CLASS="REFNAMEDIV"
NAME="AEN3289"
>SDL_Event -- General event structure<
/DIV
CLASS="REFSECT1"
NAME="AEN3292"
>Structure Definition<
/H2
CLASS="PROGRAMLISTING"
>typedef union{
Uint8 type;
SDL_ActiveEvent active;
SDL_KeyboardEvent key;
SDL_MouseMotionEvent motion;
SDL_MouseButtonEvent button;
SDL_JoyAxisEvent jaxis;
SDL_JoyBallEvent jball;
SDL_JoyHatEvent jhat;
SDL_JoyButtonEvent jbutton;
SDL_ResizeEvent resize;
SDL_QuitEvent quit;
SDL_UserEvent user;
SDL_SywWMEvent syswm;
CLASS="REFSECT1"
NAME="AEN3295"
CLASS="INFORMALTABLE"
NAME="AEN3297"
BORDER="0"
CLASS="CALSTABLE"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="STRUCTFIELD"
ALIGN="LEFT"
VALIGN="TOP"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="STRUCTFIELD"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdlactiveevent.html"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="STRUCTFIELD"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdlkeyboardevent.html"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="STRUCTFIELD"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdlmousemotionevent.html"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="STRUCTFIELD"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdlmousebuttonevent.html"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="STRUCTFIELD"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdljoyaxisevent.html"
>Joystick
axis motion event<
/A
ALIGN="LEFT"
VALIGN="TOP"
CLASS="STRUCTFIELD"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdljoyballevent.html"
>Joystick trackball motion event<
/A
ALIGN="LEFT"
VALIGN="TOP"
CLASS="STRUCTFIELD"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdljoyhatevent.html"
>Joystick hat motion event<
/A
ALIGN="LEFT"
VALIGN="TOP"
CLASS="STRUCTFIELD"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdljoybuttonevent.html"
>Joystick button event<
/A
ALIGN="LEFT"
VALIGN="TOP"
CLASS="STRUCTFIELD"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdlresizeevent.html"
>Application window resize event<
/A
ALIGN="LEFT"
VALIGN="TOP"
CLASS="STRUCTFIELD"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdlquitevent.html"
>Application quit request event<
/A
ALIGN="LEFT"
VALIGN="TOP"
CLASS="STRUCTFIELD"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdluserevent.html"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="STRUCTFIELD"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdlsyswmevent.html"
>Undefined window manager event<
/A
CLASS="REFSECT1"
NAME="AEN3364"
CLASS="STRUCTNAME"
> union is the core to all event handling is SDL, its probably the most important structure after <
SPAN
CLASS="STRUCTNAME"
CLASS="STRUCTNAME"
> is a union of all event structures used in SDL, using it is a simple matter of knowing which union member relates to which event <
TT
CLASS="STRUCTFIELD"
CLASS="INFORMALTABLE"
NAME="AEN3372"
BORDER="1"
CLASS="CALSTABLE"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="STRUCTFIELD"
ALIGN="LEFT"
VALIGN="TOP"
ALIGN="LEFT"
VALIGN="MIDDLE"
CLASS="LITERAL"
ALIGN="LEFT"
VALIGN="MIDDLE"
HREF="sdlactiveevent.html"
CLASS="STRUCTNAME"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="LITERAL"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdlkeyboardevent.html"
CLASS="STRUCTNAME"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="LITERAL"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdlmousemotionevent.html"
CLASS="STRUCTNAME"
>SDL_MouseMotionEvent<
/SPAN
ALIGN="LEFT"
VALIGN="TOP"
CLASS="LITERAL"
>SDL_MOUSEBUTTONDOWN
/UP<
/TT
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdlmousebuttonevent.html"
CLASS="STRUCTNAME"
>SDL_MouseButtonEvent<
/SPAN
ALIGN="LEFT"
VALIGN="TOP"
CLASS="LITERAL"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdljoyaxisevent.html"
CLASS="STRUCTNAME"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="LITERAL"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdljoyballevent.html"
CLASS="STRUCTNAME"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="LITERAL"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdljoyhatevent.html"
CLASS="STRUCTNAME"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="LITERAL"
>SDL_JOYBUTTONDOWN
/UP<
/TT
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdljoybuttonevent.html"
CLASS="STRUCTNAME"
>SDL_JoyButtonEvent<
/SPAN
ALIGN="LEFT"
VALIGN="TOP"
CLASS="LITERAL"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdlquitevent.html"
CLASS="STRUCTNAME"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="LITERAL"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdlsyswmevent.html"
CLASS="STRUCTNAME"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="LITERAL"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdlresizeevent.html"
CLASS="STRUCTNAME"
ALIGN="LEFT"
VALIGN="TOP"
CLASS="LITERAL"
ALIGN="LEFT"
VALIGN="TOP"
HREF="sdluserevent.html"
CLASS="STRUCTNAME"
CLASS="REFSECT1"
NAME="AEN3452"
CLASS="STRUCTNAME"
> structure has two uses<
/P
COMPACT="COMPACT"
>Reading events on the event queue<
/P
>Placing events on the event queue<
/P
>Reading events from the event queue is done with either <
A
HREF="sdlpollevent.html"
CLASS="FUNCTION"
HREF="sdlpeepevents.html"
CLASS="FUNCTION"
>. We'll use <TT
CLASS="FUNCTION"
>SDL_PollEvent</TT
> and step through an example.</P
><P
>First off, we create an empty <SPAN
CLASS="STRUCTNAME"
>SDL_Event</SPAN
> structure.
<PRE
CLASS="PROGRAMLISTING"
>SDL_Event test_event;</PRE
>
<TT
CLASS="FUNCTION"
>SDL_PollEvent</TT
> removes the next event from the event queue, if there are no events on the queue it returns <SPAN
CLASS="RETURNVALUE"
>0</SPAN
> otherwise it returns <SPAN
CLASS="RETURNVALUE"
>1</SPAN
>. We use a <TT
CLASS="FUNCTION"
>while</TT
> loop to process each event in turn.
<PRE
CLASS="PROGRAMLISTING"
>while(SDL_PollEvent(&test_event)) {</PRE
>
The <TT
CLASS="FUNCTION"
>SDL_PollEvent</TT
> function take a pointer to an <SPAN
CLASS="STRUCTNAME"
>SDL_Event</SPAN
> structure that is to be filled with event information. We know that if <TT
CLASS="FUNCTION"
>SDL_PollEvent</TT
> removes an event from the queue then the event information will be placed in our <SPAN
CLASS="STRUCTNAME"
>test_event</SPAN
> structure, but we also know that the <I
CLASS="EMPHASIS"
>type</I
> of event will be placed in the <TT
CLASS="STRUCTFIELD"
><I
>type</I
></TT
> member of <SPAN
CLASS="STRUCTNAME"
>test_event</SPAN
>. So to handle each event <TT
CLASS="STRUCTFIELD"
><I
>type</I
></TT
> seperately we use a <TT
CLASS="FUNCTION"
>switch</TT
> statement.
<PRE
CLASS="PROGRAMLISTING"
> switch(test_event.type) {</PRE
>
We need to know what kind of events we're looking
for <
I
CLASS="EMPHASIS"
CLASS="STRUCTFIELD"
>'s of those events. So lets 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 <TT
CLASS="LITERAL"
>SDL_MOUSEMOTION</TT
> is, more than likely, the event we're looking
for. A little <
A
HREF="sdlmousemotionevent.html"
> research tells use that <
TT
CLASS="LITERAL"
> events are handled within the <
A
HREF="sdlmousemotionevent.html"
CLASS="STRUCTNAME"
>SDL_MouseMotionEvent<
/SPAN
> structure which is the <
TT
CLASS="STRUCTFIELD"
CLASS="STRUCTNAME"
>. We can check
for the <
TT
CLASS="LITERAL"
CLASS="STRUCTFIELD"
CLASS="FUNCTION"
> statement like so:
CLASS="PROGRAMLISTING"
> case SDL_MOUSEMOTION:<
/PRE
>
All we need do now is read the information out of the <
TT
CLASS="STRUCTFIELD"
CLASS="STRUCTNAME"
>.
CLASS="PROGRAMLISTING"
> printf("We got a motion event.\n");
printf("Current mouse position is: (%d, %d)\n", test_event.motion.x, test_event.motion.y);
break;
default:
printf("Unhandled Event!\n");
break;
}
}
printf
("Event queue empty.\n");<
/PRE
>It is also possible to push events onto the event queue and so use it as a two-way communication path. Both <
A
HREF="sdlpushevent.html"
CLASS="FUNCTION"
HREF="sdlpeepevents.html"
CLASS="FUNCTION"
> allow you to place events onto the event queue. This is usually used to place a <
TT
CLASS="LITERAL"
> 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 <
TT
CLASS="STRUCTFIELD"
> member and filling the appropriate member structure with information.
CLASS="PROGRAMLISTING"
>SDL_Event user_event;
user_event.type=SDL_USEREVENT;
user_event.user.code=2;
user_event.user.data1=NULL;
user_event.user.data2=NULL;
SDL_PushEvent
(&#
38;user_event
);<
/PRE
CLASS="REFSECT1"
NAME="AEN3509"
HREF="sdlpollevent.html"
CLASS="FUNCTION"
>,
HREF="sdlpushevent.html"
CLASS="FUNCTION"
>,
HREF="sdlpeepevents.html"
CLASS="FUNCTION"
CLASS="NAVFOOTER"
ALIGN="LEFT"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
WIDTH="33%"
ALIGN="left"
VALIGN="top"
HREF="eventstructures.html"
WIDTH="34%"
ALIGN="center"
VALIGN="top"
HREF="index.html"
WIDTH="33%"
ALIGN="right"
VALIGN="top"
HREF="sdlactiveevent.html"
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>SDL Event Structures.<
/TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
HREF="eventstructures.html"
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>