test/testpalette.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. MIN
  2. MAX
  3. sdlerr
  4. make_bg
  5. hflip
  6. main

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

/* [<][>][^][v][top][bottom][index][help] */