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