1 /* Simple program: Move N sprites around on the screen as fast as possible */
12 #define NUM_SPRITES 100
17 SDL_Rect *sprite_rects;
22 Uint16 sprite_w, sprite_h;
24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
33 LoadSprite(char *file)
37 /* Load the sprite image */
38 sprite = SDL_LoadBMP(file);
40 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
44 /* Set transparent pixel as the pixel at (0,0) */
45 if (sprite->format->palette) {
46 SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
47 *(Uint8 *) sprite->pixels);
50 /* Convert sprite to video format */
51 temp = SDL_DisplayFormat(sprite);
52 SDL_FreeSurface(sprite);
54 fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError());
59 /* We're ready to roll. :) */
64 MoveSprites(SDL_Surface * screen, Uint32 background)
67 SDL_Rect area, *position, *velocity;
70 /* Erase all the sprites if necessary */
71 if (sprites_visible) {
72 SDL_FillRect(screen, NULL, background);
75 /* Move the sprite, bounce at the wall, and draw */
76 for (i = 0; i < numsprites; ++i) {
77 position = &positions[i];
78 velocity = &velocities[i];
79 position->x += velocity->x;
80 if ((position->x < 0) || (position->x >= (screen->w - sprite_w))) {
81 velocity->x = -velocity->x;
82 position->x += velocity->x;
84 position->y += velocity->y;
85 if ((position->y < 0) || (position->y >= (screen->h - sprite_w))) {
86 velocity->y = -velocity->y;
87 position->y += velocity->y;
90 /* Blit the sprite onto the screen */
92 SDL_BlitSurface(sprite, NULL, screen, &area);
93 sprite_rects[nupdates++] = area;
97 if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
100 Uint32 color = SDL_MapRGB(screen->format, 255, 0, 0);
103 (sin((float) t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w - 20);
108 SDL_FillRect(screen, &r, color);
113 /* Update the screen! */
114 if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
117 SDL_UpdateRects(screen, nupdates, sprite_rects);
122 /* This is a way of telling whether or not to use hardware surfaces */
124 FastestFlags(Uint32 flags, int width, int height, int bpp)
126 const SDL_VideoInfo *info;
128 /* Hardware acceleration is only used in fullscreen mode */
129 flags |= SDL_FULLSCREEN;
131 /* Check for various video capabilities */
132 info = SDL_GetVideoInfo();
133 if (info->blit_hw_CC && info->blit_fill) {
134 /* We use accelerated colorkeying and color filling */
135 flags |= SDL_HWSURFACE;
137 /* If we have enough video memory, and will use accelerated
138 blits directly to it, then use page flipping.
140 if ((flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
141 /* Direct hardware blitting without double-buffering
142 causes really bad flickering.
144 if (info->video_mem * 1024 > (height * width * bpp / 8)) {
145 flags |= SDL_DOUBLEBUF;
147 flags &= ~SDL_HWSURFACE;
151 /* Return the flags */
156 main(int argc, char *argv[])
166 Uint32 then, now, frames;
169 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
170 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
174 numsprites = NUM_SPRITES;
175 videoflags = SDL_SWSURFACE | SDL_ANYFORMAT;
182 if (strcmp(argv[argc - 1], "-width") == 0) {
183 width = atoi(argv[argc]);
185 } else if (strcmp(argv[argc - 1], "-height") == 0) {
186 height = atoi(argv[argc]);
188 } else if (strcmp(argv[argc - 1], "-bpp") == 0) {
189 video_bpp = atoi(argv[argc]);
190 videoflags &= ~SDL_ANYFORMAT;
192 } else if (strcmp(argv[argc], "-fast") == 0) {
193 videoflags = FastestFlags(videoflags, width, height, video_bpp);
194 } else if (strcmp(argv[argc], "-hw") == 0) {
195 videoflags ^= SDL_HWSURFACE;
196 } else if (strcmp(argv[argc], "-flip") == 0) {
197 videoflags ^= SDL_DOUBLEBUF;
198 } else if (strcmp(argv[argc], "-debugflip") == 0) {
200 } else if (strcmp(argv[argc], "-fullscreen") == 0) {
201 videoflags ^= SDL_FULLSCREEN;
202 } else if (isdigit(argv[argc][0])) {
203 numsprites = atoi(argv[argc]);
206 "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
213 screen = SDL_SetVideoMode(width, height, video_bpp, videoflags);
215 fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
216 width, height, SDL_GetError());
220 /* Load the sprite */
221 if (LoadSprite("icon.bmp") < 0) {
225 /* Allocate memory for the sprite info */
226 mem = (Uint8 *) malloc(4 * sizeof(SDL_Rect) * numsprites);
228 SDL_FreeSurface(sprite);
229 fprintf(stderr, "Out of memory!\n");
232 sprite_rects = (SDL_Rect *) mem;
233 positions = sprite_rects;
234 sprite_rects += numsprites;
235 velocities = sprite_rects;
236 sprite_rects += numsprites;
237 sprite_w = sprite->w;
238 sprite_h = sprite->h;
240 for (i = 0; i < numsprites; ++i) {
241 positions[i].x = rand() % (screen->w - sprite_w);
242 positions[i].y = rand() % (screen->h - sprite_h);
243 positions[i].w = sprite->w;
244 positions[i].h = sprite->h;
247 while (!velocities[i].x && !velocities[i].y) {
248 velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
249 velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
253 /* Clear the background to grey */
254 background = SDL_MapRGB(screen->format, 0xA0, 0xA0, 0xA0);
255 SDL_FillRect(screen, NULL, background);
258 /* Print out information about our surfaces */
259 printf("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel);
260 if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
261 printf("Screen is in video memory\n");
263 printf("Screen is in system memory\n");
265 if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
266 printf("Screen has double-buffering enabled\n");
268 if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
269 printf("Sprite is in video memory\n");
271 printf("Sprite is in system memory\n");
273 /* Run a sample blit to trigger blit acceleration */
280 SDL_BlitSurface(sprite, NULL, screen, &dst);
281 SDL_FillRect(screen, &dst, background);
283 if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) {
284 printf("Sprite blit uses hardware acceleration\n");
286 if ((sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
287 printf("Sprite blit uses RLE acceleration\n");
290 /* Loop, blitting sprites and waiting for a keystroke */
292 then = SDL_GetTicks();
296 /* Check for events */
298 while (SDL_PollEvent(&event)) {
299 switch (event.type) {
300 case SDL_MOUSEBUTTONDOWN:
301 SDL_WarpMouse(screen->w / 2, screen->h / 2);
304 /* Any keypress quits the app... */
312 MoveSprites(screen, background);
314 SDL_FreeSurface(sprite);
317 /* Print out some timing information */
318 now = SDL_GetTicks();
320 double fps = ((double) frames * 1000) / (now - then);
321 printf("%2.2f frames per second\n", fps);