test/testwm.c
changeset 1895 c121d94672cb
parent 1779 67fc81efcfc3
child 2060 866052b01ee5
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
     8 #include "SDL.h"
     8 #include "SDL.h"
     9 
     9 
    10 /* Is the cursor visible? */
    10 /* Is the cursor visible? */
    11 static int visible = 1;
    11 static int visible = 1;
    12 
    12 
    13 static Uint8  video_bpp;
    13 static Uint8 video_bpp;
    14 static Uint32 video_flags;
    14 static Uint32 video_flags;
    15 
    15 
    16 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    16 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    17 static void quit(int rc)
    17 static void
    18 {
    18 quit(int rc)
    19 	SDL_Quit();
    19 {
    20 	exit(rc);
    20     SDL_Quit();
    21 }
    21     exit(rc);
    22 
    22 }
    23 int SetVideoMode(int w, int h)
    23 
    24 {
    24 int
    25 	SDL_Surface *screen;
    25 SetVideoMode(int w, int h)
    26 	int i;
    26 {
    27 	Uint8 *buffer;
    27     SDL_Surface *screen;
    28 	SDL_Color palette[256];
    28     int i;
    29 
    29     Uint8 *buffer;
    30 	screen = SDL_SetVideoMode(w, h, video_bpp, video_flags);
    30     SDL_Color palette[256];
    31 	if (  screen == NULL ) {
    31 
    32 		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
    32     screen = SDL_SetVideoMode(w, h, video_bpp, video_flags);
    33 					w, h, video_bpp, SDL_GetError());
    33     if (screen == NULL) {
    34 		return(-1);
    34         fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
    35 	}
    35                 w, h, video_bpp, SDL_GetError());
    36 	printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
    36         return (-1);
    37 						"fullscreen" : "windowed");
    37     }
    38 
    38     printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
    39 	/* Set the surface pixels and refresh! */
    39            "fullscreen" : "windowed");
    40 	for ( i=0; i<256; ++i ) {
    40 
    41 		palette[i].r = 255-i;
    41     /* Set the surface pixels and refresh! */
    42 		palette[i].g = 255-i;
    42     for (i = 0; i < 256; ++i) {
    43 		palette[i].b = 255-i;
    43         palette[i].r = 255 - i;
    44 	}
    44         palette[i].g = 255 - i;
    45 	SDL_SetColors(screen, palette, 0, 256);
    45         palette[i].b = 255 - i;
    46 	if ( SDL_LockSurface(screen) < 0 ) {
    46     }
    47 		fprintf(stderr, "Couldn't lock display surface: %s\n",
    47     SDL_SetColors(screen, palette, 0, 256);
    48 							SDL_GetError());
    48     if (SDL_LockSurface(screen) < 0) {
    49 		return(-1);
    49         fprintf(stderr, "Couldn't lock display surface: %s\n",
    50 	}
    50                 SDL_GetError());
    51 	buffer = (Uint8 *)screen->pixels;
    51         return (-1);
    52 	for ( i=0; i<screen->h; ++i ) {
    52     }
    53 		memset(buffer,(i*255)/screen->h,
    53     buffer = (Uint8 *) screen->pixels;
    54 				screen->w*screen->format->BytesPerPixel);
    54     for (i = 0; i < screen->h; ++i) {
    55 		buffer += screen->pitch;
    55         memset(buffer, (i * 255) / screen->h,
    56 	}
    56                screen->w * screen->format->BytesPerPixel);
    57 	SDL_UnlockSurface(screen);
    57         buffer += screen->pitch;
    58 	SDL_UpdateRect(screen, 0, 0, 0, 0);
    58     }
    59 
    59     SDL_UnlockSurface(screen);
    60 	return(0);
    60     SDL_UpdateRect(screen, 0, 0, 0, 0);
    61 }
    61 
    62 
    62     return (0);
    63 SDL_Surface *LoadIconSurface(char *file, Uint8 **maskp)
    63 }
    64 {
    64 
    65 	SDL_Surface *icon;
    65 SDL_Surface *
    66 	Uint8       *pixels;
    66 LoadIconSurface(char *file, Uint8 ** maskp)
    67 	Uint8       *mask;
    67 {
    68 	int          mlen, i, j;
    68     SDL_Surface *icon;
    69 
    69     Uint8 *pixels;
    70 	*maskp = NULL;
    70     Uint8 *mask;
    71 
    71     int mlen, i, j;
    72 	/* Load the icon surface */
    72 
    73 	icon = SDL_LoadBMP(file);
    73     *maskp = NULL;
    74 	if ( icon == NULL ) {
    74 
    75 		fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
    75     /* Load the icon surface */
    76 		return(NULL);
    76     icon = SDL_LoadBMP(file);
    77 	}
    77     if (icon == NULL) {
    78 
    78         fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
    79 	/* Check width and height 
    79         return (NULL);
    80 	if ( (icon->w%8) != 0 ) {
    80     }
    81 		fprintf(stderr, "Icon width must be a multiple of 8!\n");
    81 
    82 		SDL_FreeSurface(icon);
    82     /* Check width and height 
    83 		return(NULL);
    83        if ( (icon->w%8) != 0 ) {
    84 	}
    84        fprintf(stderr, "Icon width must be a multiple of 8!\n");
    85 */
    85        SDL_FreeSurface(icon);
    86     
    86        return(NULL);
    87     
    87        }
    88 	if ( icon->format->palette == NULL ) {
    88      */
    89 		fprintf(stderr, "Icon must have a palette!\n");
    89 
    90 		SDL_FreeSurface(icon);
    90 
    91 		return(NULL);
    91     if (icon->format->palette == NULL) {
    92 	}
    92         fprintf(stderr, "Icon must have a palette!\n");
    93 
    93         SDL_FreeSurface(icon);
    94 	/* Set the colorkey */
    94         return (NULL);
    95 	SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *)icon->pixels));
    95     }
    96 
    96 
    97 	/* Create the mask */
    97     /* Set the colorkey */
    98 	pixels = (Uint8 *)icon->pixels;
    98     SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *) icon->pixels));
    99 	printf("Transparent pixel: (%d,%d,%d)\n",
    99 
   100 				icon->format->palette->colors[*pixels].r,
   100     /* Create the mask */
   101 				icon->format->palette->colors[*pixels].g,
   101     pixels = (Uint8 *) icon->pixels;
   102 				icon->format->palette->colors[*pixels].b);
   102     printf("Transparent pixel: (%d,%d,%d)\n",
   103 	mlen = (icon->w*icon->h + 7) / 8;
   103            icon->format->palette->colors[*pixels].r,
   104 	mask = (Uint8 *)malloc(mlen);
   104            icon->format->palette->colors[*pixels].g,
   105 	if ( mask == NULL ) {
   105            icon->format->palette->colors[*pixels].b);
   106 		fprintf(stderr, "Out of memory!\n");
   106     mlen = (icon->w * icon->h + 7) / 8;
   107 		SDL_FreeSurface(icon);
   107     mask = (Uint8 *) malloc(mlen);
   108 		return(NULL);
   108     if (mask == NULL) {
   109 	}
   109         fprintf(stderr, "Out of memory!\n");
   110 	memset(mask, 0, mlen);
   110         SDL_FreeSurface(icon);
   111 	for ( i=0; i < icon->h; i++)
   111         return (NULL);
   112         for (j=0; j < icon->w; j++) {
   112     }
       
   113     memset(mask, 0, mlen);
       
   114     for (i = 0; i < icon->h; i++)
       
   115         for (j = 0; j < icon->w; j++) {
   113             int pindex = i * icon->pitch + j;
   116             int pindex = i * icon->pitch + j;
   114             int mindex = i * icon->w + j;
   117             int mindex = i * icon->w + j;
   115             if ( pixels[pindex] != *pixels )
   118             if (pixels[pindex] != *pixels)
   116                 mask[mindex>>3] |= 1 << (7 - (mindex & 7));
   119                 mask[mindex >> 3] |= 1 << (7 - (mindex & 7));
   117         }
   120         }
   118 	*maskp = mask;
   121     *maskp = mask;
   119 	return(icon);
   122     return (icon);
   120 }
   123 }
   121 
   124 
   122 void HotKey_ToggleFullScreen(void)
   125 void
   123 {
   126 HotKey_ToggleFullScreen(void)
   124 	SDL_Surface *screen;
   127 {
   125 
   128     SDL_Surface *screen;
   126 	screen = SDL_GetVideoSurface();
   129 
   127 	if ( SDL_WM_ToggleFullScreen(screen) ) {
   130     screen = SDL_GetVideoSurface();
   128 		printf("Toggled fullscreen mode - now %s\n",
   131     if (SDL_WM_ToggleFullScreen(screen)) {
   129 		    (screen->flags&SDL_FULLSCREEN) ? "fullscreen" : "windowed");
   132         printf("Toggled fullscreen mode - now %s\n",
   130 	} else {
   133                (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
   131 		printf("Unable to toggle fullscreen mode\n");
   134     } else {
   132 		video_flags ^= SDL_FULLSCREEN;
   135         printf("Unable to toggle fullscreen mode\n");
   133 		SetVideoMode(screen->w, screen->h);
   136         video_flags ^= SDL_FULLSCREEN;
   134 	}
   137         SetVideoMode(screen->w, screen->h);
   135 }
   138     }
   136 
   139 }
   137 void HotKey_ToggleGrab(void)
   140 
   138 {
   141 void
   139 	SDL_GrabMode mode;
   142 HotKey_ToggleGrab(void)
   140 
   143 {
   141 	printf("Ctrl-G: toggling input grab!\n");
   144     SDL_GrabMode mode;
   142 	mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
   145 
   143 	if ( mode == SDL_GRAB_ON ) {
   146     printf("Ctrl-G: toggling input grab!\n");
   144 		printf("Grab was on\n");
   147     mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
   145 	} else {
   148     if (mode == SDL_GRAB_ON) {
   146 		printf("Grab was off\n");
   149         printf("Grab was on\n");
   147 	}
   150     } else {
   148 	mode = SDL_WM_GrabInput(mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
   151         printf("Grab was off\n");
   149 	if ( mode == SDL_GRAB_ON ) {
   152     }
   150 		printf("Grab is now on\n");
   153     mode = SDL_WM_GrabInput(mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
   151 	} else {
   154     if (mode == SDL_GRAB_ON) {
   152 		printf("Grab is now off\n");
   155         printf("Grab is now on\n");
   153 	}
   156     } else {
   154 }
   157         printf("Grab is now off\n");
   155 
   158     }
   156 void HotKey_Iconify(void)
   159 }
   157 {
   160 
   158 	printf("Ctrl-Z: iconifying window!\n");
   161 void
   159 	SDL_WM_IconifyWindow();
   162 HotKey_Iconify(void)
   160 }
   163 {
   161 
   164     printf("Ctrl-Z: iconifying window!\n");
   162 void HotKey_Quit(void)
   165     SDL_WM_IconifyWindow();
   163 {
   166 }
   164 	SDL_Event event;
   167 
   165 
   168 void
   166 	printf("Posting internal quit request\n");
   169 HotKey_Quit(void)
   167 	event.type = SDL_USEREVENT;
   170 {
   168 	SDL_PushEvent(&event);
   171     SDL_Event event;
   169 }
   172 
   170 
   173     printf("Posting internal quit request\n");
   171 int SDLCALL FilterEvents(const SDL_Event *event)
   174     event.type = SDL_USEREVENT;
   172 {
   175     SDL_PushEvent(&event);
   173 	static int reallyquit = 0;
   176 }
   174 
   177 
   175 	switch (event->type) {
   178 static int SDLCALL(*old_filterfunc) (void *, SDL_Event *);
   176 
   179 static void *old_filterdata;
   177 		case SDL_ACTIVEEVENT:
   180 
   178 			/* See what happened */
   181 int SDLCALL
   179 			printf("App %s ",
   182 FilterEvents(void *userdata, SDL_Event * event)
   180 				event->active.gain ? "gained" : "lost");
   183 {
   181 			if ( event->active.state & SDL_APPACTIVE )
   184     static int reallyquit = 0;
   182 				printf("active ");
   185 
   183 			if ( event->active.state & SDL_APPINPUTFOCUS )
   186     if (old_filterfunc) {
   184 				printf("input ");
   187         old_filterfunc(old_filterdata, event);
   185 			if ( event->active.state & SDL_APPMOUSEFOCUS )
   188     }
   186 				printf("mouse ");
   189 
   187 			printf("focus\n");
   190     switch (event->type) {
   188 
   191 
   189 			/* See if we are iconified or restored */
   192     case SDL_ACTIVEEVENT:
   190 			if ( event->active.state & SDL_APPACTIVE ) {
   193         /* See what happened */
   191 				printf("App has been %s\n",
   194         printf("App %s ", event->active.gain ? "gained" : "lost");
   192 					event->active.gain ?
   195         if (event->active.state & SDL_APPACTIVE)
   193 						 "restored" : "iconified");
   196             printf("active ");
   194 			}
   197         if (event->active.state & SDL_APPINPUTFOCUS)
   195 			return(0);
   198             printf("input ");
   196 
   199         if (event->active.state & SDL_APPMOUSEFOCUS)
   197 		/* We want to toggle visibility on buttonpress */
   200             printf("mouse ");
   198 		case SDL_MOUSEBUTTONDOWN:
   201         printf("focus\n");
   199 		case SDL_MOUSEBUTTONUP:
   202 
   200 			if ( event->button.state == SDL_PRESSED ) {
   203         /* See if we are iconified or restored */
   201 				visible = !visible;
   204         if (event->active.state & SDL_APPACTIVE) {
   202 				SDL_ShowCursor(visible);
   205             printf("App has been %s\n",
   203 			}
   206                    event->active.gain ? "restored" : "iconified");
   204 			printf("Mouse button %d has been %s\n",
   207         }
   205 				event->button.button,
   208         return (0);
   206 				(event->button.state == SDL_PRESSED) ?
   209 
   207 						"pressed" : "released");
   210         /* We want to toggle visibility on buttonpress */
   208 			return(0);
   211     case SDL_MOUSEBUTTONDOWN:
   209 
   212     case SDL_MOUSEBUTTONUP:
   210 		/* Show relative mouse motion */
   213         if (event->button.state == SDL_PRESSED) {
   211 		case SDL_MOUSEMOTION:
   214             visible = !visible;
       
   215             SDL_ShowCursor(visible);
       
   216         }
       
   217         printf("Mouse button %d has been %s\n",
       
   218                event->button.button,
       
   219                (event->button.state == SDL_PRESSED) ? "pressed" : "released");
       
   220         return (0);
       
   221 
       
   222         /* Show relative mouse motion */
       
   223     case SDL_MOUSEMOTION:
   212 #if 0
   224 #if 0
   213 			printf("Mouse motion: {%d,%d} (%d,%d)\n",
   225         printf("Mouse motion: {%d,%d} (%d,%d)\n",
   214 				event->motion.x, event->motion.y,
   226                event->motion.x, event->motion.y,
   215 				event->motion.xrel, event->motion.yrel);
   227                event->motion.xrel, event->motion.yrel);
   216 #endif
   228 #endif
   217 			return(0);
   229         return (0);
   218 
   230 
   219 		case SDL_KEYDOWN:
   231     case SDL_KEYDOWN:
   220 			if ( event->key.keysym.sym == SDLK_ESCAPE ) {
   232         if (event->key.keysym.sym == SDLK_ESCAPE) {
   221 				HotKey_Quit();
   233             HotKey_Quit();
   222 			}
   234         }
   223 			if ( (event->key.keysym.sym == SDLK_g) &&
   235         if ((event->key.keysym.sym == SDLK_g) &&
   224 			     (event->key.keysym.mod & KMOD_CTRL) ) {
   236             (event->key.keysym.mod & KMOD_CTRL)) {
   225 				HotKey_ToggleGrab();
   237             HotKey_ToggleGrab();
   226 			}
   238         }
   227 			if ( (event->key.keysym.sym == SDLK_z) &&
   239         if ((event->key.keysym.sym == SDLK_z) &&
   228 			     (event->key.keysym.mod & KMOD_CTRL) ) {
   240             (event->key.keysym.mod & KMOD_CTRL)) {
   229 				HotKey_Iconify();
   241             HotKey_Iconify();
   230 			}
   242         }
   231 			if ( (event->key.keysym.sym == SDLK_RETURN) &&
   243         if ((event->key.keysym.sym == SDLK_RETURN) &&
   232 			     (event->key.keysym.mod & KMOD_ALT) ) {
   244             (event->key.keysym.mod & KMOD_ALT)) {
   233 				HotKey_ToggleFullScreen();
   245             HotKey_ToggleFullScreen();
   234 			}
   246         }
   235 			return(0);
   247         return (0);
   236 
   248 
   237 		/* Pass the video resize event through .. */
   249         /* Pass the video resize event through .. */
   238 		case SDL_VIDEORESIZE:
   250     case SDL_VIDEORESIZE:
   239 			return(1);
   251         return (1);
   240 
   252 
   241 		/* This is important!  Queue it if we want to quit. */
   253         /* This is important!  Queue it if we want to quit. */
   242 		case SDL_QUIT:
   254     case SDL_QUIT:
   243 			if ( ! reallyquit ) {
   255         if (!reallyquit) {
   244 				reallyquit = 1;
   256             reallyquit = 1;
   245 				printf("Quit requested\n");
   257             printf("Quit requested\n");
   246 				return(0);
   258             return (0);
   247 			}
   259         }
   248 			printf("Quit demanded\n");
   260         printf("Quit demanded\n");
   249 			return(1);
   261         return (1);
   250 
   262 
   251 		/* This will never happen because events queued directly
   263         /* This will never happen because events queued directly
   252 		   to the event queue are not filtered.
   264            to the event queue are not filtered.
   253 		 */
   265          */
   254 		case SDL_USEREVENT:
   266     case SDL_USEREVENT:
   255 			return(1);
   267         return (1);
   256 
   268 
   257 		/* Drop all other events */
   269         /* Drop all other events */
   258 		default:
   270     default:
   259 			return(0);
   271         return (0);
   260 	}
   272     }
   261 }
   273 }
   262 
   274 
   263 int main(int argc, char *argv[])
   275 int
   264 {
   276 main(int argc, char *argv[])
   265 	SDL_Event event;
   277 {
   266 	char *title;
   278     SDL_Event event;
   267 	SDL_Surface *icon;
   279     char *title;
   268 	Uint8 *icon_mask;
   280     SDL_Surface *icon;
   269 	int parsed;
   281     Uint8 *icon_mask;
   270 	int w, h;
   282     int parsed;
   271 
   283     int w, h;
   272 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   284 
   273 		fprintf(stderr,
   285     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   274 			"Couldn't initialize SDL: %s\n", SDL_GetError());
   286         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   275 		return(1);
   287         return (1);
   276 	}
   288     }
   277 
   289 
   278 	/* Check command line arguments */
   290     /* Check command line arguments */
   279 	w = 640;
   291     w = 640;
   280 	h = 480;
   292     h = 480;
   281 	video_bpp = 8;
   293     video_bpp = 8;
   282 	video_flags = SDL_SWSURFACE;
   294     video_flags = SDL_SWSURFACE;
   283 	parsed = 1;
   295     parsed = 1;
   284 	while ( parsed ) {
   296     while (parsed) {
   285 		if ( (argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0) ) {
   297         if ((argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0)) {
   286 			video_flags |= SDL_FULLSCREEN;
   298             video_flags |= SDL_FULLSCREEN;
   287 			argc -= 1;
   299             argc -= 1;
   288 			argv += 1;
   300             argv += 1;
   289 		} else
   301         } else if ((argc >= 2) && (strcmp(argv[1], "-resize") == 0)) {
   290 		if ( (argc >= 2) && (strcmp(argv[1], "-resize") == 0) ) {
   302             video_flags |= SDL_RESIZABLE;
   291 			video_flags |= SDL_RESIZABLE;
   303             argc -= 1;
   292 			argc -= 1;
   304             argv += 1;
   293 			argv += 1;
   305         } else if ((argc >= 2) && (strcmp(argv[1], "-noframe") == 0)) {
   294 		} else
   306             video_flags |= SDL_NOFRAME;
   295 		if ( (argc >= 2) && (strcmp(argv[1], "-noframe") == 0) ) {
   307             argc -= 1;
   296 			video_flags |= SDL_NOFRAME;
   308             argv += 1;
   297 			argc -= 1;
   309         } else if ((argc >= 3) && (strcmp(argv[1], "-width") == 0)) {
   298 			argv += 1;
   310             w = atoi(argv[2]);
   299 		} else
   311             argc -= 2;
   300 		if ( (argc >= 3) && (strcmp(argv[1], "-width") == 0) ) {
   312             argv += 2;
   301 			w = atoi(argv[2]);
   313         } else if ((argc >= 3) && (strcmp(argv[1], "-height") == 0)) {
   302 			argc -= 2;
   314             h = atoi(argv[2]);
   303 			argv += 2;
   315             argc -= 2;
   304 		} else
   316             argv += 2;
   305 		if ( (argc >= 3) && (strcmp(argv[1], "-height") == 0) ) {
   317         } else if ((argc >= 3) && (strcmp(argv[1], "-bpp") == 0)) {
   306 			h = atoi(argv[2]);
   318             video_bpp = atoi(argv[2]);
   307 			argc -= 2;
   319             argc -= 2;
   308 			argv += 2;
   320             argv += 2;
   309 		} else
   321         } else {
   310 		if ( (argc >= 3) && (strcmp(argv[1], "-bpp") == 0) ) {
   322             parsed = 0;
   311 			video_bpp = atoi(argv[2]);
   323         }
   312 			argc -= 2;
   324     }
   313 			argv += 2;
   325 
   314 		} else {
   326     /* Set the icon -- this must be done before the first mode set */
   315 			parsed = 0;
   327     icon = LoadIconSurface("icon.bmp", &icon_mask);
   316 		}
   328     if (icon != NULL) {
   317 	}
   329         SDL_WM_SetIcon(icon, icon_mask);
   318 
   330     }
   319 	/* Set the icon -- this must be done before the first mode set */
   331     if (icon_mask != NULL)
   320 	icon = LoadIconSurface("icon.bmp", &icon_mask);
   332         free(icon_mask);
   321 	if ( icon != NULL ) {
   333 
   322 		SDL_WM_SetIcon(icon, icon_mask);
   334     /* Set the title bar */
   323 	}
   335     if (argv[1] == NULL)
   324 	if ( icon_mask != NULL )
   336         title = "Testing  1.. 2.. 3...";
   325 		free(icon_mask);
   337     else
   326 
   338         title = argv[1];
   327 	/* Set the title bar */
   339     SDL_WM_SetCaption(title, "testwm");
   328 	if ( argv[1] == NULL )
   340 
   329 		title = "Testing  1.. 2.. 3...";
   341     /* See if it's really set */
   330 	else
   342     SDL_WM_GetCaption(&title, NULL);
   331 		title = argv[1];
   343     if (title)
   332 	SDL_WM_SetCaption(title, "testwm");
   344         printf("Title was set to: %s\n", title);
   333 
   345     else
   334 	/* See if it's really set */
   346         printf("No window title was set!\n");
   335 	SDL_WM_GetCaption(&title, NULL);
   347 
   336 	if ( title )
   348     /* Initialize the display */
   337 		printf("Title was set to: %s\n", title);
   349     if (SetVideoMode(w, h) < 0) {
   338 	else
   350         quit(1);
   339 		printf("No window title was set!\n");
   351     }
   340 
   352 
   341 	/* Initialize the display */
   353     /* Set an event filter that discards everything but QUIT */
   342 	if ( SetVideoMode(w, h) < 0 ) {
   354     SDL_GetEventFilter(&old_filterfunc, &old_filterdata);
   343 		quit(1);
   355     SDL_SetEventFilter(FilterEvents, NULL);
   344 	}
   356 
   345 
   357     /* Ignore key up events, they don't even get filtered */
   346 	/* Set an event filter that discards everything but QUIT */
   358     SDL_EventState(SDL_KEYUP, SDL_IGNORE);
   347 	SDL_SetEventFilter(FilterEvents);
   359 
   348 
   360     /* Loop, waiting for QUIT */
   349 	/* Ignore key up events, they don't even get filtered */
   361     while (SDL_WaitEvent(&event)) {
   350 	SDL_EventState(SDL_KEYUP, SDL_IGNORE);
   362         switch (event.type) {
   351 
   363         case SDL_VIDEORESIZE:
   352 	/* Loop, waiting for QUIT */
   364             printf("Got a resize event: %dx%d\n",
   353 	while ( SDL_WaitEvent(&event) ) {
   365                    event.resize.w, event.resize.h);
   354 		switch (event.type) {
   366             SetVideoMode(event.resize.w, event.resize.h);
   355 			case SDL_VIDEORESIZE:
   367             break;
   356 				printf("Got a resize event: %dx%d\n",
   368         case SDL_USEREVENT:
   357 				       event.resize.w, event.resize.h);
   369             printf("Handling internal quit request\n");
   358 				SetVideoMode(event.resize.w, event.resize.h);
   370             /* Fall through to the quit handler */
   359 				break;
   371         case SDL_QUIT:
   360 			case SDL_USEREVENT:
   372             printf("Bye bye..\n");
   361 				printf("Handling internal quit request\n");
   373             quit(0);
   362 				/* Fall through to the quit handler */
   374         default:
   363 			case SDL_QUIT:
   375             /* This should never happen */
   364 				printf("Bye bye..\n");
   376             printf("Warning: Event %d wasn't filtered\n", event.type);
   365 				quit(0);
   377             break;
   366 			default:
   378         }
   367 				/* This should never happen */
   379     }
   368 				printf("Warning: Event %d wasn't filtered\n",
   380     printf("SDL_WaitEvent() error: %s\n", SDL_GetError());
   369 								event.type);
   381     SDL_Quit();
   370 				break;
   382     return (255);
   371 		}
   383 }
   372 	}
       
   373 	printf("SDL_WaitEvent() error: %s\n", SDL_GetError());
       
   374 	SDL_Quit();
       
   375 	return(255);
       
   376 }