test/testsprite.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Oct 2002 08:45:07 +0000
changeset 529 21409a7a5fee
parent 288 2f5a6062db86
child 587 f00ccf8d8edc
permissions -rw-r--r--
*** empty log message ***
slouken@0
     1
/* Simple program:  Move N sprites around on the screen as fast as possible */
slouken@0
     2
slouken@0
     3
#include <stdio.h>
slouken@0
     4
#include <stdlib.h>
slouken@0
     5
#include <string.h>
slouken@0
     6
#include <ctype.h>
slouken@0
     7
#include <time.h>
slouken@0
     8
slouken@0
     9
#include "SDL.h"
slouken@0
    10
slouken@0
    11
#define NUM_SPRITES	100
slouken@0
    12
#define MAX_SPEED 	1
slouken@0
    13
slouken@0
    14
SDL_Surface *sprite;
slouken@0
    15
int numsprites;
slouken@0
    16
SDL_Rect *sprite_rects;
slouken@0
    17
SDL_Rect *positions;
slouken@0
    18
SDL_Rect *velocities;
slouken@0
    19
int sprites_visible;
slouken@288
    20
Uint16 sprite_w, sprite_h;
slouken@0
    21
slouken@0
    22
int LoadSprite(SDL_Surface *screen, char *file)
slouken@0
    23
{
slouken@0
    24
	SDL_Surface *temp;
slouken@0
    25
slouken@0
    26
	/* Load the sprite image */
slouken@0
    27
	sprite = SDL_LoadBMP(file);
slouken@0
    28
	if ( sprite == NULL ) {
slouken@0
    29
		fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
slouken@0
    30
		return(-1);
slouken@0
    31
	}
slouken@0
    32
slouken@0
    33
	/* Set transparent pixel as the pixel at (0,0) */
slouken@0
    34
	if ( sprite->format->palette ) {
slouken@0
    35
		SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY|SDL_RLEACCEL),
slouken@0
    36
						*(Uint8 *)sprite->pixels);
slouken@0
    37
	}
slouken@0
    38
slouken@0
    39
	/* Convert sprite to video format */
slouken@0
    40
	temp = SDL_DisplayFormat(sprite);
slouken@0
    41
	SDL_FreeSurface(sprite);
slouken@0
    42
	if ( temp == NULL ) {
slouken@0
    43
		fprintf(stderr, "Couldn't convert background: %s\n",
slouken@0
    44
							SDL_GetError());
slouken@0
    45
		return(-1);
slouken@0
    46
	}
slouken@0
    47
	sprite = temp;
slouken@0
    48
slouken@0
    49
	/* We're ready to roll. :) */
slouken@0
    50
	return(0);
slouken@0
    51
}
slouken@0
    52
slouken@0
    53
void MoveSprites(SDL_Surface *screen, Uint32 background)
slouken@0
    54
{
slouken@0
    55
	int i, nupdates;
slouken@0
    56
	SDL_Rect area, *position, *velocity;
slouken@0
    57
slouken@0
    58
	nupdates = 0;
slouken@0
    59
	/* Erase all the sprites if necessary */
slouken@0
    60
	if ( sprites_visible ) {
slouken@0
    61
		SDL_FillRect(screen, NULL, background);
slouken@0
    62
	}
slouken@0
    63
slouken@0
    64
	/* Move the sprite, bounce at the wall, and draw */
slouken@0
    65
	for ( i=0; i<numsprites; ++i ) {
slouken@0
    66
		position = &positions[i];
slouken@0
    67
		velocity = &velocities[i];
slouken@0
    68
		position->x += velocity->x;
slouken@288
    69
		if ( (position->x < 0) || (position->x >= (screen->w - sprite_w)) ) {
slouken@0
    70
			velocity->x = -velocity->x;
slouken@0
    71
			position->x += velocity->x;
slouken@0
    72
		}
slouken@0
    73
		position->y += velocity->y;
slouken@288
    74
		if ( (position->y < 0) || (position->y >= (screen->h - sprite_w)) ) {
slouken@0
    75
			velocity->y = -velocity->y;
slouken@0
    76
			position->y += velocity->y;
slouken@0
    77
		}
slouken@0
    78
slouken@0
    79
		/* Blit the sprite onto the screen */
slouken@0
    80
		area = *position;
slouken@0
    81
		SDL_BlitSurface(sprite, NULL, screen, &area);
slouken@0
    82
		sprite_rects[nupdates++] = area;
slouken@0
    83
	}
slouken@0
    84
slouken@0
    85
	/* Update the screen! */
slouken@0
    86
	if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
slouken@0
    87
		SDL_Flip(screen);
slouken@0
    88
	} else {
slouken@0
    89
		SDL_UpdateRects(screen, nupdates, sprite_rects);
slouken@0
    90
	}
