1<HTML 2><HEAD 3><TITLE 4>Event Examples</TITLE 5><META 6NAME="GENERATOR" 7CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ 8"><LINK 9REL="HOME" 10TITLE="SDL Library Documentation" 11HREF="index.html"><LINK 12REL="UP" 13TITLE="Examples" 14HREF="guideexamples.html"><LINK 15REL="PREVIOUS" 16TITLE="Examples" 17HREF="guideexamples.html"><LINK 18REL="NEXT" 19TITLE="Audio Examples" 20HREF="guideaudioexamples.html"></HEAD 21><BODY 22CLASS="SECT1" 23BGCOLOR="#FFF8DC" 24TEXT="#000000" 25LINK="#0000ee" 26VLINK="#551a8b" 27ALINK="#ff0000" 28><DIV 29CLASS="NAVHEADER" 30><TABLE 31SUMMARY="Header navigation table" 32WIDTH="100%" 33BORDER="0" 34CELLPADDING="0" 35CELLSPACING="0" 36><TR 37><TH 38COLSPAN="3" 39ALIGN="center" 40>SDL Library Documentation</TH 41></TR 42><TR 43><TD 44WIDTH="10%" 45ALIGN="left" 46VALIGN="bottom" 47><A 48HREF="guideexamples.html" 49ACCESSKEY="P" 50>Prev</A 51></TD 52><TD 53WIDTH="80%" 54ALIGN="center" 55VALIGN="bottom" 56>Chapter 4. Examples</TD 57><TD 58WIDTH="10%" 59ALIGN="right" 60VALIGN="bottom" 61><A 62HREF="guideaudioexamples.html" 63ACCESSKEY="N" 64>Next</A 65></TD 66></TR 67></TABLE 68><HR 69ALIGN="LEFT" 70WIDTH="100%"></DIV 71><DIV 72CLASS="SECT1" 73><H1 74CLASS="SECT1" 75><A 76NAME="GUIDEEVENTEXAMPLES" 77></A 78>Event Examples</H1 79><P 80></P 81><DIV 82CLASS="SECT2" 83><H2 84CLASS="SECT2" 85><A 86NAME="AEN375" 87></A 88>Filtering and Handling Events</H2 89><P 90><PRE 91CLASS="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 */ 98int 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 115int 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 184CLASS="NAVFOOTER" 185><HR 186ALIGN="LEFT" 187WIDTH="100%"><TABLE 188SUMMARY="Footer navigation table" 189WIDTH="100%" 190BORDER="0" 191CELLPADDING="0" 192CELLSPACING="0" 193><TR 194><TD 195WIDTH="33%" 196ALIGN="left" 197VALIGN="top" 198><A 199HREF="guideexamples.html" 200ACCESSKEY="P" 201>Prev</A 202></TD 203><TD 204WIDTH="34%" 205ALIGN="center" 206VALIGN="top" 207><A 208HREF="index.html" 209ACCESSKEY="H" 210>Home</A 211></TD 212><TD 213WIDTH="33%" 214ALIGN="right" 215VALIGN="top" 216><A 217HREF="guideaudioexamples.html" 218ACCESSKEY="N" 219>Next</A 220></TD 221></TR 222><TR 223><TD 224WIDTH="33%" 225ALIGN="left" 226VALIGN="top" 227>Examples</TD 228><TD 229WIDTH="34%" 230ALIGN="center" 231VALIGN="top" 232><A 233HREF="guideexamples.html" 234ACCESSKEY="U" 235>Up</A 236></TD 237><TD 238WIDTH="33%" 239ALIGN="right" 240VALIGN="top" 241>Audio Examples</TD 242></TR 243></TABLE 244></DIV 245></BODY 246></HTML 247>