test/testpalette.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
--- a/test/testpalette.c	Mon May 29 03:53:21 2006 +0000
+++ b/test/testpalette.c	Mon May 29 04:04:35 2006 +0000
@@ -56,51 +56,51 @@
 
 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
 static void
-quit (int rc)
+quit(int rc)
 {
-    SDL_Quit ();
-    exit (rc);
+    SDL_Quit();
+    exit(rc);
 }
 
 static void
-sdlerr (char *when)
+sdlerr(char *when)
 {
-    fprintf (stderr, "SDL error: %s: %s\n", when, SDL_GetError ());
-    quit (1);
+    fprintf(stderr, "SDL error: %s: %s\n", when, SDL_GetError());
+    quit(1);
 }
 
 /* create a background surface */
 static SDL_Surface *
-make_bg (SDL_Surface * screen, int startcol)
+make_bg(SDL_Surface * screen, int startcol)
 {
     int i;
     SDL_Surface *bg =
-        SDL_CreateRGBSurface (SDL_SWSURFACE, screen->w, screen->h,
-                              8, 0, 0, 0, 0);
+        SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h,
+                             8, 0, 0, 0, 0);
     if (!bg)
-        sdlerr ("creating background surface");
+        sdlerr("creating background surface");
 
     /* set the palette to the logical screen palette so that blits
        won't be translated */
-    SDL_SetColors (bg, screen->format->palette->colors, 0, 256);
+    SDL_SetColors(bg, screen->format->palette->colors, 0, 256);
 
     /* Make a wavy background pattern using colours 0-63 */
-    if (SDL_LockSurface (bg) < 0)
-        sdlerr ("locking background");
+    if (SDL_LockSurface(bg) < 0)
+        sdlerr("locking background");
     for (i = 0; i < SCRH; i++) {
         Uint8 *p = (Uint8 *) bg->pixels + i * bg->pitch;
         int j, d;
         d = 0;
         for (j = 0; j < SCRW; j++) {
-            int v = MAX (d, -2);
-            v = MIN (v, 2);
+            int v = MAX(d, -2);
+            v = MIN(v, 2);
             if (i > 0)
                 v += p[-bg->pitch] + 65 - startcol;
             p[j] = startcol + (v & 63);
-            d += ((rand () >> 3) % 3) - 1;
+            d += ((rand() >> 3) % 3) - 1;
         }
     }
-    SDL_UnlockSurface (bg);
+    SDL_UnlockSurface(bg);
     return (bg);
 }
 
@@ -109,16 +109,16 @@
  * extension to arbitrary bitness is left as an exercise for the reader.
  */
 static SDL_Surface *
-hflip (SDL_Surface * s)
+hflip(SDL_Surface * s)
 {
     int i;
-    SDL_Surface *z = SDL_CreateRGBSurface (SDL_SWSURFACE, s->w, s->h, 8,
-                                           0, 0, 0, 0);
+    SDL_Surface *z = SDL_CreateRGBSurface(SDL_SWSURFACE, s->w, s->h, 8,
+                                          0, 0, 0, 0);
     /* copy palette */
-    SDL_SetColors (z, s->format->palette->colors,
-                   0, s->format->palette->ncolors);
-    if (SDL_LockSurface (s) < 0 || SDL_LockSurface (z) < 0)
-        sdlerr ("locking flip images");
+    SDL_SetColors(z, s->format->palette->colors,
+                  0, s->format->palette->ncolors);
+    if (SDL_LockSurface(s) < 0 || SDL_LockSurface(z) < 0)
+        sdlerr("locking flip images");
 
     for (i = 0; i < s->h; i++) {
         int j;
@@ -128,13 +128,13 @@
             to[-j] = from[j];
     }
 
-    SDL_UnlockSurface (z);
-    SDL_UnlockSurface (s);
+    SDL_UnlockSurface(z);
+    SDL_UnlockSurface(s);
     return z;
 }
 
 int
