test/testalpha.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
     1.1 --- a/test/testalpha.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/test/testalpha.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -10,523 +10,529 @@
     1.4  
     1.5  #include "SDL.h"
     1.6  
     1.7 -#define FRAME_TICKS	(1000/30)		/* 30 frames/second */
     1.8 +#define FRAME_TICKS	(1000/30)       /* 30 frames/second */
     1.9  
    1.10  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.11 -static void quit(int rc)
    1.12 +static void
    1.13 +quit (int rc)
    1.14  {
    1.15 -	SDL_Quit();
    1.16 -	exit(rc);
    1.17 +    SDL_Quit ();
    1.18 +    exit (rc);
    1.19  }
    1.20  
    1.21  /* Fill the screen with a gradient */
    1.22 -static void FillBackground(SDL_Surface *screen)
    1.23 +static void
    1.24 +FillBackground (SDL_Surface * screen)
    1.25  {
    1.26 -	Uint8 *buffer;
    1.27 -	Uint16 *buffer16;
    1.28 -        Uint16 color;
    1.29 -        Uint8  gradient;
    1.30 -	int    i, k;
    1.31 +    Uint8 *buffer;
    1.32 +    Uint16 *buffer16;
    1.33 +    Uint16 color;
    1.34 +    Uint8 gradient;
    1.35 +    int i, k;
    1.36  
    1.37 -	/* Set the surface pixels and refresh! */
    1.38 -	if ( SDL_LockSurface(screen) < 0 ) {
    1.39 -		fprintf(stderr, "Couldn't lock the display surface: %s\n",
    1.40 -							SDL_GetError());
    1.41 -		quit(2);
    1.42 -	}
    1.43 -	buffer=(Uint8 *)screen->pixels;
    1.44 -	if (screen->format->BytesPerPixel!=2) {
    1.45 -		for ( i=0; i<screen->h; ++i ) {
    1.46 -			memset(buffer,(i*255)/screen->h, screen->w*screen->format->BytesPerPixel);
    1.47 -			buffer += screen->pitch;
    1.48 -		}
    1.49 -	}
    1.50 -        else
    1.51 -        {
    1.52 -		for ( i=0; i<screen->h; ++i ) {
    1.53 -			gradient=((i*255)/screen->h);
    1.54 -                        color = (Uint16)SDL_MapRGB(screen->format, gradient, gradient, gradient);
    1.55 -                        buffer16=(Uint16*)buffer;
    1.56 -                        for (k=0; k<screen->w; k++)
    1.57 -                        {
    1.58 -                            *(buffer16+k)=color;
    1.59 -                        }
    1.60 -			buffer += screen->pitch;
    1.61 -		}
    1.62 +    /* Set the surface pixels and refresh! */
    1.63 +    if (SDL_LockSurface (screen) < 0) {
    1.64 +        fprintf (stderr, "Couldn't lock the display surface: %s\n",
    1.65 +                 SDL_GetError ());
    1.66 +        quit (2);
    1.67 +    }
    1.68 +    buffer = (Uint8 *) screen->pixels;
    1.69 +    if (screen->format->BytesPerPixel != 2) {
    1.70 +        for (i = 0; i < screen->h; ++i) {
    1.71 +            memset (buffer, (i * 255) / screen->h,
    1.72 +                    screen->w * screen->format->BytesPerPixel);
    1.73 +            buffer += screen->pitch;
    1.74          }
    1.75 +    } else {
    1.76 +        for (i = 0; i < screen->h; ++i) {
    1.77 +            gradient = ((i * 255) / screen->h);
    1.78 +            color =
    1.79 +                (Uint16) SDL_MapRGB (screen->format, gradient, gradient,
    1.80 +                                     gradient);
    1.81 +            buffer16 = (Uint16 *) buffer;
    1.82 +            for (k = 0; k < screen->w; k++) {
    1.83 +                *(buffer16 + k) = color;
    1.84 +            }
    1.85 +            buffer += screen->pitch;
    1.86 +        }
    1.87 +    }
    1.88  
    1.89 -	SDL_UnlockSurface(screen);
    1.90 -	SDL_UpdateRect(screen, 0, 0, 0, 0);
    1.91 +    SDL_UnlockSurface (screen);
    1.92 +    SDL_UpdateRect (screen, 0, 0, 0, 0);
    1.93  }
    1.94  
    1.95  /* Create a "light" -- a yellowish surface with variable alpha */
    1.96 -SDL_Surface *CreateLight(int radius)
    1.97 +SDL_Surface *
    1.98 +CreateLight (int radius)
    1.99  {
   1.100 -	Uint8  trans, alphamask;
   1.101 -	int    range, addition;
   1.102 -	int    xdist, ydist;
   1.103 -	Uint16 x, y;
   1.104 -	Uint16 skip;
   1.105 -	Uint32 pixel;
   1.106 -	SDL_Surface *light;
   1.107 +    Uint8 trans, alphamask;
   1.108 +    int range, addition;
   1.109 +    int xdist, ydist;
   1.110 +    Uint16 x, y;
   1.111 +    Uint16 skip;
   1.112 +    Uint32 pixel;
   1.113 +    SDL_Surface *light;
   1.114  
   1.115  #ifdef LIGHT_16BIT
   1.116 -	Uint16 *buf;
   1.117 +    Uint16 *buf;
   1.118  
   1.119 -	/* Create a 16 (4/4/4/4) bpp square with a full 4-bit alpha channel */
   1.120 -	/* Note: this isn't any faster than a 32 bit alpha surface */
   1.121 -	alphamask = 0x0000000F;
   1.122 -	light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*radius, 2*radius, 16,
   1.123 -			0x0000F000, 0x00000F00, 0x000000F0, alphamask);
   1.124 +    /* Create a 16 (4/4/4/4) bpp square with a full 4-bit alpha channel */
   1.125 +    /* Note: this isn't any faster than a 32 bit alpha surface */
   1.126 +    alphamask = 0x0000000F;
   1.127 +    light = SDL_CreateRGBSurface (SDL_SWSURFACE, 2 * radius, 2 * radius, 16,
   1.128 +                                  0x0000F000, 0x00000F00, 0x000000F0,
   1.129 +                                  alphamask);
   1.130  #else
   1.131 -	Uint32 *buf;
   1.132 +    Uint32 *buf;
   1.133  
   1.134 -	/* Create a 32 (8/8/8/8) bpp square with a full 8-bit alpha channel */
   1.135 -	alphamask = 0x000000FF;
   1.136 -	light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*radius, 2*radius, 32,
   1.137 -			0xFF000000, 0x00FF0000, 0x0000FF00, alphamask);
   1.138 -	if ( light == NULL ) {
   1.139 -		fprintf(stderr, "Couldn't create light: %s\n", SDL_GetError());
   1.140 -		return(NULL);
   1.141 -	}
   1.142 +    /* Create a 32 (8/8/8/8) bpp square with a full 8-bit alpha channel */
   1.143 +    alphamask = 0x000000FF;
   1.144 +    light = SDL_CreateRGBSurface (SDL_SWSURFACE, 2 * radius, 2 * radius, 32,
   1.145 +                                  0xFF000000, 0x00FF0000, 0x0000FF00,
   1.146 +                                  alphamask);
   1.147 +    if (light == NULL) {
   1.148 +        fprintf (stderr, "Couldn't create light: %s\n", SDL_GetError ());
   1.149 +        return (NULL);
   1.150 +    }
   1.151  #endif
   1.152  
   1.153 -	/* Fill with a light yellow-orange color */
   1.154 -	skip = light->pitch-(light->w*light->format->BytesPerPixel);
   1.155 +    /* Fill with a light yellow-orange color */
   1.156 +    skip = light->pitch - (light->w * light->format->BytesPerPixel);
   1.157  #ifdef LIGHT_16BIT
   1.158 -	buf = (Uint16 *)light->pixels;
   1.159 +    buf = (Uint16 *) light->pixels;
   1.160  #else
   1.161 -	buf = (Uint32 *)light->pixels;
   1.162 +    buf = (Uint32 *) light->pixels;
   1.163  #endif
   1.164 -        /* Get a tranparent pixel value - we'll add alpha later */
   1.165 -	pixel = SDL_MapRGBA(light->format, 0xFF, 0xDD, 0x88, 0);
   1.166 -	for ( y=0; y<light->h; ++y ) {
   1.167 -		for ( x=0; x<light->w; ++x ) {
   1.168 -			*buf++ = pixel;
   1.169 -		}
   1.170 -		buf += skip;	/* Almost always 0, but just in case... */
   1.171 -	}
   1.172 +    /* Get a tranparent pixel value - we'll add alpha later */
   1.173 +    pixel = SDL_MapRGBA (light->format, 0xFF, 0xDD, 0x88, 0);
   1.174 +    for (y = 0; y < light->h; ++y) {
   1.175 +        for (x = 0; x < light->w; ++x) {
   1.176 +            *buf++ = pixel;
   1.177 +        }
   1.178 +        buf += skip;            /* Almost always 0, but just in case... */
   1.179 +    }
   1.180  
   1.181 -	/* Calculate alpha values for the surface. */
   1.182 +    /* Calculate alpha values for the surface. */
   1.183  #ifdef LIGHT_16BIT
   1.184 -	buf = (Uint16 *)light->pixels;
   1.185 +    buf = (Uint16 *) light->pixels;
   1.186  #else
   1.187 -	buf = (Uint32 *)light->pixels;
   1.188 +    buf = (Uint32 *) light->pixels;
   1.189  #endif
   1.190 -	for ( y=0; y<light->h; ++y ) {
   1.191 -		for ( x=0; x<light->w; ++x ) {
   1.192 -			/* Slow distance formula (from center of light) */
   1.193 -			xdist = x-(light->w/2);
   1.194 -			ydist = y-(light->h/2);
   1.195 -			range = (int)sqrt(xdist*xdist+ydist*ydist);
   1.196 +    for (y = 0; y < light->h; ++y) {
   1.197 +        for (x = 0; x < light->w; ++x) {
   1.198 +            /* Slow distance formula (from center of light) */
   1.199 +            xdist = x - (light->w / 2);
   1.200 +            ydist = y - (light->h / 2);
   1.201 +            range = (int) sqrt (xdist * xdist + ydist * ydist);
   1.202  
   1.203 -			/* Scale distance to range of transparency (0-255) */
   1.204 -			if ( range > radius ) {
   1.205 -				trans = alphamask;
   1.206 -			} else {
   1.207 -				/* Increasing transparency with distance */
   1.208 -				trans = (Uint8)((range*alphamask)/radius);
   1.209 +            /* Scale distance to range of transparency (0-255) */
   1.210 +            if (range > radius) {
   1.211 +                trans = alphamask;
   1.212 +            } else {
   1.213 +                /* Increasing transparency with distance */
   1.214 +                trans = (Uint8) ((range * alphamask) / radius);
   1.215  
   1.216 -				/* Lights are very transparent */
   1.217 -				addition = (alphamask+1)/8;
   1.218 -				if ( (int)trans+addition > alphamask ) {
   1.219 -					trans = alphamask;
   1.220 -				} else {
   1.221 -					trans += addition;
   1.222 -				}
   1.223 -			}
   1.224 -			/* We set the alpha component as the right N bits */
   1.225 -			*buf++ |= (255-trans);
   1.226 -		}
   1.227 -		buf += skip;	/* Almost always 0, but just in case... */
   1.228 -	}
   1.229 -	/* Enable RLE acceleration of this alpha surface */
   1.230 -	SDL_SetAlpha(light, SDL_SRCALPHA|SDL_RLEACCEL, 0);
   1.231 +                /* Lights are very transparent */
   1.232 +                addition = (alphamask + 1) / 8;
   1.233 +                if ((int) trans + addition > alphamask) {
   1.234 +                    trans = alphamask;
   1.235 +                } else {
   1.236 +                    trans += addition;
   1.237 +                }
   1.238 +            }
   1.239 +            /* We set the alpha component as the right N bits */
   1.240 +            *buf++ |= (255 - trans);
   1.241 +        }
   1.242 +        buf += skip;            /* Almost always 0, but just in case... */
   1.243 +    }
   1.244 +    /* Enable RLE acceleration of this alpha surface */
   1.245 +    SDL_SetAlpha (light, SDL_SRCALPHA | SDL_RLEACCEL, 0);
   1.246  
   1.247 -	/* We're done! */
   1.248 -	return(light);
   1.249 +    /* We're done! */
   1.250 +    return (light);
   1.251  }
   1.252  
   1.253  static Uint32 flashes = 0;
   1.254  static Uint32 flashtime = 0;
   1.255  
   1.256 -void FlashLight(SDL_Surface *screen, SDL_Surface *light, int x, int y)
   1.257 +void
   1.258 +FlashLight (SDL_Surface * screen, SDL_Surface * light, int x, int y)
   1.259  {
   1.260 -	SDL_Rect position;
   1.261 -	Uint32   ticks1;
   1.262 -	Uint32   ticks2;
   1.263 +    SDL_Rect position;
   1.264 +    Uint32 ticks1;
   1.265 +    Uint32 ticks2;
   1.266  
   1.267 -	/* Easy, center light */
   1.268 -	position.x = x-(light->w/2);
   1.269 -	position.y = y-(light->h/2);
   1.270 -	position.w = light->w;
   1.271 -	position.h = light->h;
   1.272 -	ticks1 = SDL_GetTicks();
   1.273 -	SDL_BlitSurface(light, NULL, screen, &position);
   1.274 -	ticks2 = SDL_GetTicks();
   1.275 -	SDL_UpdateRects(screen, 1, &position);
   1.276 -	++flashes;
   1.277 +    /* Easy, center light */
   1.278 +    position.x = x - (light->w / 2);
   1.279 +    position.y = y - (light->h / 2);
   1.280 +    position.w = light->w;
   1.281 +    position.h = light->h;
   1.282 +    ticks1 = SDL_GetTicks ();
   1.283 +    SDL_BlitSurface (light, NULL, screen, &position);
   1.284 +    ticks2 = SDL_GetTicks ();
   1.285 +    SDL_UpdateRects (screen, 1, &position);
   1.286 +    ++flashes;
   1.287  
   1.288 -	/* Update time spend doing alpha blitting */
   1.289 -	flashtime += (ticks2-ticks1);
   1.290 +    /* Update time spend doing alpha blitting */
   1.291 +    flashtime += (ticks2 - ticks1);
   1.292  }
   1.293  
   1.294  static int sprite_visible = 0;
   1.295  static SDL_Surface *sprite;
   1.296  static SDL_Surface *backing;
   1.297 -static SDL_Rect    position;
   1.298 -static int         x_vel, y_vel;
   1.299 -static int	   alpha_vel;
   1.300 +static SDL_Rect position;
   1.301 +static int x_vel, y_vel;
   1.302 +static int alpha_vel;
   1.303  
   1.304 -int LoadSprite(SDL_Surface *screen, char *file)
   1.305 +int
   1.306 +LoadSprite (SDL_Surface * screen, char *file)
   1.307  {
   1.308 -	SDL_Surface *converted;
   1.309 -
   1.310 -	/* Load the sprite image */
   1.311 -	sprite = SDL_LoadBMP(file);
   1.312 -	if ( sprite == NULL ) {
   1.313 -		fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
   1.314 -		return(-1);
   1.315 -	}
   1.316 +    SDL_Surface *converted;
   1.317  
   1.318 -	/* Set transparent pixel as the pixel at (0,0) */
   1.319 -	if ( sprite->format->palette ) {
   1.320 -		SDL_SetColorKey(sprite, SDL_SRCCOLORKEY,
   1.321 -						*(Uint8 *)sprite->pixels);
   1.322 -	}
   1.323 +    /* Load the sprite image */
   1.324 +    sprite = SDL_LoadBMP (file);
   1.325 +    if (sprite == NULL) {
   1.326 +        fprintf (stderr, "Couldn't load %s: %s", file, SDL_GetError ());
   1.327 +        return (-1);
   1.328 +    }
   1.329  
   1.330 -	/* Convert sprite to video format */
   1.331 -	converted = SDL_DisplayFormat(sprite);
   1.332 -	SDL_FreeSurface(sprite);
   1.333 -	if ( converted == NULL ) {
   1.334 -		fprintf(stderr, "Couldn't convert background: %s\n",
   1.335 -							SDL_GetError());
   1.336 -		return(-1);
   1.337 -	}
   1.338 -	sprite = converted;
   1.339 +    /* Set transparent pixel as the pixel at (0,0) */
   1.340 +    if (sprite->format->palette) {
   1.341 +        SDL_SetColorKey (sprite, SDL_SRCCOLORKEY, *(Uint8 *) sprite->pixels);
   1.342 +    }
   1.343 +
   1.344 +    /* Convert sprite to video format */
   1.345 +    converted = SDL_DisplayFormat (sprite);
   1.346 +    SDL_FreeSurface (sprite);
   1.347 +    if (converted == NULL) {
   1.348 +        fprintf (stderr, "Couldn't convert background: %s\n",
   1.349 +                 SDL_GetError ());
   1.350 +        return (-1);
   1.351 +    }
   1.352 +    sprite = converted;
   1.353  
   1.354 -	/* Create the background */
   1.355 -	backing = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite->w, sprite->h, 8,
   1.356 -								0, 0, 0, 0);
   1.357 -	if ( backing == NULL ) {
   1.358 -		fprintf(stderr, "Couldn't create background: %s\n",
   1.359 -							SDL_GetError());
   1.360 -		SDL_FreeSurface(sprite);
   1.361 -		return(-1);
   1.362 -	}
   1.363 +    /* Create the background */
   1.364 +    backing = SDL_CreateRGBSurface (SDL_SWSURFACE, sprite->w, sprite->h, 8,
   1.365 +                                    0, 0, 0, 0);
   1.366 +    if (backing == NULL) {
   1.367 +        fprintf (stderr, "Couldn't create background: %s\n", SDL_GetError ());
   1.368 +        SDL_FreeSurface (sprite);
   1.369 +        return (-1);
   1.370 +    }
   1.371  
   1.372 -	/* Convert background to video format */
   1.373 -	converted = SDL_DisplayFormat(backing);
   1.374 -	SDL_FreeSurface(backing);
   1.375 -	if ( converted == NULL ) {
   1.376 -		fprintf(stderr, "Couldn't convert background: %s\n",
   1.377 -							SDL_GetError());
   1.378 -		SDL_FreeSurface(sprite);
   1.379 -		return(-1);
   1.380 -	}
   1.381 -	backing = converted;
   1.382 +    /* Convert background to video format */
   1.383 +    converted = SDL_DisplayFormat (backing);
   1.384 +    SDL_FreeSurface (backing);
   1.385 +    if (converted == NULL) {
   1.386 +        fprintf (stderr, "Couldn't convert background: %s\n",
   1.387 +                 SDL_GetError ());
   1.388 +        SDL_FreeSurface (sprite);
   1.389 +        return (-1);
   1.390 +    }
   1.391 +    backing = converted;
   1.392  
   1.393 -	/* Set the initial position of the sprite */
   1.394 -	position.x = (screen->w-sprite->w)/2;
   1.395 -	position.y = (screen->h-sprite->h)/2;
   1.396 -	position.w = sprite->w;
   1.397 -	position.h = sprite->h;
   1.398 -	x_vel = 0; y_vel = 0;
   1.399 -	alpha_vel = 1;
   1.400 +    /* Set the initial position of the sprite */
   1.401 +    position.x = (screen->w - sprite->w) / 2;
   1.402 +    position.y = (screen->h - sprite->h) / 2;
   1.403 +    position.w = sprite->w;
   1.404 +    position.h = sprite->h;
   1.405 +    x_vel = 0;
   1.406 +    y_vel = 0;
   1.407 +    alpha_vel = 1;
   1.408  
   1.409 -	/* We're ready to roll. :) */
   1.410 -	return(0);
   1.411 +    /* We're ready to roll. :) */
   1.412 +    return (0);
   1.413  }
   1.414  
   1.415 -void AttractSprite(Uint16 x, Uint16 y)
   1.416 +void
   1.417 +AttractSprite (Uint16 x, Uint16 y)
   1.418  {
   1.419 -	x_vel = ((int)x-position.x)/10;
   1.420 -	y_vel = ((int)y-position.y)/10;
   1.421 +    x_vel = ((int) x - position.x) / 10;
   1.422 +    y_vel = ((int) y - position.y) / 10;
   1.423  }
   1.424  
   1.425 -void MoveSprite(SDL_Surface *screen, SDL_Surface *light)
   1.426 +void
   1.427 +MoveSprite (SDL_Surface * screen, SDL_Surface * light)
   1.428  {
   1.429 -	SDL_Rect updates[2];
   1.430 -	int alpha;
   1.431 +    SDL_Rect updates[2];
   1.432 +    int alpha;
   1.433  
   1.434 -	/* Erase the sprite if it was visible */
   1.435 -	if ( sprite_visible ) {
   1.436 -		updates[0] = position;
   1.437 -		SDL_BlitSurface(backing, NULL, screen, &updates[0]);
   1.438 -	} else {
   1.439 -		updates[0].x = 0; updates[0].y = 0;
   1.440 -		updates[0].w = 0; updates[0].h = 0;
   1.441 -		sprite_visible = 1;
   1.442 -	}
   1.443 +    /* Erase the sprite if it was visible */
   1.444 +    if (sprite_visible) {
   1.445 +        updates[0] = position;
   1.446 +        SDL_BlitSurface (backing, NULL, screen, &updates[0]);
   1.447 +    } else {
   1.448 +        updates[0].x = 0;
   1.449 +        updates[0].y = 0;
   1.450 +        updates[0].w = 0;
   1.451 +        updates[0].h = 0;
   1.452 +        sprite_visible = 1;
   1.453 +    }
   1.454  
   1.455 -	/* Since the sprite is off the screen, we can do other drawing
   1.456 -	   without being overwritten by the saved area behind the sprite.
   1.457 -	 */
   1.458 -	if ( light != NULL ) {
   1.459 -		int x, y;
   1.460 +    /* Since the sprite is off the screen, we can do other drawing
   1.461 +       without being overwritten by the saved area behind the sprite.
   1.462 +     */
   1.463 +    if (light != NULL) {
   1.464 +        int x, y;
   1.465 +
   1.466 +        SDL_GetMouseState (&x, &y);
   1.467 +        FlashLight (screen, light, x, y);
   1.468 +    }
   1.469  
   1.470 -		SDL_GetMouseState(&x, &y);
   1.471 -		FlashLight(screen, light, x, y);
   1.472 -	}
   1.473 -	   
   1.474 -	/* Move the sprite, bounce at the wall */
   1.475 -	position.x += x_vel;
   1.476 -	if ( (position.x < 0) || (position.x >= screen->w) ) {
   1.477 -		x_vel = -x_vel;
   1.478 -		position.x += x_vel;
   1.479 -	}
   1.480 -	position.y += y_vel;
   1.481 -	if ( (position.y < 0) || (position.y >= screen->h) ) {
   1.482 -		y_vel = -y_vel;
   1.483 -		position.y += y_vel;
   1.484 -	}
   1.485 +    /* Move the sprite, bounce at the wall */
   1.486 +    position.x += x_vel;
   1.487 +    if ((position.x < 0) || (position.x >= screen->w)) {
   1.488 +        x_vel = -x_vel;
   1.489 +        position.x += x_vel;
   1.490 +    }
   1.491 +    position.y += y_vel;
   1.492 +    if ((position.y < 0) || (position.y >= screen->h)) {
   1.493 +        y_vel = -y_vel;
   1.494 +        position.y += y_vel;
   1.495 +    }
   1.496  
   1.497 -	/* Update transparency (fade in and out) */
   1.498 -	alpha = sprite->format->alpha;
   1.499 -	if ( (alpha+alpha_vel) < 0 ) {
   1.500 -		alpha_vel = -alpha_vel;
   1.501 -	} else
   1.502 -	if ( (alpha+alpha_vel) > 255 ) {
   1.503 -		alpha_vel = -alpha_vel;
   1.504 -	}
   1.505 -	SDL_SetAlpha(sprite, SDL_SRCALPHA, (Uint8)(alpha+alpha_vel));
   1.506 +    /* Update transparency (fade in and out) */
   1.507 +    alpha = sprite->format->alpha;
   1.508 +    if ((alpha + alpha_vel) < 0) {
   1.509 +        alpha_vel = -alpha_vel;
   1.510 +    } else if ((alpha + alpha_vel) > 255) {
   1.511 +        alpha_vel = -alpha_vel;
   1.512 +    }
   1.513 +    SDL_SetAlpha (sprite, SDL_SRCALPHA, (Uint8) (alpha + alpha_vel));
   1.514  
   1.515 -	/* Save the area behind the sprite */
   1.516 -	updates[1] = position;
   1.517 -	SDL_BlitSurface(screen, &updates[1], backing, NULL);
   1.518 -	
   1.519 -	/* Blit the sprite onto the screen */
   1.520 -	updates[1] = position;
   1.521 -	SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
   1.522 +    /* Save the area behind the sprite */
   1.523 +    updates[1] = position;
   1.524 +    SDL_BlitSurface (screen, &updates[1], backing, NULL);
   1.525  
   1.526 -	/* Make it so! */
   1.527 -	SDL_UpdateRects(screen, 2, updates);
   1.528 +    /* Blit the sprite onto the screen */
   1.529 +    updates[1] = position;
   1.530 +    SDL_BlitSurface (sprite, NULL, screen, &updates[1]);
   1.531 +
   1.532 +    /* Make it so! */
   1.533 +    SDL_UpdateRects (screen, 2, updates);
   1.534  }
   1.535  
   1.536 -void WarpSprite(SDL_Surface *screen, int x, int y)
   1.537 +void
   1.538 +WarpSprite (SDL_Surface * screen, int x, int y)
   1.539  {
   1.540 -	SDL_Rect updates[2];
   1.541 +    SDL_Rect updates[2];
   1.542  
   1.543 -	/* Erase, move, Draw, update */
   1.544 -	updates[0] = position;
   1.545 -	SDL_BlitSurface(backing, NULL, screen, &updates[0]);
   1.546 -	position.x = x-sprite->w/2;	/* Center about X */
   1.547 -	position.y = y-sprite->h/2;	/* Center about Y */
   1.548 -	updates[1] = position;
   1.549 -	SDL_BlitSurface(screen, &updates[1], backing, NULL);
   1.550 -	updates[1] = position;
   1.551 -	SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
   1.552 -	SDL_UpdateRects(screen, 2, updates);
   1.553 +    /* Erase, move, Draw, update */
   1.554 +    updates[0] = position;
   1.555 +    SDL_BlitSurface (backing, NULL, screen, &updates[0]);
   1.556 +    position.x = x - sprite->w / 2;     /* Center about X */
   1.557 +    position.y = y - sprite->h / 2;     /* Center about Y */
   1.558 +    updates[1] = position;
   1.559 +    SDL_BlitSurface (screen, &updates[1], backing, NULL);
   1.560 +    updates[1] = position;
   1.561 +    SDL_BlitSurface (sprite, NULL, screen, &updates[1]);
   1.562 +    SDL_UpdateRects (screen, 2, updates);
   1.563  }
   1.564  
   1.565 -int main(int argc, char *argv[])
   1.566 +int
   1.567 +main (int argc, char *argv[])
   1.568  {
   1.569 -	const SDL_VideoInfo *info;
   1.570 -	SDL_Surface *screen;
   1.571 -	int    w, h;
   1.572 -	Uint8  video_bpp;
   1.573 -	Uint32 videoflags;
   1.574 -	int    i, done;
   1.575 -	SDL_Event event;
   1.576 -	SDL_Surface *light;
   1.577 -	int mouse_pressed;
   1.578 -	Uint32 ticks, lastticks;
   1.579 +    const SDL_VideoInfo *info;
   1.580 +    SDL_Surface *screen;
   1.581 +    int w, h;
   1.582 +    Uint8 video_bpp;
   1.583 +    Uint32 videoflags;
   1.584 +    int i, done;
   1.585 +    SDL_Event event;
   1.586 +    SDL_Surface *light;
   1.587 +    int mouse_pressed;
   1.588 +    Uint32 ticks, lastticks;
   1.589  
   1.590  
   1.591 -	/* Initialize SDL */
   1.592 -	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   1.593 -		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   1.594 -		return(1);
   1.595 -	}
   1.596 +    /* Initialize SDL */
   1.597 +    if (SDL_Init (SDL_INIT_VIDEO) < 0) {
   1.598 +        fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
   1.599 +        return (1);
   1.600 +    }
   1.601  
   1.602 -	/* Alpha blending doesn't work well at 8-bit color */
   1.603 +    /* Alpha blending doesn't work well at 8-bit color */
   1.604  #ifdef _WIN32_WCE
   1.605 -	/* Pocket PC */
   1.606 -	w = 240;
   1.607 -	h = 320;
   1.608 +    /* Pocket PC */
   1.609 +    w = 240;
   1.610 +    h = 320;
   1.611  #else
   1.612 -	w = 640;
   1.613 -	h = 480;
   1.614 +    w = 640;
   1.615 +    h = 480;
   1.616  #endif
   1.617 -	info = SDL_GetVideoInfo();
   1.618 -	if ( info->vfmt->BitsPerPixel > 8 ) {
   1.619 -		video_bpp = info->vfmt->BitsPerPixel;
   1.620 -	} else {
   1.621 -		video_bpp = 16;
   1.622 -                fprintf(stderr, "forced 16 bpp mode\n");
   1.623 -	}
   1.624 -	videoflags = SDL_SWSURFACE;
   1.625 -	for ( i = 1; argv[i]; ++i ) {
   1.626 -		if ( strcmp(argv[i], "-bpp") == 0 ) {
   1.627 -			video_bpp = atoi(argv[++i]);
   1.628 -                        if (video_bpp<=8) {
   1.629 -                            video_bpp=16;
   1.630 -                            fprintf(stderr, "forced 16 bpp mode\n");
   1.631 -                        }
   1.632 -		} else
   1.633 -		if ( strcmp(argv[i], "-hw") == 0 ) {
   1.634 -			videoflags |= SDL_HWSURFACE;
   1.635 -		} else
   1.636 -		if ( strcmp(argv[i], "-warp") == 0 ) {
   1.637 -			videoflags |= SDL_HWPALETTE;
   1.638 -		} else
   1.639 -		if ( strcmp(argv[i], "-width") == 0 && argv[i+1] ) {
   1.640 -			w = atoi(argv[++i]);
   1.641 -		} else
   1.642 -		if ( strcmp(argv[i], "-height") == 0 && argv[i+1] ) {
   1.643 -			h = atoi(argv[++i]);
   1.644 -		} else
   1.645 -		if ( strcmp(argv[i], "-resize") == 0 ) {
   1.646 -			videoflags |= SDL_RESIZABLE;
   1.647 -		} else
   1.648 -		if ( strcmp(argv[i], "-noframe") == 0 ) {
   1.649 -			videoflags |= SDL_NOFRAME;
   1.650 -		} else
   1.651 -		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
   1.652 -			videoflags |= SDL_FULLSCREEN;
   1.653 -		} else {
   1.654 -			fprintf(stderr, 
   1.655 -			"Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n",
   1.656 -								argv[0]);
   1.657 -			quit(1);
   1.658 -		}
   1.659 -	}
   1.660 +    info = SDL_GetVideoInfo ();
   1.661 +    if (info->vfmt->BitsPerPixel > 8) {
   1.662 +        video_bpp = info->vfmt->BitsPerPixel;
   1.663 +    } else {
   1.664 +        video_bpp = 16;
   1.665 +        fprintf (stderr, "forced 16 bpp mode\n");
   1.666 +    }
   1.667 +    videoflags = SDL_SWSURFACE;
   1.668 +    for (i = 1; argv[i]; ++i) {
   1.669 +        if (strcmp (argv[i], "-bpp") == 0) {
   1.670 +            video_bpp = atoi (argv[++i]);
   1.671 +            if (video_bpp <= 8) {
   1.672 +                video_bpp = 16;
   1.673 +                fprintf (stderr, "forced 16 bpp mode\n");
   1.674 +            }
   1.675 +        } else if (strcmp (argv[i], "-hw") == 0) {
   1.676 +            videoflags |= SDL_HWSURFACE;
   1.677 +        } else if (strcmp (argv[i], "-warp") == 0) {
   1.678 +            videoflags |= SDL_HWPALETTE;
   1.679 +        } else if (strcmp (argv[i], "-width") == 0 && argv[i + 1]) {
   1.680 +            w = atoi (argv[++i]);
   1.681 +        } else if (strcmp (argv[i], "-height") == 0 && argv[i + 1]) {
   1.682 +            h = atoi (argv[++i]);
   1.683 +        } else if (strcmp (argv[i], "-resize") == 0) {
   1.684 +            videoflags |= SDL_RESIZABLE;
   1.685 +        } else if (strcmp (argv[i], "-noframe") == 0) {
   1.686 +            videoflags |= SDL_NOFRAME;
   1.687 +        } else if (strcmp (argv[i], "-fullscreen") == 0) {
   1.688 +            videoflags |= SDL_FULLSCREEN;
   1.689 +        } else {
   1.690 +            fprintf (stderr,
   1.691 +                     "Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n",
   1.692 +                     argv[0]);
   1.693 +            quit (1);
   1.694 +        }
   1.695 +    }
   1.696  
   1.697 -	/* Set video mode */
   1.698 -	if ( (screen=SDL_SetVideoMode(w,h,video_bpp,videoflags)) == NULL ) {
   1.699 -		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
   1.700 -						w, h, video_bpp, SDL_GetError());
   1.701 -		quit(2);
   1.702 -	}
   1.703 -	FillBackground(screen);
   1.704 +    /* Set video mode */
   1.705 +    if ((screen = SDL_SetVideoMode (w, h, video_bpp, videoflags)) == NULL) {
   1.706 +        fprintf (stderr, "Couldn't set %dx%dx%d video mode: %s\n",
   1.707 +                 w, h, video_bpp, SDL_GetError ());
   1.708 +        quit (2);
   1.709 +    }
   1.710 +    FillBackground (screen);
   1.711  
   1.712 -	/* Create the light */
   1.713 -	light = CreateLight(82);
   1.714 -	if ( light == NULL ) {
   1.715 -		quit(1);
   1.716 -	}
   1.717 +    /* Create the light */
   1.718 +    light = CreateLight (82);
   1.719 +    if (light == NULL) {
   1.720 +        quit (1);
   1.721 +    }
   1.722 +
   1.723 +    /* Load the sprite */
   1.724 +    if (LoadSprite (screen, "icon.bmp") < 0) {
   1.725 +        SDL_FreeSurface (light);
   1.726 +        quit (1);
   1.727 +    }
   1.728  
   1.729 -	/* Load the sprite */
   1.730 -	if ( LoadSprite(screen, "icon.bmp") < 0 ) {
   1.731 -		SDL_FreeSurface(light);
   1.732 -		quit(1);
   1.733 -	}
   1.734 +    /* Print out information about our surfaces */
   1.735 +    printf ("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel);
   1.736 +    if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
   1.737 +        printf ("Screen is in video memory\n");
   1.738 +    } else {
   1.739 +        printf ("Screen is in system memory\n");
   1.740 +    }
   1.741 +    if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
   1.742 +        printf ("Screen has double-buffering enabled\n");
   1.743 +    }
   1.744 +    if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
   1.745 +        printf ("Sprite is in video memory\n");
   1.746 +    } else {
   1.747 +        printf ("Sprite is in system memory\n");
   1.748 +    }
   1.749  
   1.750 -	/* Print out information about our surfaces */
   1.751 -	printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel);
   1.752 -	if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
   1.753 -		printf("Screen is in video memory\n");
   1.754 -	} else {
   1.755 -		printf("Screen is in system memory\n");
   1.756 -	}
   1.757 -	if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
   1.758 -		printf("Screen has double-buffering enabled\n");
   1.759 -	}
   1.760 -	if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
   1.761 -		printf("Sprite is in video memory\n");
   1.762 -	} else {
   1.763 -		printf("Sprite is in system memory\n");
   1.764 -	}
   1.765 +    /* Run a sample blit to trigger blit acceleration */
   1.766 +    MoveSprite (screen, NULL);
   1.767 +    if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) {
   1.768 +        printf ("Sprite blit uses hardware alpha acceleration\n");
   1.769 +    } else {
   1.770 +        printf ("Sprite blit dosn't uses hardware alpha acceleration\n");
   1.771 +    }
   1.772  
   1.773 -	/* Run a sample blit to trigger blit acceleration */
   1.774 -	MoveSprite(screen, NULL);
   1.775 -	if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
   1.776 -		printf("Sprite blit uses hardware alpha acceleration\n");
   1.777 -	} else {
   1.778 -		printf("Sprite blit dosn't uses hardware alpha acceleration\n");
   1.779 -	}
   1.780 -
   1.781 -	/* Set a clipping rectangle to clip the outside edge of the screen */
   1.782 -	{ SDL_Rect clip;
   1.783 -		clip.x = 32;
   1.784 -		clip.y = 32;
   1.785 -		clip.w = screen->w-(2*32);
   1.786 -		clip.h = screen->h-(2*32);
   1.787 -		SDL_SetClipRect(screen, &clip);
   1.788 -	}
   1.789 +    /* Set a clipping rectangle to clip the outside edge of the screen */
   1.790 +    {
   1.791 +        SDL_Rect clip;
   1.792 +        clip.x = 32;
   1.793 +        clip.y = 32;
   1.794 +        clip.w = screen->w - (2 * 32);
   1.795 +        clip.h = screen->h - (2 * 32);
   1.796 +        SDL_SetClipRect (screen, &clip);
   1.797 +    }
   1.798  
   1.799 -	/* Wait for a keystroke */
   1.800 -	lastticks = SDL_GetTicks();
   1.801 -	done = 0;
   1.802 -	mouse_pressed = 0;
   1.803 -	while ( !done ) {
   1.804 -		/* Update the frame -- move the sprite */
   1.805 -		if ( mouse_pressed ) {
   1.806 -			MoveSprite(screen, light);
   1.807 -			mouse_pressed = 0;
   1.808 -		} else {
   1.809 -			MoveSprite(screen, NULL);
   1.810 -		}
   1.811 +    /* Wait for a keystroke */
   1.812 +    lastticks = SDL_GetTicks ();
   1.813 +    done = 0;
   1.814 +    mouse_pressed = 0;
   1.815 +    while (!done) {
   1.816 +        /* Update the frame -- move the sprite */
   1.817 +        if (mouse_pressed) {
   1.818 +            MoveSprite (screen, light);
   1.819 +            mouse_pressed = 0;
   1.820 +        } else {
   1.821 +            MoveSprite (screen, NULL);
   1.822 +        }
   1.823  
   1.824 -		/* Slow down the loop to 30 frames/second */
   1.825 -		ticks = SDL_GetTicks();
   1.826 -		if ( (ticks-lastticks) < FRAME_TICKS ) {
   1.827 +        /* Slow down the loop to 30 frames/second */
   1.828 +        ticks = SDL_GetTicks ();
   1.829 +        if ((ticks - lastticks) < FRAME_TICKS) {
   1.830  #ifdef CHECK_SLEEP_GRANULARITY
   1.831 -fprintf(stderr, "Sleeping %d ticks\n", FRAME_TICKS-(ticks-lastticks));
   1.832 +            fprintf (stderr, "Sleeping %d ticks\n",
   1.833 +                     FRAME_TICKS - (ticks - lastticks));
   1.834  #endif
   1.835 -			SDL_Delay(FRAME_TICKS-(ticks-lastticks));
   1.836 +            SDL_Delay (FRAME_TICKS - (ticks - lastticks));
   1.837  #ifdef CHECK_SLEEP_GRANULARITY
   1.838 -fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks()-ticks));
   1.839 +            fprintf (stderr, "Slept %d ticks\n", (SDL_GetTicks () - ticks));
   1.840  #endif
   1.841 -		}
   1.842 -		lastticks = ticks;
   1.843 +        }
   1.844 +        lastticks = ticks;
   1.845  
   1.846 -		/* Check for events */
   1.847 -		while ( SDL_PollEvent(&event) ) {
   1.848 -			switch (event.type) {
   1.849 -				case SDL_VIDEORESIZE:
   1.850 -					screen = SDL_SetVideoMode(event.resize.w, event.resize.h, video_bpp, videoflags);
   1.851 -					if ( screen ) {
   1.852 -						FillBackground(screen);
   1.853 -					}
   1.854 -					break;
   1.855 -				/* Attract sprite while mouse is held down */
   1.856 -				case SDL_MOUSEMOTION:
   1.857 -					if (event.motion.state != 0) {
   1.858 -						AttractSprite(event.motion.x,
   1.859 -								event.motion.y);
   1.860 -						mouse_pressed = 1;
   1.861 -					}
   1.862 -					break;
   1.863 -				case SDL_MOUSEBUTTONDOWN:
   1.864 -					if ( event.button.button == 1 ) {
   1.865 -						AttractSprite(event.button.x,
   1.866 -						              event.button.y);
   1.867 -						mouse_pressed = 1;
   1.868 -					} else {
   1.869 -						SDL_Rect area;
   1.870 +        /* Check for events */
   1.871 +        while (SDL_PollEvent (&event)) {
   1.872 +            switch (event.type) {
   1.873 +            case SDL_VIDEORESIZE:
   1.874 +                screen =
   1.875 +                    SDL_SetVideoMode (event.resize.w, event.resize.h,
   1.876 +                                      video_bpp, videoflags);
   1.877 +                if (screen) {
   1.878 +                    FillBackground (screen);
   1.879 +                }
   1.880 +                break;
   1.881 +                /* Attract sprite while mouse is held down */
   1.882 +            case SDL_MOUSEMOTION:
   1.883 +                if (event.motion.state != 0) {
   1.884 +                    AttractSprite (event.motion.x, event.motion.y);
   1.885 +                    mouse_pressed = 1;
   1.886 +                }
   1.887 +                break;
   1.888 +            case SDL_MOUSEBUTTONDOWN:
   1.889 +                if (event.button.button == 1) {
   1.890 +                    AttractSprite (event.button.x, event.button.y);
   1.891 +                    mouse_pressed = 1;
   1.892 +                } else {
   1.893 +                    SDL_Rect area;
   1.894  
   1.895 -						area.x = event.button.x-16;
   1.896 -						area.y = event.button.y-16;
   1.897 -						area.w = 32;
   1.898 -						area.h = 32;
   1.899 -						SDL_FillRect(screen, &area, 0);
   1.900 -						SDL_UpdateRects(screen,1,&area);
   1.901 -					}
   1.902 -					break;
   1.903 -				case SDL_KEYDOWN:
   1.904 -					if ( event.key.keysym.sym == SDLK_ESCAPE ) {
   1.905 -						done = 1;
   1.906 -					}
   1.907 -					break;
   1.908 -				case SDL_QUIT:
   1.909 -					done = 1;
   1.910 -					break;
   1.911 -				default:
   1.912 -					break;
   1.913 -			}
   1.914 -		}
   1.915 -	}
   1.916 -	SDL_FreeSurface(light);
   1.917 -	SDL_FreeSurface(sprite);
   1.918 -	SDL_FreeSurface(backing);
   1.919 +                    area.x = event.button.x - 16;
   1.920 +                    area.y = event.button.y - 16;
   1.921 +                    area.w = 32;
   1.922 +                    area.h = 32;
   1.923 +                    SDL_FillRect (screen, &area, 0);
   1.924 +                    SDL_UpdateRects (screen, 1, &area);
   1.925 +                }
   1.926 +                break;
   1.927 +            case SDL_KEYDOWN:
   1.928 +                if (event.key.keysym.sym == SDLK_ESCAPE) {
   1.929 +                    done = 1;
   1.930 +                }
   1.931 +                break;
   1.932 +            case SDL_QUIT:
   1.933 +                done = 1;
   1.934 +                break;
   1.935 +            default:
   1.936 +                break;
   1.937 +            }
   1.938 +        }
   1.939 +    }
   1.940 +    SDL_FreeSurface (light);
   1.941 +    SDL_FreeSurface (sprite);
   1.942 +    SDL_FreeSurface (backing);
   1.943  
   1.944 -	/* Print out some timing information */
   1.945 -	if ( flashes > 0 ) {
   1.946 -		printf("%d alpha blits, ~%4.4f ms per blit\n", 
   1.947 -			flashes, (float)flashtime/flashes);
   1.948 -	}
   1.949 +    /* Print out some timing information */
   1.950 +    if (flashes > 0) {
   1.951 +        printf ("%d alpha blits, ~%4.4f ms per blit\n",
   1.952 +                flashes, (float) flashtime / flashes);
   1.953 +    }
   1.954  
   1.955 -	SDL_Quit();
   1.956 -	return(0);
   1.957 +    SDL_Quit ();
   1.958 +    return (0);
   1.959  }