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