Mercurial > sdl-ios-xcode
comparison test/testoverlay.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 | 6399f4e90211 |
children | 4d3bb026cd16 |
comparison
equal
deleted
inserted
replaced
1150:7d8e1925f35b | 1151:be9c9c8f6d53 |
---|---|
18 SDL_Overlay *overlay; | 18 SDL_Overlay *overlay; |
19 int scale; | 19 int scale; |
20 int monochrome; | 20 int monochrome; |
21 int luminance; | 21 int luminance; |
22 int w, h; | 22 int w, h; |
23 | |
24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ | |
25 static void quit(int rc) | |
26 { | |
27 SDL_Quit(); | |
28 exit(rc); | |
29 } | |
23 | 30 |
24 /* NOTE: These RGB conversion functions are not intended for speed, | 31 /* NOTE: These RGB conversion functions are not intended for speed, |
25 only as examples. | 32 only as examples. |
26 */ | 33 */ |
27 | 34 |
345 argv += 2; | 352 argv += 2; |
346 argc -= 2; | 353 argc -= 2; |
347 } else { | 354 } else { |
348 fprintf(stderr, | 355 fprintf(stderr, |
349 "The -delay option requires an argument\n"); | 356 "The -delay option requires an argument\n"); |
350 exit(1); | 357 return(1); |
351 } | 358 } |
352 } else | 359 } else |
353 if ( strcmp(argv[1], "-width") == 0 ) { | 360 if ( strcmp(argv[1], "-width") == 0 ) { |
354 if ( argv[2] && ((w = atoi(argv[2])) > 0) ) { | 361 if ( argv[2] && ((w = atoi(argv[2])) > 0) ) { |
355 argv += 2; | 362 argv += 2; |
356 argc -= 2; | 363 argc -= 2; |
357 } else { | 364 } else { |
358 fprintf(stderr, | 365 fprintf(stderr, |
359 "The -width option requires an argument\n"); | 366 "The -width option requires an argument\n"); |
360 exit(1); | 367 return(1); |
361 } | 368 } |
362 } else | 369 } else |
363 if ( strcmp(argv[1], "-height") == 0 ) { | 370 if ( strcmp(argv[1], "-height") == 0 ) { |
364 if ( argv[2] && ((h = atoi(argv[2])) > 0) ) { | 371 if ( argv[2] && ((h = atoi(argv[2])) > 0) ) { |
365 argv += 2; | 372 argv += 2; |
366 argc -= 2; | 373 argc -= 2; |
367 } else { | 374 } else { |
368 fprintf(stderr, | 375 fprintf(stderr, |
369 "The -height option requires an argument\n"); | 376 "The -height option requires an argument\n"); |
370 exit(1); | 377 return(1); |
371 } | 378 } |
372 } else | 379 } else |
373 if ( strcmp(argv[1], "-bpp") == 0 ) { | 380 if ( strcmp(argv[1], "-bpp") == 0 ) { |
374 if ( argv[2] ) { | 381 if ( argv[2] ) { |
375 desired_bpp = atoi(argv[2]); | 382 desired_bpp = atoi(argv[2]); |
376 argv += 2; | 383 argv += 2; |
377 argc -= 2; | 384 argc -= 2; |
378 } else { | 385 } else { |
379 fprintf(stderr, | 386 fprintf(stderr, |
380 "The -bpp option requires an argument\n"); | 387 "The -bpp option requires an argument\n"); |
381 exit(1); | 388 return(1); |
382 } | 389 } |
383 } else | 390 } else |
384 if ( strcmp(argv[1], "-lum") == 0 ) { | 391 if ( strcmp(argv[1], "-lum") == 0 ) { |
385 if ( argv[2] ) { | 392 if ( argv[2] ) { |
386 luminance = atoi(argv[2]); | 393 luminance = atoi(argv[2]); |
387 argv += 2; | 394 argv += 2; |
388 argc -= 2; | 395 argc -= 2; |
389 } else { | 396 } else { |
390 fprintf(stderr, | 397 fprintf(stderr, |
391 "The -lum option requires an argument\n"); | 398 "The -lum option requires an argument\n"); |
392 exit(1); | 399 return(1); |
393 } | 400 } |
394 } else | 401 } else |
395 if ( strcmp(argv[1], "-format") == 0 ) { | 402 if ( strcmp(argv[1], "-format") == 0 ) { |
396 if ( argv[2] ) { | 403 if ( argv[2] ) { |
397 if(!strcmp(argv[2],"YV12")) | 404 if(!strcmp(argv[2],"YV12")) |
405 else if(!strcmp(argv[2],"YVYU")) | 412 else if(!strcmp(argv[2],"YVYU")) |
406 overlay_format = SDL_YVYU_OVERLAY; | 413 overlay_format = SDL_YVYU_OVERLAY; |
407 else | 414 else |
408 { | 415 { |
409 fprintf(stderr, "The -format option %s is not recognized\n",argv[2]); | 416 fprintf(stderr, "The -format option %s is not recognized\n",argv[2]); |
410 exit(1); | 417 return(1); |
411 } | 418 } |
412 argv += 2; | 419 argv += 2; |
413 argc -= 2; | 420 argc -= 2; |
414 } else { | 421 } else { |
415 fprintf(stderr, | 422 fprintf(stderr, |
416 "The -format option requires an argument\n"); | 423 "The -format option requires an argument\n"); |
417 exit(1); | 424 return(1); |
418 } | 425 } |
419 } else | 426 } else |
420 if ( strcmp(argv[1], "-hw") == 0 ) { | 427 if ( strcmp(argv[1], "-hw") == 0 ) { |
421 video_flags |= SDL_HWSURFACE; | 428 video_flags |= SDL_HWSURFACE; |
422 argv += 1; | 429 argv += 1; |
437 argv += 1; | 444 argv += 1; |
438 argc -= 1; | 445 argc -= 1; |
439 } else | 446 } else |
440 if (( strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0)) { | 447 if (( strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0)) { |
441 PrintUsage(argv0); | 448 PrintUsage(argv0); |
442 exit(1); | 449 return(1); |
443 } else | 450 } else |
444 if ( strcmp(argv[1], "-fullscreen") == 0 ) { | 451 if ( strcmp(argv[1], "-fullscreen") == 0 ) { |
445 video_flags |= SDL_FULLSCREEN; | 452 video_flags |= SDL_FULLSCREEN; |
446 argv += 1; | 453 argv += 1; |
447 argc -= 1; | 454 argc -= 1; |
449 break; | 456 break; |
450 } | 457 } |
451 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { | 458 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { |
452 fprintf(stderr, | 459 fprintf(stderr, |
453 "Couldn't initialize SDL: %s\n", SDL_GetError()); | 460 "Couldn't initialize SDL: %s\n", SDL_GetError()); |
454 exit(1); | 461 return(1); |
455 } | 462 } |
456 atexit(SDL_Quit); /* Clean up on exit */ | |
457 | 463 |
458 /* Initialize the display */ | 464 /* Initialize the display */ |
459 screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags); | 465 screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags); |
460 if ( screen == NULL ) { | 466 if ( screen == NULL ) { |
461 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", | 467 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", |
462 w, h, desired_bpp, SDL_GetError()); | 468 w, h, desired_bpp, SDL_GetError()); |
463 exit(1); | 469 quit(1); |
464 } | 470 } |
465 printf("Set%s %dx%dx%d mode\n", | 471 printf("Set%s %dx%dx%d mode\n", |
466 screen->flags & SDL_FULLSCREEN ? " fullscreen" : "", | 472 screen->flags & SDL_FULLSCREEN ? " fullscreen" : "", |
467 screen->w, screen->h, screen->format->BitsPerPixel); | 473 screen->w, screen->h, screen->format->BitsPerPixel); |
468 printf("(video surface located in %s memory)\n", | 474 printf("(video surface located in %s memory)\n", |
479 bmpfile=(argv[1]?argv[1]:"sample.bmp"); | 485 bmpfile=(argv[1]?argv[1]:"sample.bmp"); |
480 pic = SDL_LoadBMP(bmpfile); | 486 pic = SDL_LoadBMP(bmpfile); |
481 if ( pic == NULL ) { | 487 if ( pic == NULL ) { |
482 fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, | 488 fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, |
483 SDL_GetError()); | 489 SDL_GetError()); |
484 exit(1); | 490 quit(1); |
485 } | 491 } |
486 | 492 |
487 /* Convert the picture to 32bits, for easy conversion */ | 493 /* Convert the picture to 32bits, for easy conversion */ |
488 { | 494 { |
489 SDL_Surface *newsurf; | 495 SDL_Surface *newsurf; |
516 newsurf=SDL_ConvertSurface(pic, &format, SDL_SWSURFACE); | 522 newsurf=SDL_ConvertSurface(pic, &format, SDL_SWSURFACE); |
517 if(!newsurf) | 523 if(!newsurf) |
518 { | 524 { |
519 fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", | 525 fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", |
520 SDL_GetError()); | 526 SDL_GetError()); |
521 exit(1); | 527 quit(1); |
522 } | 528 } |
523 SDL_FreeSurface(pic); | 529 SDL_FreeSurface(pic); |
524 pic=newsurf; | 530 pic=newsurf; |
525 } | 531 } |
526 | 532 |
527 /* Create the overlay */ | 533 /* Create the overlay */ |
528 overlay = SDL_CreateYUVOverlay(pic->w, pic->h, overlay_format, screen); | 534 overlay = SDL_CreateYUVOverlay(pic->w, pic->h, overlay_format, screen); |
529 if ( overlay == NULL ) { | 535 if ( overlay == NULL ) { |
530 fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError()); | 536 fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError()); |
531 exit(1); | 537 quit(1); |
532 } | 538 } |
533 printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes, | 539 printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes, |
534 overlay->hw_overlay?"hardware":"software", | 540 overlay->hw_overlay?"hardware":"software", |
535 overlay->format==SDL_YV12_OVERLAY?"YV12": | 541 overlay->format==SDL_YV12_OVERLAY?"YV12": |
536 overlay->format==SDL_IYUV_OVERLAY?"IYUV": | 542 overlay->format==SDL_IYUV_OVERLAY?"IYUV": |
564 case SDL_IYUV_OVERLAY: | 570 case SDL_IYUV_OVERLAY: |
565 ConvertRGBtoIYUV(pic,overlay,monochrome,luminance); | 571 ConvertRGBtoIYUV(pic,overlay,monochrome,luminance); |
566 break; | 572 break; |
567 default: | 573 default: |
568 printf("cannot convert RGB picture to obtained YUV format!\n"); | 574 printf("cannot convert RGB picture to obtained YUV format!\n"); |
569 exit(1); | 575 quit(1); |
570 break; | 576 break; |
571 } | 577 } |
572 #ifdef BENCHMARK_SDL | 578 #ifdef BENCHMARK_SDL |
573 now = SDL_GetTicks(); | 579 now = SDL_GetTicks(); |
574 printf("Conversion Time: %d milliseconds\n", now-then); | 580 printf("Conversion Time: %d milliseconds\n", now-then); |
582 #ifdef BENCHMARK_SDL | 588 #ifdef BENCHMARK_SDL |
583 now = SDL_GetTicks(); | 589 now = SDL_GetTicks(); |
584 printf("Time: %d milliseconds\n", now-then); | 590 printf("Time: %d milliseconds\n", now-then); |
585 #endif | 591 #endif |
586 SDL_Delay(delay*1000); | 592 SDL_Delay(delay*1000); |
593 SDL_Quit(); | |
587 return(0); | 594 return(0); |
588 } | 595 } |
589 | 596 |