Mercurial > sdl-ios-xcode
comparison test/testalpha.c @ 1895:c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 10 Jul 2006 21:04:37 +0000 |
parents | 67fc81efcfc3 |
children | c785543d1843 |
comparison
equal
deleted
inserted
replaced
1894:c69cee13dd76 | 1895:c121d94672cb |
---|---|
8 #include <string.h> | 8 #include <string.h> |
9 #include <math.h> | 9 #include <math.h> |
10 | 10 |
11 #include "SDL.h" | 11 #include "SDL.h" |
12 | 12 |
13 #define FRAME_TICKS (1000/30) /* 30 frames/second */ | 13 #define FRAME_TICKS (1000/30) /* 30 frames/second */ |
14 | 14 |
15 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ | 15 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
16 static void quit(int rc) | 16 static void |
17 { | 17 quit(int rc) |
18 SDL_Quit(); | 18 { |
19 exit(rc); | 19 SDL_Quit(); |
20 exit(rc); | |
20 } | 21 } |
21 | 22 |
22 /* Fill the screen with a gradient */ | 23 /* Fill the screen with a gradient */ |
23 static void FillBackground(SDL_Surface *screen) | 24 static void |
24 { | 25 FillBackground(SDL_Surface * screen) |
25 Uint8 *buffer; | 26 { |
26 Uint16 *buffer16; | 27 Uint8 *buffer; |
27 Uint16 color; | 28 Uint16 *buffer16; |
28 Uint8 gradient; | 29 Uint16 color; |
29 int i, k; | 30 Uint8 gradient; |
30 | 31 int i, k; |
31 /* Set the surface pixels and refresh! */ | 32 |
32 if ( SDL_LockSurface(screen) < 0 ) { | 33 /* Set the surface pixels and refresh! */ |
33 fprintf(stderr, "Couldn't lock the display surface: %s\n", | 34 if (SDL_LockSurface(screen) < 0) { |
34 SDL_GetError()); | 35 fprintf(stderr, "Couldn't lock the display surface: %s\n", |
35 quit(2); | 36 SDL_GetError()); |
36 } | 37 quit(2); |
37 buffer=(Uint8 *)screen->pixels; | 38 } |
38 if (screen->format->BytesPerPixel!=2) { | 39 buffer = (Uint8 *) screen->pixels; |
39 for ( i=0; i<screen->h; ++i ) { | 40 if (screen->format->BytesPerPixel != 2) { |
40 memset(buffer,(i*255)/screen->h, screen->w*screen->format->BytesPerPixel); | 41 for (i = 0; i < screen->h; ++i) { |
41 buffer += screen->pitch; | 42 memset(buffer, (i * 255) / screen->h, |
42 } | 43 screen->w * screen->format->BytesPerPixel); |
43 } | 44 buffer += screen->pitch; |
44 else | 45 } |
45 { | 46 } else { |
46 for ( i=0; i<screen->h; ++i ) { | 47 for (i = 0; i < screen->h; ++i) { |
47 gradient=((i*255)/screen->h); | 48 gradient = ((i * 255) / screen->h); |
48 color = (Uint16)SDL_MapRGB(screen->format, gradient, gradient, gradient); | 49 color = |
49 buffer16=(Uint16*)buffer; | 50 (Uint16) SDL_MapRGB(screen->format, gradient, gradient, |
50 for (k=0; k<screen->w; k++) | 51 gradient); |
51 { | 52 buffer16 = (Uint16 *) buffer; |
52 *(buffer16+k)=color; | 53 for (k = 0; k < screen->w; k++) { |
53 } | 54 *(buffer16 + k) = color; |
54 buffer += screen->pitch; | 55 } |
55 } | 56 buffer += screen->pitch; |
56 } | 57 } |
57 | 58 } |
58 SDL_UnlockSurface(screen); | 59 |
59 SDL_UpdateRect(screen, 0, 0, 0, 0); | 60 SDL_UnlockSurface(screen); |
61 SDL_UpdateRect(screen, 0, 0, 0, 0); | |
60 } | 62 } |
61 | 63 |
62 /* Create a "light" -- a yellowish surface with variable alpha */ | 64 /* Create a "light" -- a yellowish surface with variable alpha */ |
63 SDL_Surface *CreateLight(int radius) | 65 SDL_Surface * |
64 { | 66 CreateLight(int radius) |
65 Uint8 trans, alphamask; | 67 { |
66 int range, addition; | 68 Uint8 trans, alphamask; |
67 int xdist, ydist; | 69 int range, addition; |
68 Uint16 x, y; | 70 int xdist, ydist; |
69 Uint16 skip; | 71 Uint16 x, y; |
70 Uint32 pixel; | 72 Uint16 skip; |
71 SDL_Surface *light; | 73 Uint32 pixel; |
74 SDL_Surface *light; | |
72 | 75 |
73 #ifdef LIGHT_16BIT | 76 #ifdef LIGHT_16BIT |
74 Uint16 *buf; | 77 Uint16 *buf; |
75 | 78 |
76 /* Create a 16 (4/4/4/4) bpp square with a full 4-bit alpha channel */ | 79 /* Create a 16 (4/4/4/4) bpp square with a full 4-bit alpha channel */ |
77 /* Note: this isn't any faster than a 32 bit alpha surface */ | 80 /* Note: this isn't any faster than a 32 bit alpha surface */ |
78 alphamask = 0x0000000F; | 81 alphamask = 0x0000000F; |
79 light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*radius, 2*radius, 16, | 82 light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2 * radius, 2 * radius, 16, |
80 0x0000F000, 0x00000F00, 0x000000F0, alphamask); | 83 0x0000F000, 0x00000F00, 0x000000F0, |
84 alphamask); | |
81 #else | 85 #else |
82 Uint32 *buf; | 86 Uint32 *buf; |
83 | 87 |
84 /* Create a 32 (8/8/8/8) bpp square with a full 8-bit alpha channel */ | 88 /* Create a 32 (8/8/8/8) bpp square with a full 8-bit alpha channel */ |
85 alphamask = 0x000000FF; | 89 alphamask = 0x000000FF; |
86 light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*radius, 2*radius, 32, | 90 light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2 * radius, 2 * radius, 32, |
87 0xFF000000, 0x00FF0000, 0x0000FF00, alphamask); | 91 0xFF000000, 0x00FF0000, 0x0000FF00, |
88 if ( light == NULL ) { | 92 alphamask); |
89 fprintf(stderr, "Couldn't create light: %s\n", SDL_GetError()); | 93 if (light == NULL) { |
90 return(NULL); | 94 fprintf(stderr, "Couldn't create light: %s\n", SDL_GetError()); |
91 } | 95 return (NULL); |
96 } | |
92 #endif | 97 #endif |
93 | 98 |
94 /* Fill with a light yellow-orange color */ | 99 /* Fill with a light yellow-orange color */ |
95 skip = light->pitch-(light->w*light->format->BytesPerPixel); | 100 skip = light->pitch - (light->w * light->format->BytesPerPixel); |
96 #ifdef LIGHT_16BIT | 101 #ifdef LIGHT_16BIT |
97 buf = (Uint16 *)light->pixels; | 102 buf = (Uint16 *) light->pixels; |
98 #else | 103 #else |
99 buf = (Uint32 *)light->pixels; | 104 buf = (Uint32 *) light->pixels; |
100 #endif | 105 #endif |
101 /* Get a tranparent pixel value - we'll add alpha later */ | 106 /* Get a tranparent pixel value - we'll add alpha later */ |
102 pixel = SDL_MapRGBA(light->format, 0xFF, 0xDD, 0x88, 0); | 107 pixel = SDL_MapRGBA(light->format, 0xFF, 0xDD, 0x88, 0); |
103 for ( y=0; y<light->h; ++y ) { | 108 for (y = 0; y < light->h; ++y) { |
104 for ( x=0; x<light->w; ++x ) { | 109 for (x = 0; x < light->w; ++x) { |
105 *buf++ = pixel; | 110 *buf++ = pixel; |
106 } | 111 } |
107 buf += skip; /* Almost always 0, but just in case... */ | 112 buf += skip; /* Almost always 0, but just in case... */ |
108 } | 113 } |
109 | 114 |
110 /* Calculate alpha values for the surface. */ | 115 /* Calculate alpha values for the surface. */ |
111 #ifdef LIGHT_16BIT | 116 #ifdef LIGHT_16BIT |
112 buf = (Uint16 *)light->pixels; | 117 buf = (Uint16 *) light->pixels; |
113 #else | 118 #else |
114 buf = (Uint32 *)light->pixels; | 119 buf = (Uint32 *) light->pixels; |
115 #endif | 120 #endif |
116 for ( y=0; y<light->h; ++y ) { | 121 for (y = 0; y < light->h; ++y) { |
117 for ( x=0; x<light->w; ++x ) { | 122 for (x = 0; x < light->w; ++x) { |
118 /* Slow distance formula (from center of light) */ | 123 /* Slow distance formula (from center of light) */ |
119 xdist = x-(light->w/2); | 124 xdist = x - (light->w / 2); |
120 ydist = y-(light->h/2); | 125 ydist = y - (light->h / 2); |
121 range = (int)sqrt(xdist*xdist+ydist*ydist); | 126 range = (int) sqrt(xdist * xdist + ydist * ydist); |
122 | 127 |
123 /* Scale distance to range of transparency (0-255) */ | 128 /* Scale distance to range of transparency (0-255) */ |
124 if ( range > radius ) { | 129 if (range > radius) { |
125 trans = alphamask; | 130 trans = alphamask; |
126 } else { | 131 } else { |
127 /* Increasing transparency with distance */ | 132 /* Increasing transparency with distance */ |
128 trans = (Uint8)((range*alphamask)/radius); | 133 trans = (Uint8) ((range * alphamask) / radius); |
129 | 134 |
130 /* Lights are very transparent */ | 135 /* Lights are very transparent */ |
131 addition = (alphamask+1)/8; | 136 addition = (alphamask + 1) / 8; |
132 if ( (int)trans+addition > alphamask ) { | 137 if ((int) trans + addition > alphamask) { |
133 trans = alphamask; | 138 trans = alphamask; |
134 } else { | 139 } else { |
135 trans += addition; | 140 trans += addition; |
136 } | 141 } |
137 } | 142 } |
138 /* We set the alpha component as the right N bits */ | 143 /* We set the alpha component as the right N bits */ |
139 *buf++ |= (255-trans); | 144 *buf++ |= (255 - trans); |
140 } | 145 } |
141 buf += skip; /* Almost always 0, but just in case... */ | 146 buf += skip; /* Almost always 0, but just in case... */ |
142 } | 147 } |
143 /* Enable RLE acceleration of this alpha surface */ | 148 /* Enable RLE acceleration of this alpha surface */ |
144 SDL_SetAlpha(light, SDL_SRCALPHA|SDL_RLEACCEL, 0); | 149 SDL_SetAlpha(light, SDL_SRCALPHA | SDL_RLEACCEL, 0); |
145 | 150 |
146 /* We're done! */ | 151 /* We're done! */ |
147 return(light); | 152 return (light); |
148 } | 153 } |
149 | 154 |
150 static Uint32 flashes = 0; | 155 static Uint32 flashes = 0; |
151 static Uint32 flashtime = 0; | 156 static Uint32 flashtime = 0; |
152 | 157 |
153 void FlashLight(SDL_Surface *screen, SDL_Surface *light, int x, int y) | 158 void |
154 { | 159 FlashLight(SDL_Surface * screen, SDL_Surface * light, int x, int y) |
155 SDL_Rect position; | 160 { |
156 Uint32 ticks1; | 161 SDL_Rect position; |
157 Uint32 ticks2; | 162 Uint32 ticks1; |
158 | 163 Uint32 ticks2; |
159 /* Easy, center light */ | 164 |
160 position.x = x-(light->w/2); | 165 /* Easy, center light */ |
161 position.y = y-(light->h/2); | 166 position.x = x - (light->w / 2); |
162 position.w = light->w; | 167 position.y = y - (light->h / 2); |
163 position.h = light->h; | 168 position.w = light->w; |
164 ticks1 = SDL_GetTicks(); | 169 position.h = light->h; |
165 SDL_BlitSurface(light, NULL, screen, &position); | 170 ticks1 = SDL_GetTicks(); |
166 ticks2 = SDL_GetTicks(); | 171 SDL_BlitSurface(light, NULL, screen, &position); |
167 SDL_UpdateRects(screen, 1, &position); | 172 ticks2 = SDL_GetTicks(); |
168 ++flashes; | 173 SDL_UpdateRects(screen, 1, &position); |
169 | 174 ++flashes; |
170 /* Update time spend doing alpha blitting */ | 175 |
171 flashtime += (ticks2-ticks1); | 176 /* Update time spend doing alpha blitting */ |
177 flashtime += (ticks2 - ticks1); | |
172 } | 178 } |
173 | 179 |
174 static int sprite_visible = 0; | 180 static int sprite_visible = 0; |
175 static SDL_Surface *sprite; | 181 static SDL_Surface *sprite; |
176 static SDL_Surface *backing; | 182 static SDL_Surface *backing; |
177 static SDL_Rect position; | 183 static SDL_Rect position; |
178 static int x_vel, y_vel; | 184 static int x_vel, y_vel; |
179 static int alpha_vel; | 185 static int alpha_vel; |
180 | 186 |
181 int LoadSprite(SDL_Surface *screen, char *file) | 187 int |
182 { | 188 LoadSprite(SDL_Surface * screen, char *file) |
183 SDL_Surface *converted; | 189 { |
184 | 190 SDL_Surface *converted; |
185 /* Load the sprite image */ | 191 |
186 sprite = SDL_LoadBMP(file); | 192 /* Load the sprite image */ |
187 if ( sprite == NULL ) { | 193 sprite = SDL_LoadBMP(file); |
188 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError()); | 194 if (sprite == NULL) { |
189 return(-1); | 195 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError()); |
190 } | 196 return (-1); |
191 | 197 } |
192 /* Set transparent pixel as the pixel at (0,0) */ | 198 |
193 if ( sprite->format->palette ) { | 199 /* Set transparent pixel as the pixel at (0,0) */ |
194 SDL_SetColorKey(sprite, SDL_SRCCOLORKEY, | 200 if (sprite->format->palette) { |
195 *(Uint8 *)sprite->pixels); | 201 SDL_SetColorKey(sprite, SDL_SRCCOLORKEY, *(Uint8 *) sprite->pixels); |
196 } | 202 } |
197 | 203 |
198 /* Convert sprite to video format */ | 204 /* Convert sprite to video format */ |
199 converted = SDL_DisplayFormat(sprite); | 205 converted = SDL_DisplayFormat(sprite); |
200 SDL_FreeSurface(sprite); | 206 SDL_FreeSurface(sprite); |
201 if ( converted == NULL ) { | 207 if (converted == NULL) { |
202 fprintf(stderr, "Couldn't convert background: %s\n", | 208 fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError()); |
203 SDL_GetError()); | 209 return (-1); |
204 return(-1); | 210 } |
205 } | 211 sprite = converted; |
206 sprite = converted; | 212 |
207 | 213 /* Create the background */ |
208 /* Create the background */ | 214 backing = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite->w, sprite->h, 8, |
209 backing = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite->w, sprite->h, 8, | 215 0, 0, 0, 0); |
210 0, 0, 0, 0); | 216 if (backing == NULL) { |
211 if ( backing == NULL ) { | 217 fprintf(stderr, "Couldn't create background: %s\n", SDL_GetError()); |
212 fprintf(stderr, "Couldn't create background: %s\n", | 218 SDL_FreeSurface(sprite); |
213 SDL_GetError()); | 219 return (-1); |
214 SDL_FreeSurface(sprite); | 220 } |
215 return(-1); | 221 |
216 } | 222 /* Convert background to video format */ |
217 | 223 converted = SDL_DisplayFormat(backing); |
218 /* Convert background to video format */ | 224 SDL_FreeSurface(backing); |
219 converted = SDL_DisplayFormat(backing); | 225 if (converted == NULL) { |
220 SDL_FreeSurface(backing); | 226 fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError()); |
221 if ( converted == NULL ) { | 227 SDL_FreeSurface(sprite); |
222 fprintf(stderr, "Couldn't convert background: %s\n", | 228 return (-1); |
223 SDL_GetError()); | 229 } |
224 SDL_FreeSurface(sprite); | 230 backing = converted; |
225 return(-1); | 231 |
226 } | 232 /* Set the initial position of the sprite */ |
227 backing = converted; | 233 position.x = (screen->w - sprite->w) / 2; |
228 | 234 position.y = (screen->h - sprite->h) / 2; |
229 /* Set the initial position of the sprite */ | 235 position.w = sprite->w; |
230 position.x = (screen->w-sprite->w)/2; | 236 position.h = sprite->h; |
231 position.y = (screen->h-sprite->h)/2; | 237 x_vel = 0; |
232 position.w = sprite->w; | 238 y_vel = 0; |
233 position.h = sprite->h; | 239 alpha_vel = 1; |
234 x_vel = 0; y_vel = 0; | 240 |
235 alpha_vel = 1; | 241 /* We're ready to roll. :) */ |
236 | 242 return (0); |
237 /* We're ready to roll. :) */ | 243 } |
238 return(0); | 244 |
239 } | 245 void |
240 | 246 AttractSprite(Uint16 x, Uint16 y) |
241 void AttractSprite(Uint16 x, Uint16 y) | 247 { |
242 { | 248 x_vel = ((int) x - position.x) / 10; |
243 x_vel = ((int)x-position.x)/10; | 249 y_vel = ((int) y - position.y) / 10; |
244 y_vel = ((int)y-position.y)/10; | 250 } |
245 } | 251 |
246 | 252 void |
247 void MoveSprite(SDL_Surface *screen, SDL_Surface *light) | 253 MoveSprite(SDL_Surface * screen, SDL_Surface * light) |
248 { | 254 { |
249 SDL_Rect updates[2]; | 255 SDL_Rect updates[2]; |
250 int alpha; | 256 int alpha; |
251 | 257 |
252 /* Erase the sprite if it was visible */ | 258 /* Erase the sprite if it was visible */ |
253 if ( sprite_visible ) { | 259 if (sprite_visible) { |
254 updates[0] = position; | 260 updates[0] = position; |
255 SDL_BlitSurface(backing, NULL, screen, &updates[0]); | 261 SDL_BlitSurface(backing, NULL, screen, &updates[0]); |
256 } else { | 262 } else { |
257 updates[0].x = 0; updates[0].y = 0; | 263 updates[0].x = 0; |
258 updates[0].w = 0; updates[0].h = 0; | 264 updates[0].y = 0; |
259 sprite_visible = 1; | 265 updates[0].w = 0; |
260 } | 266 updates[0].h = 0; |
261 | 267 sprite_visible = 1; |
262 /* Since the sprite is off the screen, we can do other drawing | 268 } |
263 without being overwritten by the saved area behind the sprite. | 269 |
264 */ | 270 /* Since the sprite is off the screen, we can do other drawing |
265 if ( light != NULL ) { | 271 without being overwritten by the saved area behind the sprite. |
266 int x, y; | 272 */ |
267 | 273 if (light != NULL) { |
268 SDL_GetMouseState(&x, &y); | 274 int x, y; |
269 FlashLight(screen, light, x, y); | 275 |
270 } | 276 SDL_GetMouseState(&x, &y); |
271 | 277 FlashLight(screen, light, x, y); |
272 /* Move the sprite, bounce at the wall */ | 278 } |
273 position.x += x_vel; | 279 |
274 if ( (position.x < 0) || (position.x >= screen->w) ) { | 280 /* Move the sprite, bounce at the wall */ |
275 x_vel = -x_vel; | 281 position.x += x_vel; |
276 position.x += x_vel; | 282 if ((position.x < 0) || (position.x >= screen->w)) { |
277 } | 283 x_vel = -x_vel; |
278 position.y += y_vel; | 284 position.x += x_vel; |
279 if ( (position.y < 0) || (position.y >= screen->h) ) { | 285 } |
280 y_vel = -y_vel; | 286 position.y += y_vel; |
281 position.y += y_vel; | 287 if ((position.y < 0) || (position.y >= screen->h)) { |
282 } | 288 y_vel = -y_vel; |
283 | 289 position.y += y_vel; |
284 /* Update transparency (fade in and out) */ | 290 } |
285 alpha = sprite->format->alpha; | 291 |
286 if ( (alpha+alpha_vel) < 0 ) { | 292 /* Update transparency (fade in and out) */ |
287 alpha_vel = -alpha_vel; | 293 alpha = sprite->format->alpha; |
288 } else | 294 if ((alpha + alpha_vel) < 0) { |
289 if ( (alpha+alpha_vel) > 255 ) { | 295 alpha_vel = -alpha_vel; |
290 alpha_vel = -alpha_vel; | 296 } else if ((alpha + alpha_vel) > 255) { |
291 } | 297 alpha_vel = -alpha_vel; |
292 SDL_SetAlpha(sprite, SDL_SRCALPHA, (Uint8)(alpha+alpha_vel)); | 298 } |
293 | 299 SDL_SetAlpha(sprite, SDL_SRCALPHA, (Uint8) (alpha + alpha_vel)); |
294 /* Save the area behind the sprite */ | 300 |
295 updates[1] = position; | 301 /* Save the area behind the sprite */ |
296 SDL_BlitSurface(screen, &updates[1], backing, NULL); | 302 updates[1] = position; |
297 | 303 SDL_BlitSurface(screen, &updates[1], backing, NULL); |
298 /* Blit the sprite onto the screen */ | 304 |
299 updates[1] = position; | 305 /* Blit the sprite onto the screen */ |
300 SDL_BlitSurface(sprite, NULL, screen, &updates[1]); | 306 updates[1] = position; |
301 | 307 SDL_BlitSurface(sprite, NULL, screen, &updates[1]); |
302 /* Make it so! */ | 308 |
303 SDL_UpdateRects(screen, 2, updates); | 309 /* Make it so! */ |
304 } | 310 SDL_UpdateRects(screen, 2, updates); |
305 | 311 } |
306 void WarpSprite(SDL_Surface *screen, int x, int y) | 312 |
307 { | 313 void |
308 SDL_Rect updates[2]; | 314 WarpSprite(SDL_Surface * screen, int x, int y) |
309 | 315 { |
310 /* Erase, move, Draw, update */ | 316 SDL_Rect updates[2]; |
311 updates[0] = position; | 317 |
312 SDL_BlitSurface(backing, NULL, screen, &updates[0]); | 318 /* Erase, move, Draw, update */ |
313 position.x = x-sprite->w/2; /* Center about X */ | 319 updates[0] = position; |
314 position.y = y-sprite->h/2; /* Center about Y */ | 320 SDL_BlitSurface(backing, NULL, screen, &updates[0]); |
315 updates[1] = position; | 321 position.x = x - sprite->w / 2; /* Center about X */ |
316 SDL_BlitSurface(screen, &updates[1], backing, NULL); | 322 position.y = y - sprite->h / 2; /* Center about Y */ |
317 updates[1] = position; | 323 updates[1] = position; |
318 SDL_BlitSurface(sprite, NULL, screen, &updates[1]); | 324 SDL_BlitSurface(screen, &updates[1], backing, NULL); |
319 SDL_UpdateRects(screen, 2, updates); | 325 updates[1] = position; |
320 } | 326 SDL_BlitSurface(sprite, NULL, screen, &updates[1]); |
321 | 327 SDL_UpdateRects(screen, 2, updates); |
322 int main(int argc, char *argv[]) | 328 } |
323 { | 329 |
324 const SDL_VideoInfo *info; | 330 int |
325 SDL_Surface *screen; | 331 main(int argc, char *argv[]) |
326 int w, h; | 332 { |
327 Uint8 video_bpp; | 333 const SDL_VideoInfo *info; |
328 Uint32 videoflags; | 334 SDL_Surface *screen; |
329 int i, done; | 335 int w, h; |
330 SDL_Event event; | 336 Uint8 video_bpp; |
331 SDL_Surface *light; | 337 Uint32 videoflags; |
332 int mouse_pressed; | 338 int i, done; |
333 Uint32 ticks, lastticks; | 339 SDL_Event event; |
334 | 340 SDL_Surface *light; |
335 | 341 int mouse_pressed; |
336 /* Initialize SDL */ | 342 Uint32 ticks, lastticks; |
337 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { | 343 |
338 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); | 344 |
339 return(1); | 345 /* Initialize SDL */ |
340 } | 346 if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
341 | 347 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
342 /* Alpha blending doesn't work well at 8-bit color */ | 348 return (1); |
349 } | |
350 | |
351 /* Alpha blending doesn't work well at 8-bit color */ | |
343 #ifdef _WIN32_WCE | 352 #ifdef _WIN32_WCE |
344 /* Pocket PC */ | 353 /* Pocket PC */ |
345 w = 240; | 354 w = 240; |
346 h = 320; | 355 h = 320; |
347 #else | 356 #else |
348 w = 640; | 357 w = 640; |
349 h = 480; | 358 h = 480; |
350 #endif | 359 #endif |
351 info = SDL_GetVideoInfo(); | 360 info = SDL_GetVideoInfo(); |
352 if ( info->vfmt->BitsPerPixel > 8 ) { | 361 if (info->vfmt->BitsPerPixel > 8) { |
353 video_bpp = info->vfmt->BitsPerPixel; | 362 video_bpp = info->vfmt->BitsPerPixel; |
354 } else { | 363 } else { |
355 video_bpp = 16; | 364 video_bpp = 16; |
365 fprintf(stderr, "forced 16 bpp mode\n"); | |
366 } | |
367 videoflags = SDL_SWSURFACE; | |
368 for (i = 1; argv[i]; ++i) { | |
369 if (strcmp(argv[i], "-bpp") == 0) { | |
370 video_bpp = atoi(argv[++i]); | |
371 if (video_bpp <= 8) { | |
372 video_bpp = 16; | |
356 fprintf(stderr, "forced 16 bpp mode\n"); | 373 fprintf(stderr, "forced 16 bpp mode\n"); |
357 } | 374 } |
358 videoflags = SDL_SWSURFACE; | 375 } else if (strcmp(argv[i], "-hw") == 0) { |
359 for ( i = 1; argv[i]; ++i ) { | 376 videoflags |= SDL_HWSURFACE; |
360 if ( strcmp(argv[i], "-bpp") == 0 ) { | 377 } else if (strcmp(argv[i], "-warp") == 0) { |
361 video_bpp = atoi(argv[++i]); | 378 videoflags |= SDL_HWPALETTE; |
362 if (video_bpp<=8) { | 379 } else if (strcmp(argv[i], "-width") == 0 && argv[i + 1]) { |
363 video_bpp=16; | 380 w = atoi(argv[++i]); |
364 fprintf(stderr, "forced 16 bpp mode\n"); | 381 } else if (strcmp(argv[i], "-height") == 0 && argv[i + 1]) { |
365 } | 382 h = atoi(argv[++i]); |
366 } else | 383 } else if (strcmp(argv[i], "-resize") == 0) { |
367 if ( strcmp(argv[i], "-hw") == 0 ) { | 384 videoflags |= SDL_RESIZABLE; |
368 videoflags |= SDL_HWSURFACE; | 385 } else if (strcmp(argv[i], "-noframe") == 0) { |
369 } else | 386 videoflags |= SDL_NOFRAME; |
370 if ( strcmp(argv[i], "-warp") == 0 ) { | 387 } else if (strcmp(argv[i], "-fullscreen") == 0) { |
371 videoflags |= SDL_HWPALETTE; | 388 videoflags |= SDL_FULLSCREEN; |
372 } else | 389 } else { |
373 if ( strcmp(argv[i], "-width") == 0 && argv[i+1] ) { | 390 fprintf(stderr, |
374 w = atoi(argv[++i]); | 391 "Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n", |
375 } else | 392 argv[0]); |
376 if ( strcmp(argv[i], "-height") == 0 && argv[i+1] ) { | 393 quit(1); |
377 h = atoi(argv[++i]); | 394 } |
378 } else | 395 } |
379 if ( strcmp(argv[i], "-resize") == 0 ) { | 396 |
380 videoflags |= SDL_RESIZABLE; | 397 /* Set video mode */ |
381 } else | 398 if ((screen = SDL_SetVideoMode(w, h, video_bpp, videoflags)) == NULL) { |
382 if ( strcmp(argv[i], "-noframe") == 0 ) { | 399 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", |
383 videoflags |= SDL_NOFRAME; | 400 w, h, video_bpp, SDL_GetError()); |
384 } else | 401 quit(2); |
385 if ( strcmp(argv[i], "-fullscreen") == 0 ) { | 402 } |
386 videoflags |= SDL_FULLSCREEN; | 403 FillBackground(screen); |
387 } else { | 404 |
388 fprintf(stderr, | 405 /* Create the light */ |
389 "Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n", | 406 light = CreateLight(82); |
390 argv[0]); | 407 if (light == NULL) { |
391 quit(1); | 408 quit(1); |
392 } | 409 } |
393 } | 410 |
394 | 411 /* Load the sprite */ |
395 /* Set video mode */ | 412 if (LoadSprite(screen, "icon.bmp") < 0) { |
396 if ( (screen=SDL_SetVideoMode(w,h,video_bpp,videoflags)) == NULL ) { | 413 SDL_FreeSurface(light); |
397 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", | 414 quit(1); |
398 w, h, video_bpp, SDL_GetError()); | 415 } |
399 quit(2); | 416 |
400 } | 417 /* Print out information about our surfaces */ |
401 FillBackground(screen); | 418 printf("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel); |
402 | 419 if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) { |
403 /* Create the light */ | 420 printf("Screen is in video memory\n"); |
404 light = CreateLight(82); | 421 } else { |
405 if ( light == NULL ) { | 422 printf("Screen is in system memory\n"); |
406 quit(1); | 423 } |
407 } | 424 if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) { |
408 | 425 printf("Screen has double-buffering enabled\n"); |
409 /* Load the sprite */ | 426 } |
410 if ( LoadSprite(screen, "icon.bmp") < 0 ) { | 427 if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) { |
411 SDL_FreeSurface(light); | 428 printf("Sprite is in video memory\n"); |
412 quit(1); | 429 } else { |
413 } | 430 printf("Sprite is in system memory\n"); |
414 | 431 } |
415 /* Print out information about our surfaces */ | 432 |
416 printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel); | 433 /* Run a sample blit to trigger blit acceleration */ |
417 if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { | 434 MoveSprite(screen, NULL); |
418 printf("Screen is in video memory\n"); | 435 if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) { |
419 } else { | 436 printf("Sprite blit uses hardware alpha acceleration\n"); |
420 printf("Screen is in system memory\n"); | 437 } else { |
421 } | 438 printf("Sprite blit dosn't uses hardware alpha acceleration\n"); |
422 if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) { | 439 } |
423 printf("Screen has double-buffering enabled\n"); | 440 |
424 } | 441 /* Set a clipping rectangle to clip the outside edge of the screen */ |
425 if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { | 442 { |
426 printf("Sprite is in video memory\n"); | 443 SDL_Rect clip; |
427 } else { | 444 clip.x = 32; |
428 printf("Sprite is in system memory\n"); | 445 clip.y = 32; |
429 } | 446 clip.w = screen->w - (2 * 32); |
430 | 447 clip.h = screen->h - (2 * 32); |
431 /* Run a sample blit to trigger blit acceleration */ | 448 SDL_SetClipRect(screen, &clip); |
432 MoveSprite(screen, NULL); | 449 } |
433 if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) { | 450 |
434 printf("Sprite blit uses hardware alpha acceleration\n"); | 451 /* Wait for a keystroke */ |
435 } else { | 452 lastticks = SDL_GetTicks(); |
436 printf("Sprite blit dosn't uses hardware alpha acceleration\n"); | 453 done = 0; |
437 } | 454 mouse_pressed = 0; |
438 | 455 while (!done) { |
439 /* Set a clipping rectangle to clip the outside edge of the screen */ | 456 /* Update the frame -- move the sprite */ |
440 { SDL_Rect clip; | 457 if (mouse_pressed) { |
441 clip.x = 32; | 458 MoveSprite(screen, light); |
442 clip.y = 32; | 459 mouse_pressed = 0; |
443 clip.w = screen->w-(2*32); | 460 } else { |
444 clip.h = screen->h-(2*32); | 461 MoveSprite(screen, NULL); |
445 SDL_SetClipRect(screen, &clip); | 462 } |
446 } | 463 |
447 | 464 /* Slow down the loop to 30 frames/second */ |
448 /* Wait for a keystroke */ | 465 ticks = SDL_GetTicks(); |
449 lastticks = SDL_GetTicks(); | 466 if ((ticks - lastticks) < FRAME_TICKS) { |
450 done = 0; | |
451 mouse_pressed = 0; | |
452 while ( !done ) { | |
453 /* Update the frame -- move the sprite */ | |
454 if ( mouse_pressed ) { | |
455 MoveSprite(screen, light); | |
456 mouse_pressed = 0; | |
457 } else { | |
458 MoveSprite(screen, NULL); | |
459 } | |
460 | |
461 /* Slow down the loop to 30 frames/second */ | |
462 ticks = SDL_GetTicks(); | |
463 if ( (ticks-lastticks) < FRAME_TICKS ) { | |
464 #ifdef CHECK_SLEEP_GRANULARITY | 467 #ifdef CHECK_SLEEP_GRANULARITY |
465 fprintf(stderr, "Sleeping %d ticks\n", FRAME_TICKS-(ticks-lastticks)); | 468 fprintf(stderr, "Sleeping %d ticks\n", |
469 FRAME_TICKS - (ticks - lastticks)); | |
466 #endif | 470 #endif |
467 SDL_Delay(FRAME_TICKS-(ticks-lastticks)); | 471 SDL_Delay(FRAME_TICKS - (ticks - lastticks)); |
468 #ifdef CHECK_SLEEP_GRANULARITY | 472 #ifdef CHECK_SLEEP_GRANULARITY |
469 fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks()-ticks)); | 473 fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks() - ticks)); |
470 #endif | 474 #endif |
471 } | 475 } |
472 lastticks = ticks; | 476 lastticks = ticks; |
473 | 477 |
474 /* Check for events */ | 478 /* Check for events */ |
475 while ( SDL_PollEvent(&event) ) { | 479 while (SDL_PollEvent(&event)) { |
476 switch (event.type) { | 480 switch (event.type) { |
477 case SDL_VIDEORESIZE: | 481 case SDL_VIDEORESIZE: |
478 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, video_bpp, videoflags); | 482 screen = |
479 if ( screen ) { | 483 SDL_SetVideoMode(event.resize.w, event.resize.h, |
480 FillBackground(screen); | 484 video_bpp, videoflags); |
481 } | 485 if (screen) { |
482 break; | 486 FillBackground(screen); |
483 /* Attract sprite while mouse is held down */ | 487 } |
484 case SDL_MOUSEMOTION: | 488 break; |
485 if (event.motion.state != 0) { | 489 /* Attract sprite while mouse is held down */ |
486 AttractSprite(event.motion.x, | 490 case SDL_MOUSEMOTION: |
487 event.motion.y); | 491 if (event.motion.state != 0) { |
488 mouse_pressed = 1; | 492 AttractSprite(event.motion.x, event.motion.y); |
489 } | 493 mouse_pressed = 1; |
490 break; | 494 } |
491 case SDL_MOUSEBUTTONDOWN: | 495 break; |
492 if ( event.button.button == 1 ) { | 496 case SDL_MOUSEBUTTONDOWN: |
493 AttractSprite(event.button.x, | 497 if (event.button.button == 1) { |
494 event.button.y); | 498 AttractSprite(event.button.x, event.button.y); |
495 mouse_pressed = 1; | 499 mouse_pressed = 1; |
496 } else { | 500 } else { |
497 SDL_Rect area; | 501 SDL_Rect area; |
498 | 502 |
499 area.x = event.button.x-16; | 503 area.x = event.button.x - 16; |
500 area.y = event.button.y-16; | 504 area.y = event.button.y - 16; |
501 area.w = 32; | 505 area.w = 32; |
502 area.h = 32; | 506 area.h = 32; |
503 SDL_FillRect(screen, &area, 0); | 507 SDL_FillRect(screen, &area, 0); |
504 SDL_UpdateRects(screen,1,&area); | 508 SDL_UpdateRects(screen, 1, &area); |
505 } | 509 } |
506 break; | 510 break; |
507 case SDL_KEYDOWN: | 511 case SDL_KEYDOWN: |
508 if ( event.key.keysym.sym == SDLK_ESCAPE ) { | 512 if (event.key.keysym.sym == SDLK_ESCAPE) { |
509 done = 1; | 513 done = 1; |
510 } | 514 } |
511 break; | 515 break; |
512 case SDL_QUIT: | 516 case SDL_QUIT: |
513 done = 1; | 517 done = 1; |
514 break; | 518 break; |
515 default: | 519 default: |
516 break; | 520 break; |
517 } | 521 } |
518 } | 522 } |
519 } | 523 } |
520 SDL_FreeSurface(light); | 524 SDL_FreeSurface(light); |
521 SDL_FreeSurface(sprite); | 525 SDL_FreeSurface(sprite); |
522 SDL_FreeSurface(backing); | 526 SDL_FreeSurface(backing); |
523 | 527 |
524 /* Print out some timing information */ | 528 /* Print out some timing information */ |
525 if ( flashes > 0 ) { | 529 if (flashes > 0) { |
526 printf("%d alpha blits, ~%4.4f ms per blit\n", | 530 printf("%d alpha blits, ~%4.4f ms per blit\n", |
527 flashes, (float)flashtime/flashes); | 531 flashes, (float) flashtime / flashes); |
528 } | 532 } |
529 | 533 |
530 SDL_Quit(); | 534 SDL_Quit(); |
531 return(0); | 535 return (0); |
532 } | 536 } |