test/testalpha.c
changeset 1895 c121d94672cb
parent 1779 67fc81efcfc3
child 2267 c785543d1843
     1.1 --- a/test/testalpha.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/test/testalpha.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -10,523 +10,527 @@
     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", SDL_GetError());
   1.349 +        return (-1);
   1.350 +    }
   1.351 +    sprite = converted;
   1.352  
   1.353 -	/* Create the background */
   1.354 -	backing = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite->w, sprite->h, 8,
   1.355 -								0, 0, 0, 0);
   1.356 -	if ( backing == NULL ) {
   1.357 -		fprintf(stderr, "Couldn't create background: %s\n",
   1.358 -							SDL_GetError());
   1.359 -		SDL_FreeSurface(sprite);
   1.360 -		return(-1);
   1.361 -	}
   1.362 +    /* Create the background */
   1.363 +    backing = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite->w, sprite->h, 8,
   1.364 +                                   0, 0, 0, 0);
   1.365 +    if (backing == NULL) {
   1.366 +        fprintf(stderr, "Couldn't create background: %s\n", SDL_GetError());
   1.367 +        SDL_FreeSurface(sprite);
   1.368 +        return (-1);
   1.369 +    }
   1.370  
   1.371 -	/* Convert background to video format */
   1.372 -	converted = SDL_DisplayFormat(backing);
   1.373 -	SDL_FreeSurface(backing);
   1.374 -	if ( converted == NULL ) {
   1.375 -		fprintf(stderr, "Couldn't convert background: %s\n",
   1.376 -							SDL_GetError());
   1.377 -		SDL_FreeSurface(sprite);
   1.378 -		return(-1);
   1.379 -	}
   1.380 -	backing = converted;
   1.381 +    /* Convert background to video format */
   1.382 +    converted = SDL_DisplayFormat(backing);
   1.383 +    SDL_FreeSurface(backing);
   1.384 +    if (converted == NULL) {
   1.385 +        fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError());
   1.386 +        SDL_FreeSurface(sprite);
   1.387 +        return (-1);
   1.388 +    }
   1.389 +    backing = converted;
   1.390  
   1.391 -	/* Set the initial position of the sprite */
   1.392 -	position.x = (screen->w-sprite->w)/2;
   1.393 -	position.y = (screen->h-sprite->h)/2;
   1.394 -	position.w = sprite->w;
   1.395 -	position.h = sprite->h;
   1.396 -	x_vel = 0; y_vel = 0;
   1.397 -	alpha_vel = 1;
   1.398 +    /* Set the initial position of the sprite */
   1.399 +    position.x = (screen->w - sprite->w) / 2;
   1.400 +    position.y = (screen->h - sprite->h) / 2;
   1.401 +    position.w = sprite->w;
   1.402 +    position.h = sprite->h;
   1.403 +    x_vel = 0;
   1.404 +    y_vel = 0;
   1.405 +    alpha_vel = 1;
   1.406  
   1.407 -	/* We're ready to roll. :) */
   1.408 -	return(0);
   1.409 +    /* We're ready to roll. :) */
   1.410 +    return (0);
   1.411  }
   1.412  
   1.413 -void AttractSprite(Uint16 x, Uint16 y)
   1.414 +void
   1.415 +AttractSprite(Uint16 x, Uint16 y)
   1.416  {
   1.417 -	x_vel = ((int)x-position.x)/10;
   1.418 -	y_vel = ((int)y-position.y)/10;
   1.419 +    x_vel = ((int) x - position.x) / 10;
   1.420 +    y_vel = ((int) y - position.y) / 10;
   1.421  }
   1.422  
   1.423 -void MoveSprite(SDL_Surface *screen, SDL_Surface *light)
   1.424 +void
   1.425 +MoveSprite(SDL_Surface * screen, SDL_Surface * light)
   1.426  {
   1.427 -	SDL_Rect updates[2];
   1.428 -	int alpha;
   1.429 +    SDL_Rect updates[2];
   1.430 +    int alpha;
   1.431  
   1.432 -	/* Erase the sprite if it was visible */
   1.433 -	if ( sprite_visible ) {
   1.434 -		updates[0] = position;
   1.435 -		SDL_BlitSurface(backing, NULL, screen, &updates[0]);
   1.436 -	} else {
   1.437 -		updates[0].x = 0; updates[0].y = 0;
   1.438 -		updates[0].w = 0; updates[0].h = 0;
   1.439 -		sprite_visible = 1;
   1.440 -	}
   1.441 +    /* Erase the sprite if it was visible */
   1.442 +    if (sprite_visible) {
   1.443 +        updates[0] = position;
   1.444 +        SDL_BlitSurface(backing, NULL, screen, &updates[0]);
   1.445 +    } else {
   1.446 +        updates[0].x = 0;
   1.447 +        updates[0].y = 0;
   1.448 +        updates[0].w = 0;
   1.449 +        updates[0].h = 0;
   1.450 +        sprite_visible = 1;
   1.451 +    }
   1.452  
   1.453 -	/* Since the sprite is off the screen, we can do other drawing
   1.454 -	   without being overwritten by the saved area behind the sprite.
   1.455 -	 */
   1.456 -	if ( light != NULL ) {
   1.457 -		int x, y;
   1.458 +    /* Since the sprite is off the screen, we can do other drawing
   1.459 +       without being overwritten by the saved area behind the sprite.
   1.460 +     */
   1.461 +    if (light != NULL) {
   1.462 +        int x, y;
   1.463 +
   1.464 +        SDL_GetMouseState(&x, &y);
   1.465 +        FlashLight(screen, light, x, y);
   1.466 +    }
   1.467  
   1.468 -		SDL_GetMouseState(&x, &y);
   1.469 -		FlashLight(screen, light, x, y);
   1.470 -	}
   1.471 -	   
   1.472 -	/* Move the sprite, bounce at the wall */
   1.473 -	position.x += x_vel;
   1.474 -	if ( (position.x < 0) || (position.x >= screen->w) ) {
   1.475 -		x_vel = -x_vel;
   1.476 -		position.x += x_vel;
   1.477 -	}
   1.478 -	position.y += y_vel;
   1.479 -	if ( (position.y < 0) || (position.y >= screen->h) ) {
   1.480 -		y_vel = -y_vel;
   1.481 -		position.y += y_vel;
   1.482 -	}
   1.483 +    /* Move the sprite, bounce at the wall */
   1.484 +    position.x += x_vel;
   1.485 +    if ((position.x < 0) || (position.x >= screen->w)) {
   1.486 +        x_vel = -x_vel;
   1.487 +        position.x += x_vel;
   1.488 +    }
   1.489 +    position.y += y_vel;
   1.490 +    if ((position.y < 0) || (position.y >= screen->h)) {
   1.491 +        y_vel = -y_vel;
   1.492 +        position.y += y_vel;
   1.493 +    }
   1.494  
   1.495 -	/* Update transparency (fade in and out) */
   1.496 -	alpha = sprite->format->alpha;
   1.497 -	if ( (alpha+alpha_vel) < 0 ) {
   1.498 -		alpha_vel = -alpha_vel;
   1.499 -	} else
   1.500 -	if ( (alpha+alpha_vel) > 255 ) {
   1.501 -		alpha_vel = -alpha_vel;
   1.502 -	}
   1.503 -	SDL_SetAlpha(sprite, SDL_SRCALPHA, (Uint8)(alpha+alpha_vel));
   1.504 +    /* Update transparency (fade in and out) */
   1.505 +    alpha = sprite->format->alpha;
   1.506 +    if ((alpha + alpha_vel) < 0) {
   1.507 +        alpha_vel = -alpha_vel;
   1.508 +    } else if ((alpha + alpha_vel) > 255) {
   1.509 +        alpha_vel = -alpha_vel;
   1.510 +    }
   1.511 +    SDL_SetAlpha(sprite, SDL_SRCALPHA, (Uint8) (alpha + alpha_vel));
   1.512  
   1.513 -	/* Save the area behind the sprite */
   1.514 -	updates[1] = position;
   1.515 -	SDL_BlitSurface(screen, &updates[1], backing, NULL);
   1.516 -	
   1.517 -	/* Blit the sprite onto the screen */
   1.518 -	updates[1] = position;
   1.519 -	SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
   1.520 +    /* Save the area behind the sprite */
   1.521 +    updates[1] = position;
   1.522 +    SDL_BlitSurface(screen, &updates[1], backing, NULL);
   1.523  
   1.524 -	/* Make it so! */
   1.525 -	SDL_UpdateRects(screen, 2, updates);
   1.526 +    /* Blit the sprite onto the screen */
   1.527 +    updates[1] = position;
   1.528 +    SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
   1.529 +
   1.530 +    /* Make it so! */
   1.531 +    SDL_UpdateRects(screen, 2, updates);
   1.532  }
   1.533  
   1.534 -void WarpSprite(SDL_Surface *screen, int x, int y)
   1.535 +void
   1.536 +WarpSprite(SDL_Surface * screen, int x, int y)
   1.537  {
   1.538 -	SDL_Rect updates[2];
   1.539 +    SDL_Rect updates[2];
   1.540  
   1.541 -	/* Erase, move, Draw, update */
   1.542 -	updates[0] = position;
   1.543 -	SDL_BlitSurface(backing, NULL, screen, &updates[0]);
   1.544 -	position.x = x-sprite->w/2;	/* Center about X */
   1.545 -	position.y = y-sprite->h/2;	/* Center about Y */
   1.546 -	updates[1] = position;
   1.547 -	SDL_BlitSurface(screen, &updates[1], backing, NULL);
   1.548 -	updates[1] = position;
   1.549 -	SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
   1.550 -	SDL_UpdateRects(screen, 2, updates);
   1.551 +    /* Erase, move, Draw, update */
   1.552 +    updates[0] = position;
   1.553 +    SDL_BlitSurface(backing, NULL, screen, &updates[0]);
   1.554 +    position.x = x - sprite->w / 2;     /* Center about X */
   1.555 +    position.y = y - sprite->h / 2;     /* Center about Y */
   1.556 +    updates[1] = position;
   1.557 +    SDL_BlitSurface(screen, &updates[1], backing, NULL);
   1.558 +    updates[1] = position;
   1.559 +    SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
   1.560 +    SDL_UpdateRects(screen, 2, updates);
   1.561  }
   1.562  
   1.563 -int main(int argc, char *argv[])
   1.564 +int
   1.565 +main(int argc, char *argv[])
   1.566  {
   1.567 -	const SDL_VideoInfo *info;
   1.568 -	SDL_Surface *screen;
   1.569 -	int    w, h;
   1.570 -	Uint8  video_bpp;
   1.571 -	Uint32 videoflags;
   1.572 -	int    i, done;
   1.573 -	SDL_Event event;
   1.574 -	SDL_Surface *light;
   1.575 -	int mouse_pressed;
   1.576 -	Uint32 ticks, lastticks;
   1.577 +    const SDL_VideoInfo *info;
   1.578 +    SDL_Surface *screen;
   1.579 +    int w, h;
   1.580 +    Uint8 video_bpp;
   1.581 +    Uint32 videoflags;
   1.582 +    int i, done;
   1.583 +    SDL_Event event;
   1.584 +    SDL_Surface *light;
   1.585 +    int mouse_pressed;
   1.586 +    Uint32 ticks, lastticks;
   1.587  
   1.588  
   1.589 -	/* Initialize SDL */
   1.590 -	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   1.591 -		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   1.592 -		return(1);
   1.593 -	}
   1.594 +    /* Initialize SDL */
   1.595 +    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   1.596 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.597 +        return (1);
   1.598 +    }
   1.599  
   1.600 -	/* Alpha blending doesn't work well at 8-bit color */
   1.601 +    /* Alpha blending doesn't work well at 8-bit color */
   1.602  #ifdef _WIN32_WCE
   1.603 -	/* Pocket PC */
   1.604 -	w = 240;
   1.605 -	h = 320;
   1.606 +    /* Pocket PC */
   1.607 +    w = 240;
   1.608 +    h = 320;
   1.609  #else
   1.610 -	w = 640;
   1.611 -	h = 480;
   1.612 +    w = 640;
   1.613 +    h = 480;
   1.614  #endif
   1.615 -	info = SDL_GetVideoInfo();
   1.616 -	if ( info->vfmt->BitsPerPixel > 8 ) {
   1.617 -		video_bpp = info->vfmt->BitsPerPixel;
   1.618 -	} else {
   1.619 -		video_bpp = 16;
   1.620 +    info = SDL_GetVideoInfo();
   1.621 +    if (info->vfmt->BitsPerPixel > 8) {
   1.622 +        video_bpp = info->vfmt->BitsPerPixel;
   1.623 +    } else {
   1.624 +        video_bpp = 16;
   1.625 +        fprintf(stderr, "forced 16 bpp mode\n");
   1.626 +    }
   1.627 +    videoflags = SDL_SWSURFACE;
   1.628 +    for (i = 1; argv[i]; ++i) {
   1.629 +        if (strcmp(argv[i], "-bpp") == 0) {
   1.630 +            video_bpp = atoi(argv[++i]);
   1.631 +            if (video_bpp <= 8) {
   1.632 +                video_bpp = 16;
   1.633                  fprintf(stderr, "forced 16 bpp mode\n");
   1.634 -	}
   1.635 -	videoflags = SDL_SWSURFACE;
   1.636 -	for ( i = 1; argv[i]; ++i ) {
   1.637 -		if ( strcmp(argv[i], "-bpp") == 0 ) {
   1.638 -			video_bpp = atoi(argv[++i]);
   1.639 -                        if (video_bpp<=8) {
   1.640 -                            video_bpp=16;
   1.641 -                            fprintf(stderr, "forced 16 bpp mode\n");
   1.642 -                        }
   1.643 -		} else
   1.644 -		if ( strcmp(argv[i], "-hw") == 0 ) {
   1.645 -			videoflags |= SDL_HWSURFACE;
   1.646 -		} else
   1.647 -		if ( strcmp(argv[i], "-warp") == 0 ) {
   1.648 -			videoflags |= SDL_HWPALETTE;
   1.649 -		} else
   1.650 -		if ( strcmp(argv[i], "-width") == 0 && argv[i+1] ) {
   1.651 -			w = atoi(argv[++i]);
   1.652 -		} else
   1.653 -		if ( strcmp(argv[i], "-height") == 0 && argv[i+1] ) {
   1.654 -			h = atoi(argv[++i]);
   1.655 -		} else
   1.656 -		if ( strcmp(argv[i], "-resize") == 0 ) {
   1.657 -			videoflags |= SDL_RESIZABLE;
   1.658 -		} else
   1.659 -		if ( strcmp(argv[i], "-noframe") == 0 ) {
   1.660 -			videoflags |= SDL_NOFRAME;
   1.661 -		} else
   1.662 -		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
   1.663 -			videoflags |= SDL_FULLSCREEN;
   1.664 -		} else {
   1.665 -			fprintf(stderr, 
   1.666 -			"Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n",
   1.667 -								argv[0]);
   1.668 -			quit(1);
   1.669 -		}
   1.670 -	}
   1.671 +            }
   1.672 +        } else if (strcmp(argv[i], "-hw") == 0) {
   1.673 +            videoflags |= SDL_HWSURFACE;
   1.674 +        } else if (strcmp(argv[i], "-warp") == 0) {
   1.675 +            videoflags |= SDL_HWPALETTE;
   1.676 +        } else if (strcmp(argv[i], "-width") == 0 && argv[i + 1]) {
   1.677 +            w = atoi(argv[++i]);
   1.678 +        } else if (strcmp(argv[i], "-height") == 0 && argv[i + 1]) {
   1.679 +            h = atoi(argv[++i]);
   1.680 +        } else if (strcmp(argv[i], "-resize") == 0) {
   1.681 +            videoflags |= SDL_RESIZABLE;
   1.682 +        } else if (strcmp(argv[i], "-noframe") == 0) {
   1.683 +            videoflags |= SDL_NOFRAME;
   1.684 +        } else if (strcmp(argv[i], "-fullscreen") == 0) {
   1.685 +            videoflags |= SDL_FULLSCREEN;
   1.686 +        } else {
   1.687 +            fprintf(stderr,
   1.688 +                    "Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n",
   1.689 +                    argv[0]);
   1.690 +            quit(1);
   1.691 +        }
   1.692 +    }
   1.693 +
   1.694 +    /* Set video mode */
   1.695 +    if ((screen = SDL_SetVideoMode(w, h, video_bpp, videoflags)) == NULL) {
   1.696 +        fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
   1.697 +                w, h, video_bpp, SDL_GetError());
   1.698 +        quit(2);
   1.699 +    }
   1.700 +    FillBackground(screen);
   1.701  
   1.702 -	/* Set video mode */
   1.703 -	if ( (screen=SDL_SetVideoMode(w,h,video_bpp,videoflags)) == NULL ) {
   1.704 -		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
   1.705 -						w, h, video_bpp, SDL_GetError());
   1.706 -		quit(2);
   1.707 -	}
   1.708 -	FillBackground(screen);
   1.709 +    /* Create the light */
   1.710 +    light = CreateLight(82);
   1.711 +    if (light == NULL) {
   1.712 +        quit(1);
   1.713 +    }
   1.714  
   1.715 -	/* Create the light */
   1.716 -	light = CreateLight(82);
   1.717 -	if ( light == NULL ) {
   1.718 -		quit(1);
   1.719 -	}
   1.720 +    /* Load the sprite */
   1.721 +    if (LoadSprite(screen, "icon.bmp") < 0) {
   1.722 +        SDL_FreeSurface(light);
   1.723 +        quit(1);
   1.724 +    }
   1.725  
   1.726 -	/* Load the sprite */
   1.727 -	if ( LoadSprite(screen, "icon.bmp") < 0 ) {
   1.728 -		SDL_FreeSurface(light);
   1.729 -		quit(1);
   1.730 -	}
   1.731 +    /* Print out information about our surfaces */
   1.732 +    printf("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel);
   1.733 +    if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
   1.734 +        printf("Screen is in video memory\n");
   1.735 +    } else {
   1.736 +        printf("Screen is in system memory\n");
   1.737 +    }
   1.738 +    if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
   1.739 +        printf("Screen has double-buffering enabled\n");
   1.740 +    }
   1.741 +    if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
   1.742 +        printf("Sprite is in video memory\n");
   1.743 +    } else {
   1.744 +        printf("Sprite is in system memory\n");
   1.745 +    }
   1.746  
   1.747 -	/* Print out information about our surfaces */
   1.748 -	printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel);
   1.749 -	if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
   1.750 -		printf("Screen is in video memory\n");
   1.751 -	} else {
   1.752 -		printf("Screen is in system memory\n");
   1.753 -	}
   1.754 -	if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
   1.755 -		printf("Screen has double-buffering enabled\n");
   1.756 -	}
   1.757 -	if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
   1.758 -		printf("Sprite is in video memory\n");
   1.759 -	} else {
   1.760 -		printf("Sprite is in system memory\n");
   1.761 -	}
   1.762 +    /* Run a sample blit to trigger blit acceleration */
   1.763 +    MoveSprite(screen, NULL);
   1.764 +    if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) {
   1.765 +        printf("Sprite blit uses hardware alpha acceleration\n");
   1.766 +    } else {
   1.767 +        printf("Sprite blit dosn't uses hardware alpha acceleration\n");
   1.768 +    }
   1.769  
   1.770 -	/* Run a sample blit to trigger blit acceleration */
   1.771 -	MoveSprite(screen, NULL);
   1.772 -	if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
   1.773 -		printf("Sprite blit uses hardware alpha acceleration\n");
   1.774 -	} else {
   1.775 -		printf("Sprite blit dosn't uses hardware alpha acceleration\n");
   1.776 -	}
   1.777 -
   1.778 -	/* Set a clipping rectangle to clip the outside edge of the screen */
   1.779 -	{ SDL_Rect clip;
   1.780 -		clip.x = 32;
   1.781 -		clip.y = 32;
   1.782 -		clip.w = screen->w-(2*32);
   1.783 -		clip.h = screen->h-(2*32);
   1.784 -		SDL_SetClipRect(screen, &clip);
   1.785 -	}
   1.786 +    /* Set a clipping rectangle to clip the outside edge of the screen */
   1.787 +    {
   1.788 +        SDL_Rect clip;
   1.789 +        clip.x = 32;
   1.790 +        clip.y = 32;
   1.791 +        clip.w = screen->w - (2 * 32);
   1.792 +        clip.h = screen->h - (2 * 32);
   1.793 +        SDL_SetClipRect(screen, &clip);
   1.794 +    }
   1.795  
   1.796 -	/* Wait for a keystroke */
   1.797 -	lastticks = SDL_GetTicks();
   1.798 -	done = 0;
   1.799 -	mouse_pressed = 0;
   1.800 -	while ( !done ) {
   1.801 -		/* Update the frame -- move the sprite */
   1.802 -		if ( mouse_pressed ) {
   1.803 -			MoveSprite(screen, light);
   1.804 -			mouse_pressed = 0;
   1.805 -		} else {
   1.806 -			MoveSprite(screen, NULL);
   1.807 -		}
   1.808 +    /* Wait for a keystroke */
   1.809 +    lastticks = SDL_GetTicks();
   1.810 +    done = 0;
   1.811 +    mouse_pressed = 0;
   1.812 +    while (!done) {
   1.813 +        /* Update the frame -- move the sprite */
   1.814 +        if (mouse_pressed) {
   1.815 +            MoveSprite(screen, light);
   1.816 +            mouse_pressed = 0;
   1.817 +        } else {
   1.818 +            MoveSprite(screen, NULL);
   1.819 +        }
   1.820  
   1.821 -		/* Slow down the loop to 30 frames/second */
   1.822 -		ticks = SDL_GetTicks();
   1.823 -		if ( (ticks-lastticks) < FRAME_TICKS ) {
   1.824 +        /* Slow down the loop to 30 frames/second */
   1.825 +        ticks = SDL_GetTicks();
   1.826 +        if ((ticks - lastticks) < FRAME_TICKS) {
   1.827  #ifdef CHECK_SLEEP_GRANULARITY
   1.828 -fprintf(stderr, "Sleeping %d ticks\n", FRAME_TICKS-(ticks-lastticks));
   1.829 +            fprintf(stderr, "Sleeping %d ticks\n",
   1.830 +                    FRAME_TICKS - (ticks - lastticks));
   1.831  #endif
   1.832 -			SDL_Delay(FRAME_TICKS-(ticks-lastticks));
   1.833 +            SDL_Delay(FRAME_TICKS - (ticks - lastticks));
   1.834  #ifdef CHECK_SLEEP_GRANULARITY
   1.835 -fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks()-ticks));
   1.836 +            fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks() - ticks));
   1.837  #endif
   1.838 -		}
   1.839 -		lastticks = ticks;
   1.840 +        }
   1.841 +        lastticks = ticks;
   1.842  
   1.843 -		/* Check for events */
   1.844 -		while ( SDL_PollEvent(&event) ) {
   1.845 -			switch (event.type) {
   1.846 -				case SDL_VIDEORESIZE:
   1.847 -					screen = SDL_SetVideoMode(event.resize.w, event.resize.h, video_bpp, videoflags);
   1.848 -					if ( screen ) {
   1.849 -						FillBackground(screen);
   1.850 -					}
   1.851 -					break;
   1.852 -				/* Attract sprite while mouse is held down */
   1.853 -				case SDL_MOUSEMOTION:
   1.854 -					if (event.motion.state != 0) {
   1.855 -						AttractSprite(event.motion.x,
   1.856 -								event.motion.y);
   1.857 -						mouse_pressed = 1;
   1.858 -					}
   1.859 -					break;
   1.860 -				case SDL_MOUSEBUTTONDOWN:
   1.861 -					if ( event.button.button == 1 ) {
   1.862 -						AttractSprite(event.button.x,
   1.863 -						              event.button.y);
   1.864 -						mouse_pressed = 1;
   1.865 -					} else {
   1.866 -						SDL_Rect area;
   1.867 +        /* Check for events */
   1.868 +        while (SDL_PollEvent(&event)) {
   1.869 +            switch (event.type) {
   1.870 +            case SDL_VIDEORESIZE:
   1.871 +                screen =
   1.872 +                    SDL_SetVideoMode(event.resize.w, event.resize.h,
   1.873 +                                     video_bpp, videoflags);
   1.874 +                if (screen) {
   1.875 +                    FillBackground(screen);
   1.876 +                }
   1.877 +                break;
   1.878 +                /* Attract sprite while mouse is held down */
   1.879 +            case SDL_MOUSEMOTION:
   1.880 +                if (event.motion.state != 0) {
   1.881 +                    AttractSprite(event.motion.x, event.motion.y);
   1.882 +                    mouse_pressed = 1;
   1.883 +                }
   1.884 +                break;
   1.885 +            case SDL_MOUSEBUTTONDOWN:
   1.886 +                if (event.button.button == 1) {
   1.887 +                    AttractSprite(event.button.x, event.button.y);
   1.888 +                    mouse_pressed = 1;
   1.889 +                } else {
   1.890 +                    SDL_Rect area;
   1.891  
   1.892 -						area.x = event.button.x-16;
   1.893 -						area.y = event.button.y-16;
   1.894 -						area.w = 32;
   1.895 -						area.h = 32;
   1.896 -						SDL_FillRect(screen, &area, 0);
   1.897 -						SDL_UpdateRects(screen,1,&area);
   1.898 -					}
   1.899 -					break;
   1.900 -				case SDL_KEYDOWN:
   1.901 -					if ( event.key.keysym.sym == SDLK_ESCAPE ) {
   1.902 -						done = 1;
   1.903 -					}
   1.904 -					break;
   1.905 -				case SDL_QUIT:
   1.906 -					done = 1;
   1.907 -					break;
   1.908 -				default:
   1.909 -					break;
   1.910 -			}
   1.911 -		}
   1.912 -	}
   1.913 -	SDL_FreeSurface(light);
   1.914 -	SDL_FreeSurface(sprite);
   1.915 -	SDL_FreeSurface(backing);
   1.916 +                    area.x = event.button.x - 16;
   1.917 +                    area.y = event.button.y - 16;
   1.918 +                    area.w = 32;
   1.919 +                    area.h = 32;
   1.920 +                    SDL_FillRect(screen, &area, 0);
   1.921 +                    SDL_UpdateRects(screen, 1, &area);
   1.922 +                }
   1.923 +                break;
   1.924 +            case SDL_KEYDOWN:
   1.925 +                if (event.key.keysym.sym == SDLK_ESCAPE) {
   1.926 +                    done = 1;
   1.927 +                }
   1.928 +                break;
   1.929 +            case SDL_QUIT:
   1.930 +                done = 1;
   1.931 +                break;
   1.932 +            default:
   1.933 +                break;
   1.934 +            }
   1.935 +        }
   1.936 +    }
   1.937 +    SDL_FreeSurface(light);
   1.938 +    SDL_FreeSurface(sprite);
   1.939 +    SDL_FreeSurface(backing);
   1.940  
   1.941 -	/* Print out some timing information */
   1.942 -	if ( flashes > 0 ) {
   1.943 -		printf("%d alpha blits, ~%4.4f ms per blit\n", 
   1.944 -			flashes, (float)flashtime/flashes);
   1.945 -	}
   1.946 +    /* Print out some timing information */
   1.947 +    if (flashes > 0) {
   1.948 +        printf("%d alpha blits, ~%4.4f ms per blit\n",
   1.949 +               flashes, (float) flashtime / flashes);
   1.950 +    }
   1.951  
   1.952 -	SDL_Quit();
   1.953 -	return(0);
   1.954 +    SDL_Quit();
   1.955 +    return (0);
   1.956  }