-main (int argc, char **argv)
+main(int argc, char **argv)
 {
     SDL_Color cmap[256];
     SDL_Surface *screen;
@@ -149,57 +149,56 @@
     int gamma_fade = 0;
     int gamma_ramp = 0;
 
-    if (SDL_Init (SDL_INIT_VIDEO) < 0)
-        sdlerr ("initialising SDL");
+    if (SDL_Init(SDL_INIT_VIDEO) < 0)
+        sdlerr("initialising SDL");
 
     while (--argc) {
         ++argv;
-        if (strcmp (*argv, "-hw") == 0)
+        if (strcmp(*argv, "-hw") == 0)
             vidflags |= SDL_HWSURFACE;
-        else if (strcmp (*argv, "-fullscreen") == 0)
+        else if (strcmp(*argv, "-fullscreen") == 0)
             vidflags |= SDL_FULLSCREEN;
-        else if (strcmp (*argv, "-nofade") == 0)
+        else if (strcmp(*argv, "-nofade") == 0)
             fade_max = 1;
-        else if (strcmp (*argv, "-gamma") == 0)
+        else if (strcmp(*argv, "-gamma") == 0)
             gamma_fade = 1;
-        else if (strcmp (*argv, "-gammaramp") == 0)
+        else if (strcmp(*argv, "-gammaramp") == 0)
             gamma_ramp = 1;
         else {
-            fprintf (stderr,
-                     "usage: testpalette "
-                     " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
-            quit (1);
+            fprintf(stderr,
+                    "usage: testpalette "
+                    " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
+            quit(1);
         }
     }
 
     /* Ask explicitly for 8bpp and a hardware palette */
     if ((screen =
-         SDL_SetVideoMode (SCRW, SCRH, 8, vidflags | SDL_HWPALETTE)) == NULL)
-    {
-        fprintf (stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
-                 SCRW, SCRH, SDL_GetError ());
-        quit (1);
+         SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE)) == NULL) {
+        fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
+                SCRW, SCRH, SDL_GetError());
+        quit(1);
     }
 
     if (vidflags & SDL_FULLSCREEN)
-        SDL_ShowCursor (SDL_FALSE);
+        SDL_ShowCursor(SDL_FALSE);
 
-    if ((boat[0] = SDL_LoadBMP ("sail.bmp")) == NULL)
-        sdlerr ("loading sail.bmp");
+    if ((boat[0] = SDL_LoadBMP("sail.bmp")) == NULL)
+        sdlerr("loading sail.bmp");
     /* We've chosen magenta (#ff00ff) as colour key for the boat */
-    SDL_SetColorKey (boat[0], SDL_SRCCOLORKEY | SDL_RLEACCEL,
-                     SDL_MapRGB (boat[0]->format, 0xff, 0x00, 0xff));
+    SDL_SetColorKey(boat[0], SDL_SRCCOLORKEY | SDL_RLEACCEL,
+                    SDL_MapRGB(boat[0]->format, 0xff, 0x00, 0xff));
     boatcols = boat[0]->format->palette->ncolors;
-    boat[1] = hflip (boat[0]);
-    SDL_SetColorKey (boat[1], SDL_SRCCOLORKEY | SDL_RLEACCEL,
-                     SDL_MapRGB (boat[1]->format, 0xff, 0x00, 0xff));
+    boat[1] = hflip(boat[0]);
+    SDL_SetColorKey(boat[1], SDL_SRCCOLORKEY | SDL_RLEACCEL,
+                    SDL_MapRGB(boat[1]->format, 0xff, 0x00, 0xff));
 
     /*
      * First set the physical screen palette to black, so the user won't
      * see our initial drawing on the screen.
      */
-    memset (cmap, 0, sizeof (cmap));
-    SDL_SetPalette (screen, SDL_PHYSPAL, cmap, 0, 256);
+    memset(cmap, 0, sizeof(cmap));
+    SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, 256);
 
     /*
      * Proper palette management is important when playing games with the
@@ -208,34 +207,34 @@
      * index 0..(boatcols-1):           used for the boat
      * index boatcols..(boatcols+63):   used for the waves
      */
-    SDL_SetPalette (screen, SDL_LOGPAL,
-                    boat[0]->format->palette->colors, 0, boatcols);
-    SDL_SetPalette (screen, SDL_LOGPAL, wavemap, boatcols, 64);
+    SDL_SetPalette(screen, SDL_LOGPAL,
+                   boat[0]->format->palette->colors, 0, boatcols);
+    SDL_SetPalette(screen, SDL_LOGPAL, wavemap, boatcols, 64);
 
     /*
      * Now the logical screen palette is set, and will remain unchanged.
      * The boats already have the same palette so fast blits can be used.
      */
-    memcpy (cmap, screen->format->palette->colors, 256 * sizeof (SDL_Color));
+    memcpy(cmap, screen->format->palette->colors, 256 * sizeof(SDL_Color));
 
     /* save the index of the red colour for later */
-    red = SDL_MapRGB (screen->format, 0xff, 0x00, 0x00);
+    red = SDL_MapRGB(screen->format, 0xff, 0x00, 0x00);
 
-    bg = make_bg (screen, boatcols);    /* make a nice wavy background surface */
+    bg = make_bg(screen, boatcols);     /* make a nice wavy background surface */
 
     /* initial screen contents */
