comparison test/testalpha.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 05c551e5bc64
children 51a8702d8ecd
comparison
equal deleted inserted replaced
1150:7d8e1925f35b 1151:be9c9c8f6d53
9 #include <math.h> 9 #include <math.h>
10 10
11 #include "SDL.h" 11 #include "SDL.h"
12 12
13 #define FRAME_TICKS (1000/30) /* 30 frames/second */ 13 #define FRAME_TICKS (1000/30) /* 30 frames/second */
14
15 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
16 static void quit(int rc)
17 {
18 SDL_Quit();
19 exit(rc);
20 }
21
14 22
15 /* Create a "light" -- a yellowish surface with variable alpha */ 23 /* Create a "light" -- a yellowish surface with variable alpha */
16 SDL_Surface *CreateLight(SDL_Surface *screen, int radius) 24 SDL_Surface *CreateLight(SDL_Surface *screen, int radius)
17 { 25 {
18 Uint8 trans, alphamask; 26 Uint8 trans, alphamask;
290 298
291 299
292 /* Initialize SDL */ 300 /* Initialize SDL */
293 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { 301 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
294 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 302 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
295 exit(1); 303 return(1);
296 } 304 }
297 atexit(SDL_Quit);
298 305
299 /* Alpha blending doesn't work well at 8-bit color */ 306 /* Alpha blending doesn't work well at 8-bit color */
300 info = SDL_GetVideoInfo(); 307 info = SDL_GetVideoInfo();
301 if ( info->vfmt->BitsPerPixel > 8 ) { 308 if ( info->vfmt->BitsPerPixel > 8 ) {
302 video_bpp = info->vfmt->BitsPerPixel; 309 video_bpp = info->vfmt->BitsPerPixel;
325 videoflags |= SDL_FULLSCREEN; 332 videoflags |= SDL_FULLSCREEN;
326 } else { 333 } else {
327 fprintf(stderr, 334 fprintf(stderr,
328 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n", 335 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
329 argv[0]); 336 argv[0]);
330 exit(1); 337 quit(1);
331 } 338 }
332 } 339 }
333 340
334 /* Set 640x480 video mode */ 341 /* Set 640x480 video mode */
335 if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) { 342 if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
336 fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n", 343 fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
337 video_bpp, SDL_GetError()); 344 video_bpp, SDL_GetError());
338 exit(2); 345 quit(2);
339 } 346 }
340 347
341 /* Set the surface pixels and refresh! */ 348 /* Set the surface pixels and refresh! */
342 if ( SDL_LockSurface(screen) < 0 ) { 349 if ( SDL_LockSurface(screen) < 0 ) {
343 fprintf(stderr, "Couldn't lock the display surface: %s\n", 350 fprintf(stderr, "Couldn't lock the display surface: %s\n",
344 SDL_GetError()); 351 SDL_GetError());
345 exit(2); 352 quit(2);
346 } 353 }
347 buffer=(Uint8 *)screen->pixels; 354 buffer=(Uint8 *)screen->pixels;
348 if (screen->format->BytesPerPixel!=2) { 355 if (screen->format->BytesPerPixel!=2) {
349 for ( i=0; i<screen->h; ++i ) { 356 for ( i=0; i<screen->h; ++i ) {
350 memset(buffer,(i*255)/screen->h, screen->pitch); 357 memset(buffer,(i*255)/screen->h, screen->pitch);
369 SDL_UpdateRect(screen, 0, 0, 0, 0); 376 SDL_UpdateRect(screen, 0, 0, 0, 0);
370 377
371 /* Create the light */ 378 /* Create the light */
372 light = CreateLight(screen, 82); 379 light = CreateLight(screen, 82);
373 if ( light == NULL ) { 380 if ( light == NULL ) {
374 exit(1); 381 quit(1);
375 } 382 }
376 383
377 /* Load the sprite */ 384 /* Load the sprite */
378 if ( LoadSprite(screen, "icon.bmp") < 0 ) { 385 if ( LoadSprite(screen, "icon.bmp") < 0 ) {
379 SDL_FreeSurface(light); 386 SDL_FreeSurface(light);
380 exit(1); 387 quit(1);
381 } 388 }
382 389
383 /* Print out information about our surfaces */ 390 /* Print out information about our surfaces */
384 printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel); 391 printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel);
385 if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { 392 if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
490 /* Print out some timing information */ 497 /* Print out some timing information */
491 if ( flashes > 0 ) { 498 if ( flashes > 0 ) {
492 printf("%d alpha blits, ~%4.4f ms per blit\n", 499 printf("%d alpha blits, ~%4.4f ms per blit\n",
493 flashes, (float)flashtime/flashes); 500 flashes, (float)flashtime/flashes);
494 } 501 }
502
503 SDL_Quit();
495 return(0); 504 return(0);
496 } 505 }