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