Mercurial > sdl-ios-xcode
comparison test/testbitmap.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 | 609543e2b3a1 |
children | 782fd950bd46 c121d94672cb |
comparison
equal
deleted
inserted
replaced
1150:7d8e1925f35b | 1151:be9c9c8f6d53 |
---|---|
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 #include <string.h> | 6 #include <string.h> |
7 | 7 |
8 #include "SDL.h" | 8 #include "SDL.h" |
9 #include "picture.xbm" | 9 #include "picture.xbm" |
10 | |
11 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ | |
12 static void quit(int rc) | |
13 { | |
14 SDL_Quit(); | |
15 exit(rc); | |
16 } | |
10 | 17 |
11 SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits) | 18 SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits) |
12 { | 19 { |
13 SDL_Surface *bitmap; | 20 SDL_Surface *bitmap; |
14 Uint8 *line; | 21 Uint8 *line; |
59 | 66 |
60 | 67 |
61 /* Initialize SDL */ | 68 /* Initialize SDL */ |
62 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { | 69 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { |
63 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); | 70 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); |
64 exit(1); | 71 return(1); |
65 } | 72 } |
66 atexit(SDL_Quit); | |
67 | 73 |
68 video_bpp = 0; | 74 video_bpp = 0; |
69 videoflags = SDL_SWSURFACE; | 75 videoflags = SDL_SWSURFACE; |
70 while ( argc > 1 ) { | 76 while ( argc > 1 ) { |
71 --argc; | 77 --argc; |
83 videoflags |= SDL_FULLSCREEN; | 89 videoflags |= SDL_FULLSCREEN; |
84 } else { | 90 } else { |
85 fprintf(stderr, | 91 fprintf(stderr, |
86 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n", | 92 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n", |
87 argv[0]); | 93 argv[0]); |
88 exit(1); | 94 quit(1); |
89 } | 95 } |
90 } | 96 } |
91 | 97 |
92 /* Set 640x480 video mode */ | 98 /* Set 640x480 video mode */ |
93 if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) { | 99 if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) { |
94 fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n", | 100 fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n", |
95 video_bpp, SDL_GetError()); | 101 video_bpp, SDL_GetError()); |
96 exit(2); | 102 quit(2); |
97 } | 103 } |
98 | 104 |
99 if (video_bpp==8) { | 105 if (video_bpp==8) { |
100 /* Set a gray colormap, reverse order from white to black */ | 106 /* Set a gray colormap, reverse order from white to black */ |
101 for ( i=0; i<256; ++i ) { | 107 for ( i=0; i<256; ++i ) { |
108 | 114 |
109 /* Set the surface pixels and refresh! */ | 115 /* Set the surface pixels and refresh! */ |
110 if ( SDL_LockSurface(screen) < 0 ) { | 116 if ( SDL_LockSurface(screen) < 0 ) { |
111 fprintf(stderr, "Couldn't lock the display surface: %s\n", | 117 fprintf(stderr, "Couldn't lock the display surface: %s\n", |
112 SDL_GetError()); | 118 SDL_GetError()); |
113 exit(2); | 119 quit(2); |
114 } | 120 } |
115 buffer=(Uint8 *)screen->pixels; | 121 buffer=(Uint8 *)screen->pixels; |
116 if (screen->format->BytesPerPixel!=2) { | 122 if (screen->format->BytesPerPixel!=2) { |
117 for ( i=0; i<screen->h; ++i ) { | 123 for ( i=0; i<screen->h; ++i ) { |
118 memset(buffer,(i*255)/screen->h, screen->pitch); | 124 memset(buffer,(i*255)/screen->h, screen->pitch); |
137 | 143 |
138 /* Load the bitmap */ | 144 /* Load the bitmap */ |
139 bitmap = LoadXBM(screen, picture_width, picture_height, | 145 bitmap = LoadXBM(screen, picture_width, picture_height, |
140 (Uint8 *)picture_bits); | 146 (Uint8 *)picture_bits); |
141 if ( bitmap == NULL ) { | 147 if ( bitmap == NULL ) { |
142 exit(1); | 148 quit(1); |
143 } | 149 } |
144 | 150 |
145 /* Wait for a keystroke */ | 151 /* Wait for a keystroke */ |
146 done = 0; | 152 done = 0; |
147 while ( !done ) { | 153 while ( !done ) { |
171 break; | 177 break; |
172 } | 178 } |
173 } | 179 } |
174 } | 180 } |
175 SDL_FreeSurface(bitmap); | 181 SDL_FreeSurface(bitmap); |
182 SDL_Quit(); | |
176 return(0); | 183 return(0); |
177 } | 184 } |