test/testalpha.c
author Sam Lantinga <slouken@lokigames.com>
Mon, 09 Jul 2001 15:46:41 +0000
changeset 96 799bea5504e2
parent 0 74212992fb08
child 691 609543e2b3a1
permissions -rw-r--r--
Fixed blocking open bug when using blocking audio writes
     1 
     2 /* Simple program:  Fill a colormap with gray and stripe it down the screen,
     3 		    Then move an alpha valued sprite around the screen.
     4  */
     5 
     6 #include <stdio.h>
     7 #include <stdlib.h>
     8 #include <string.h>
     9 #include <math.h>
    10 
    11 #include "SDL.h"
    12 
    13 #define FRAME_TICKS	(1000/30)		/* 30 frames/second */
    14 
    15 /* Create a "light" -- a yellowish surface with variable alpha */
    16 SDL_Surface *CreateLight(SDL_Surface *screen, int radius)
    17 {
    18 	Uint8  trans, alphamask;
    19 	int    range, addition;
    20 	int    xdist, ydist;
    21 	Uint16 x, y;
    22 	Uint16 skip;
    23 	Uint32 pixel;
    24 	SDL_Surface *light;
    25 
    26 #ifdef LIGHT_16BIT
    27 	Uint16 *buf;
    28 
    29 	/* Create a 16 (4/4/4/4) bpp square with a full 4-bit alpha channel */
    30 	/* Note: this isn't any faster than a 32 bit alpha surface */
    31 	alphamask = 0x0000000F;
    32 	light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*radius, 2*radius, 16,
    33 			0x0000F000, 0x00000F00, 0x000000F0, alphamask);
    34 #else
    35 	Uint32 *buf;
    36 
    37 	/* Create a 32 (8/8/8/8) bpp square with a full 8-bit alpha channel */
    38 	alphamask = 0x000000FF;
    39 	light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*radius, 2*radius, 32,
    40 			0xFF000000, 0x00FF0000, 0x0000FF00, alphamask);
    41 	if ( light == NULL ) {
    42 		fprintf(stderr, "Couldn't create light: %s\n", SDL_GetError());
    43 		return(NULL);
    44 	}
    45 #endif
    46 
    47 	/* Fill with a light yellow-orange color */
    48 	skip = light->pitch-(light->w*light->format->BytesPerPixel);
    49 #ifdef LIGHT_16BIT
    50 	buf = (Uint16 *)light->pixels;
    51 #else
    52 	buf = (Uint32 *)light->pixels;
    53 #endif
    54         /* Get a tranparent pixel value - we'll add alpha later */
    55 	pixel = SDL_MapRGBA(light->format, 0xFF, 0xDD, 0x88, 0);
    56 	for ( y=0; y<light->h; ++y ) {
    57 		for ( x=0; x<light->w; ++x ) {
    58 			*buf++ = pixel;
    59 		}
    60 		buf += skip;	/* Almost always 0, but just in case... */
    61 	}
    62 
    63 	/* Calculate alpha values for the surface. */
    64 #ifdef LIGHT_16BIT
    65 	buf = (Uint16 *)light->pixels;
    66 #else
    67 	buf = (Uint32 *)light->pixels;
    68 #endif
    69 	for ( y=0; y<light->h; ++y ) {
    70 		for ( x=0; x<light->w; ++x ) {
    71 			/* Slow distance formula (from center of light) */
    72 			xdist = x-(light->w/2);
    73 			ydist = y-(light->h/2);
    74 			range = (int)sqrt(xdist*xdist+ydist*ydist);
    75 
    76 			/* Scale distance to range of transparency (0-255) */
    77 			if ( range > radius ) {
    78 				trans = alphamask;
    79 			} else {
    80 				/* Increasing transparency with distance */
    81 				trans = (Uint8)((range*alphamask)/radius);
    82 
    83 				/* Lights are very transparent */
    84 				addition = (alphamask+1)/8;
    85 				if ( (int)trans+addition > alphamask ) {
    86 					trans = alphamask;
    87 				} else {
    88 					trans += addition;
    89 				}
    90 			}
    91 			/* We set the alpha component as the right N bits */
    92 			*buf++ |= (255-trans);
    93 		}
    94 		buf += skip;	/* Almost always 0, but just in case... */
    95 	}
    96 	/* Enable RLE acceleration of this alpha surface */
    97 	SDL_SetAlpha(light, SDL_SRCALPHA|SDL_RLEACCEL, 0);
    98 
    99 	/* We're done! */
   100 	return(light);
   101 }
   102 
   103 static Uint32 flashes = 0;
   104 static Uint32 flashtime = 0;
   105 
   106 void FlashLight(SDL_Surface *screen, SDL_Surface *light, int x, int y)
   107 {
   108 	SDL_Rect position;
   109 	Uint32   ticks1;
   110 	Uint32   ticks2;
   111 
   112 	/* Easy, center light */
   113 	position.x = x-(light->w/2);
   114 	position.y = y-(light->h/2);
   115 	position.w = light->w;
   116 	position.h = light->h;
   117 	ticks1 = SDL_GetTicks();
   118 	SDL_BlitSurface(light, NULL, screen, &position);
   119 	ticks2 = SDL_GetTicks();
   120 	SDL_UpdateRects(screen, 1, &position);
   121 	++flashes;
   122 
   123 	/* Update time spend doing alpha blitting */
   124 	flashtime += (ticks2-ticks1);
   125 }
   126 
   127 static int sprite_visible = 0;
   128 static SDL_Surface *sprite;
   129 static SDL_Surface *backing;
   130 static SDL_Rect    position;
   131 static int         x_vel, y_vel;
   132 static int	   alpha_vel;
   133 
   134 int LoadSprite(SDL_Surface *screen, char *file)
   135 {
   136 	SDL_Surface *converted;
   137 
   138 	/* Load the sprite image */
   139 	sprite = SDL_LoadBMP(file);
   140 	if ( sprite == NULL ) {
   141 		fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
   142 		return(-1);
   143 	}
   144 
   145 	/* Set transparent pixel as the pixel at (0,0) */
   146 	if ( sprite->format->palette ) {
   147 		SDL_SetColorKey(sprite, SDL_SRCCOLORKEY,
   148 						*(Uint8 *)sprite->pixels);
   149 	}
   150 
   151 	/* Convert sprite to video format */
   152 	converted = SDL_DisplayFormat(sprite);
   153 	SDL_FreeSurface(sprite);
   154 	if ( converted == NULL ) {
   155 		fprintf(stderr, "Couldn't convert background: %s\n",
   156 							SDL_GetError());
   157 		return(-1);
   158 	}
   159 	sprite = converted;
   160 
   161 	/* Create the background */
   162 	backing = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite->w, sprite->h, 8,
   163 								0, 0, 0, 0);
   164 	if ( backing == NULL ) {
   165 		fprintf(stderr, "Couldn't create background: %s\n",
   166 							SDL_GetError());
   167 		SDL_FreeSurface(sprite);
   168 		return(-1);
   169 	}
   170 
   171 	/* Convert background to video format */
   172 	converted = SDL_DisplayFormat(backing);
   173 	SDL_FreeSurface(backing);
   174 	if ( converted == NULL ) {
   175 		fprintf(stderr, "Couldn't convert background: %s\n",
   176 							SDL_GetError());
   177 		SDL_FreeSurface(sprite);
   178 		return(-1);
   179 	}
   180 	backing = converted;
   181 
   182 	/* Set the initial position of the sprite */
   183 	position.x = (screen->w-sprite->w)/2;
   184 	position.y = (screen->h-sprite->h)/2;
   185 	position.w = sprite->w;
   186 	position.h = sprite->h;
   187 	x_vel = 0; y_vel = 0;
   188 	alpha_vel = 1;
   189 
   190 	/* We're ready to roll. :) */
   191 	return(0);
   192 }
   193 
   194 void AttractSprite(Uint16 x, Uint16 y)
   195 {
   196 	x_vel = ((int)x-position.x)/10;
   197 	y_vel = ((int)y-position.y)/10;
   198 }
   199 
   200 void MoveSprite(SDL_Surface *screen, SDL_Surface *light)
   201 {
   202 	SDL_Rect updates[2];
   203 	int alpha;
   204 
   205 	/* Erase the sprite if it was visible */
   206 	if ( sprite_visible ) {
   207 		updates[0] = position;
   208 		SDL_BlitSurface(backing, NULL, screen, &updates[0]);
   209 	} else {
   210 		updates[0].x = 0; updates[0].y = 0;
   211 		updates[0].w = 0; updates[0].h = 0;
   212 		sprite_visible = 1;
   213 	}
   214 
   215 	/* Since the sprite is off the screen, we can do other drawing
   216 	   without being overwritten by the saved area behind the sprite.
   217 	 */
   218 	if ( light != NULL ) {
   219 		int x, y;
   220 
   221 		SDL_GetMouseState(&x, &y);
   222 		FlashLight(screen, light, x, y);
   223 	}
   224 	   
   225 	/* Move the sprite, bounce at the wall */
   226 	position.x += x_vel;
   227 	if ( (position.x < 0) || (position.x >= screen->w) ) {
   228 		x_vel = -x_vel;
   229 		position.x += x_vel;
   230 	}
   231 	position.y += y_vel;
   232 	if ( (position.y < 0) || (position.y >= screen->h) ) {
   233 		y_vel = -y_vel;
   234 		position.y += y_vel;
   235 	}
   236 
   237 	/* Update transparency (fade in and out) */
   238 	alpha = sprite->format->alpha;
   239 	if ( (alpha+alpha_vel) < 0 ) {
   240 		alpha_vel = -alpha_vel;
   241 	} else
   242 	if ( (alpha+alpha_vel) > 255 ) {
   243 		alpha_vel = -alpha_vel;
   244 	}
   245 	SDL_SetAlpha(sprite, SDL_SRCALPHA, (Uint8)(alpha+alpha_vel));
   246 
   247 	/* Save the area behind the sprite */
   248 	updates[1] = position;
   249 	SDL_BlitSurface(screen, &updates[1], backing, NULL);
   250 	
   251 	/* Blit the sprite onto the screen */
   252 	updates[1] = position;
   253 	SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
   254 
   255 	/* Make it so! */
   256 	SDL_UpdateRects(screen, 2, updates);
   257 }
   258 
   259 void WarpSprite(SDL_Surface *screen, int x, int y)
   260 {
   261 	SDL_Rect updates[2];
   262 
   263 	/* Erase, move, Draw, update */
   264 	updates[0] = position;
   265 	SDL_BlitSurface(backing, NULL, screen, &updates[0]);
   266 	position.x = x-sprite->w/2;	/* Center about X */
   267 	position.y = y-sprite->h/2;	/* Center about Y */
   268 	updates[1] = position;
   269 	SDL_BlitSurface(screen, &updates[1], backing, NULL);
   270 	updates[1] = position;
   271 	SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
   272 	SDL_UpdateRects(screen, 2, updates);
   273 }
   274 
   275 int main(int argc, char *argv[])
   276 {
   277 	const SDL_VideoInfo *info;
   278 	SDL_Surface *screen;
   279 	Uint8  video_bpp;
   280 	Uint32 videoflags;
   281 	Uint8 *buffer;
   282 	int    i, done;
   283 	SDL_Event event;
   284 	SDL_Surface *light;
   285 	int mouse_pressed;
   286 	Uint32 ticks, lastticks;
   287 
   288 	/* Initialize SDL */
   289 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   290 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   291 		exit(1);
   292 	}
   293 	atexit(SDL_Quit);
   294 
   295 	/* Alpha blending doesn't work well at 8-bit color */
   296 	info = SDL_GetVideoInfo();
   297 	if ( info->vfmt->BitsPerPixel > 8 ) {
   298 		video_bpp = info->vfmt->BitsPerPixel;
   299 	} else {
   300 		video_bpp = 16;
   301 	}
   302 	videoflags = SDL_SWSURFACE;
   303 	while ( argc > 1 ) {
   304 		--argc;
   305 		if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
   306 			video_bpp = atoi(argv[argc]);
   307 			--argc;
   308 		} else
   309 		if ( strcmp(argv[argc], "-hw") == 0 ) {
   310 			videoflags |= SDL_HWSURFACE;
   311 		} else
   312 		if ( strcmp(argv[argc], "-warp") == 0 ) {
   313 			videoflags |= SDL_HWPALETTE;
   314 		} else
   315 		if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
   316 			videoflags |= SDL_FULLSCREEN;
   317 		} else {
   318 			fprintf(stderr, 
   319 			"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
   320 								argv[0]);
   321 			exit(1);
   322 		}
   323 	}
   324 
   325 	/* Set 640x480 video mode */
   326 	if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
   327 		fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
   328 						video_bpp, SDL_GetError());
   329 		exit(2);
   330 	}
   331 
   332 	/* Set the surface pixels and refresh! */
   333 	if ( SDL_LockSurface(screen) < 0 ) {
   334 		fprintf(stderr, "Couldn't lock the display surface: %s\n",
   335 							SDL_GetError());
   336 		exit(2);
   337 	}
   338 	buffer=(Uint8 *)screen->pixels;
   339 	for ( i=0; i<screen->h; ++i ) {
   340 		memset(buffer,(i*255)/screen->h, screen->pitch);
   341 		buffer += screen->pitch;
   342 	}
   343 	SDL_UnlockSurface(screen);
   344 	SDL_UpdateRect(screen, 0, 0, 0, 0);
   345 
   346 	/* Create the light */
   347 	light = CreateLight(screen, 82);
   348 	if ( light == NULL ) {
   349 		exit(1);
   350 	}
   351 
   352 	/* Load the sprite */
   353 	if ( LoadSprite(screen, "icon.bmp") < 0 ) {
   354 		SDL_FreeSurface(light);
   355 		exit(1);
   356 	}
   357 
   358 	/* Set a clipping rectangle to clip the outside edge of the screen */
   359 	{ SDL_Rect clip;
   360 		clip.x = 32;
   361 		clip.y = 32;
   362 		clip.w = screen->w-(2*32);
   363 		clip.h = screen->h-(2*32);
   364 		SDL_SetClipRect(screen, &clip);
   365 	}
   366 
   367 	/* Wait for a keystroke */
   368 	lastticks = SDL_GetTicks();
   369 	done = 0;
   370 	mouse_pressed = 0;
   371 	while ( !done ) {
   372 		/* Update the frame -- move the sprite */
   373 		if ( mouse_pressed ) {
   374 			MoveSprite(screen, light);
   375 			mouse_pressed = 0;
   376 		} else {
   377 			MoveSprite(screen, NULL);
   378 		}
   379 
   380 		/* Slow down the loop to 30 frames/second */
   381 		ticks = SDL_GetTicks();
   382 		if ( (ticks-lastticks) < FRAME_TICKS ) {
   383 #ifdef CHECK_SLEEP_GRANULARITY
   384 fprintf(stderr, "Sleeping %d ticks\n", FRAME_TICKS-(ticks-lastticks));
   385 #endif
   386 			SDL_Delay(FRAME_TICKS-(ticks-lastticks));
   387 #ifdef CHECK_SLEEP_GRANULARITY
   388 fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks()-ticks));
   389 #endif
   390 		}
   391 		lastticks = ticks;
   392 
   393 		/* Check for events */
   394 		while ( SDL_PollEvent(&event) ) {
   395 			switch (event.type) {
   396 				/* Attract sprite while mouse is held down */
   397 				case SDL_MOUSEMOTION:
   398 					if (event.motion.state != 0) {
   399 						AttractSprite(event.motion.x,
   400 								event.motion.y);
   401 						mouse_pressed = 1;
   402 					}
   403 					break;
   404 				case SDL_MOUSEBUTTONDOWN:
   405 					if ( event.button.button == 1 ) {
   406 						AttractSprite(event.button.x,
   407 						              event.button.y);
   408 						mouse_pressed = 1;
   409 					} else {
   410 						SDL_Rect area;
   411 
   412 						area.x = event.button.x-16;
   413 						area.y = event.button.y-16;
   414 						area.w = 32;
   415 						area.h = 32;
   416 						SDL_FillRect(screen, &area, 0);
   417 						SDL_UpdateRects(screen,1,&area);
   418 					}
   419 					break;
   420 				case SDL_KEYDOWN:
   421 					/* Any keypress quits the app... */
   422 				case SDL_QUIT:
   423 					done = 1;
   424 					break;
   425 				default:
   426 					break;
   427 			}
   428 		}
   429 	}
   430 	SDL_FreeSurface(light);
   431 	SDL_FreeSurface(sprite);
   432 	SDL_FreeSurface(backing);
   433 
   434 	/* Print out some timing information */
   435 	if ( flashes > 0 ) {
   436 		printf("%d alpha blits, ~%4.4f ms per blit\n", 
   437 			flashes, (float)flashtime/flashes);
   438 	}
   439 	return(0);
   440 }