slouken@0
    91
	sprites_visible = 1;
slouken@0
    92
}
slouken@0
    93
slouken@0
    94
/* This is a way of telling whether or not to use hardware surfaces */
slouken@107
    95
Uint32 FastestFlags(Uint32 flags, int width, int height, int bpp)
slouken@0
    96
{
slouken@0
    97
	const SDL_VideoInfo *info;
slouken@0
    98
slouken@0
    99
	/* Hardware acceleration is only used in fullscreen mode */
slouken@0
   100
	flags |= SDL_FULLSCREEN;
slouken@0
   101
slouken@0
   102
	/* Check for various video capabilities */
slouken@0
   103
	info = SDL_GetVideoInfo();
slouken@0
   104
	if ( info->blit_hw_CC && info->blit_fill ) {
slouken@0
   105
		/* We use accelerated colorkeying and color filling */
slouken@0
   106
		flags |= SDL_HWSURFACE;
slouken@0
   107
	}
slouken@0
   108
	/* If we have enough video memory, and will use accelerated
slouken@0
   109
	   blits directly to it, then use page flipping.
slouken@0
   110
	 */
slouken@0
   111
	if ( (flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
slouken@0
   112
		/* Direct hardware blitting without double-buffering
slouken@0
   113
		   causes really bad flickering.
slouken@0
   114
		 */
slouken@107
   115
		if ( info->video_mem*1024 > (height*width*bpp/8) ) {
slouken@0
   116
			flags |= SDL_DOUBLEBUF;
slouken@0
   117
		} else {
slouken@0
   118
			flags &= ~SDL_HWSURFACE;
slouken@0
   119
		}
slouken@0
   120
	}
slouken@0
   121
slouken@0
   122
	/* Return the flags */
slouken@0
   123
	return(flags);
slouken@0
   124
}
slouken@0
   125
slouken@0
   126
int main(int argc, char *argv[])
slouken@0
   127
{
slouken@0
   128
	SDL_Surface *screen;
slouken@0
   129
	Uint8 *mem;
slouken@0
   130
	int width, height;
slouken@0
   131
	Uint8  video_bpp;
slouken@0
   132
	Uint32 videoflags;
slouken@0
   133
	Uint32 background;
slouken@0
   134
	int    i, done;
slouken@0
   135
	SDL_Event event;
slouken@0
   136
	Uint32 then, now, frames;
slouken@0
   137
slouken@0
   138
	/* Initialize SDL */
slouken@0
   139
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
slouken@0
   140
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
   141
		exit(1);
slouken@0
   142
	}
slouken@0
   143
	atexit(SDL_Quit);
slouken@0
   144
slouken@0
   145
	numsprites = NUM_SPRITES;
slouken@0
   146
	videoflags = SDL_SWSURFACE|SDL_ANYFORMAT;
slouken@0
   147
	width = 640;
slouken@0
   148
	height = 480;
slouken@0
   149
	video_bpp = 8;
slouken@0
   150
	while ( argc > 1 ) {
slouken@0
   151
		--argc;
slouken@0
   152
		if ( strcmp(argv[argc-1], "-width") == 0 ) {
slouken@0
   153
			width = atoi(argv[argc]);
slouken@0
   154
			--argc;
slouken@0
   155
		} else
slouken@0
   156
		if ( strcmp(argv[argc-1], "-height") == 0 ) {
slouken@0
   157
			height = atoi(argv[argc]);
slouken@0
   158
			--argc;
slouken@0
   159
		} else
slouken@0
   160
		if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
slouken@0
   161
			video_bpp = atoi(argv[argc]);
slouken@0
   162
			videoflags &= ~SDL_ANYFORMAT;
slouken@0
   163
			--argc;
slouken@0
   164
		} else
slouken@0
   165
		if ( strcmp(argv[argc], "-fast") == 0 ) {
slouken@107
   166
			videoflags = FastestFlags(videoflags, width, height, video_bpp);
slouken@0
   167
		} else
slouken@0
   168
		if ( strcmp(argv[argc], "-hw") == 0 ) {
slouken@0
   169
			videoflags ^= SDL_HWSURFACE;
slouken@0
   170
		} else
slouken@0
   171
		if ( strcmp(argv[argc], "-flip") == 0 ) {
slouken@0
   172
			videoflags ^= SDL_DOUBLEBUF;
slouken@0
   173
		} else
slouken@0
   174
		if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
slouken@0
   175
			videoflags ^= SDL_FULLSCREEN;
slouken@0
   176
		} else
