test/testpalette.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
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 /*
     2  * testpalette.c
     3  *
     4  * A simple test of runtime palette modification for animation
     5  * (using the SDL_SetPalette() API). 
     6  */
     7 
     8 #include <stdio.h>
     9 #include <stdlib.h>
    10 #include <string.h>
    11 #include <math.h>
    12 
    13 /* This isn't in the Windows headers */
    14 #ifndef M_PI
    15 #define M_PI	3.14159265358979323846
    16 #endif
    17 
    18 #include "SDL.h"
    19 
    20 /* screen size */
    21 #define SCRW 640
    22 #define SCRH 480
    23 
    24 #define NBOATS 5
    25 #define SPEED 2
    26 
    27 #ifndef MIN
    28 #define MIN(a, b) ((a) < (b) ? (a) : (b))
    29 #endif
    30 #ifndef MAX
    31 #define MAX(a, b) ((a) > (b) ? (a) : (b))
    32 #endif
    33 
    34 /*
    35  * wave colours: Made by taking a narrow cross-section of a wave picture
    36  * in Gimp, saving in PPM ascii format and formatting with Emacs macros.
    37  */
    38 static SDL_Color wavemap[] = {
    39     {0, 2, 103}, {0, 7, 110}, {0, 13, 117}, {0, 19, 125},
    40     {0, 25, 133}, {0, 31, 141}, {0, 37, 150}, {0, 43, 158},
    41     {0, 49, 166}, {0, 55, 174}, {0, 61, 182}, {0, 67, 190},
    42     {0, 73, 198}, {0, 79, 206}, {0, 86, 214}, {0, 96, 220},
    43     {5, 105, 224}, {12, 112, 226}, {19, 120, 227}, {26, 128, 229},
    44     {33, 135, 230}, {40, 143, 232}, {47, 150, 234}, {54, 158, 236},
    45     {61, 165, 238}, {68, 173, 239}, {75, 180, 241}, {82, 188, 242},
    46     {89, 195, 244}, {96, 203, 246}, {103, 210, 248}, {112, 218, 250},
    47     {124, 224, 250}, {135, 226, 251}, {146, 229, 251}, {156, 231, 252},
    48     {167, 233, 252}, {178, 236, 252}, {189, 238, 252}, {200, 240, 252},
    49     {211, 242, 252}, {222, 244, 252}, {233, 247, 252}, {242, 249, 252},
    50     {237, 250, 252}, {209, 251, 252}, {174, 251, 252}, {138, 252, 252},
    51     {102, 251, 252}, {63, 250, 252}, {24, 243, 252}, {7, 225, 252},
    52     {4, 203, 252}, {3, 181, 252}, {2, 158, 252}, {1, 136, 251},
    53     {0, 111, 248}, {0, 82, 234}, {0, 63, 213}, {0, 50, 192},
    54     {0, 39, 172}, {0, 28, 152}, {0, 17, 132}, {0, 7, 114}
    55 };
    56 
    57 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    58 static void
    59 quit (int rc)
    60 {
    61     SDL_Quit ();
    62     exit (rc);
    63 }
    64 
    65 static void
    66 sdlerr (char *when)
    67 {
    68     fprintf (stderr, "SDL error: %s: %s\n", when, SDL_GetError ());
    69     quit (1);
    70 }
    71 
    72 /* create a background surface */
    73 static SDL_Surface *
    74 make_bg (SDL_Surface * screen, int startcol)
    75 {
    76     int i;
    77     SDL_Surface *bg =
    78         SDL_CreateRGBSurface (SDL_SWSURFACE, screen->w, screen->h,
    79                               8, 0, 0, 0, 0);
    80     if (!bg)
    81         sdlerr ("creating background surface");
    82 
    83     /* set the palette to the logical screen palette so that blits
    84        won't be translated */
    85     SDL_SetColors (bg, screen->format->palette->colors, 0, 256);
    86 
    87     /* Make a wavy background pattern using colours 0-63 */
    88     if (SDL_LockSurface (bg) < 0)
    89         sdlerr ("locking background");
    90     for (i = 0; i < SCRH; i++) {
    91         Uint8 *p = (Uint8 *) bg->pixels + i * bg->pitch;
    92         int j, d;
    93         d = 0;
    94         for (j = 0; j < SCRW; j++) {
    95             int v = MAX (d, -2);
    96             v = MIN (v, 2);
    97             if (i > 0)
    98                 v += p[-bg->pitch] + 65 - startcol;
    99             p[j] = startcol + (v & 63);
   100             d += ((rand () >> 3) % 3) - 1;
   101         }
   102     }
   103     SDL_UnlockSurface (bg);
   104     return (bg);
   105 }
   106 
   107 /*
   108  * Return a surface flipped horisontally. Only works for 8bpp;
   109  * extension to arbitrary bitness is left as an exercise for the reader.
   110  */
   111 static SDL_Surface *
   112 hflip (SDL_Surface * s)
   113 {
   114     int i;
   115     SDL_Surface *z = SDL_CreateRGBSurface (SDL_SWSURFACE, s->w, s->h, 8,
   116                                            0, 0, 0, 0);
   117     /* copy palette */
   118     SDL_SetColors (z, s->format->palette->colors,
   119                    0, s->format->palette->ncolors);
   120     if (SDL_LockSurface (s) < 0 || SDL_LockSurface (z) < 0)
   121         sdlerr ("locking flip images");
   122 
   123     for (i = 0; i < s->h; i++) {
   124         int j;
   125         Uint8 *from = (Uint8 *) s->pixels + i * s->pitch;
   126         Uint8 *to = (Uint8 *) z->pixels + i * z->pitch + s->w - 1;
   127         for (j = 0; j < s->w; j++)
   128             to[-j] = from[j];
   129     }
   130 
   131     SDL_UnlockSurface (z);
   132     SDL_UnlockSurface (s);
   133     return z;
   134 }
   135 
   136 int
   137 main (int argc, char **argv)
   138 {
   139     SDL_Color cmap[256];
   140     SDL_Surface *screen;
   141     SDL_Surface *bg;
   142     SDL_Surface *boat[2];
   143     unsigned vidflags = 0;
   144     unsigned start;
   145     int fade_max = 400;
   146     int fade_level, fade_dir;
   147     int boatcols, frames, i, red;
   148     int boatx[NBOATS], boaty[NBOATS], boatdir[NBOATS];
   149     int gamma_fade = 0;
   150     int gamma_ramp = 0;
   151 
   152     if (SDL_Init (SDL_INIT_VIDEO) < 0)
   153         sdlerr ("initialising SDL");
   154 
   155     while (--argc) {
   156         ++argv;
   157         if (strcmp (*argv, "-hw") == 0)
   158             vidflags |= SDL_HWSURFACE;
   159         else if (strcmp (*argv, "-fullscreen") == 0)
   160             vidflags |= SDL_FULLSCREEN;
   161         else if (strcmp (*argv, "-nofade") == 0)
   162             fade_max = 1;
   163         else if (strcmp (*argv, "-gamma") == 0)
   164             gamma_fade = 1;
   165         else if (strcmp (*argv, "-gammaramp") == 0)
   166             gamma_ramp = 1;
   167         else {
   168             fprintf (stderr,
   169                      "usage: testpalette "
   170                      " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
   171             quit (1);
   172         }
   173     }
   174 
   175     /* Ask explicitly for 8bpp and a hardware palette */
   176     if ((screen =
   177          SDL_SetVideoMode (SCRW, SCRH, 8, vidflags | SDL_HWPALETTE)) == NULL)
   178     {
   179         fprintf (stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
   180                  SCRW, SCRH, SDL_GetError ());
   181         quit (1);
   182     }
   183 
   184     if (vidflags & SDL_FULLSCREEN)
   185         SDL_ShowCursor (SDL_FALSE);
   186 
   187     if ((boat[0] = SDL_LoadBMP ("sail.bmp")) == NULL)
   188         sdlerr ("loading sail.bmp");
   189     /* We've chosen magenta (#ff00ff) as colour key for the boat */
   190     SDL_SetColorKey (boat[0], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   191                      SDL_MapRGB (boat[0]->format, 0xff, 0x00, 0xff));
   192     boatcols = boat[0]->format->palette->ncolors;
   193     boat[1] = hflip (boat[0]);
   194     SDL_SetColorKey (boat[1], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   195                      SDL_MapRGB (boat[1]->format, 0xff, 0x00, 0xff));
   196 
   197     /*
   198      * First set the physical screen palette to black, so the user won't
   199      * see our initial drawing on the screen.
   200      */
   201     memset (cmap, 0, sizeof (cmap));
   202     SDL_SetPalette (screen, SDL_PHYSPAL, cmap, 0, 256);
   203 
   204     /*
   205      * Proper palette management is important when playing games with the
   206      * colormap. We have divided the palette as follows:
   207      *
   208      * index 0..(boatcols-1):           used for the boat
   209      * index boatcols..(boatcols+63):   used for the waves
   210      */
   211     SDL_SetPalette (screen, SDL_LOGPAL,
   212                     boat[0]->format->palette->colors, 0, boatcols);
   213     SDL_SetPalette (screen, SDL_LOGPAL, wavemap, boatcols, 64);
   214 
   215     /*
   216      * Now the logical screen palette is set, and will remain unchanged.
   217      * The boats already have the same palette so fast blits can be used.
   218      */
   219     memcpy (cmap, screen->format->palette->colors, 256 * sizeof (SDL_Color));
   220 
   221     /* save the index of the red colour for later */
   222     red = SDL_MapRGB (screen->format, 0xff, 0x00, 0x00);
   223 
   224     bg = make_bg (screen, boatcols);    /* make a nice wavy background surface */
   225 
   226     /* initial screen contents */
   227     if (SDL_BlitSurface (bg, NULL, screen, NULL) < 0)
   228         sdlerr ("blitting background to screen");
   229     SDL_Flip (screen);          /* actually put the background on screen */
   230 
   231     /* determine initial boat placements */
   232     for (i = 0; i < NBOATS; i++) {
   233         boatx[i] = (rand () % (SCRW + boat[0]->w)) - boat[0]->w;
   234         boaty[i] = i * (SCRH - boat[0]->h) / (NBOATS - 1);
   235         boatdir[i] = ((rand () >> 5) & 1) * 2 - 1;
   236     }
   237 
   238     start = SDL_GetTicks ();
   239     frames = 0;
   240     fade_dir = 1;
   241     fade_level = 0;
   242     do {
   243         SDL_Event e;
   244         SDL_Rect updates[NBOATS];
   245         SDL_Rect r;
   246         int redphase;
   247 
   248         /* A small event loop: just exit on any key or mouse button event */
   249         while (SDL_PollEvent (&e)) {
   250             if (e.type == SDL_KEYDOWN || e.type == SDL_QUIT
   251                 || e.type == SDL_MOUSEBUTTONDOWN) {
   252                 if (fade_dir < 0)
   253                     fade_level = 0;
   254                 fade_dir = -1;
   255             }
   256         }
   257 
   258         /* move boats */
   259         for (i = 0; i < NBOATS; i++) {
   260             int old_x = boatx[i];
   261             /* update boat position */
   262             boatx[i] += boatdir[i] * SPEED;
   263             if (boatx[i] <= -boat[0]->w || boatx[i] >= SCRW)
   264                 boatdir[i] = -boatdir[i];
   265 
   266             /* paint over the old boat position */
   267             r.x = old_x;
   268             r.y = boaty[i];
   269             r.w = boat[0]->w;
   270             r.h = boat[0]->h;
   271             if (SDL_BlitSurface (bg, &r, screen, &r) < 0)
   272                 sdlerr ("blitting background");
   273 
   274             /* construct update rectangle (bounding box of old and new pos) */
   275             updates[i].x = MIN (old_x, boatx[i]);
   276             updates[i].y = boaty[i];
   277             updates[i].w = boat[0]->w + SPEED;
   278             updates[i].h = boat[0]->h;
   279             /* clip update rectangle to screen */
   280             if (updates[i].x < 0) {
   281                 updates[i].w += updates[i].x;
   282                 updates[i].x = 0;
   283             }
   284             if (updates[i].x + updates[i].w > SCRW)
   285                 updates[i].w = SCRW - updates[i].x;
   286         }
   287 
   288         for (i = 0; i < NBOATS; i++) {
   289             /* paint boat on new position */
   290             r.x = boatx[i];
   291             r.y = boaty[i];
   292             if (SDL_BlitSurface (boat[(boatdir[i] + 1) / 2], NULL,
   293                                  screen, &r) < 0)
   294                 sdlerr ("blitting boat");
   295         }
   296 
   297         /* cycle wave palette */
   298         for (i = 0; i < 64; i++)
   299             cmap[boatcols + ((i + frames) & 63)] = wavemap[i];
   300 
   301         if (fade_dir) {
   302             /* Fade the entire palette in/out */
   303             fade_level += fade_dir;
   304 
   305             if (gamma_fade) {
   306                 /* Fade linearly in gamma level (lousy) */
   307                 float level = (float) fade_level / fade_max;
   308                 if (SDL_SetGamma (level, level, level) < 0)
   309                     sdlerr ("setting gamma");
   310 
   311             } else if (gamma_ramp) {
   312                 /* Fade using gamma ramp (better) */
   313                 Uint16 ramp[256];
   314                 for (i = 0; i < 256; i++)
   315                     ramp[i] = (i * fade_level / fade_max) << 8;
   316                 if (SDL_SetGammaRamp (ramp, ramp, ramp) < 0)
   317                     sdlerr ("setting gamma ramp");
   318 
   319             } else {
   320                 /* Fade using direct palette manipulation (best) */
   321                 memcpy (cmap, screen->format->palette->colors,
   322                         boatcols * sizeof (SDL_Color));
   323                 for (i = 0; i < boatcols + 64; i++) {
   324                     cmap[i].r = cmap[i].r * fade_level / fade_max;
   325                     cmap[i].g = cmap[i].g * fade_level / fade_max;
   326                     cmap[i].b = cmap[i].b * fade_level / fade_max;
   327                 }
   328             }
   329             if (fade_level == fade_max)
   330                 fade_dir = 0;
   331         }
   332 
   333         /* pulse the red colour (done after the fade, for a night effect) */
   334         redphase = frames % 64;
   335         cmap[red].r = (int) (255 * sin (redphase * M_PI / 63));
   336 
   337         SDL_SetPalette (screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
   338 
   339         /* update changed areas of the screen */
   340         SDL_UpdateRects (screen, NBOATS, updates);
   341         frames++;
   342     }
   343     while (fade_level > 0);
   344 
   345     printf ("%d frames, %.2f fps\n",
   346             frames, 1000.0 * frames / (SDL_GetTicks () - start));
   347 
   348     if (vidflags & SDL_FULLSCREEN)
   349         SDL_ShowCursor (SDL_TRUE);
   350     SDL_Quit ();
   351     return 0;
   352 }