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