0
|
1 <HTML
|
|
2 ><HEAD
|
|
3 ><TITLE
|
|
4 >Event Examples</TITLE
|
|
5 ><META
|
|
6 NAME="GENERATOR"
|
|
7 CONTENT="Modular DocBook HTML Stylesheet Version 1.61
|
|
8 "><LINK
|
|
9 REL="HOME"
|
|
10 TITLE="SDL Library Documentation"
|
|
11 HREF="index.html"><LINK
|
|
12 REL="UP"
|
|
13 TITLE="Examples"
|
|
14 HREF="guideexamples.html"><LINK
|
|
15 REL="PREVIOUS"
|
|
16 TITLE="Video Examples"
|
|
17 HREF="guidevideoexamples.html"><LINK
|
|
18 REL="NEXT"
|
|
19 TITLE="Audio Examples"
|
|
20 HREF="guideaudioexamples.html"></HEAD
|
|
21 ><BODY
|
|
22 CLASS="SECT1"
|
|
23 BGCOLOR="#FFF8DC"
|
|
24 TEXT="#000000"
|
|
25 LINK="#0000ee"
|
|
26 VLINK="#551a8b"
|
|
27 ALINK="#ff0000"
|
|
28 ><DIV
|
|
29 CLASS="NAVHEADER"
|
|
30 ><TABLE
|
|
31 WIDTH="100%"
|
|
32 BORDER="0"
|
|
33 CELLPADDING="0"
|
|
34 CELLSPACING="0"
|
|
35 ><TR
|
|
36 ><TH
|
|
37 COLSPAN="3"
|
|
38 ALIGN="center"
|
|
39 >SDL Library Documentation</TH
|
|
40 ></TR
|
|
41 ><TR
|
|
42 ><TD
|
|
43 WIDTH="10%"
|
|
44 ALIGN="left"
|
|
45 VALIGN="bottom"
|
|
46 ><A
|
|
47 HREF="guidevideoexamples.html"
|
|
48 >Prev</A
|
|
49 ></TD
|
|
50 ><TD
|
|
51 WIDTH="80%"
|
|
52 ALIGN="center"
|
|
53 VALIGN="bottom"
|
|
54 >Chapter 4. Examples</TD
|
|
55 ><TD
|
|
56 WIDTH="10%"
|
|
57 ALIGN="right"
|
|
58 VALIGN="bottom"
|
|
59 ><A
|
|
60 HREF="guideaudioexamples.html"
|
|
61 >Next</A
|
|
62 ></TD
|
|
63 ></TR
|
|
64 ></TABLE
|
|
65 ><HR
|
|
66 ALIGN="LEFT"
|
|
67 WIDTH="100%"></DIV
|
|
68 ><DIV
|
|
69 CLASS="SECT1"
|
|
70 ><H1
|
|
71 CLASS="SECT1"
|
|
72 ><A
|
|
73 NAME="GUIDEEVENTEXAMPLES"
|
|
74 >Event Examples</A
|
|
75 ></H1
|
|
76 ><P
|
|
77 ></P
|
|
78 ><DIV
|
|
79 CLASS="SECT2"
|
|
80 ><H2
|
|
81 CLASS="SECT2"
|
|
82 ><A
|
|
83 NAME="AEN336"
|
|
84 >Filtering and Handling Events</A
|
|
85 ></H2
|
|
86 ><P
|
|
87 ><PRE
|
|
88 CLASS="PROGRAMLISTING"
|
|
89 >#include <stdio.h>
|
|
90 #include <stdlib.h>
|
|
91
|
|
92 #include "SDL.h"
|
|
93
|
|
94 /* This function may run in a separate event thread */
|
|
95 int FilterEvents(const SDL_Event *event) {
|
|
96 static int boycott = 1;
|
|
97
|
|
98 /* This quit event signals the closing of the window */
|
|
99 if ( (event->type == SDL_QUIT) && boycott ) {
|
|
100 printf("Quit event filtered out -- try again.\n");
|
|
101 boycott = 0;
|
|
102 return(0);
|
|
103 }
|
|
104 if ( event->type == SDL_MOUSEMOTION ) {
|
|
105 printf("Mouse moved to (%d,%d)\n",
|
|
106 event->motion.x, event->motion.y);
|
|
107 return(0); /* Drop it, we've handled it */
|
|
108 }
|
|
109 return(1);
|
|
110 }
|
|
111
|
|
112 int main(int argc, char *argv[])
|
|
113 {
|
|
114 SDL_Event event;
|
|
115
|
|
116 /* Initialize the SDL library (starts the event loop) */
|
|
117 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
|
118 fprintf(stderr,
|
|
119 "Couldn't initialize SDL: %s\n", SDL_GetError());
|
|
120 exit(1);
|
|
121 }
|
|
122
|
|
123 /* Clean up on exit, exit on window close and interrupt */
|
|
124 atexit(SDL_Quit);
|
|
125
|
|
126 /* Ignore key events */
|
|
127 SDL_EventState(SDL_KEYDOWN, SDL_IGNORE);
|
|
128 SDL_EventState(SDL_KEYUP, SDL_IGNORE);
|
|
129
|
|
130 /* Filter quit and mouse motion events */
|
|
131 SDL_SetEventFilter(FilterEvents);
|
|
132
|
|
133 /* The mouse isn't much use unless we have a display for reference */
|
|
134 if ( SDL_SetVideoMode(640, 480, 8, 0) == NULL ) {
|
|
135 fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",
|
|
136 SDL_GetError());
|
|
137 exit(1);
|
|
138 }
|
|
139
|
|
140 /* Loop waiting for ESC+Mouse_Button */
|
|
141 while ( SDL_WaitEvent(&event) >= 0 ) {
|
|
142 switch (event.type) {
|
|
143 case SDL_ACTIVEEVENT: {
|
|
144 if ( event.active.state & SDL_APPACTIVE ) {
|
|
145 if ( event.active.gain ) {
|
|
146 printf("App activated\n");
|
|
147 } else {
|
|
148 printf("App iconified\n");
|
|
149 }
|
|
150 }
|
|
151 }
|
|
152 break;
|
|
153
|
|
154 case SDL_MOUSEBUTTONDOWN: {
|
|
155 Uint8 *keys;
|
|
156
|
|
157 keys = SDL_GetKeyState(NULL);
|
|
158 if ( keys[SDLK_ESCAPE] == SDL_PRESSED ) {
|
|
159 printf("Bye bye...\n");
|
|
160 exit(0);
|
|
161 }
|
|
162 printf("Mouse button pressed\n");
|
|
163 }
|
|
164 break;
|
|
165
|
|
166 case SDL_QUIT: {
|
|
167 printf("Quit requested, quitting.\n");
|
|
168 exit(0);
|
|
169 }
|
|
170 break;
|
|
171 }
|
|
172 }
|
|
173 /* This should never happen */
|
|
174 printf("SDL_WaitEvent error: %s\n", SDL_GetError());
|
|
175 exit(1);
|
|
176 }</PRE
|
|
177 ></P
|
|
178 ></DIV
|
|
179 ></DIV
|
|
180 ><DIV
|
|
181 CLASS="NAVFOOTER"
|
|
182 ><HR
|
|
183 ALIGN="LEFT"
|
|
184 WIDTH="100%"><TABLE
|
|
185 WIDTH="100%"
|
|
186 BORDER="0"
|
|
187 CELLPADDING="0"
|
|
188 CELLSPACING="0"
|
|
189 ><TR
|
|
190 ><TD
|
|
191 WIDTH="33%"
|
|
192 ALIGN="left"
|
|
193 VALIGN="top"
|
|
194 ><A
|
|
195 HREF="guidevideoexamples.html"
|
|
196 >Prev</A
|
|
197 ></TD
|
|
198 ><TD
|
|
199 WIDTH="34%"
|
|
200 ALIGN="center"
|
|
201 VALIGN="top"
|
|
202 ><A
|
|
203 HREF="index.html"
|
|
204 >Home</A
|
|
205 ></TD
|
|
206 ><TD
|
|
207 WIDTH="33%"
|
|
208 ALIGN="right"
|
|
209 VALIGN="top"
|
|
210 ><A
|
|
211 HREF="guideaudioexamples.html"
|
|
212 >Next</A
|
|
213 ></TD
|
|
214 ></TR
|
|
215 ><TR
|
|
216 ><TD
|
|
217 WIDTH="33%"
|
|
218 ALIGN="left"
|
|
219 VALIGN="top"
|
|
220 >Video Examples</TD
|
|
221 ><TD
|
|
222 WIDTH="34%"
|
|
223 ALIGN="center"
|
|
224 VALIGN="top"
|
|
225 ><A
|
|
226 HREF="guideexamples.html"
|
|
227 >Up</A
|
|
228 ></TD
|
|
229 ><TD
|
|
230 WIDTH="33%"
|
|
231 ALIGN="right"
|
|
232 VALIGN="top"
|
|
233 >Audio Examples</TD
|
|
234 ></TR
|
|
235 ></TABLE
|
|
236 ></DIV
|
|
237 ></BODY
|
|
238 ></HTML
|
|
239 > |