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