test/testsprite.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1666 6e7ec5cb83c3
child 1712 931d111e737a
     1.1 --- a/test/testsprite.c	Mon May 29 03:53:21 2006 +0000
     1.2 +++ b/test/testsprite.c	Mon May 29 04:04:35 2006 +0000
     1.3 @@ -23,36 +23,35 @@
     1.4  
     1.5  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     1.6  static void
     1.7 -quit (int rc)
     1.8 +quit(int rc)
     1.9  {
    1.10 -    SDL_Quit ();
    1.11 -    exit (rc);
    1.12 +    SDL_Quit();
    1.13 +    exit(rc);
    1.14  }
    1.15  
    1.16  int
    1.17 -LoadSprite (char *file)
    1.18 +LoadSprite(char *file)
    1.19  {
    1.20      SDL_Surface *temp;
    1.21  
    1.22      /* Load the sprite image */
    1.23 -    sprite = SDL_LoadBMP (file);
    1.24 +    sprite = SDL_LoadBMP(file);
    1.25      if (sprite == NULL) {
    1.26 -        fprintf (stderr, "Couldn't load %s: %s", file, SDL_GetError ());
    1.27 +        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    1.28          return (-1);
    1.29      }
    1.30  
    1.31      /* Set transparent pixel as the pixel at (0,0) */
    1.32      if (sprite->format->palette) {
    1.33 -        SDL_SetColorKey (sprite, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
    1.34 -                         *(Uint8 *) sprite->pixels);
    1.35 +        SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
    1.36 +                        *(Uint8 *) sprite->pixels);
    1.37      }
    1.38  
    1.39      /* Convert sprite to video format */
    1.40 -    temp = SDL_DisplayFormat (sprite);
    1.41 -    SDL_FreeSurface (sprite);
    1.42 +    temp = SDL_DisplayFormat(sprite);
    1.43 +    SDL_FreeSurface(sprite);
    1.44      if (temp == NULL) {
    1.45 -        fprintf (stderr, "Couldn't convert background: %s\n",
    1.46 -                 SDL_GetError ());
    1.47 +        fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError());
    1.48          return (-1);
    1.49      }
    1.50      sprite = temp;
    1.51 @@ -62,7 +61,7 @@
    1.52  }
    1.53  
    1.54  void
    1.55 -MoveSprites (SDL_Surface * screen, Uint32 background)
    1.56 +MoveSprites(SDL_Surface * screen, Uint32 background)
    1.57  {
    1.58      int i, nupdates;
    1.59      SDL_Rect area, *position, *velocity;
    1.60 @@ -70,7 +69,7 @@
    1.61      nupdates = 0;
    1.62      /* Erase all the sprites if necessary */
    1.63      if (sprites_visible) {
    1.64 -        SDL_FillRect (screen, NULL, background);
    1.65 +        SDL_FillRect(screen, NULL, background);
    1.66      }
    1.67  
    1.68      /* Move the sprite, bounce at the wall, and draw */
    1.69 @@ -90,7 +89,7 @@
    1.70  
    1.71          /* Blit the sprite onto the screen */
    1.72          area = *position;
    1.73 -        SDL_BlitSurface (sprite, NULL, screen, &area);
    1.74 +        SDL_BlitSurface(sprite, NULL, screen, &area);
    1.75          sprite_rects[nupdates++] = area;
    1.76      }
    1.77  
    1.78 @@ -98,31 +97,31 @@
    1.79          if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
    1.80              static int t = 0;
    1.81  
    1.82 -            Uint32 color = SDL_MapRGB (screen->format, 255, 0, 0);
    1.83 +            Uint32 color = SDL_MapRGB(screen->format, 255, 0, 0);
    1.84              SDL_Rect r;
    1.85              r.x =
    1.86 -                (sin ((float) t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w - 20);
    1.87 +                (sin((float) t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w - 20);
    1.88              r.y = 0;
    1.89              r.w = 20;
    1.90              r.h = screen->h;
    1.91  
    1.92 -            SDL_FillRect (screen, &r, color);
    1.93 +            SDL_FillRect(screen, &r, color);
    1.94              t += 2;
    1.95          }
    1.96      }
    1.97  
    1.98      /* Update the screen! */
    1.99      if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
   1.100 -        SDL_Flip (screen);
   1.101 +        SDL_Flip(screen);
   1.102      } else {
   1.103 -        SDL_UpdateRects (screen, nupdates, sprite_rects);
   1.104 +        SDL_UpdateRects(screen, nupdates, sprite_rects);
   1.105      }
   1.106      sprites_visible = 1;
   1.107  }
   1.108  
   1.109  /* This is a way of telling whether or not to use hardware surfaces */
   1.110  Uint32
   1.111 -FastestFlags (Uint32 flags, int width, int height, int bpp)
   1.112 +FastestFlags(Uint32 flags, int width, int height, int bpp)
   1.113  {
   1.114      const SDL_VideoInfo *info;
   1.115  
   1.116 @@ -130,7 +129,7 @@
   1.117      flags |= SDL_FULLSCREEN;
   1.118  
   1.119      /* Check for various video capabilities */
   1.120 -    info = SDL_GetVideoInfo ();
   1.121 +    info = SDL_GetVideoInfo();
   1.122      if (info->blit_hw_CC && info->blit_fill) {
   1.123          /* We use accelerated colorkeying and color filling */
   1.124          flags |= SDL_HWSURFACE;
   1.125 @@ -154,7 +153,7 @@
   1.126  }
   1.127  
   1.128  int
   1.129 -main (int argc, char *argv[])
   1.130 +main(int argc, char *argv[])
   1.131  {
   1.132      SDL_Surface *screen;
   1.133      Uint8 *mem;
   1.134 @@ -167,8 +166,8 @@
   1.135      Uint32 then, now, frames;
   1.136  
   1.137      /* Initialize SDL */
   1.138 -    if (SDL_Init (SDL_INIT_VIDEO) < 0) {
   1.139 -        fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
   1.140 +    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   1.141 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.142          return (1);
   1.143      }
   1.144  
   1.145 @@ -180,55 +179,55 @@
   1.146      debug_flip = 0;
   1.147      while (argc > 1) {
   1.148          --argc;
   1.149 -        if (strcmp (argv[argc - 1], "-width") == 0) {
   1.150 -            width = atoi (argv[argc]);
   1.151 +        if (strcmp(argv[argc - 1], "-width") == 0) {
   1.152 +            width = atoi(argv[argc]);
   1.153              --argc;
   1.154 -        } else if (strcmp (argv[argc - 1], "-height") == 0) {
   1.155 -            height = atoi (argv[argc]);
   1.156 +        } else if (strcmp(argv[argc - 1], "-height") == 0) {
   1.157 +            height = atoi(argv[argc]);
   1.158              --argc;
   1.159 -        } else if (strcmp (argv[argc - 1], "-bpp") == 0) {
   1.160 -            video_bpp = atoi (argv[argc]);
   1.161 +        } else if (strcmp(argv[argc - 1], "-bpp") == 0) {
   1.162 +            video_bpp = atoi(argv[argc]);
   1.163              videoflags &= ~SDL_ANYFORMAT;
   1.164              --argc;
   1.165 -        } else if (strcmp (argv[argc], "-fast") == 0) {
   1.166 -            videoflags = FastestFlags (videoflags, width, height, video_bpp);
   1.167 -        } else if (strcmp (argv[argc], "-hw") == 0) {
   1.168 +        } else if (strcmp(argv[argc], "-fast") == 0) {
   1.169 +            videoflags = FastestFlags(videoflags, width, height, video_bpp);
   1.170 +        } else if (strcmp(argv[argc], "-hw") == 0) {
   1.171              videoflags ^= SDL_HWSURFACE;
   1.172 -        } else if (strcmp (argv[argc], "-flip") == 0) {
   1.173 +        } else if (strcmp(argv[argc], "-flip") == 0) {
   1.174              videoflags ^= SDL_DOUBLEBUF;
   1.175 -        } else if (strcmp (argv[argc], "-debugflip") == 0) {
   1.176 +        } else if (strcmp(argv[argc], "-debugflip") == 0) {
   1.177              debug_flip ^= 1;
   1.178 -        } else if (strcmp (argv[argc], "-fullscreen") == 0) {
   1.179 +        } else if (strcmp(argv[argc], "-fullscreen") == 0) {
   1.180              videoflags ^= SDL_FULLSCREEN;
   1.181 -        } else if (isdigit (argv[argc][0])) {
   1.182 -            numsprites = atoi (argv[argc]);
   1.183 +        } else if (isdigit(argv[argc][0])) {
   1.184 +            numsprites = atoi(argv[argc]);
   1.185          } else {
   1.186 -            fprintf (stderr,
   1.187 -                     "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
   1.188 -                     argv[0]);
   1.189 -            quit (1);
   1.190 +            fprintf(stderr,
   1.191 +                    "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
   1.192 +                    argv[0]);
   1.193 +            quit(1);
   1.194          }
   1.195      }
   1.196  
   1.197      /* Set video mode */
   1.198 -    screen = SDL_SetVideoMode (width, height, video_bpp, videoflags);
   1.199 +    screen = SDL_SetVideoMode(width, height, video_bpp, videoflags);
   1.200      if (!screen) {
   1.201 -        fprintf (stderr, "Couldn't set %dx%d video mode: %s\n",
   1.202 -                 width, height, SDL_GetError ());
   1.203 -        quit (2);
   1.204 +        fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
   1.205 +                width, height, SDL_GetError());
   1.206 +        quit(2);
   1.207      }
   1.208  
   1.209      /* Load the sprite */
   1.210 -    if (LoadSprite ("icon.bmp") < 0) {
   1.211 -        quit (1);
   1.212 +    if (LoadSprite("icon.bmp") < 0) {
   1.213 +        quit(1);
   1.214      }
   1.215  
   1.216      /* Allocate memory for the sprite info */
   1.217 -    mem = (Uint8 *) malloc (4 * sizeof (SDL_Rect) * numsprites);
   1.218 +    mem = (Uint8 *) malloc(4 * sizeof(SDL_Rect) * numsprites);
   1.219      if (mem == NULL) {
   1.220 -        SDL_FreeSurface (sprite);
   1.221 -        fprintf (stderr, "Out of memory!\n");
   1.222 -        quit (2);
   1.223 +        SDL_FreeSurface(sprite);
   1.224 +        fprintf(stderr, "Out of memory!\n");
   1.225 +        quit(2);
   1.226      }
   1.227      sprite_rects = (SDL_Rect *) mem;
   1.228      positions = sprite_rects;
   1.229 @@ -237,35 +236,35 @@
   1.230      sprite_rects += numsprites;
   1.231      sprite_w = sprite->w;
   1.232      sprite_h = sprite->h;
   1.233 -    srand (time (NULL));
   1.234 +    srand(time(NULL));
   1.235      for (i = 0; i < numsprites; ++i) {
   1.236 -        positions[i].x = rand () % (screen->w - sprite_w);
   1.237 -        positions[i].y = rand () % (screen->h - sprite_h);
   1.238 +        positions[i].x = rand() % (screen->w - sprite_w);
   1.239 +        positions[i].y = rand() % (screen->h - sprite_h);
   1.240          positions[i].w = sprite->w;
   1.241          positions[i].h = sprite->h;
   1.242          velocities[i].x = 0;
   1.243          velocities[i].y = 0;
   1.244          while (!velocities[i].x && !velocities[i].y) {
   1.245 -            velocities[i].x = (rand () % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   1.246 -            velocities[i].y = (rand () % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   1.247 +            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   1.248 +            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   1.249          }
   1.250      }
   1.251 -    background = SDL_MapRGB (screen->format, 0x00, 0x00, 0x00);
   1.252 +    background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
   1.253  
   1.254      /* Print out information about our surfaces */
   1.255 -    printf ("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel);
   1.256 +    printf("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel);
   1.257      if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
   1.258 -        printf ("Screen is in video memory\n");
   1.259 +        printf("Screen is in video memory\n");
   1.260      } else {
   1.261 -        printf ("Screen is in system memory\n");
   1.262 +        printf("Screen is in system memory\n");
   1.263      }
   1.264      if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
   1.265 -        printf ("Screen has double-buffering enabled\n");
   1.266 +        printf("Screen has double-buffering enabled\n");
   1.267      }
   1.268      if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
   1.269 -        printf ("Sprite is in video memory\n");
   1.270 +        printf("Sprite is in video memory\n");
   1.271      } else {
   1.272 -        printf ("Sprite is in system memory\n");
   1.273 +        printf("Sprite is in system memory\n");
   1.274      }
   1.275      /* Run a sample blit to trigger blit acceleration */
   1.276      {
   1.277 @@ -274,28 +273,28 @@
   1.278          dst.y = 0;
   1.279          dst.w = sprite->w;
   1.280          dst.h = sprite->h;
   1.281 -        SDL_BlitSurface (sprite, NULL, screen, &dst);
   1.282 -        SDL_FillRect (screen, &dst, background);
   1.283 +        SDL_BlitSurface(sprite, NULL, screen, &dst);
   1.284 +        SDL_FillRect(screen, &dst, background);
   1.285      }
   1.286      if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) {
   1.287 -        printf ("Sprite blit uses hardware acceleration\n");
   1.288 +        printf("Sprite blit uses hardware acceleration\n");
   1.289      }
   1.290      if ((sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
   1.291 -        printf ("Sprite blit uses RLE acceleration\n");
   1.292 +        printf("Sprite blit uses RLE acceleration\n");
   1.293      }
   1.294  
   1.295      /* Loop, blitting sprites and waiting for a keystroke */
   1.296      frames = 0;
   1.297 -    then = SDL_GetTicks ();
   1.298 +    then = SDL_GetTicks();
   1.299      done = 0;
   1.300      sprites_visible = 0;
   1.301      while (!done) {
   1.302          /* Check for events */
   1.303          ++frames;
   1.304 -        while (SDL_PollEvent (&event)) {
   1.305 +        while (SDL_PollEvent(&event)) {
   1.306              switch (event.type) {
   1.307              case SDL_MOUSEBUTTONDOWN:
   1.308 -                SDL_WarpMouse (screen->w / 2, screen->h / 2);
   1.309 +                SDL_WarpMouse(screen->w / 2, screen->h / 2);
   1.310                  break;
   1.311              case SDL_KEYDOWN:
   1.312                  /* Any keypress quits the app... */
   1.313 @@ -306,17 +305,17 @@
   1.314                  break;
   1.315              }
   1.316          }
   1.317 -        MoveSprites (screen, background);
   1.318 +        MoveSprites(screen, background);
   1.319      }
   1.320 -    SDL_FreeSurface (sprite);
   1.321 -    free (mem);
   1.322 +    SDL_FreeSurface(sprite);
   1.323 +    free(mem);
   1.324  
   1.325      /* Print out some timing information */
   1.326 -    now = SDL_GetTicks ();
   1.327 +    now = SDL_GetTicks();
   1.328      if (now > then) {
   1.329 -        printf ("%2.2f frames per second\n",
   1.330 -                ((double) frames * 1000) / (now - then));
   1.331 +        printf("%2.2f frames per second\n",
   1.332 +               ((double) frames * 1000) / (now - then));
   1.333      }
   1.334 -    SDL_Quit ();
   1.335 +    SDL_Quit();
   1.336      return (0);
   1.337  }