-    if (SDL_BlitSurface (bg, NULL, screen, NULL) < 0)
-        sdlerr ("blitting background to screen");
-    SDL_Flip (screen);          /* actually put the background on screen */
+    if (SDL_BlitSurface(bg, NULL, screen, NULL) < 0)
+        sdlerr("blitting background to screen");
+    SDL_Flip(screen);           /* actually put the background on screen */
 
     /* determine initial boat placements */
     for (i = 0; i < NBOATS; i++) {
-        boatx[i] = (rand () % (SCRW + boat[0]->w)) - boat[0]->w;
+        boatx[i] = (rand() % (SCRW + boat[0]->w)) - boat[0]->w;
         boaty[i] = i * (SCRH - boat[0]->h) / (NBOATS - 1);
-        boatdir[i] = ((rand () >> 5) & 1) * 2 - 1;
+        boatdir[i] = ((rand() >> 5) & 1) * 2 - 1;
     }
 
-    start = SDL_GetTicks ();
+    start = SDL_GetTicks();
     frames = 0;
     fade_dir = 1;
     fade_level = 0;
@@ -246,7 +245,7 @@
         int redphase;
 
         /* A small event loop: just exit on any key or mouse button event */
-        while (SDL_PollEvent (&e)) {
+        while (SDL_PollEvent(&e)) {
             if (e.type == SDL_KEYDOWN || e.type == SDL_QUIT
                 || e.type == SDL_MOUSEBUTTONDOWN) {
                 if (fade_dir < 0)
@@ -268,11 +267,11 @@
             r.y = boaty[i];
             r.w = boat[0]->w;
             r.h = boat[0]->h;
-            if (SDL_BlitSurface (bg, &r, screen, &r) < 0)
-                sdlerr ("blitting background");
+            if (SDL_BlitSurface(bg, &r, screen, &r) < 0)
+                sdlerr("blitting background");
 
             /* construct update rectangle (bounding box of old and new pos) */
-            updates[i].x = MIN (old_x, boatx[i]);
+            updates[i].x = MIN(old_x, boatx[i]);
             updates[i].y = boaty[i];
             updates[i].w = boat[0]->w + SPEED;
             updates[i].h = boat[0]->h;
@@ -289,9 +288,9 @@
             /* paint boat on new position */
             r.x = boatx[i];
             r.y = boaty[i];
-            if (SDL_BlitSurface (boat[(boatdir[i] + 1) / 2], NULL,
-                                 screen, &r) < 0)
-                sdlerr ("blitting boat");
+            if (SDL_BlitSurface(boat[(boatdir[i] + 1) / 2], NULL,
+                                screen, &r) < 0)
+                sdlerr("blitting boat");
         }
 
         /* cycle wave palette */
@@ -305,21 +304,21 @@
             if (gamma_fade) {
                 /* Fade linearly in gamma level (lousy) */
                 float level = (float) fade_level / fade_max;
-                if (SDL_SetGamma (level, level, level) < 0)
-                    sdlerr ("setting gamma");
+                if (SDL_SetGamma(level, level, level) < 0)
+                    sdlerr("setting gamma");
 
             } else if (gamma_ramp) {
                 /* Fade using gamma ramp (better) */
                 Uint16 ramp[256];
                 for (i = 0; i < 256; i++)
                     ramp[i] = (i * fade_level / fade_max) << 8;
-                if (SDL_SetGammaRamp (ramp, ramp, ramp) < 0)
-                    sdlerr ("setting gamma ramp");
+                if (SDL_SetGammaRamp(ramp, ramp, ramp) < 0)
+                    sdlerr("setting gamma ramp");
 
             } else {
                 /* Fade using direct palette manipulation (best) */
-                memcpy (cmap, screen->format->palette->colors,
-                        boatcols * sizeof (SDL_Color));
+                memcpy(cmap, screen->format->palette->colors,
+                       boatcols * sizeof(SDL_Color));
                 for (i = 0; i < boatcols + 64; i++) {
                     cmap[i].r = cmap[i].r * fade_level / fade_max;
                     cmap[i].g = cmap[i].g * fade_level / fade_max;
@@ -332,21 +331,21 @@
 
         /* pulse the red colour (done after the fade, for a night effect) */
         redphase = frames % 64;
-        cmap[red].r = (int) (255 * sin (redphase * M_PI / 63));
+        cmap[red].r = (int) (255 * sin(redphase * M_PI / 63));
 
-        SDL_SetPalette (screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
+        SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
 
         /* update changed areas of the screen */
-        SDL_UpdateRects (screen, NBOATS, updates);
+        SDL_UpdateRects(screen, NBOATS, updates);
         frames++;
     }
     while (fade_level > 0);
 
-    printf ("%d frames, %.2f fps\n",
-            frames, 1000.0 * frames / (SDL_GetTicks () - start));
+    printf("%d frames, %.2f fps\n",
+           frames, 1000.0 * frames / (SDL_GetTicks() - start));
 
     if (vidflags & SDL_FULLSCREEN)
-        SDL_ShowCursor (SDL_TRUE);
-    SDL_Quit ();
+        SDL_ShowCursor(SDL_TRUE);
+    SDL_Quit();
     return 0;
 }