comparison test/testalpha.c @ 1668:4da1ee79c9af SDL-1.3

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