comparison test/testoverlay2.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 6b8f0543337c
children 4d3bb026cd16
comparison
equal deleted inserted replaced
1150:7d8e1925f35b 1151:be9c9c8f6d53
39 {206, 165, 132}, {206, 173, 140}, {206, 206, 206}, {214, 173, 115}, 39 {206, 165, 132}, {206, 173, 140}, {206, 206, 206}, {214, 173, 115},
40 {214, 173, 140}, {222, 181, 148}, {222, 189, 132}, {222, 189, 156}, 40 {214, 173, 140}, {222, 181, 148}, {222, 189, 132}, {222, 189, 156},
41 {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173} 41 {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173}
42 }; 42 };
43 43
44
45 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
46 static void quit(int rc)
47 {
48 SDL_Quit();
49 exit(rc);
50 }
51
44 /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */ 52 /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */
45 53
46 /* NOTE: These RGB conversion functions are not intended for speed, 54 /* NOTE: These RGB conversion functions are not intended for speed,
47 only as examples. 55 only as examples.
48 */ 56 */
286 int fps=12; 294 int fps=12;
287 int fpsdelay; 295 int fpsdelay;
288 int overlay_format=SDL_YUY2_OVERLAY; 296 int overlay_format=SDL_YUY2_OVERLAY;
289 int scale=5; 297 int scale=5;
290 298
299 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
300 {
301 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
302 return 3;
303 }
304
291 while ( argc > 1 ) 305 while ( argc > 1 )
292 { 306 {
293 if (strcmp(argv[1], "-fps")== 0) 307 if (strcmp(argv[1], "-fps")== 0)
294 { 308 {
295 if (argv[2]) 309 if (argv[2])
296 { 310 {
297 fps = atoi(argv[2]); 311 fps = atoi(argv[2]);
298 if (fps==0) 312 if (fps==0)
299 { 313 {
300 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); 314 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
301 return -1; 315 quit(10);
302 } 316 }
303 if ((fps<0) || (fps>1000)) 317 if ((fps<0) || (fps>1000))
304 { 318 {
305 fprintf(stderr, "The -fps option must be in range from 1 to 1000, default is 12.\n"); 319 fprintf(stderr, "The -fps option must be in range from 1 to 1000, default is 12.\n");
306 return -1; 320 quit(10);
307 } 321 }
308 argv += 2; 322 argv += 2;
309 argc -= 2; 323 argc -= 2;
310 } 324 }
311 else 325 else
312 { 326 {
313 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); 327 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
314 return -1; 328 quit(10);
315 } 329 }
316 } else 330 } else
317 if (strcmp(argv[1], "-format") == 0) 331 if (strcmp(argv[1], "-format") == 0)
318 { 332 {
319 if (argv[2]) 333 if (argv[2])
329 else if(!strcmp(argv[2],"YVYU")) 343 else if(!strcmp(argv[2],"YVYU"))
330 overlay_format = SDL_YVYU_OVERLAY; 344 overlay_format = SDL_YVYU_OVERLAY;
331 else 345 else
332 { 346 {
333 fprintf(stderr, "The -format option %s is not recognized, see help for info.\n", argv[2]); 347 fprintf(stderr, "The -format option %s is not recognized, see help for info.\n", argv[2]);
334 return -1; 348 quit(10);
335 } 349 }
336 argv += 2; 350 argv += 2;
337 argc -= 2; 351 argc -= 2;
338 } 352 }
339 else 353 else
340 { 354 {
341 fprintf(stderr, "The -format option requires an argument, default is YUY2.\n"); 355 fprintf(stderr, "The -format option requires an argument, default is YUY2.\n");
342 return -1; 356 quit(10);
343 } 357 }
344 } else 358 } else
345 if (strcmp(argv[1], "-scale") == 0) 359 if (strcmp(argv[1], "-scale") == 0)
346 { 360 {
347 if (argv[2]) 361 if (argv[2])
348 { 362 {
349 scale = atoi(argv[2]); 363 scale = atoi(argv[2]);
350 if (scale==0) 364 if (scale==0)
351 { 365 {
352 fprintf(stderr, "The -scale option requires an argument [from 1 to 50], default is 5.\n"); 366 fprintf(stderr, "The -scale option requires an argument [from 1 to 50], default is 5.\n");
353 return -1; 367 quit(10);
354 } 368 }
355 if ((scale<0) || (scale>50)) 369 if ((scale<0) || (scale>50))
356 { 370 {
357 fprintf(stderr, "The -scale option must be in range from 1 to 50, default is 5.\n"); 371 fprintf(stderr, "The -scale option must be in range from 1 to 50, default is 5.\n");
358 return -1; 372 quit(10);
359 } 373 }
360 argv += 2; 374 argv += 2;
361 argc -= 2; 375 argc -= 2;
362 } 376 }
363 else 377 else
364 { 378 {
365 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); 379 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
366 return -1; 380 quit(10);
367 } 381 }
368 } else 382 } else
369 if ((strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0)) 383 if ((strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0))
370 { 384 {
371 PrintUsage(argv[0]); 385 PrintUsage(argv[0]);
372 return 0; 386 quit(0);
373 } else 387 } else
374 { 388 {
375 fprintf(stderr, "Unrecognized option: %s.\n", argv[1]); 389 fprintf(stderr, "Unrecognized option: %s.\n", argv[1]);
376 return -1; 390 quit(10);
377 } 391 }
378 break; 392 break;
379 } 393 }
380 394
381 RawMooseData=(Uint8*)malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT); 395 RawMooseData=(Uint8*)malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
382 if (RawMooseData==NULL) 396 if (RawMooseData==NULL)
383 { 397 {
384 fprintf(stderr, "Can't allocate memory for movie !\n"); 398 fprintf(stderr, "Can't allocate memory for movie !\n");
385 free(RawMooseData); 399 free(RawMooseData);
386 return 1; 400 quit(1);
387 } 401 }
388 402
389 /* load the trojan moose images */ 403 /* load the trojan moose images */
390 handle=SDL_RWFromFile("moose.dat", "rb"); 404 handle=SDL_RWFromFile("moose.dat", "rb");
391 if (handle==NULL) 405 if (handle==NULL)
392 { 406 {
393 fprintf(stderr, "Can't find the file moose.dat !\n"); 407 fprintf(stderr, "Can't find the file moose.dat !\n");
394 free(RawMooseData); 408 free(RawMooseData);
395 return 2; 409 quit(2);
396 } 410 }
397 411
398 SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT); 412 SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
399 413
400 SDL_RWclose(handle); 414 SDL_RWclose(handle);
401 415
402 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
403 {
404 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
405 free(RawMooseData);
406 return 3;
407 }
408 atexit(SDL_Quit);
409
410 /* Set video mode */ 416 /* Set video mode */
411 if ( (screen=SDL_SetVideoMode(MOOSEPIC_W*scale, MOOSEPIC_H*scale, 0, SDL_RESIZABLE | SDL_SWSURFACE)) == NULL ) 417 if ( (screen=SDL_SetVideoMode(MOOSEPIC_W*scale, MOOSEPIC_H*scale, 0, SDL_RESIZABLE | SDL_SWSURFACE)) == NULL )
412 { 418 {
413 fprintf(stderr, "Couldn't set video mode: %s\n", 0, SDL_GetError()); 419 fprintf(stderr, "Couldn't set video mode: %s\n", 0, SDL_GetError());
414 free(RawMooseData); 420 free(RawMooseData);
415 return 4; 421 quit(4);
416 } 422 }
417 423
418 /* Set the window manager title bar */ 424 /* Set the window manager title bar */
419 SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2"); 425 SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2");
420 426
424 MOOSEPIC_H, 8, MOOSEPIC_W, 0, 0, 0, 0); 430 MOOSEPIC_H, 8, MOOSEPIC_W, 0, 0, 0, 0);
425 if (MooseFrame[i]==NULL) 431 if (MooseFrame[i]==NULL)
426 { 432 {
427 fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", 0, SDL_GetError()); 433 fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", 0, SDL_GetError());
428 free(RawMooseData); 434 free(RawMooseData);
429 return 5; 435 quit(5);
430 } 436 }
431 SDL_SetColors(MooseFrame[i], MooseColors, 0, 84); 437 SDL_SetColors(MooseFrame[i], MooseColors, 0, 84);
432 438
433 { 439 {
434 SDL_Surface *newsurf; 440 SDL_Surface *newsurf;
460 466
461 newsurf=SDL_ConvertSurface(MooseFrame[i], &format, SDL_SWSURFACE); 467 newsurf=SDL_ConvertSurface(MooseFrame[i], &format, SDL_SWSURFACE);
462 if(!newsurf) 468 if(!newsurf)
463 { 469 {
464 fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError()); 470 fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError());
465 return 6; 471 quit(6);
466 } 472 }
467 SDL_FreeSurface(MooseFrame[i]); 473 SDL_FreeSurface(MooseFrame[i]);
468 MooseFrame[i]=newsurf; 474 MooseFrame[i]=newsurf;
469 } 475 }
470 } 476 }
473 479
474 overlay=SDL_CreateYUVOverlay(MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen); 480 overlay=SDL_CreateYUVOverlay(MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen);
475 if (!overlay) 481 if (!overlay)
476 { 482 {
477 fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError()); 483 fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
478 return 7; 484 quit(7);
479 } 485 }
480 486
481 printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes, 487 printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes,
482 overlay->hw_overlay?"hardware":"software", 488 overlay->hw_overlay?"hardware":"software",
483 overlay->format==SDL_YV12_OVERLAY?"YV12": 489 overlay->format==SDL_YV12_OVERLAY?"YV12":
536 SDL_FreeYUVOverlay(overlay); 542 SDL_FreeYUVOverlay(overlay);
537 for (i=0; i<MOOSEFRAMES_COUNT; i++) 543 for (i=0; i<MOOSEFRAMES_COUNT; i++)
538 { 544 {
539 SDL_FreeSurface(MooseFrame[i]); 545 SDL_FreeSurface(MooseFrame[i]);
540 } 546 }
541 return 0; 547 quit(0);
542 } 548 }
543 } 549 }
544 550
545 if ((!paused)||(resized)) 551 if ((!paused)||(resized))
546 { 552 {
584 } 590 }
585 /* kind of timeslice to OS */ 591 /* kind of timeslice to OS */
586 SDL_Delay(1); 592 SDL_Delay(1);
587 } 593 }
588 594
595 SDL_Quit();
589 return 0; 596 return 0;
590 } 597 }
591 598