SDL doesn't actually support the physical/logical palette split anymore.
authorSam Lantinga <slouken@libsdl.org>
Tue, 01 Feb 2011 21:51:09 -0800
changeset 51518ef640410ac9
parent 5150 1435f8a6425c
child 5152 3b85dfaf2893
SDL doesn't actually support the physical/logical palette split anymore.
test/Makefile.in
test/sail.bmp
test/testpalette.c
     1.1 --- a/test/Makefile.in	Tue Feb 01 21:40:03 2011 -0800
     1.2 +++ b/test/Makefile.in	Tue Feb 01 21:51:09 2011 -0800
     1.3 @@ -7,7 +7,7 @@
     1.4  CFLAGS  = @CFLAGS@
     1.5  LIBS	= @LIBS@
     1.6  
     1.7 -TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testatomic$(EXE) testaudioinfo$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcursor$(EXE) testdraw2$(EXE) testdyngles$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testfill$(EXE) testgamma$(EXE) testgl2$(EXE) testgles$(EXE) testgl$(EXE) testhaptic$(EXE) testhread$(EXE) testiconv$(EXE) testime$(EXE) testintersections$(EXE) testjoystick$(EXE) testkeys$(EXE) testloadso$(EXE) testlock$(EXE) testmultiaudio$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testpower$(EXE) testresample$(EXE) testsem$(EXE) testshape$(EXE) testsprite2$(EXE) testsprite$(EXE) testspriteminimal$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm2$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE) testgesture$(EXE)
     1.8 +TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testatomic$(EXE) testaudioinfo$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcursor$(EXE) testdraw2$(EXE) testdyngles$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testfill$(EXE) testgamma$(EXE) testgl2$(EXE) testgles$(EXE) testgl$(EXE) testhaptic$(EXE) testhread$(EXE) testiconv$(EXE) testime$(EXE) testintersections$(EXE) testjoystick$(EXE) testkeys$(EXE) testloadso$(EXE) testlock$(EXE) testmultiaudio$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testplatform$(EXE) testpower$(EXE) testresample$(EXE) testsem$(EXE) testshape$(EXE) testsprite2$(EXE) testsprite$(EXE) testspriteminimal$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm2$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE) testgesture$(EXE)
     1.9  
    1.10  all: Makefile $(TARGETS)
    1.11  
    1.12 @@ -101,9 +101,6 @@
    1.13  testoverlay$(EXE): $(srcdir)/testoverlay.c
    1.14  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.15  
    1.16 -testpalette$(EXE): $(srcdir)/testpalette.c
    1.17 -	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
    1.18 -
    1.19  testplatform$(EXE): $(srcdir)/testplatform.c
    1.20  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.21  
     2.1 Binary file test/sail.bmp has changed
     3.1 --- a/test/testpalette.c	Tue Feb 01 21:40:03 2011 -0800
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,347 +0,0 @@
     3.4 -/*
     3.5 - * testpalette.c
     3.6 - *
     3.7 - * A simple test of runtime palette modification for animation
     3.8 - * (using the SDL_SetPalette() API). 
     3.9 - */
    3.10 -
    3.11 -#include <stdio.h>
    3.12 -#include <stdlib.h>
    3.13 -#include <string.h>
    3.14 -#include <math.h>
    3.15 -
    3.16 -#include "SDL.h"
    3.17 -
    3.18 -/* screen size */
    3.19 -#define SCRW 640
    3.20 -#define SCRH 480
    3.21 -
    3.22 -#define NBOATS 5
    3.23 -#define SPEED 2
    3.24 -
    3.25 -#ifndef MIN
    3.26 -#define MIN(a, b) ((a) < (b) ? (a) : (b))
    3.27 -#endif
    3.28 -#ifndef MAX
    3.29 -#define MAX(a, b) ((a) > (b) ? (a) : (b))
    3.30 -#endif
    3.31 -
    3.32 -/*
    3.33 - * wave colours: Made by taking a narrow cross-section of a wave picture
    3.34 - * in Gimp, saving in PPM ascii format and formatting with Emacs macros.
    3.35 - */
    3.36 -static SDL_Color wavemap[] = {
    3.37 -    {0, 2, 103}, {0, 7, 110}, {0, 13, 117}, {0, 19, 125},
    3.38 -    {0, 25, 133}, {0, 31, 141}, {0, 37, 150}, {0, 43, 158},
    3.39 -    {0, 49, 166}, {0, 55, 174}, {0, 61, 182}, {0, 67, 190},
    3.40 -    {0, 73, 198}, {0, 79, 206}, {0, 86, 214}, {0, 96, 220},
    3.41 -    {5, 105, 224}, {12, 112, 226}, {19, 120, 227}, {26, 128, 229},
    3.42 -    {33, 135, 230}, {40, 143, 232}, {47, 150, 234}, {54, 158, 236},
    3.43 -    {61, 165, 238}, {68, 173, 239}, {75, 180, 241}, {82, 188, 242},
    3.44 -    {89, 195, 244}, {96, 203, 246}, {103, 210, 248}, {112, 218, 250},
    3.45 -    {124, 224, 250}, {135, 226, 251}, {146, 229, 251}, {156, 231, 252},
    3.46 -    {167, 233, 252}, {178, 236, 252}, {189, 238, 252}, {200, 240, 252},
    3.47 -    {211, 242, 252}, {222, 244, 252}, {233, 247, 252}, {242, 249, 252},
    3.48 -    {237, 250, 252}, {209, 251, 252}, {174, 251, 252}, {138, 252, 252},
    3.49 -    {102, 251, 252}, {63, 250, 252}, {24, 243, 252}, {7, 225, 252},
    3.50 -    {4, 203, 252}, {3, 181, 252}, {2, 158, 252}, {1, 136, 251},
    3.51 -    {0, 111, 248}, {0, 82, 234}, {0, 63, 213}, {0, 50, 192},
    3.52 -    {0, 39, 172}, {0, 28, 152}, {0, 17, 132}, {0, 7, 114}
    3.53 -};
    3.54 -
    3.55 -/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    3.56 -static void
    3.57 -quit(int rc)
    3.58 -{
    3.59 -    SDL_Quit();
    3.60 -    exit(rc);
    3.61 -}
    3.62 -
    3.63 -static void
    3.64 -sdlerr(char *when)
    3.65 -{
    3.66 -    fprintf(stderr, "SDL error: %s: %s\n", when, SDL_GetError());
    3.67 -    quit(1);
    3.68 -}
    3.69 -
    3.70 -/* create a background surface */
    3.71 -static SDL_Surface *
    3.72 -make_bg(SDL_Surface * screen, int startcol)
    3.73 -{
    3.74 -    int i;
    3.75 -    SDL_Surface *bg =
    3.76 -        SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h,
    3.77 -                             8, 0, 0, 0, 0);
    3.78 -    if (!bg)
    3.79 -        sdlerr("creating background surface");
    3.80 -
    3.81 -    /* set the palette to the logical screen palette so that blits
    3.82 -       won't be translated */
    3.83 -    SDL_SetSurfacePalette(bg, screen->format->palette);
    3.84 -
    3.85 -    /* Make a wavy background pattern using colours 0-63 */
    3.86 -    if (SDL_LockSurface(bg) < 0)
    3.87 -        sdlerr("locking background");
    3.88 -    for (i = 0; i < SCRH; i++) {
    3.89 -        Uint8 *p = (Uint8 *) bg->pixels + i * bg->pitch;
    3.90 -        int j, d;
    3.91 -        d = 0;
    3.92 -        for (j = 0; j < SCRW; j++) {
    3.93 -            int v = MAX(d, -2);
    3.94 -            v = MIN(v, 2);
    3.95 -            if (i > 0)
    3.96 -                v += p[-bg->pitch] + 65 - startcol;
    3.97 -            p[j] = startcol + (v & 63);
    3.98 -            d += ((rand() >> 3) % 3) - 1;
    3.99 -        }
   3.100 -    }
   3.101 -    SDL_UnlockSurface(bg);
   3.102 -    return (bg);
   3.103 -}
   3.104 -
   3.105 -/*
   3.106 - * Return a surface flipped horisontally. Only works for 8bpp;
   3.107 - * extension to arbitrary bitness is left as an exercise for the reader.
   3.108 - */
   3.109 -static SDL_Surface *
   3.110 -hflip(SDL_Surface * s)
   3.111 -{
   3.112 -    int i;
   3.113 -    SDL_Surface *z = SDL_CreateRGBSurface(SDL_SWSURFACE, s->w, s->h, 8,
   3.114 -                                          0, 0, 0, 0);
   3.115 -    /* copy palette */
   3.116 -    SDL_SetColors(z, s->format->palette->colors,
   3.117 -                  0, s->format->palette->ncolors);
   3.118 -    if (SDL_LockSurface(s) < 0 || SDL_LockSurface(z) < 0)
   3.119 -        sdlerr("locking flip images");
   3.120 -
   3.121 -    for (i = 0; i < s->h; i++) {
   3.122 -        int j;
   3.123 -        Uint8 *from = (Uint8 *) s->pixels + i * s->pitch;
   3.124 -        Uint8 *to = (Uint8 *) z->pixels + i * z->pitch + s->w - 1;
   3.125 -        for (j = 0; j < s->w; j++)
   3.126 -            to[-j] = from[j];
   3.127 -    }
   3.128 -
   3.129 -    SDL_UnlockSurface(z);
   3.130 -    SDL_UnlockSurface(s);
   3.131 -    return z;
   3.132 -}
   3.133 -
   3.134 -int
   3.135 -main(int argc, char **argv)
   3.136 -{
   3.137 -    SDL_Color cmap[256];
   3.138 -    SDL_Surface *screen;
   3.139 -    SDL_Surface *bg;
   3.140 -    SDL_Surface *boat[2];
   3.141 -    unsigned vidflags = 0;
   3.142 -    unsigned start;
   3.143 -    int fade_max = 400;
   3.144 -    int fade_level, fade_dir;
   3.145 -    int boatcols, frames, i, red;
   3.146 -    int boatx[NBOATS], boaty[NBOATS], boatdir[NBOATS];
   3.147 -    int gamma_fade = 0;
   3.148 -    int gamma_ramp = 0;
   3.149 -
   3.150 -    if (SDL_Init(SDL_INIT_VIDEO) < 0)
   3.151 -        sdlerr("initialising SDL");
   3.152 -
   3.153 -    while (--argc) {
   3.154 -        ++argv;
   3.155 -        if (strcmp(*argv, "-hw") == 0)
   3.156 -            vidflags |= SDL_HWSURFACE;
   3.157 -        else if (strcmp(*argv, "-fullscreen") == 0)
   3.158 -            vidflags |= SDL_FULLSCREEN;
   3.159 -        else if (strcmp(*argv, "-nofade") == 0)
   3.160 -            fade_max = 1;
   3.161 -        else if (strcmp(*argv, "-gamma") == 0)
   3.162 -            gamma_fade = 1;
   3.163 -        else if (strcmp(*argv, "-gammaramp") == 0)
   3.164 -            gamma_ramp = 1;
   3.165 -        else {
   3.166 -            fprintf(stderr,
   3.167 -                    "usage: testpalette "
   3.168 -                    " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
   3.169 -            quit(1);
   3.170 -        }
   3.171 -    }
   3.172 -
   3.173 -    /* Ask explicitly for 8bpp and a hardware palette */
   3.174 -    if ((screen =
   3.175 -         SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE)) == NULL) {
   3.176 -        fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
   3.177 -                SCRW, SCRH, SDL_GetError());
   3.178 -        quit(1);
   3.179 -    }
   3.180 -
   3.181 -    if (vidflags & SDL_FULLSCREEN)
   3.182 -        SDL_ShowCursor(SDL_FALSE);
   3.183 -
   3.184 -    if ((boat[0] = SDL_LoadBMP("sail.bmp")) == NULL)
   3.185 -        sdlerr("loading sail.bmp");
   3.186 -    /* We've chosen magenta (#ff00ff) as colour key for the boat */
   3.187 -    SDL_SetColorKey(boat[0], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   3.188 -                    SDL_MapRGB(boat[0]->format, 0xff, 0x00, 0xff));
   3.189 -    boatcols = boat[0]->format->palette->ncolors;
   3.190 -    if (boatcols >= 256)
   3.191 -        sdlerr("too many colors in sail.bmp");
   3.192 -    boat[1] = hflip(boat[0]);
   3.193 -    SDL_SetColorKey(boat[1], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   3.194 -                    SDL_MapRGB(boat[1]->format, 0xff, 0x00, 0xff));
   3.195 -
   3.196 -    /*
   3.197 -     * First set the physical screen palette to black, so the user won't
   3.198 -     * see our initial drawing on the screen.
   3.199 -     */
   3.200 -    memset(cmap, 0, sizeof(cmap));
   3.201 -    SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, 256);
   3.202 -
   3.203 -    /*
   3.204 -     * Proper palette management is important when playing games with the
   3.205 -     * colormap. We have divided the palette as follows:
   3.206 -     *
   3.207 -     * index 0..(boatcols-1):           used for the boat
   3.208 -     * index boatcols..(boatcols+63):   used for the waves
   3.209 -     */
   3.210 -    SDL_SetPalette(screen, SDL_LOGPAL,
   3.211 -                   boat[0]->format->palette->colors, 0, boatcols);
   3.212 -    SDL_SetPalette(screen, SDL_LOGPAL, wavemap, boatcols, 64);
   3.213 -
   3.214 -    /*
   3.215 -     * Now the logical screen palette is set, and will remain unchanged.
   3.216 -     * The boats already have the same palette so fast blits can be used.
   3.217 -     */
   3.218 -    memcpy(cmap, screen->format->palette->colors, 256 * sizeof(SDL_Color));
   3.219 -
   3.220 -    /* save the index of the red colour for later */
   3.221 -    red = SDL_MapRGB(screen->format, 0xff, 0x00, 0x00);
   3.222 -
   3.223 -    bg = make_bg(screen, boatcols);     /* make a nice wavy background surface */
   3.224 -
   3.225 -    /* initial screen contents */
   3.226 -    if (SDL_BlitSurface(bg, NULL, screen, NULL) < 0)
   3.227 -        sdlerr("blitting background to screen");
   3.228 -    SDL_Flip(screen);           /* actually put the background on screen */
   3.229 -
   3.230 -    /* determine initial boat placements */
   3.231 -    for (i = 0; i < NBOATS; i++) {
   3.232 -        boatx[i] = (rand() % (SCRW + boat[0]->w)) - boat[0]->w;
   3.233 -        boaty[i] = i * (SCRH - boat[0]->h) / (NBOATS - 1);
   3.234 -        boatdir[i] = ((rand() >> 5) & 1) * 2 - 1;
   3.235 -    }
   3.236 -
   3.237 -    start = SDL_GetTicks();
   3.238 -    frames = 0;
   3.239 -    fade_dir = 1;
   3.240 -    fade_level = 0;
   3.241 -    do {
   3.242 -        SDL_Event e;
   3.243 -        SDL_Rect updates[NBOATS];
   3.244 -        SDL_Rect r;
   3.245 -        int redphase;
   3.246 -
   3.247 -        /* A small event loop: just exit on any key or mouse button event */
   3.248 -        while (SDL_PollEvent(&e)) {
   3.249 -            if (e.type == SDL_KEYDOWN || e.type == SDL_QUIT
   3.250 -                || e.type == SDL_MOUSEBUTTONDOWN) {
   3.251 -                if (fade_dir < 0)
   3.252 -                    fade_level = 0;
   3.253 -                fade_dir = -1;
   3.254 -            }
   3.255 -        }
   3.256 -
   3.257 -        /* move boats */
   3.258 -        for (i = 0; i < NBOATS; i++) {
   3.259 -            int old_x = boatx[i];
   3.260 -            /* update boat position */
   3.261 -            boatx[i] += boatdir[i] * SPEED;
   3.262 -            if (boatx[i] <= -boat[0]->w || boatx[i] >= SCRW)
   3.263 -                boatdir[i] = -boatdir[i];
   3.264 -
   3.265 -            /* paint over the old boat position */
   3.266 -            r.x = old_x;
   3.267 -            r.y = boaty[i];
   3.268 -            r.w = boat[0]->w;
   3.269 -            r.h = boat[0]->h;
   3.270 -            if (SDL_BlitSurface(bg, &r, screen, &r) < 0)
   3.271 -                sdlerr("blitting background");
   3.272 -
   3.273 -            /* construct update rectangle (bounding box of old and new pos) */
   3.274 -            updates[i].x = MIN(old_x, boatx[i]);
   3.275 -            updates[i].y = boaty[i];
   3.276 -            updates[i].w = boat[0]->w + SPEED;
   3.277 -            updates[i].h = boat[0]->h;
   3.278 -            /* clip update rectangle to screen */
   3.279 -            if (updates[i].x < 0) {
   3.280 -                updates[i].w += updates[i].x;
   3.281 -                updates[i].x = 0;
   3.282 -            }
   3.283 -            if (updates[i].x + updates[i].w > SCRW)
   3.284 -                updates[i].w = SCRW - updates[i].x;
   3.285 -        }
   3.286 -
   3.287 -        for (i = 0; i < NBOATS; i++) {
   3.288 -            /* paint boat on new position */
   3.289 -            r.x = boatx[i];
   3.290 -            r.y = boaty[i];
   3.291 -            if (SDL_BlitSurface(boat[(boatdir[i] + 1) / 2], NULL,
   3.292 -                                screen, &r) < 0)
   3.293 -                sdlerr("blitting boat");
   3.294 -        }
   3.295 -
   3.296 -        /* cycle wave palette */
   3.297 -        for (i = 0; i < 64; i++)
   3.298 -            cmap[boatcols + ((i + frames) & 63)] = wavemap[i];
   3.299 -
   3.300 -        if (fade_dir) {
   3.301 -            /* Fade the entire palette in/out */
   3.302 -            fade_level += fade_dir;
   3.303 -
   3.304 -            if (gamma_fade) {
   3.305 -                /* Fade linearly in gamma level (lousy) */
   3.306 -                float level = (float) fade_level / fade_max;
   3.307 -                if (SDL_SetGamma(level, level, level) < 0)
   3.308 -                    sdlerr("setting gamma");
   3.309 -
   3.310 -            } else if (gamma_ramp) {
   3.311 -                /* Fade using gamma ramp (better) */
   3.312 -                Uint16 ramp[256];
   3.313 -                for (i = 0; i < 256; i++)
   3.314 -                    ramp[i] = (i * fade_level / fade_max) << 8;
   3.315 -                if (SDL_SetGammaRamp(ramp, ramp, ramp) < 0)
   3.316 -                    sdlerr("setting gamma ramp");
   3.317 -
   3.318 -            } else {
   3.319 -                /* Fade using direct palette manipulation (best) */
   3.320 -                memcpy(cmap, screen->format->palette->colors,
   3.321 -                       boatcols * sizeof(SDL_Color));
   3.322 -                for (i = 0; i < boatcols + 64; i++) {
   3.323 -                    cmap[i].r = cmap[i].r * fade_level / fade_max;
   3.324 -                    cmap[i].g = cmap[i].g * fade_level / fade_max;
   3.325 -                    cmap[i].b = cmap[i].b * fade_level / fade_max;
   3.326 -                }
   3.327 -            }
   3.328 -            if (fade_level == fade_max)
   3.329 -                fade_dir = 0;
   3.330 -        }
   3.331 -
   3.332 -        /* pulse the red colour (done after the fade, for a night effect) */
   3.333 -        redphase = frames % 64;
   3.334 -        cmap[red].r = (int) (255 * sin(redphase * M_PI / 63));
   3.335 -
   3.336 -        SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
   3.337 -
   3.338 -        /* update changed areas of the screen */
   3.339 -        SDL_UpdateRects(screen, NBOATS, updates);
   3.340 -        frames++;
   3.341 -    } while (fade_level > 0);
   3.342 -
   3.343 -    printf("%d frames, %.2f fps\n",
   3.344 -           frames, 1000.0 * frames / (SDL_GetTicks() - start));
   3.345 -
   3.346 -    if (vidflags & SDL_FULLSCREEN)
   3.347 -        SDL_ShowCursor(SDL_TRUE);
   3.348 -    SDL_Quit();
   3.349 -    return 0;
   3.350 -}