test/testalpha.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 06 May 2004 15:55:06 +0000
changeset 886 05c551e5bc64
parent 691 609543e2b3a1
child 1151 be9c9c8f6d53
permissions -rw-r--r--
Date: Sat, 24 Apr 2004 15:13:32 +0300
From: "Mike Gorchak"
Subject: SDL updates for the QNX6

1. Updated the README.QNX
2. Updated libtool scripts, which are shipped with SDL for QNX6 support.
3. Added some code to support the new QNX 6.3.0, which is in beta now.
4. Added code to detect the hw features, which driver supports.
5. Added hw alpha blits code.
6. Fixed bug when application switches to fullscreen more the 2 times. (afte\
r that window becames always stay on top).
7. Updated a bit README for the tests.
8. Added information about acceleration show in the testalpha.c test.
9. Added small fixes to the testoverlay2.c test.
10. Added alpha and cc+alpha blits benchmarks to the testvidinfo.c test.
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@691
   282
	int    i, k, 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@691
   287
	Uint16 *buffer16;
slouken@691
   288
        Uint16 color;
slouken@691
   289
        Uint8  gradient;
slouken@691
   290
slouken@0
   291
slouken@0
   292
	/* Initialize SDL */
slouken@0
   293
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
slouken@0
   294
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
   295
		exit(1);
slouken@0
   296
	}
slouken@0
   297
	atexit(SDL_Quit);
slouken@0
   298
slouken@0
   299
	/* Alpha blending doesn't work well at 8-bit color */
slouken@0
   300
	info = SDL_GetVideoInfo();
slouken@0
   301
	if ( info->vfmt->BitsPerPixel > 8 ) {
slouken@0
   302
		video_bpp = info->vfmt->BitsPerPixel;
slouken@0
   303
	} else {
slouken@0
   304
		video_bpp = 16;
slouken@691
   305
                fprintf(stderr, "forced 16 bpp mode\n");
slouken@0
   306
	}
slouken@0
   307
	videoflags = SDL_SWSURFACE;
slouken@0
   308
	while ( argc > 1 ) {
slouken@0
   309
		--argc;
slouken@0
   310
		if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
slouken@0
   311
			video_bpp = atoi(argv[argc]);
slouken@691
   312
                        if (video_bpp<=8) {
slouken@691
   313
                            video_bpp=16;
slouken@691
   314
                            fprintf(stderr, "forced 16 bpp mode\n");
slouken@691
   315
                        }
slouken@0
   316
			--argc;
slouken@0
   317
		} else
slouken@0
   318
		if ( strcmp(argv[argc], "-hw") == 0 ) {
slouken@0
   319
			videoflags |= SDL_HWSURFACE;
slouken@0
   320
		} else
slouken@0
   321
		if ( strcmp(argv[argc], "-warp") == 0 ) {
slouken@0
   322
			videoflags |= SDL_HWPALETTE;
slouken@0
   323
		} else
slouken@0
   324
		if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
slouken@0
   325
			videoflags |= SDL_FULLSCREEN;
slouken@0
   326
		} else {
slouken@0
   327
			fprintf(stderr, 
slouken@0
   328
			"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
slouken@0
   329
								argv[0]);
slouken@0
   330
			exit(1);
slouken@0
   331
		}
slouken@0
   332
	}
slouken@0
   333
slouken@0
   334
	/* Set 640x480 video mode */
slouken@0
   335
	if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
slouken@0
   336
		fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
slouken@0
   337
						video_bpp, SDL_GetError());
slouken@0
   338
		exit(2);
slouken@0
   339
	}
slouken@0
   340
slouken@0
   341
	/* Set the surface pixels and refresh! */
slouken@0
   342
	if ( SDL_LockSurface(screen) < 0 ) {
slouken@0
   343
		fprintf(stderr, "Couldn't lock the display surface: %s\n",
slouken@0
   344
							SDL_GetError());
slouken@0
   345
		exit(2);
slouken@0
   346
	}
