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