comparison test/testwin.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 153f221b7d48
children 51a8702d8ecd
comparison
equal deleted inserted replaced
1150:7d8e1925f35b 1151:be9c9c8f6d53
8 #define BENCHMARK_SDL 8 #define BENCHMARK_SDL
9 9
10 #define NOTICE(X) printf("%s", X); 10 #define NOTICE(X) printf("%s", X);
11 11
12 #include "SDL.h" 12 #include "SDL.h"
13
14 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
15 static void quit(int rc)
16 {
17 SDL_Quit();
18 exit(rc);
19 }
13 20
14 void DrawPict(SDL_Surface *screen, char *bmpfile, 21 void DrawPict(SDL_Surface *screen, char *bmpfile,
15 int speedy, int flip, int nofade) 22 int speedy, int flip, int nofade)
16 { 23 {
17 SDL_Surface *picture; 24 SDL_Surface *picture;
243 delay = 1; 250 delay = 1;
244 w = 640; 251 w = 640;
245 h = 480; 252 h = 480;
246 desired_bpp = 0; 253 desired_bpp = 0;
247 video_flags = 0; 254 video_flags = 0;
255
256 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
257 fprintf(stderr,
258 "Couldn't initialize SDL: %s\n", SDL_GetError());
259 return(1);
260 }
261
248 while ( argc > 1 ) { 262 while ( argc > 1 ) {
249 if ( strcmp(argv[1], "-speedy") == 0 ) { 263 if ( strcmp(argv[1], "-speedy") == 0 ) {
250 speedy = 1; 264 speedy = 1;
251 argv += 1; 265 argv += 1;
252 argc -= 1; 266 argc -= 1;
262 argv += 2; 276 argv += 2;
263 argc -= 2; 277 argc -= 2;
264 } else { 278 } else {
265 fprintf(stderr, 279 fprintf(stderr,
266 "The -delay option requires an argument\n"); 280 "The -delay option requires an argument\n");
267 exit(1); 281 quit(1);
268 } 282 }
269 } else 283 } else
270 if ( strcmp(argv[1], "-width") == 0 ) { 284 if ( strcmp(argv[1], "-width") == 0 ) {
271 if ( argv[2] && ((w = atoi(argv[2])) > 0) ) { 285 if ( argv[2] && ((w = atoi(argv[2])) > 0) ) {
272 argv += 2; 286 argv += 2;
273 argc -= 2; 287 argc -= 2;
274 } else { 288 } else {
275 fprintf(stderr, 289 fprintf(stderr,
276 "The -width option requires an argument\n"); 290 "The -width option requires an argument\n");
277 exit(1); 291 quit(1);
278 } 292 }
279 } else 293 } else
280 if ( strcmp(argv[1], "-height") == 0 ) { 294 if ( strcmp(argv[1], "-height") == 0 ) {
281 if ( argv[2] && ((h = atoi(argv[2])) > 0) ) { 295 if ( argv[2] && ((h = atoi(argv[2])) > 0) ) {
282 argv += 2; 296 argv += 2;
283 argc -= 2; 297 argc -= 2;
284 } else { 298 } else {
285 fprintf(stderr, 299 fprintf(stderr,
286 "The -height option requires an argument\n"); 300 "The -height option requires an argument\n");
287 exit(1); 301 quit(1);
288 } 302 }
289 } else 303 } else
290 if ( strcmp(argv[1], "-bpp") == 0 ) { 304 if ( strcmp(argv[1], "-bpp") == 0 ) {
291 if ( argv[2] ) { 305 if ( argv[2] ) {
292 desired_bpp = atoi(argv[2]); 306 desired_bpp = atoi(argv[2]);
293 argv += 2; 307 argv += 2;
294 argc -= 2; 308 argc -= 2;
295 } else { 309 } else {
296 fprintf(stderr, 310 fprintf(stderr,
297 "The -bpp option requires an argument\n"); 311 "The -bpp option requires an argument\n");
298 exit(1); 312 quit(1);
299 } 313 }
300 } else 314 } else
301 if ( strcmp(argv[1], "-warp") == 0 ) { 315 if ( strcmp(argv[1], "-warp") == 0 ) {
302 video_flags |= SDL_HWPALETTE; 316 video_flags |= SDL_HWPALETTE;
303 argv += 1; 317 argv += 1;
318 argv += 1; 332 argv += 1;
319 argc -= 1; 333 argc -= 1;
320 } else 334 } else
321 break; 335 break;
322 } 336 }
323
324 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
325 fprintf(stderr,
326 "Couldn't initialize SDL: %s\n", SDL_GetError());
327 exit(1);
328 }
329 atexit(SDL_Quit); /* Clean up on exit */
330 337
331 /* Initialize the display */ 338 /* Initialize the display */
332 screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags); 339 screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
333 if ( screen == NULL ) { 340 if ( screen == NULL ) {
334 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", 341 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
335 w, h, desired_bpp, SDL_GetError()); 342 w, h, desired_bpp, SDL_GetError());
336 exit(1); 343 quit(1);
337 } 344 }
338 printf("Set%s %dx%dx%d mode\n", 345 printf("Set%s %dx%dx%d mode\n",
339 screen->flags & SDL_FULLSCREEN ? " fullscreen" : "", 346 screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
340 screen->w, screen->h, screen->format->BitsPerPixel); 347 screen->w, screen->h, screen->format->BitsPerPixel);
341 printf("(video surface located in %s memory)\n", 348 printf("(video surface located in %s memory)\n",
356 printf("Time: %d milliseconds\n", now-then); 363 printf("Time: %d milliseconds\n", now-then);
357 #else 364 #else
358 DrawPict(screen, argv[1], speedy, flip, nofade); 365 DrawPict(screen, argv[1], speedy, flip, nofade);
359 #endif 366 #endif
360 SDL_Delay(delay*1000); 367 SDL_Delay(delay*1000);
368 SDL_Quit();
361 return(0); 369 return(0);
362 } 370 }