test/testsprite.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1666 6e7ec5cb83c3
child 1712 931d111e737a
permissions -rw-r--r--
more tweaking indent options
     1 /* Simple program:  Move N sprites around on the screen as fast as possible */
     2 
     3 #include <stdio.h>
     4 #include <stdlib.h>
     5 #include <string.h>
     6 #include <ctype.h>
     7 #include <math.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 int debug_flip;
    22 Uint16 sprite_w, sprite_h;
    23 
    24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    25 static void
    26 quit(int rc)
    27 {
    28     SDL_Quit();
    29     exit(rc);
    30 }
    31 
    32 int
    33 LoadSprite(char *file)
    34 {
    35     SDL_Surface *temp;
    36 
    37     /* Load the sprite image */
    38     sprite = SDL_LoadBMP(file);
    39     if (sprite == NULL) {
    40         fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    41         return (-1);
    42     }
    43 
    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);
    48     }
    49 
    50     /* Convert sprite to video format */
    51     temp = SDL_DisplayFormat(sprite);
    52     SDL_FreeSurface(sprite);
    53     if (temp == NULL) {
    54         fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError());
    55         return (-1);
    56     }
    57     sprite = temp;
    58 
    59     /* We're ready to roll. :) */
    60     return (0);
    61 }
    62 
    63 void
    64 MoveSprites(SDL_Surface * screen, Uint32 background)
    65 {
    66     int i, nupdates;
    67     SDL_Rect area, *position, *velocity;
    68 
    69     nupdates = 0;
    70     /* Erase all the sprites if necessary */
    71     if (sprites_visible) {
    72         SDL_FillRect(screen, NULL, background);
    73     }
    74 
    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;
    83         }
    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;
    88         }
    89 
    90         /* Blit the sprite onto the screen */
    91         area = *position;
    92         SDL_BlitSurface(sprite, NULL, screen, &area);
    93         sprite_rects[nupdates++] = area;
    94     }
    95 
    96     if (debug_flip) {
    97         if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
    98             static int t = 0;
    99 
   100             Uint32 color = SDL_MapRGB(screen->format, 255, 0, 0);
   101             SDL_Rect r;
   102             r.x =
   103                 (sin((float) t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w - 20);
   104             r.y = 0;
   105             r.w = 20;
   106             r.h = screen->h;
   107 
   108             SDL_FillRect(screen, &r, color);
   109             t += 2;
   110         }
   111     }
   112 
   113     /* Update the screen! */
   114     if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
   115         SDL_Flip(screen);
   116     } else {
   117         SDL_UpdateRects(screen, nupdates, sprite_rects);
   118     }
   119     sprites_visible = 1;
   120 }
   121 
   122 /* This is a way of telling whether or not to use hardware surfaces */
   123 Uint32
   124 FastestFlags(Uint32 flags, int width, int height, int bpp)
   125 {
   126     const SDL_VideoInfo *info;
   127 
   128     /* Hardware acceleration is only used in fullscreen mode */
   129     flags |= SDL_FULLSCREEN;
   130 
   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;
   136     }
   137     /* If we have enough video memory, and will use accelerated
   138        blits directly to it, then use page flipping.
   139      */
   140     if ((flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
   141         /* Direct hardware blitting without double-buffering
   142            causes really bad flickering.
   143          */
   144         if (info->video_mem * 1024 > (height * width * bpp / 8)) {
   145             flags |= SDL_DOUBLEBUF;
   146         } else {
   147             flags &= ~SDL_HWSURFACE;
   148         }
   149     }
   150 
   151     /* Return the flags */
   152     return (flags);
   153 }
   154 
   155 int
   156 main(int argc, char *argv[])
   157 {
   158     SDL_Surface *screen;
   159     Uint8 *mem;
   160     int width, height;
   161     Uint8 video_bpp;
   162     Uint32 videoflags;
   163     Uint32 background;
   164     int i, done;
   165     SDL_Event event;
   166     Uint32 then, now, frames;
   167 
   168     /* Initialize SDL */
   169     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   170         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   171         return (1);
   172     }
   173 
   174     numsprites = NUM_SPRITES;
   175     videoflags = SDL_SWSURFACE | SDL_ANYFORMAT;
   176     width = 640;
   177     height = 480;
   178     video_bpp = 8;
   179     debug_flip = 0;
   180     while (argc > 1) {
   181         --argc;
   182         if (strcmp(argv[argc - 1], "-width") == 0) {
   183             width = atoi(argv[argc]);
   184             --argc;
   185         } else if (strcmp(argv[argc - 1], "-height") == 0) {
   186             height = atoi(argv[argc]);
   187             --argc;
   188         } else if (strcmp(argv[argc - 1], "-bpp") == 0) {
   189             video_bpp = atoi(argv[argc]);
   190             videoflags &= ~SDL_ANYFORMAT;
   191             --argc;
   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) {
   199             debug_flip ^= 1;
   200         } else if (strcmp(argv[argc], "-fullscreen") == 0) {
   201             videoflags ^= SDL_FULLSCREEN;
   202         } else if (isdigit(argv[argc][0])) {
   203             numsprites = atoi(argv[argc]);
   204         } else {
   205             fprintf(stderr,
   206                     "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
   207                     argv[0]);
   208             quit(1);
   209         }
   210     }
   211 
   212     /* Set video mode */
   213     screen = SDL_SetVideoMode(width, height, video_bpp, videoflags);
   214     if (!screen) {
   215         fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
   216                 width, height, SDL_GetError());
   217         quit(2);
   218     }
   219 
   220     /* Load the sprite */
   221     if (LoadSprite("icon.bmp") < 0) {
   222         quit(1);
   223     }
   224 
   225     /* Allocate memory for the sprite info */
   226     mem = (Uint8 *) malloc(4 * sizeof(SDL_Rect) * numsprites);
   227     if (mem == NULL) {
   228         SDL_FreeSurface(sprite);
   229         fprintf(stderr, "Out of memory!\n");
   230         quit(2);
   231     }
   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;
   239     srand(time(NULL));
   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;
   245         velocities[i].x = 0;
   246         velocities[i].y = 0;
   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;
   250         }
   251     }
   252     background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
   253 
   254     /* Print out information about our surfaces */
   255     printf("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel);
   256     if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
   257         printf("Screen is in video memory\n");
   258     } else {
   259         printf("Screen is in system memory\n");
   260     }
   261     if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
   262         printf("Screen has double-buffering enabled\n");
   263     }
   264     if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
   265         printf("Sprite is in video memory\n");
   266     } else {
   267         printf("Sprite is in system memory\n");
   268     }
   269     /* Run a sample blit to trigger blit acceleration */
   270     {
   271         SDL_Rect dst;
   272         dst.x = 0;
   273         dst.y = 0;
   274         dst.w = sprite->w;
   275         dst.h = sprite->h;
   276         SDL_BlitSurface(sprite, NULL, screen, &dst);
   277         SDL_FillRect(screen, &dst, background);
   278     }
   279     if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) {
   280         printf("Sprite blit uses hardware acceleration\n");
   281     }
   282     if ((sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
   283         printf("Sprite blit uses RLE acceleration\n");
   284     }
   285 
   286     /* Loop, blitting sprites and waiting for a keystroke */
   287     frames = 0;
   288     then = SDL_GetTicks();
   289     done = 0;
   290     sprites_visible = 0;
   291     while (!done) {
   292         /* Check for events */
   293         ++frames;
   294         while (SDL_PollEvent(&event)) {
   295             switch (event.type) {
   296             case SDL_MOUSEBUTTONDOWN:
   297                 SDL_WarpMouse(screen->w / 2, screen->h / 2);
   298                 break;
   299             case SDL_KEYDOWN:
   300                 /* Any keypress quits the app... */
   301             case SDL_QUIT:
   302                 done = 1;
   303                 break;
   304             default:
   305                 break;
   306             }
   307         }
   308         MoveSprites(screen, background);
   309     }
   310     SDL_FreeSurface(sprite);
   311     free(mem);
   312 
   313     /* Print out some timing information */
   314     now = SDL_GetTicks();
   315     if (now > then) {
   316         printf("%2.2f frames per second\n",
   317                ((double) frames * 1000) / (now - then));
   318     }
   319     SDL_Quit();
   320     return (0);
   321 }