slouken@0
   177
		if ( isdigit(argv[argc][0]) ) {
slouken@0
   178
			numsprites = atoi(argv[argc]);
slouken@0
   179
		} else {
slouken@0
   180
			fprintf(stderr, 
slouken@0
   181
	"Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
slouken@0
   182
								argv[0]);
slouken@0
   183
			exit(1);
slouken@0
   184
		}
slouken@0
   185
	}
slouken@0
   186
slouken@0
   187
	/* Set video mode */
slouken@0
   188
	screen = SDL_SetVideoMode(width, height, video_bpp, videoflags);
slouken@0
   189
	if ( ! screen ) {
slouken@0
   190
		fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
slouken@0
   191
					width, height, SDL_GetError());
slouken@0
   192
		exit(2);
slouken@0
   193
	}
slouken@0
   194
slouken@0
   195
	/* Load the sprite */
slouken@0
   196
	if ( LoadSprite(screen, "icon.bmp") < 0 ) {
slouken@0
   197
		exit(1);
slouken@0
   198
	}
slouken@0
   199
slouken@0
   200
	/* Allocate memory for the sprite info */
slouken@0
   201
	mem = (Uint8 *)malloc(4*sizeof(SDL_Rect)*numsprites);
slouken@0
   202
	if ( mem == NULL ) {
slouken@0
   203
		SDL_FreeSurface(sprite);
slouken@0
   204
		fprintf(stderr, "Out of memory!\n");
slouken@0
   205
		exit(2);
slouken@0
   206
	}
slouken@0
   207
	sprite_rects = (SDL_Rect *)mem;
slouken@0
   208
	positions = sprite_rects;
slouken@0
   209
	sprite_rects += numsprites;
slouken@0
   210
	velocities = sprite_rects;
slouken@0
   211
	sprite_rects += numsprites;
slouken@288
   212
	sprite_w = sprite->w;
slouken@288
   213
	sprite_h = sprite->h;
slouken@0
   214
	srand(time(NULL));