slouken@0
   347
	buffer=(Uint8 *)screen->pixels;
slouken@691
   348
	if (screen->format->BytesPerPixel!=2) {
slouken@691
   349
		for ( i=0; i<screen->h; ++i ) {
slouken@691
   350
			memset(buffer,(i*255)/screen->h, screen->pitch);
slouken@691
   351
			buffer += screen->pitch;
slouken@691
   352
		}
slouken@0
   353
	}
slouken@691
   354
        else
slouken@691
   355
        {
slouken@691
   356
		for ( i=0; i<screen->h; ++i ) {
slouken@691
   357
			gradient=((i*255)/screen->h);
slouken@691
   358
                        color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
slouken@691
   359
                        buffer16=(Uint16*)buffer;
slouken@691
   360
                        for (k=0; k<screen->w; k++)
slouken@691
   361
                        {
slouken@691
   362
                            *(buffer16+k)=color;
slouken@691
   363
                        }
slouken@691
   364
			buffer += screen->pitch;
slouken@691
   365
		}
slouken@691
   366
        }
slouken@691
   367
slouken@0
   368
	SDL_UnlockSurface(screen);
slouken@0
   369
	SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@0
   370
slouken@0
   371
	/* Create the light */
slouken@0
   372
	light = CreateLight(screen, 82);
slouken@0
   373
	if ( light == NULL ) {
slouken@0
   374
		exit(1);
slouken@0
   375
	}
slouken@0
   376
slouken@0
   377
	/* Load the sprite */
slouken@0
   378
	if ( LoadSprite(screen, "icon.bmp") < 0 ) {
slouken@0
   379
		SDL_FreeSurface(light);
slouken@0
   380
		exit(1);
slouken@0
   381
	}
slouken@0
   382
slouken@886
   383
	/* Print out information about our surfaces */
slouken@886
   384
	printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel);
slouken@886
   385
	if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
slouken@886
   386
		printf("Screen is in video memory\n");
slouken@886
   387
	} else {
slouken@886
   388
		printf("Screen is in system memory\n");
slouken@886
   389
	}
slouken@886
   390
	if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
slouken@886
   391
		printf("Screen has double-buffering enabled\n");
slouken@886
   392
	}
slouken@886
   393
	if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
slouken@886
   394
		printf("Sprite is in video memory\n");
slouken@886
   395
	} else {
slouken@886
   396
		printf("Sprite is in system memory\n");
slouken@886
   397
	}
slouken@886
   398
slouken@886
   399
	/* Run a sample blit to trigger blit acceleration */
slouken@886
   400
	{ SDL_Rect dst;
slouken@886
   401
		dst.x = 0;
slouken@886
   402
		dst.y = 0;
slouken@886
   403
		dst.w = sprite->w;
slouken@886
   404
		dst.h = sprite->h;
slouken@886
   405
		SDL_BlitSurface(sprite, NULL, screen, &dst);
slouken@886
   406
		SDL_FillRect(screen, &dst, 0);
slouken@886
   407
	}
slouken@886
   408
	if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
slouken@886
   409
		printf("Sprite blit uses hardware alpha acceleration\n");
slouken@886
   410
	} else {
slouken@886
   411
		printf("Sprite blit dosn't uses hardware alpha acceleration\n");
slouken@886
   412
	}
slouken@886
   413
slouken@0
   414
	/* Set a clipping rectangle to clip the outside edge of the screen */
slouken@0
   415
	{ SDL_Rect clip;
slouken@0
   416
		clip.x = 32;
slouken@0
   417
		clip.y = 32;
slouken@0
   418
		clip.w = screen->w-(2*32);
slouken@0
   419
		clip.h = screen->h-(2*32);
slouken@0
   420
		SDL_SetClipRect(screen, &clip);
slouken@0
   421
	}
slouken@0
   422
