comparison test/threadwin.c @ 1151:be9c9c8f6d53

Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe if SDL is built with a non-cdecl calling convention, and it's just generally bad practice anyhow. Now programs explicitly call SDL_Quit() where appropriate, wrap SDL_Quit() in a cdecl function where it can't be avoided, and rely on the parachute where a crash might have hit the atexit() before (these ARE test programs, after all!).
author Ryan C. Gordon <icculus@icculus.org>
date Wed, 28 Sep 2005 11:36:20 +0000
parents cb40b26523a5
children 14717b52abc0
comparison
equal deleted inserted replaced
1150:7d8e1925f35b 1151:be9c9c8f6d53
11 /* Are we done yet? */ 11 /* Are we done yet? */
12 static int done = 0; 12 static int done = 0;
13 13
14 /* Is the cursor visible? */ 14 /* Is the cursor visible? */
15 static int visible = 1; 15 static int visible = 1;
16
17 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
18 static void quit(int rc)
19 {
20 SDL_Quit();
21 exit(rc);
22 }
16 23
17 SDL_Surface *LoadIconSurface(char *file, Uint8 **maskp) 24 SDL_Surface *LoadIconSurface(char *file, Uint8 **maskp)
18 { 25 {
19 SDL_Surface *icon; 26 SDL_Surface *icon;
20 Uint8 *pixels; 27 Uint8 *pixels;
258 265
259 /* Initialize SDL with the requested flags */ 266 /* Initialize SDL with the requested flags */
260 if ( SDL_Init(init_flags) < 0 ) { 267 if ( SDL_Init(init_flags) < 0 ) {
261 fprintf(stderr, 268 fprintf(stderr,
262 "Couldn't initialize SDL: %s\n", SDL_GetError()); 269 "Couldn't initialize SDL: %s\n", SDL_GetError());
263 exit(1); 270 return(1);
264 } 271 }
265 atexit(SDL_Quit);
266 272
267 /* Set the icon -- this must be done before the first mode set */ 273 /* Set the icon -- this must be done before the first mode set */
268 icon = LoadIconSurface("icon.bmp", &icon_mask); 274 icon = LoadIconSurface("icon.bmp", &icon_mask);
269 if ( icon != NULL ) { 275 if ( icon != NULL ) {
270 SDL_WM_SetIcon(icon, icon_mask); 276 SDL_WM_SetIcon(icon, icon_mask);
275 /* Initialize the display */ 281 /* Initialize the display */
276 screen = SDL_SetVideoMode(640, 480, video_bpp, video_flags); 282 screen = SDL_SetVideoMode(640, 480, video_bpp, video_flags);
277 if ( screen == NULL ) { 283 if ( screen == NULL ) {
278 fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n", 284 fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
279 video_bpp, SDL_GetError()); 285 video_bpp, SDL_GetError());
280 exit(1); 286 quit(1);
281 } 287 }
282 printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ? 288 printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
283 "fullscreen" : "windowed"); 289 "fullscreen" : "windowed");
284 290
285 /* Enable printable characters */ 291 /* Enable printable characters */
300 } 306 }
301 SDL_SetColors(screen, palette, 0, 256); 307 SDL_SetColors(screen, palette, 0, 256);
302 if ( SDL_LockSurface(screen) < 0 ) { 308 if ( SDL_LockSurface(screen) < 0 ) {
303 fprintf(stderr, "Couldn't lock display surface: %s\n", 309 fprintf(stderr, "Couldn't lock display surface: %s\n",
304 SDL_GetError()); 310 SDL_GetError());
305 exit(2); 311 quit(2);
306 } 312 }
307 buffer = (Uint8 *)screen->pixels; 313 buffer = (Uint8 *)screen->pixels;
308 for ( i=0; i<screen->h; ++i ) { 314 for ( i=0; i<screen->h; ++i ) {
309 memset(buffer,(i*255)/screen->h, 315 memset(buffer,(i*255)/screen->h,
310 screen->w*screen->format->BytesPerPixel); 316 screen->w*screen->format->BytesPerPixel);
324 /* Give up some CPU so the events can accumulate */ 330 /* Give up some CPU so the events can accumulate */
325 SDL_Delay(20); 331 SDL_Delay(20);
326 } 332 }
327 SDL_WaitThread(mouse_thread, NULL); 333 SDL_WaitThread(mouse_thread, NULL);
328 SDL_WaitThread(keybd_thread, NULL); 334 SDL_WaitThread(keybd_thread, NULL);
335 SDL_Quit();
329 return(0); 336 return(0);
330 } 337 }