comparison docs/man3/SDL_Event.3 @ 0:74212992fb08

Initial revision
author Sam Lantinga <slouken@lokigames.com>
date Thu, 26 Apr 2001 16:45:43 +0000
parents
children 55f1f1b3e27d
comparison
equal deleted inserted replaced
-1:000000000000 0:74212992fb08
1 .TH "SDL_Event" "3" "Mon 12 Mar 2001, 01:03" "SDL" "SDL API Reference"
2 .SH "NAME"
3 SDL_Event\- General event structure
4 .SH "STRUCTURE DEFINITION"
5 .PP
6 .nf
7 \f(CWtypedef union{
8 Uint8 type;
9 SDL_ActiveEvent active;
10 SDL_KeyboardEvent key;
11 SDL_MouseMotionEvent motion;
12 SDL_MouseButtonEvent button;
13 SDL_JoyAxisEvent jaxis;
14 SDL_JoyBallEvent jball;
15 SDL_JoyHatEvent jhat;
16 SDL_JoyButtonEvent jbutton;
17 SDL_ResizeEvent resize;
18 SDL_QuitEvent quit;
19 SDL_UserEvent user;
20 SDL_SywWMEvent syswm;
21 } SDL_Event;\fR
22 .fi
23 .PP
24 .SH "STRUCTURE DATA"
25 .TP 20
26 \fBtype\fR
27 The type of event
28 .TP 20
29 \fBactive\fR
30 \fIActivation event\fR
31 .TP 20
32 \fBkey\fR
33 \fIKeyboard event\fR
34 .TP 20
35 \fBmotion\fR
36 \fIMouse motion event\fR
37 .TP 20
38 \fBbutton\fR
39 \fIMouse button event\fR
40 .TP 20
41 \fBjaxis\fR
42 \fIJoystick axis motion event\fR
43 .TP 20
44 \fBjball\fR
45 \fIJoystick trackball motion event\fR
46 .TP 20
47 \fBjhat\fR
48 \fIJoystick hat motion event\fR
49 .TP 20
50 \fBjbutton\fR
51 \fIJoystick button event\fR
52 .TP 20
53 \fBresize\fR
54 \fIApplication window resize event\fR
55 .TP 20
56 \fBquit\fR
57 \fIApplication quit request event\fR
58 .TP 20
59 \fBuser\fR
60 \fIUser defined event\fR
61 .TP 20
62 \fBsyswm\fR
63 \fIUndefined window manager event\fR
64 .SH "DESCRIPTION"
65 .PP
66 The \fBSDL_Event\fR union is the core to all event handling is SDL, its probably the most important structure after \fBSDL_Surface\fR\&. \fBSDL_Event\fR 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 \fBtype\fR\&.
67 .PP
68 .TP 20
69 \fBEvent \fBtype\fR\fR
70 \fBEvent Structure\fR
71 .TP 20
72 \fBSDL_ACTIVEEVENT\fP
73 \fI\fBSDL_ActiveEvent\fR\fR
74 .TP 20
75 \fBSDL_KEYDOWN/UP\fP
76 \fI\fBSDL_KeyboardEvent\fR\fR
77 .TP 20
78 \fBSDL_MOUSEMOTION\fP
79 \fI\fBSDL_MouseMotionEvent\fR\fR
80 .TP 20
81 \fBSDL_MOUSEBUTTONDOWN/UP\fP
82 \fI\fBSDL_MouseButtonEvent\fR\fR
83 .TP 20
84 \fBSDL_JOYAXISMOTION\fP
85 \fI\fBSDL_JoyAxisEvent\fR\fR
86 .TP 20
87 \fBSDL_JOYBALLMOTION\fP
88 \fI\fBSDL_JoyBallEvent\fR\fR
89 .TP 20
90 \fBSDL_JOYHATMOTION\fP
91 \fI\fBSDL_JoyHatEvent\fR\fR
92 .TP 20
93 \fBSDL_JOYBUTTONDOWN/UP\fP
94 \fI\fBSDL_JoyButtonEvent\fR\fR
95 .TP 20
96 \fBSDL_QUIT\fP
97 \fI\fBSDL_QuitEvent\fR\fR
98 .TP 20
99 \fBSDL_SYSWMEVENT\fP
100 \fI\fBSDL_SysWMEvent\fR\fR
101 .TP 20
102 \fBSDL_VIDEORESIZE\fP
103 \fI\fBSDL_ResizeEvent\fR\fR
104 .TP 20
105 \fBSDL_USEREVENT\fP
106 \fI\fBSDL_UserEvent\fR\fR
107 .SH "USE"
108 .PP
109 The \fBSDL_Event\fR structure has two uses
110 .IP " \(bu" 6
111 Reading events on the event queue
112 .IP " \(bu" 6
113 Placing events on the event queue
114 .PP
115 Reading events from the event queue is done with either \fI\fBSDL_PollEvent\fP\fR or \fI\fBSDL_PeepEvents\fP\fR\&. We\&'ll use \fBSDL_PollEvent\fP and step through an example\&.
116 .PP
117 First off, we create an empty \fBSDL_Event\fR structure\&.
118 .PP
119 .nf
120 \f(CWSDL_Event test_event;\fR
121 .fi
122 .PP
123 \fBSDL_PollEvent\fP removes the next event from the event queue, if there are no events on the queue it returns \fB0\fR otherwise it returns \fB1\fR\&. We use a \fBwhile\fP loop to process each event in turn\&.
124 .PP
125 .nf
126 \f(CWwhile(SDL_PollEvent(&test_event)) {\fR
127 .fi
128 .PP
129 The \fBSDL_PollEvent\fP function take a pointer to an \fBSDL_Event\fR structure that is to be filled with event information\&. We know that if \fBSDL_PollEvent\fP removes an event from the queue then the event information will be placed in our \fBtest_event\fR structure, but we also know that the \fItype\fP of event will be placed in the \fBtype\fR member of \fBtest_event\fR\&. So to handle each event \fBtype\fR seperately we use a \fBswitch\fP statement\&.
130 .PP
131 .nf
132 \f(CW switch(test_event\&.type) {\fR
133 .fi
134 .PP
135 We need to know what kind of events we\&'re looking for \fIand\fP the event \fBtype\fR\&'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 \fBSDL_MOUSEMOTION\fP is, more than likely, the event we\&'re looking for\&. A little \fImore\fR research tells use that \fBSDL_MOUSEMOTION\fP events are handled within the \fI\fBSDL_MouseMotionEvent\fR\fR structure which is the \fBmotion\fR member of \fBSDL_Event\fR\&. We can check for the \fBSDL_MOUSEMOTION\fP event \fBtype\fR within our \fBswitch\fP statement like so:
136 .PP
137 .nf
138 \f(CW case SDL_MOUSEMOTION:\fR
139 .fi
140 .PP
141 All we need do now is read the information out of the \fBmotion\fR member of \fBtest_event\fR\&.
142 .PP
143 .nf
144 \f(CW printf("We got a motion event\&.
145 ");
146 printf("Current mouse position is: (%d, %d)
147 ", test_event\&.motion\&.x, test_event\&.motion\&.y);
148 break;
149 default:
150 printf("Unhandled Event!
151 ");
152 break;
153 }
154 }
155 printf("Event queue empty\&.
156 ");\fR
157 .fi
158 .PP
159 .PP
160 It is also possible to push events onto the event queue and so use it as a two-way communication path\&. Both \fI\fBSDL_PushEvent\fP\fR and \fI\fBSDL_PeepEvents\fP\fR allow you to place events onto the event queue\&. This is usually used to place a \fBSDL_USEREVENT\fP 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 \fBtype\fR member and filling the appropriate member structure with information\&.
161 .PP
162 .nf
163 \f(CWSDL_Event user_event;
164
165 user_event\&.type=SDL_USEREVENT;
166 user_event\&.user\&.code=2;
167 user_event\&.user\&.data1=NULL;
168 user_event\&.user\&.data2=NULL;
169 SDL_PushEvent(&user_event);\fR
170 .fi
171 .PP
172 .SH "SEE ALSO"
173 .PP
174 \fI\fBSDL_PollEvent\fP\fR, \fI\fBSDL_PushEvent\fP\fR, \fI\fBSDL_PeepEvents\fP\fR
175 ...\" created by instant / docbook-to-man, Mon 12 Mar 2001, 01:03