slouken@0
   423
	/* Wait for a keystroke */
slouken@0
   424
	lastticks = SDL_GetTicks();
slouken@0
   425
	done = 0;
slouken@0
   426
	mouse_pressed = 0;
slouken@0
   427
	while ( !done ) {
slouken@0
   428
		/* Update the frame -- move the sprite */
slouken@0
   429
		if ( mouse_pressed ) {
slouken@0
   430
			MoveSprite(screen, light);
slouken@0
   431
			mouse_pressed = 0;
slouken@0
   432
		} else {
slouken@0
   433
			MoveSprite(screen, NULL);
slouken@0
   434
		}
slouken@0
   435
slouken@0
   436
		/* Slow down the loop to 30 frames/second */
slouken@0
   437
		ticks = SDL_GetTicks();
slouken@0
   438
		if ( (ticks-lastticks) < FRAME_TICKS ) {
slouken@0
   439
#ifdef CHECK_SLEEP_GRANULARITY
slouken@0
   440
fprintf(stderr, "Sleeping %d ticks\n", FRAME_TICKS-(ticks-lastticks));
slouken@0
   441
#endif
slouken@0
   442
			SDL_Delay(FRAME_TICKS-(ticks-lastticks));
slouken@0
   443
#ifdef CHECK_SLEEP_GRANULARITY
slouken@0
   444
fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks()-ticks));
slouken@0
   445
#endif
slouken@0
   446
		}
slouken@0
   447
		lastticks = ticks;
slouken@0
   448
slouken@0
   449
		/* Check for events */
slouken@0
   450
		while ( SDL_PollEvent(&event) ) {
slouken@0
   451
			switch (event.type) {
slouken@0
   452
				/* Attract sprite while mouse is held down */
slouken@0
   453
				case SDL_MOUSEMOTION:
slouken@0
   454
					if (event.motion.state != 0) {
slouken@0
   455
						AttractSprite(event.motion.x,
slouken@0
   456
								event.motion.y);
slouken@0
   457
						mouse_pressed = 1;
slouken@0
   458
					}
slouken@0
   459
					break;
slouken@0
   460
				case SDL_MOUSEBUTTONDOWN:
slouken@0
   461
					if ( event.button.button == 1 ) {
slouken@0
   462
						AttractSprite(event.button.x,
slouken@0
   463
						              event.button.y);
slouken@0
   464
						mouse_pressed = 1;
slouken@0
   465
					} else {
slouken@0
   466
						SDL_Rect area;
slouken@0
   467
slouken@0
   468
						area.x = event.button.x-16;
slouken@0
   469
						area.y = event.button.y-16;
slouken@0
   470
						area.w = 32;
slouken@0
   471
						area.h = 32;
slouken@0
   472
						SDL_FillRect(screen, &area, 0);
slouken@0
   473
						SDL_UpdateRects(screen,1,&area);
slouken@0
   474
					}
slouken@0
   475
					break;
slouken@0
   476
				case SDL_KEYDOWN:
slouken@0
   477
					/* Any keypress quits the app... */
slouken@0
   478
				case SDL_QUIT:
slouken@0
   479
					done = 1;
slouken@0
   480
					break;
slouken@0
   481
				default:
slouken@0
   482
					break;
slouken@0
   483
			}
slouken@0
   484
		}
slouken@0
   485
	}
slouken@0
   486
	SDL_FreeSurface(light);
slouken@0
   487
	SDL_FreeSurface(sprite);
slouken@0
   488
	SDL_FreeSurface(backing);
slouken@0
   489
slouken@0
   490
	/* Print out some timing information */
slouken@0
   491
	if ( flashes > 0 ) {
slouken@0
   492
		printf("%d alpha blits, ~%4.4f ms per blit\n", 
slouken@0
   493
			flashes, (float)flashtime/flashes);
slouken@0
   494
	}
slouken@0
   495
	return(0);
slouken@0
   496
}