comparison test/testsprite.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 b14fdadd8311
children 31103dbf1c26
comparison
equal deleted inserted replaced
1150:7d8e1925f35b 1151:be9c9c8f6d53
19 SDL_Rect *sprite_rects; 19 SDL_Rect *sprite_rects;
20 SDL_Rect *positions; 20 SDL_Rect *positions;
21 SDL_Rect *velocities; 21 SDL_Rect *velocities;
22 int sprites_visible; 22 int sprites_visible;
23 Uint16 sprite_w, sprite_h; 23 Uint16 sprite_w, sprite_h;
24
25 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
26 static void quit(int rc)
27 {
28 SDL_Quit();
29 exit(rc);
30 }
24 31
25 int LoadSprite(SDL_Surface *screen, char *file) 32 int LoadSprite(SDL_Surface *screen, char *file)
26 { 33 {
27 SDL_Surface *temp; 34 SDL_Surface *temp;
28 35
157 Uint32 then, now, frames; 164 Uint32 then, now, frames;
158 165
159 /* Initialize SDL */ 166 /* Initialize SDL */
160 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { 167 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
161 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 168 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
162 exit(1); 169 return(1);
163 } 170 }
164 atexit(SDL_Quit);
165 171
166 numsprites = NUM_SPRITES; 172 numsprites = NUM_SPRITES;
167 videoflags = SDL_SWSURFACE|SDL_ANYFORMAT; 173 videoflags = SDL_SWSURFACE|SDL_ANYFORMAT;
168 width = 640; 174 width = 640;
169 height = 480; 175 height = 480;
199 numsprites = atoi(argv[argc]); 205 numsprites = atoi(argv[argc]);
200 } else { 206 } else {
201 fprintf(stderr, 207 fprintf(stderr,
202 "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n", 208 "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
203 argv[0]); 209 argv[0]);
204 exit(1); 210 quit(1);
205 } 211 }
206 } 212 }
207 213
208 /* Set video mode */ 214 /* Set video mode */
209 screen = SDL_SetVideoMode(width, height, video_bpp, videoflags); 215 screen = SDL_SetVideoMode(width, height, video_bpp, videoflags);
210 if ( ! screen ) { 216 if ( ! screen ) {
211 fprintf(stderr, "Couldn't set %dx%d video mode: %s\n", 217 fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
212 width, height, SDL_GetError()); 218 width, height, SDL_GetError());
213 exit(2); 219 quit(2);
214 } 220 }
215 221
216 /* Load the sprite */ 222 /* Load the sprite */
217 if ( LoadSprite(screen, "icon.bmp") < 0 ) { 223 if ( LoadSprite(screen, "icon.bmp") < 0 ) {
218 exit(1); 224 quit(1);
219 } 225 }
220 226
221 /* Allocate memory for the sprite info */ 227 /* Allocate memory for the sprite info */
222 mem = (Uint8 *)malloc(4*sizeof(SDL_Rect)*numsprites); 228 mem = (Uint8 *)malloc(4*sizeof(SDL_Rect)*numsprites);
223 if ( mem == NULL ) { 229 if ( mem == NULL ) {
224 SDL_FreeSurface(sprite); 230 SDL_FreeSurface(sprite);
225 fprintf(stderr, "Out of memory!\n"); 231 fprintf(stderr, "Out of memory!\n");
226 exit(2); 232 quit(2);
227 } 233 }
228 sprite_rects = (SDL_Rect *)mem; 234 sprite_rects = (SDL_Rect *)mem;
229 positions = sprite_rects; 235 positions = sprite_rects;
230 sprite_rects += numsprites; 236 sprite_rects += numsprites;
231 velocities = sprite_rects; 237 velocities = sprite_rects;