test/testpalette.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
     1.1 --- a/test/testpalette.c	Mon May 29 03:53:21 2006 +0000
     1.2 +++ b/test/testpalette.c	Mon May 29 04:04:35 2006 +0000
     1.3 @@ -56,51 +56,51 @@
     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  static void
    1.17 -sdlerr (char *when)
    1.18 +sdlerr(char *when)
    1.19  {
    1.20 -    fprintf (stderr, "SDL error: %s: %s\n", when, SDL_GetError ());
    1.21 -    quit (1);
    1.22 +    fprintf(stderr, "SDL error: %s: %s\n", when, SDL_GetError());
    1.23 +    quit(1);
    1.24  }
    1.25  
    1.26  /* create a background surface */
    1.27  static SDL_Surface *
    1.28 -make_bg (SDL_Surface * screen, int startcol)
    1.29 +make_bg(SDL_Surface * screen, int startcol)
    1.30  {
    1.31      int i;
    1.32      SDL_Surface *bg =
    1.33 -        SDL_CreateRGBSurface (SDL_SWSURFACE, screen->w, screen->h,
    1.34 -                              8, 0, 0, 0, 0);
    1.35 +        SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h,
    1.36 +                             8, 0, 0, 0, 0);
    1.37      if (!bg)
    1.38 -        sdlerr ("creating background surface");
    1.39 +        sdlerr("creating background surface");
    1.40  
    1.41      /* set the palette to the logical screen palette so that blits
    1.42         won't be translated */
    1.43 -    SDL_SetColors (bg, screen->format->palette->colors, 0, 256);
    1.44 +    SDL_SetColors(bg, screen->format->palette->colors, 0, 256);
    1.45  
    1.46      /* Make a wavy background pattern using colours 0-63 */
    1.47 -    if (SDL_LockSurface (bg) < 0)
    1.48 -        sdlerr ("locking background");
    1.49 +    if (SDL_LockSurface(bg) < 0)
    1.50 +        sdlerr("locking background");
    1.51      for (i = 0; i < SCRH; i++) {
    1.52          Uint8 *p = (Uint8 *) bg->pixels + i * bg->pitch;
    1.53          int j, d;
    1.54          d = 0;
    1.55          for (j = 0; j < SCRW; j++) {
    1.56 -            int v = MAX (d, -2);
    1.57 -            v = MIN (v, 2);
    1.58 +            int v = MAX(d, -2);
    1.59 +            v = MIN(v, 2);
    1.60              if (i > 0)
    1.61                  v += p[-bg->pitch] + 65 - startcol;
    1.62              p[j] = startcol + (v & 63);
    1.63 -            d += ((rand () >> 3) % 3) - 1;
    1.64 +            d += ((rand() >> 3) % 3) - 1;
    1.65          }
    1.66      }
    1.67 -    SDL_UnlockSurface (bg);
    1.68 +    SDL_UnlockSurface(bg);
    1.69      return (bg);
    1.70  }
    1.71  
    1.72 @@ -109,16 +109,16 @@
    1.73   * extension to arbitrary bitness is left as an exercise for the reader.
    1.74   */
    1.75  static SDL_Surface *
    1.76 -hflip (SDL_Surface * s)
    1.77 +hflip(SDL_Surface * s)
    1.78  {
    1.79      int i;
    1.80 -    SDL_Surface *z = SDL_CreateRGBSurface (SDL_SWSURFACE, s->w, s->h, 8,
    1.81 -                                           0, 0, 0, 0);
    1.82 +    SDL_Surface *z = SDL_CreateRGBSurface(SDL_SWSURFACE, s->w, s->h, 8,
    1.83 +                                          0, 0, 0, 0);
    1.84      /* copy palette */
    1.85 -    SDL_SetColors (z, s->format->palette->colors,
    1.86 -                   0, s->format->palette->ncolors);
    1.87 -    if (SDL_LockSurface (s) < 0 || SDL_LockSurface (z) < 0)
    1.88 -        sdlerr ("locking flip images");
    1.89 +    SDL_SetColors(z, s->format->palette->colors,
    1.90 +                  0, s->format->palette->ncolors);
    1.91 +    if (SDL_LockSurface(s) < 0 || SDL_LockSurface(z) < 0)
    1.92 +        sdlerr("locking flip images");
    1.93  
    1.94      for (i = 0; i < s->h; i++) {
    1.95          int j;
    1.96 @@ -128,13 +128,13 @@
    1.97              to[-j] = from[j];
    1.98      }
    1.99  
   1.100 -    SDL_UnlockSurface (z);
   1.101 -    SDL_UnlockSurface (s);
   1.102 +    SDL_UnlockSurface(z);
   1.103 +    SDL_UnlockSurface(s);
   1.104      return z;
   1.105  }
   1.106  
   1.107  int
   1.108 -main (int argc, char **argv)
   1.109 +main(int argc, char **argv)
   1.110  {
   1.111      SDL_Color cmap[256];
   1.112      SDL_Surface *screen;
   1.113 @@ -149,57 +149,56 @@
   1.114      int gamma_fade = 0;
   1.115      int gamma_ramp = 0;
   1.116  
   1.117 -    if (SDL_Init (SDL_INIT_VIDEO) < 0)
   1.118 -        sdlerr ("initialising SDL");
   1.119 +    if (SDL_Init(SDL_INIT_VIDEO) < 0)
   1.120 +        sdlerr("initialising SDL");
   1.121  
   1.122      while (--argc) {
   1.123          ++argv;
   1.124 -        if (strcmp (*argv, "-hw") == 0)
   1.125 +        if (strcmp(*argv, "-hw") == 0)
   1.126              vidflags |= SDL_HWSURFACE;
   1.127 -        else if (strcmp (*argv, "-fullscreen") == 0)
   1.128 +        else if (strcmp(*argv, "-fullscreen") == 0)
   1.129              vidflags |= SDL_FULLSCREEN;
   1.130 -        else if (strcmp (*argv, "-nofade") == 0)
   1.131 +        else if (strcmp(*argv, "-nofade") == 0)
   1.132              fade_max = 1;
   1.133 -        else if (strcmp (*argv, "-gamma") == 0)
   1.134 +        else if (strcmp(*argv, "-gamma") == 0)
   1.135              gamma_fade = 1;
   1.136 -        else if (strcmp (*argv, "-gammaramp") == 0)
   1.137 +        else if (strcmp(*argv, "-gammaramp") == 0)
   1.138              gamma_ramp = 1;
   1.139          else {
   1.140 -            fprintf (stderr,
   1.141 -                     "usage: testpalette "
   1.142 -                     " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
   1.143 -            quit (1);
   1.144 +            fprintf(stderr,
   1.145 +                    "usage: testpalette "
   1.146 +                    " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
   1.147 +            quit(1);
   1.148          }
   1.149      }
   1.150  
   1.151      /* Ask explicitly for 8bpp and a hardware palette */
   1.152      if ((screen =
   1.153 -         SDL_SetVideoMode (SCRW, SCRH, 8, vidflags | SDL_HWPALETTE)) == NULL)
   1.154 -    {
   1.155 -        fprintf (stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
   1.156 -                 SCRW, SCRH, SDL_GetError ());
   1.157 -        quit (1);
   1.158 +         SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE)) == NULL) {
   1.159 +        fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
   1.160 +                SCRW, SCRH, SDL_GetError());
   1.161 +        quit(1);
   1.162      }
   1.163  
   1.164      if (vidflags & SDL_FULLSCREEN)
   1.165 -        SDL_ShowCursor (SDL_FALSE);
   1.166 +        SDL_ShowCursor(SDL_FALSE);
   1.167  
   1.168 -    if ((boat[0] = SDL_LoadBMP ("sail.bmp")) == NULL)
   1.169 -        sdlerr ("loading sail.bmp");
   1.170 +    if ((boat[0] = SDL_LoadBMP("sail.bmp")) == NULL)
   1.171 +        sdlerr("loading sail.bmp");
   1.172      /* We've chosen magenta (#ff00ff) as colour key for the boat */
   1.173 -    SDL_SetColorKey (boat[0], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   1.174 -                     SDL_MapRGB (boat[0]->format, 0xff, 0x00, 0xff));
   1.175 +    SDL_SetColorKey(boat[0], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   1.176 +                    SDL_MapRGB(boat[0]->format, 0xff, 0x00, 0xff));
   1.177      boatcols = boat[0]->format->palette->ncolors;
   1.178 -    boat[1] = hflip (boat[0]);
   1.179 -    SDL_SetColorKey (boat[1], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   1.180 -                     SDL_MapRGB (boat[1]->format, 0xff, 0x00, 0xff));
   1.181 +    boat[1] = hflip(boat[0]);
   1.182 +    SDL_SetColorKey(boat[1], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   1.183 +                    SDL_MapRGB(boat[1]->format, 0xff, 0x00, 0xff));
   1.184  
   1.185      /*
   1.186       * First set the physical screen palette to black, so the user won't
   1.187       * see our initial drawing on the screen.
   1.188       */
   1.189 -    memset (cmap, 0, sizeof (cmap));
   1.190 -    SDL_SetPalette (screen, SDL_PHYSPAL, cmap, 0, 256);
   1.191 +    memset(cmap, 0, sizeof(cmap));
   1.192 +    SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, 256);
   1.193  
   1.194      /*
   1.195       * Proper palette management is important when playing games with the
   1.196 @@ -208,34 +207,34 @@
   1.197       * index 0..(boatcols-1):           used for the boat
   1.198       * index boatcols..(boatcols+63):   used for the waves
   1.199       */
   1.200 -    SDL_SetPalette (screen, SDL_LOGPAL,
   1.201 -                    boat[0]->format->palette->colors, 0, boatcols);
   1.202 -    SDL_SetPalette (screen, SDL_LOGPAL, wavemap, boatcols, 64);
   1.203 +    SDL_SetPalette(screen, SDL_LOGPAL,
   1.204 +                   boat[0]->format->palette->colors, 0, boatcols);
   1.205 +    SDL_SetPalette(screen, SDL_LOGPAL, wavemap, boatcols, 64);
   1.206  
   1.207      /*
   1.208       * Now the logical screen palette is set, and will remain unchanged.
   1.209       * The boats already have the same palette so fast blits can be used.
   1.210       */
   1.211 -    memcpy (cmap, screen->format->palette->colors, 256 * sizeof (SDL_Color));
   1.212 +    memcpy(cmap, screen->format->palette->colors, 256 * sizeof(SDL_Color));
   1.213  
   1.214      /* save the index of the red colour for later */
   1.215 -    red = SDL_MapRGB (screen->format, 0xff, 0x00, 0x00);
   1.216 +    red = SDL_MapRGB(screen->format, 0xff, 0x00, 0x00);
   1.217  
   1.218 -    bg = make_bg (screen, boatcols);    /* make a nice wavy background surface */
   1.219 +    bg = make_bg(screen, boatcols);     /* make a nice wavy background surface */
   1.220  
   1.221      /* initial screen contents */
   1.222 -    if (SDL_BlitSurface (bg, NULL, screen, NULL) < 0)
   1.223 -        sdlerr ("blitting background to screen");
   1.224 -    SDL_Flip (screen);          /* actually put the background on screen */
   1.225 +    if (SDL_BlitSurface(bg, NULL, screen, NULL) < 0)
   1.226 +        sdlerr("blitting background to screen");
   1.227 +    SDL_Flip(screen);           /* actually put the background on screen */
   1.228  
   1.229      /* determine initial boat placements */
   1.230      for (i = 0; i < NBOATS; i++) {
   1.231 -        boatx[i] = (rand () % (SCRW + boat[0]->w)) - boat[0]->w;
   1.232 +        boatx[i] = (rand() % (SCRW + boat[0]->w)) - boat[0]->w;
   1.233          boaty[i] = i * (SCRH - boat[0]->h) / (NBOATS - 1);
   1.234 -        boatdir[i] = ((rand () >> 5) & 1) * 2 - 1;
   1.235 +        boatdir[i] = ((rand() >> 5) & 1) * 2 - 1;
   1.236      }
   1.237  
   1.238 -    start = SDL_GetTicks ();
   1.239 +    start = SDL_GetTicks();
   1.240      frames = 0;
   1.241      fade_dir = 1;
   1.242      fade_level = 0;
   1.243 @@ -246,7 +245,7 @@
   1.244          int redphase;
   1.245  
   1.246          /* A small event loop: just exit on any key or mouse button event */
   1.247 -        while (SDL_PollEvent (&e)) {
   1.248 +        while (SDL_PollEvent(&e)) {
   1.249              if (e.type == SDL_KEYDOWN || e.type == SDL_QUIT
   1.250                  || e.type == SDL_MOUSEBUTTONDOWN) {
   1.251                  if (fade_dir < 0)
   1.252 @@ -268,11 +267,11 @@
   1.253              r.y = boaty[i];
   1.254              r.w = boat[0]->w;
   1.255              r.h = boat[0]->h;
   1.256 -            if (SDL_BlitSurface (bg, &r, screen, &r) < 0)
   1.257 -                sdlerr ("blitting background");
   1.258 +            if (SDL_BlitSurface(bg, &r, screen, &r) < 0)
   1.259 +                sdlerr("blitting background");
   1.260  
   1.261              /* construct update rectangle (bounding box of old and new pos) */
   1.262 -            updates[i].x = MIN (old_x, boatx[i]);
   1.263 +            updates[i].x = MIN(old_x, boatx[i]);
   1.264              updates[i].y = boaty[i];
   1.265              updates[i].w = boat[0]->w + SPEED;
   1.266              updates[i].h = boat[0]->h;
   1.267 @@ -289,9 +288,9 @@
   1.268              /* paint boat on new position */
   1.269              r.x = boatx[i];
   1.270              r.y = boaty[i];
   1.271 -            if (SDL_BlitSurface (boat[(boatdir[i] + 1) / 2], NULL,
   1.272 -                                 screen, &r) < 0)
   1.273 -                sdlerr ("blitting boat");
   1.274 +            if (SDL_BlitSurface(boat[(boatdir[i] + 1) / 2], NULL,
   1.275 +                                screen, &r) < 0)
   1.276 +                sdlerr("blitting boat");
   1.277          }
   1.278  
   1.279          /* cycle wave palette */
   1.280 @@ -305,21 +304,21 @@
   1.281              if (gamma_fade) {
   1.282                  /* Fade linearly in gamma level (lousy) */
   1.283                  float level = (float) fade_level / fade_max;
   1.284 -                if (SDL_SetGamma (level, level, level) < 0)
   1.285 -                    sdlerr ("setting gamma");
   1.286 +                if (SDL_SetGamma(level, level, level) < 0)
   1.287 +                    sdlerr("setting gamma");
   1.288  
   1.289              } else if (gamma_ramp) {
   1.290                  /* Fade using gamma ramp (better) */
   1.291                  Uint16 ramp[256];
   1.292                  for (i = 0; i < 256; i++)
   1.293                      ramp[i] = (i * fade_level / fade_max) << 8;
   1.294 -                if (SDL_SetGammaRamp (ramp, ramp, ramp) < 0)
   1.295 -                    sdlerr ("setting gamma ramp");
   1.296 +                if (SDL_SetGammaRamp(ramp, ramp, ramp) < 0)
   1.297 +                    sdlerr("setting gamma ramp");
   1.298  
   1.299              } else {
   1.300                  /* Fade using direct palette manipulation (best) */
   1.301 -                memcpy (cmap, screen->format->palette->colors,
   1.302 -                        boatcols * sizeof (SDL_Color));
   1.303 +                memcpy(cmap, screen->format->palette->colors,
   1.304 +                       boatcols * sizeof(SDL_Color));
   1.305                  for (i = 0; i < boatcols + 64; i++) {
   1.306                      cmap[i].r = cmap[i].r * fade_level / fade_max;
   1.307                      cmap[i].g = cmap[i].g * fade_level / fade_max;
   1.308 @@ -332,21 +331,21 @@
   1.309  
   1.310          /* pulse the red colour (done after the fade, for a night effect) */
   1.311          redphase = frames % 64;
   1.312 -        cmap[red].r = (int) (255 * sin (redphase * M_PI / 63));
   1.313 +        cmap[red].r = (int) (255 * sin(redphase * M_PI / 63));
   1.314  
   1.315 -        SDL_SetPalette (screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
   1.316 +        SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
   1.317  
   1.318          /* update changed areas of the screen */
   1.319 -        SDL_UpdateRects (screen, NBOATS, updates);
   1.320 +        SDL_UpdateRects(screen, NBOATS, updates);
   1.321          frames++;
   1.322      }
   1.323      while (fade_level > 0);
   1.324  
   1.325 -    printf ("%d frames, %.2f fps\n",
   1.326 -            frames, 1000.0 * frames / (SDL_GetTicks () - start));
   1.327 +    printf("%d frames, %.2f fps\n",
   1.328 +           frames, 1000.0 * frames / (SDL_GetTicks() - start));
   1.329  
   1.330      if (vidflags & SDL_FULLSCREEN)
   1.331 -        SDL_ShowCursor (SDL_TRUE);
   1.332 -    SDL_Quit ();
   1.333 +        SDL_ShowCursor(SDL_TRUE);
   1.334 +    SDL_Quit();
   1.335      return 0;
   1.336  }