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