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