test/testsprite.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1516 4d241ea8a1cd
child 1666 6e7ec5cb83c3
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
     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_DisplayMode mode;
   160     SDL_Surface *screen;
   161     Uint8 *mem;
   162     int width, height;
   163     Uint8 video_bpp;
   164     Uint32 videoflags;
   165     Uint32 background;
   166     int i, done;
   167     SDL_Event event;
   168     Uint32 then, now, frames;
   169 
   170     /* Initialize SDL */
   171     if (SDL_Init (SDL_INIT_VIDEO) < 0) {
   172         fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
   173         return (1);
   174     }
   175 
   176     numsprites = NUM_SPRITES;
   177     videoflags = SDL_SWSURFACE | SDL_ANYFORMAT;
   178     width = 640;
   179     height = 480;
   180     video_bpp = 8;
   181     debug_flip = 0;
   182     while (argc > 1) {
   183         --argc;
   184         if (strcmp (argv[argc - 1], "-width") == 0) {
   185             width = atoi (argv[argc]);
   186             --argc;
   187         } else if (strcmp (argv[argc - 1], "-height") == 0) {
   188             height = atoi (argv[argc]);
   189             --argc;
   190         } else if (strcmp (argv[argc - 1], "-bpp") == 0) {
   191             video_bpp = atoi (argv[argc]);
   192             videoflags &= ~SDL_ANYFORMAT;
   193             --argc;
   194         } else if (strcmp (argv[argc], "-fast") == 0) {
   195             videoflags = FastestFlags (videoflags, width, height, video_bpp);
   196         } else if (strcmp (argv[argc], "-hw") == 0) {
   197             videoflags ^= SDL_HWSURFACE;
   198         } else if (strcmp (argv[argc], "-flip") == 0) {
   199             videoflags ^= SDL_DOUBLEBUF;
   200         } else if (strcmp (argv[argc], "-debugflip") == 0) {
   201             debug_flip ^= 1;
   202         } else if (strcmp (argv[argc], "-fullscreen") == 0) {
   203             videoflags ^= SDL_FULLSCREEN;
   204         } else if (isdigit (argv[argc][0])) {
   205             numsprites = atoi (argv[argc]);
   206         } else {
   207             fprintf (stderr,
   208                      "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
   209                      argv[0]);
   210             quit (1);
   211         }
   212     }
   213 
   214     /* Set video mode */
   215     mode.format = 0;            /* FIXME */
   216     mode.w = width;
   217     mode.h = height;
   218     mode.refresh_rate = 0;
   219     screen = SDL_SetDisplayMode (&mode, videoflags);
   220     if (!screen) {
   221         fprintf (stderr, "Couldn't set %dx%d video mode: %s\n",
   222                  width, height, SDL_GetError ());
   223         quit (2);
   224     }
   225 
   226     /* Load the sprite */
   227     if (LoadSprite ("icon.bmp") < 0) {
   228         quit (1);
   229     }
   230 
   231     /* Allocate memory for the sprite info */
   232     mem = (Uint8 *) malloc (4 * sizeof (SDL_Rect) * numsprites);
   233     if (mem == NULL) {
   234         SDL_FreeSurface (sprite);
   235         fprintf (stderr, "Out of memory!\n");
   236         quit (2);
   237     }
   238     sprite_rects = (SDL_Rect *) mem;
   239     positions = sprite_rects;
   240     sprite_rects += numsprites;
   241     velocities = sprite_rects;
   242     sprite_rects += numsprites;
   243     sprite_w = sprite->w;
   244     sprite_h = sprite->h;
   245     srand (time (NULL));
   246     for (i = 0; i < numsprites; ++i) {
   247         positions[i].x = rand () % (screen->w - sprite_w);
   248         positions[i].y = rand () % (screen->h - sprite_h);
   249         positions[i].w = sprite->w;
   250         positions[i].h = sprite->h;
   251         velocities[i].x = 0;
   252         velocities[i].y = 0;
   253         while (!velocities[i].x && !velocities[i].y) {
   254             velocities[i].x = (rand () % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   255             velocities[i].y = (rand () % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   256         }
   257     }
   258     background = SDL_MapRGB (screen->format, 0x00, 0x00, 0x00);
   259 
   260     /* Print out information about our surfaces */
   261     printf ("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel);
   262     if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
   263         printf ("Screen is in video memory\n");
   264     } else {
   265         printf ("Screen is in system memory\n");
   266     }
   267     if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
   268         printf ("Screen has double-buffering enabled\n");
   269     }
   270     if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
   271         printf ("Sprite is in video memory\n");
   272     } else {
   273         printf ("Sprite is in system memory\n");
   274     }
   275     /* Run a sample blit to trigger blit acceleration */
   276     {
   277         SDL_Rect dst;
   278         dst.x = 0;
   279         dst.y = 0;
   280         dst.w = sprite->w;
   281         dst.h = sprite->h;
   282         SDL_BlitSurface (sprite, NULL, screen, &dst);
   283         SDL_FillRect (screen, &dst, background);
   284     }
   285     if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) {
   286         printf ("Sprite blit uses hardware acceleration\n");
   287     }
   288     if ((sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
   289         printf ("Sprite blit uses RLE acceleration\n");
   290     }
   291 
   292     /* Loop, blitting sprites and waiting for a keystroke */
   293     frames = 0;
   294     then = SDL_GetTicks ();
   295     done = 0;
   296     sprites_visible = 0;
   297     while (!done) {
   298         /* Check for events */
   299         ++frames;
   300         while (SDL_PollEvent (&event)) {
   301             switch (event.type) {
   302             case SDL_MOUSEBUTTONDOWN:
   303                 SDL_WarpMouse (screen->w / 2, screen->h / 2);
   304                 break;
   305             case SDL_KEYDOWN:
   306                 /* Any keypress quits the app... */
   307             case SDL_QUIT:
   308                 done = 1;
   309                 break;
   310             default:
   311                 break;
   312             }
   313         }
   314         MoveSprites (screen, background);
   315     }
   316     SDL_FreeSurface (sprite);
   317     free (mem);
   318 
   319     /* Print out some timing information */
   320     now = SDL_GetTicks ();
   321     if (now > then) {
   322         printf ("%2.2f frames per second\n",
   323                 ((double) frames * 1000) / (now - then));
   324     }
   325     SDL_Quit ();
   326     return (0);
   327 }