test/testpalette.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 17 Dec 2009 06:08:20 +0000
changeset 3589 06f0768a904c
parent 2855 422452508c76
permissions -rw-r--r--
Fixed bug #841

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