diff 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
line wrap: on
line diff
--- a/test/testoverlay2.c	Wed Sep 28 06:38:22 2005 +0000
+++ b/test/testoverlay2.c	Wed Sep 28 11:36:20 2005 +0000
@@ -41,6 +41,14 @@
     {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173}
 };
 
+
+/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
+static void quit(int rc)
+{
+	SDL_Quit();
+	exit(rc);
+}
+
 /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */
 
 /* NOTE: These RGB conversion functions are not intended for speed,
@@ -288,6 +296,12 @@
     int overlay_format=SDL_YUY2_OVERLAY;
     int scale=5;
 
+    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
+    {
+        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
+        return 3;
+    }
+
     while ( argc > 1 )
     {
         if (strcmp(argv[1], "-fps")== 0)
@@ -298,12 +312,12 @@
                 if (fps==0)
                 {
                     fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
-                    return -1;
+                    quit(10);
                 }
                 if ((fps<0) || (fps>1000))
                 {
                     fprintf(stderr, "The -fps option must be in range from 1 to 1000, default is 12.\n");
-                    return -1;
+                    quit(10);
                 }
                 argv += 2;
                 argc -= 2;
@@ -311,7 +325,7 @@
             else
             {
                 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
-                return -1;
+                quit(10);
             }
         } else
         if (strcmp(argv[1], "-format") == 0)
@@ -331,7 +345,7 @@
                 else
                 {
                     fprintf(stderr, "The -format option %s is not recognized, see help for info.\n", argv[2]);
-                    return -1;
+                    quit(10);
                 }
                 argv += 2;
                 argc -= 2;
@@ -339,7 +353,7 @@
             else
             {
                 fprintf(stderr, "The -format option requires an argument, default is YUY2.\n");
-                return -1;
+                quit(10);
             }
         } else
         if (strcmp(argv[1], "-scale") == 0)
@@ -350,12 +364,12 @@
                 if (scale==0)
                 {
                     fprintf(stderr, "The -scale option requires an argument [from 1 to 50], default is 5.\n");
-                    return -1;
+                    quit(10);
                 }
                 if ((scale<0) || (scale>50))
                 {
                     fprintf(stderr, "The -scale option must be in range from 1 to 50, default is 5.\n");
-                    return -1;
+                    quit(10);
                 }
                 argv += 2;
                 argc -= 2;
@@ -363,17 +377,17 @@
             else
             {
                 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
-                return -1;
+                quit(10);
             }
         } else
         if ((strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0))
         {
             PrintUsage(argv[0]);
-            return 0;
+            quit(0);
         } else
         {
             fprintf(stderr, "Unrecognized option: %s.\n", argv[1]);
-            return -1;
+            quit(10);
         }
         break;
     }
@@ -383,7 +397,7 @@
     {
         fprintf(stderr, "Can't allocate memory for movie !\n");
         free(RawMooseData);
-        return 1;
+        quit(1);
     }
 
     /* load the trojan moose images */
@@ -392,27 +406,19 @@
     {
         fprintf(stderr, "Can't find the file moose.dat !\n");
         free(RawMooseData);
-        return 2;
+        quit(2);
     }
    
     SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
 
     SDL_RWclose(handle);
 
-    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
-    {
-        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
-        free(RawMooseData);
-        return 3;
-    }
-    atexit(SDL_Quit);
-
     /* Set video mode */
     if ( (screen=SDL_SetVideoMode(MOOSEPIC_W*scale, MOOSEPIC_H*scale, 0, SDL_RESIZABLE | SDL_SWSURFACE)) == NULL )
     {
         fprintf(stderr, "Couldn't set video mode: %s\n", 0, SDL_GetError());
         free(RawMooseData);
-        return 4;
+        quit(4);
     }
 
     /* Set the window manager title bar */
@@ -426,7 +432,7 @@
         {
             fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", 0, SDL_GetError());
             free(RawMooseData);
-            return 5;
+            quit(5);
         }
         SDL_SetColors(MooseFrame[i], MooseColors, 0, 84);
 
@@ -462,7 +468,7 @@
 		if(!newsurf)
 		{
                     fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError());
-                    return 6;
+                    quit(6);
 		}
 		SDL_FreeSurface(MooseFrame[i]);
 		MooseFrame[i]=newsurf;
@@ -475,7 +481,7 @@
     if (!overlay)
     {
         fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
-        return 7;
+        quit(7);
     }
 
     printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes,
@@ -538,7 +544,7 @@
                      {
                          SDL_FreeSurface(MooseFrame[i]);
                      }
-                     return 0;
+                     quit(0);
             }
         }
 
@@ -586,6 +592,7 @@
         SDL_Delay(1);
     }
 
+	SDL_Quit();
     return 0;
 }