test/testsprite.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 28 Sep 2005 11:36:20 +0000
changeset 1151 be9c9c8f6d53
parent 753 b14fdadd8311
child 1214 31103dbf1c26
permissions -rw-r--r--
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
if SDL is built with a non-cdecl calling convention, and it's just generally
bad practice anyhow.

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