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