slouken@0
   215
	for ( i=0; i<numsprites; ++i ) {
slouken@288
   216
		positions[i].x = rand()%(screen->w - sprite_w);
slouken@288
   217
		positions[i].y = rand()%(screen->h - sprite_h);
slouken@0
   218
		positions[i].w = sprite->w;
slouken@0
   219
		positions[i].h = sprite->h;
slouken@0
   220
		velocities[i].x = 0;
slouken@0
   221
		velocities[i].y = 0;
slouken@0
   222
		while ( ! velocities[i].x && ! velocities[i].y ) {
slouken@0
   223
			velocities[i].x = (rand()%(MAX_SPEED*2+1))-MAX_SPEED;
slouken@0
   224
			velocities[i].y = (rand()%(MAX_SPEED*2+1))-MAX_SPEED;
slouken@0
   225
		}
slouken@0
   226
	}
slouken@0
   227
	background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
slouken@0
   228
slouken@0
   229
	/* Print out information about our surfaces */
slouken@0
   230
	printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel);
slouken@0
   231
	if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
slouken@0
   232
		printf("Screen is in video memory\n");
slouken@0
   233
	} else {
slouken@0
   234
		printf("Screen is in system memory\n");
slouken@0
   235
	}
slouken@0
   236
	if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
slouken@0
   237
		printf("Screen has double-buffering enabled\n");
slouken@0
   238
	}
slouken@0
   239
	if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
slouken@0
   240
		printf("Sprite is in video memory\n");
slouken@0
   241
	} else {
slouken@0
   242
		printf("Sprite is in system memory\n");
slouken@0
   243
	}
slouken@0
   244
	/* Run a sample blit to trigger blit acceleration */
slouken@0
   245
	{ SDL_Rect dst;
slouken@0
   246
		dst.x = 0;
slouken@0
   247
		dst.y = 0;
slouken@0
   248
		dst.w = sprite->w;
slouken@0
   249
		dst.h = sprite->h;
slouken@0
   250
		SDL_BlitSurface(sprite, NULL, screen, &dst);
slouken@0
   251
		SDL_FillRect(screen, &dst, background);
slouken@0
   252
	}
slouken@0
   253
	if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
slouken@0
   254
		printf("Sprite blit uses hardware acceleration\n");
slouken@0
   255
	}
slouken@0
   256
	if ( (sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
slouken@0
   257
		printf("Sprite blit uses RLE acceleration\n");
slouken@0
   258
	}
slouken@0
   259
slouken@0
   260
	/* Loop, blitting sprites and waiting for a keystroke */
slouken@0
   261
	frames = 0;
slouken@0
   262
	then = SDL_GetTicks();
slouken@0
   263
	done = 0;
slouken@0
   264
	sprites_visible = 0;
slouken@0
   265
	while ( !done ) {
slouken@0
   266
		/* Check for events */
slouken@0
   267
		++frames;
slouken@0
   268
		while ( SDL_PollEvent(&event) ) {
slouken@0
   269
			switch (event.type) {
slouken@529
   270
				case SDL_MOUSEBUTTONDOWN:
slouken@529
   271
					SDL_WarpMouse(screen->w/2, screen->h/2);
slouken@529
   272
					break;
slouken@0
   273
				case SDL_KEYDOWN:
slouken@0
   274
					/* Any keypress quits the app... */
slouken@0
   275
				case SDL_QUIT:
slouken@0
   276
					done = 1;
slouken@0
   277
					break;
slouken@0
   278
				default:
slouken@0
   279
					break;
slouken@0
   280
			}
slouken@0
   281
		}
slouken@0
   282
		MoveSprites(screen, background);
slouken@0
   283
	}
slouken@0
   284
	SDL_FreeSurface(sprite);
slouken@0
   285
	free(mem);
slouken@0
   286
slouken@0
   287
	/* Print out some timing information */
slouken@0
   288
	now = SDL_GetTicks();
slouken@0
   289
	if ( now > then ) {
slouken@0
   290
		printf("%2.2f frames per second\n",
slouken@0
   291
					((double)frames*1000)/(now-then));
slouken@0
   292
	}
slouken@288
   293
	SDL_Quit();
slouken@0
   294
	return(0);
slouken@0
   295
}