test/testpalette.c
changeset 0 74212992fb08
child 1151 be9c9c8f6d53
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testpalette.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,332 @@
     1.4 +/*
     1.5 + * testpalette.c
     1.6 + *
     1.7 + * A simple test of runtime palette modification for animation
     1.8 + * (using the SDL_SetPalette() API). 
     1.9 + */
    1.10 +
    1.11 +#include <stdio.h>
    1.12 +#include <stdlib.h>
    1.13 +#include <string.h>
    1.14 +#include <math.h>
    1.15 +
    1.16 +/* This isn't in the Windows headers */
    1.17 +#ifndef M_PI
    1.18 +#define M_PI	3.14159265358979323846
    1.19 +#endif
    1.20 +
    1.21 +#include <SDL.h>
    1.22 +
    1.23 +/* screen size */
    1.24 +#define SCRW 640
    1.25 +#define SCRH 480
    1.26 +
    1.27 +#define NBOATS 5
    1.28 +#define SPEED 2
    1.29 +
    1.30 +#ifndef MIN
    1.31 +#define MIN(a, b) ((a) < (b) ? (a) : (b))
    1.32 +#endif
    1.33 +#ifndef MAX
    1.34 +#define MAX(a, b) ((a) > (b) ? (a) : (b))
    1.35 +#endif
    1.36 +
    1.37 +/*
    1.38 + * wave colours: Made by taking a narrow cross-section of a wave picture
    1.39 + * in Gimp, saving in PPM ascii format and formatting with Emacs macros.
    1.40 + */
    1.41 +static SDL_Color wavemap[] = {
    1.42 +    {0,2,103}, {0,7,110}, {0,13,117}, {0,19,125},
    1.43 +    {0,25,133}, {0,31,141}, {0,37,150}, {0,43,158},
    1.44 +    {0,49,166}, {0,55,174}, {0,61,182}, {0,67,190},
    1.45 +    {0,73,198}, {0,79,206}, {0,86,214}, {0,96,220},
    1.46 +    {5,105,224}, {12,112,226}, {19,120,227}, {26,128,229},
    1.47 +    {33,135,230}, {40,143,232}, {47,150,234}, {54,158,236},
    1.48 +    {61,165,238}, {68,173,239}, {75,180,241}, {82,188,242},
    1.49 +    {89,195,244}, {96,203,246}, {103,210,248}, {112,218,250},
    1.50 +    {124,224,250}, {135,226,251}, {146,229,251}, {156,231,252},
    1.51 +    {167,233,252}, {178,236,252}, {189,238,252}, {200,240,252},
    1.52 +    {211,242,252}, {222,244,252}, {233,247,252}, {242,249,252},
    1.53 +    {237,250,252}, {209,251,252}, {174,251,252}, {138,252,252},
    1.54 +    {102,251,252}, {63,250,252}, {24,243,252}, {7,225,252},
    1.55 +    {4,203,252}, {3,181,252}, {2,158,252}, {1,136,251},
    1.56 +    {0,111,248}, {0,82,234}, {0,63,213}, {0,50,192},
    1.57 +    {0,39,172}, {0,28,152}, {0,17,132}, {0,7,114}
    1.58 +};
    1.59 +
    1.60 +static void sdlerr(char *when)
    1.61 +{
    1.62 +    fprintf(stderr, "SDL error: %s: %s\n", when, SDL_GetError());
    1.63 +    exit(1);
    1.64 +}
    1.65 +
    1.66 +/* create a background surface */
    1.67 +static SDL_Surface *make_bg(SDL_Surface *screen, int startcol)
    1.68 +{
    1.69 +    int i;
    1.70 +    SDL_Surface *bg = SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h,
    1.71 +					   8, 0, 0, 0, 0);
    1.72 +    if(!bg)
    1.73 +	sdlerr("creating background surface");
    1.74 +
    1.75 +    /* set the palette to the logical screen palette so that blits
    1.76 +       won't be translated */
    1.77 +    SDL_SetColors(bg, screen->format->palette->colors, 0, 256);
    1.78 +
    1.79 +    /* Make a wavy background pattern using colours 0-63 */
    1.80 +    if(SDL_LockSurface(bg) < 0)
    1.81 +	sdlerr("locking background");
    1.82 +    for(i = 0; i < SCRH; i++) {
    1.83 +	Uint8 *p = (Uint8 *)bg->pixels + i * bg->pitch;
    1.84 +	int j, d;
    1.85 +	d = 0;
    1.86 +	for(j = 0; j < SCRW; j++) {
    1.87 +	    int v = MAX(d, -2);
    1.88 +	    v = MIN(v, 2);
    1.89 +	    if(i > 0)
    1.90 +		v += p[-bg->pitch] + 65 - startcol;
    1.91 +	    p[j] = startcol + (v & 63);
    1.92 +	    d += ((rand() >> 3) % 3) - 1;
    1.93 +	}
    1.94 +    }
    1.95 +    SDL_UnlockSurface(bg);
    1.96 +    return(bg);
    1.97 +}
    1.98 +
    1.99 +/*
   1.100 + * Return a surface flipped horisontally. Only works for 8bpp;
   1.101 + * extension to arbitrary bitness is left as an exercise for the reader.
   1.102 + */
   1.103 +static SDL_Surface *hflip(SDL_Surface *s)
   1.104 +{
   1.105 +    int i;
   1.106 +    SDL_Surface *z = SDL_CreateRGBSurface(SDL_SWSURFACE, s->w, s->h, 8,
   1.107 +					  0, 0, 0, 0);
   1.108 +    /* copy palette */
   1.109 +    SDL_SetColors(z, s->format->palette->colors,
   1.110 +		  0, s->format->palette->ncolors);
   1.111 +    if(SDL_LockSurface(s) < 0 || SDL_LockSurface(z) < 0)
   1.112 +	sdlerr("locking flip images");
   1.113 +
   1.114 +    for(i = 0; i < s->h; i++) {
   1.115 +	int j;
   1.116 +	Uint8 *from = (Uint8 *)s->pixels + i * s->pitch;
   1.117 +	Uint8 *to = (Uint8 *)z->pixels + i * z->pitch + s->w - 1;
   1.118 +	for(j = 0; j < s->w; j++)
   1.119 +	    to[-j] = from[j];
   1.120 +    }
   1.121 +
   1.122 +    SDL_UnlockSurface(z);
   1.123 +    SDL_UnlockSurface(s);
   1.124 +    return z;
   1.125 +}
   1.126 +
   1.127 +int main(int argc, char **argv)
   1.128 +{
   1.129 +    SDL_Color cmap[256];
   1.130 +    SDL_Surface *screen;
   1.131 +    SDL_Surface *bg;
   1.132 +    SDL_Surface *boat[2];
   1.133 +    unsigned vidflags = 0;
   1.134 +    unsigned start;
   1.135 +    int fade_max = 400;
   1.136 +    int fade_level, fade_dir;
   1.137 +    int boatcols, frames, i, red;
   1.138 +    int boatx[NBOATS], boaty[NBOATS], boatdir[NBOATS];
   1.139 +    int gamma_fade = 0;
   1.140 +    int gamma_ramp = 0;
   1.141 +
   1.142 +    if(SDL_Init(SDL_INIT_VIDEO) < 0)
   1.143 +	sdlerr("initialising SDL");
   1.144 +
   1.145 +    atexit(SDL_Quit);
   1.146 +
   1.147 +    while(--argc) {
   1.148 +	++argv;
   1.149 +	if(strcmp(*argv, "-hw") == 0)
   1.150 +	    vidflags |= SDL_HWSURFACE;
   1.151 +	else if(strcmp(*argv, "-fullscreen") == 0)
   1.152 +	    vidflags |= SDL_FULLSCREEN;
   1.153 +	else if(strcmp(*argv, "-nofade") == 0)
   1.154 +	    fade_max = 1;
   1.155 +	else if(strcmp(*argv, "-gamma") == 0)
   1.156 +	    gamma_fade = 1;
   1.157 +	else if(strcmp(*argv, "-gammaramp") == 0)
   1.158 +	    gamma_ramp = 1;
   1.159 +	else {
   1.160 +	    fprintf(stderr,
   1.161 +		    "usage: testpalette "
   1.162 +		    " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
   1.163 +	    return 1;
   1.164 +	}
   1.165 +    }
   1.166 +
   1.167 +    /* Ask explicitly for 8bpp and a hardware palette */
   1.168 +    if(!(screen = SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE))) {
   1.169 +	fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
   1.170 +		SCRW, SCRH, SDL_GetError());
   1.171 +	return 1;
   1.172 +    }
   1.173 +
   1.174 +    if(!(boat[0] = SDL_LoadBMP("sail.bmp")))
   1.175 +	sdlerr("loading sail.bmp");
   1.176 +    /* We've chosen magenta (#ff00ff) as colour key for the boat */
   1.177 +    SDL_SetColorKey(boat[0], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   1.178 +		    SDL_MapRGB(boat[0]->format, 0xff, 0x00, 0xff));
   1.179 +    boatcols = boat[0]->format->palette->ncolors;
   1.180 +    boat[1] = hflip(boat[0]);
   1.181 +    SDL_SetColorKey(boat[1], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   1.182 +		    SDL_MapRGB(boat[1]->format, 0xff, 0x00, 0xff));
   1.183 +
   1.184 +    /*
   1.185 +     * First set the physical screen palette to black, so the user won't
   1.186 +     * see our initial drawing on the screen.
   1.187 +     */
   1.188 +    memset(cmap, 0, sizeof(cmap));
   1.189 +    SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, 256);
   1.190 +
   1.191 +    /*
   1.192 +     * Proper palette management is important when playing games with the
   1.193 +     * colormap. We have divided the palette as follows:
   1.194 +     *
   1.195 +     * index 0..(boatcols-1):		used for the boat
   1.196 +     * index boatcols..(boatcols+63):	used for the waves
   1.197 +     */
   1.198 +    SDL_SetPalette(screen, SDL_LOGPAL,
   1.199 +		   boat[0]->format->palette->colors, 0, boatcols);
   1.200 +    SDL_SetPalette(screen, SDL_LOGPAL, wavemap, boatcols, 64);
   1.201 +
   1.202 +    /*
   1.203 +     * Now the logical screen palette is set, and will remain unchanged.
   1.204 +     * The boats already have the same palette so fast blits can be used.
   1.205 +     */
   1.206 +    memcpy(cmap, screen->format->palette->colors, 256 * sizeof(SDL_Color));
   1.207 +
   1.208 +    /* save the index of the red colour for later */
   1.209 +    red = SDL_MapRGB(screen->format, 0xff, 0x00, 0x00);
   1.210 +
   1.211 +    bg = make_bg(screen, boatcols); /* make a nice wavy background surface */
   1.212 +
   1.213 +    /* initial screen contents */
   1.214 +    if(SDL_BlitSurface(bg, NULL, screen, NULL) < 0)
   1.215 +	sdlerr("blitting background to screen");
   1.216 +    SDL_Flip(screen);		/* actually put the background on screen */
   1.217 +
   1.218 +    /* determine initial boat placements */
   1.219 +    for(i = 0; i < NBOATS; i++) {
   1.220 +	boatx[i] = (rand() % (SCRW + boat[0]->w)) - boat[0]->w;
   1.221 +	boaty[i] = i * (SCRH - boat[0]->h) / (NBOATS - 1);
   1.222 +	boatdir[i] = ((rand() >> 5) & 1) * 2 - 1;
   1.223 +    }
   1.224 +
   1.225 +    start = SDL_GetTicks();
   1.226 +    frames = 0;
   1.227 +    fade_dir = 1;
   1.228 +    fade_level = 0;
   1.229 +    do {
   1.230 +	SDL_Event e;
   1.231 +	SDL_Rect updates[NBOATS];
   1.232 +	SDL_Rect r;
   1.233 +	int redphase;
   1.234 +
   1.235 +	/* A small event loop: just exit on any key or mouse button event */
   1.236 +	while(SDL_PollEvent(&e)) {
   1.237 +	    if(e.type == SDL_KEYDOWN || e.type == SDL_QUIT
   1.238 +	       || e.type == SDL_MOUSEBUTTONDOWN) {
   1.239 +		if(fade_dir < 0)
   1.240 +		    fade_level = 0;
   1.241 +		fade_dir = -1;
   1.242 +	    }
   1.243 +	}
   1.244 +
   1.245 +	/* move boats */
   1.246 +	for(i = 0; i < NBOATS; i++) {
   1.247 +	    int old_x = boatx[i];
   1.248 +	    /* update boat position */
   1.249 +	    boatx[i] += boatdir[i] * SPEED;
   1.250 +	    if(boatx[i] <= -boat[0]->w || boatx[i] >= SCRW)
   1.251 +		boatdir[i] = -boatdir[i];
   1.252 +
   1.253 +	    /* paint over the old boat position */
   1.254 +	    r.x = old_x;
   1.255 +	    r.y = boaty[i];
   1.256 +	    r.w = boat[0]->w;
   1.257 +	    r.h = boat[0]->h;
   1.258 +	    if(SDL_BlitSurface(bg, &r, screen, &r) < 0)
   1.259 +		sdlerr("blitting background");
   1.260 +
   1.261 +	    /* construct update rectangle (bounding box of old and new pos) */
   1.262 +	    updates[i].x = MIN(old_x, boatx[i]);
   1.263 +	    updates[i].y = boaty[i];
   1.264 +	    updates[i].w = boat[0]->w + SPEED;
   1.265 +	    updates[i].h = boat[0]->h;
   1.266 +	    /* clip update rectangle to screen */
   1.267 +	    if(updates[i].x < 0) {
   1.268 +		updates[i].w += updates[i].x;
   1.269 +		updates[i].x = 0;
   1.270 +	    }
   1.271 +	    if(updates[i].x + updates[i].w > SCRW)
   1.272 +		updates[i].w = SCRW - updates[i].x;
   1.273 +	}
   1.274 +
   1.275 +	for(i = 0; i < NBOATS; i++) {
   1.276 +	    /* paint boat on new position */
   1.277 +	    r.x = boatx[i];
   1.278 +	    r.y = boaty[i];
   1.279 +	    if(SDL_BlitSurface(boat[(boatdir[i] + 1) / 2], NULL,
   1.280 +			       screen, &r) < 0)
   1.281 +		sdlerr("blitting boat");
   1.282 +	}
   1.283 +
   1.284 +	/* cycle wave palette */
   1.285 +	for(i = 0; i < 64; i++)
   1.286 +	    cmap[boatcols + ((i + frames) & 63)] = wavemap[i];
   1.287 +
   1.288 +	if(fade_dir) {
   1.289 +	    /* Fade the entire palette in/out */
   1.290 +	    fade_level += fade_dir;
   1.291 +
   1.292 +	    if(gamma_fade) {
   1.293 +		/* Fade linearly in gamma level (lousy) */
   1.294 +		float level = (float)fade_level / fade_max;
   1.295 +		if(SDL_SetGamma(level, level, level) < 0)
   1.296 +		    sdlerr("setting gamma");
   1.297 +
   1.298 +	    } else if(gamma_ramp) {
   1.299 +		/* Fade using gamma ramp (better) */
   1.300 +		Uint16 ramp[256];
   1.301 +		for(i = 0; i < 256; i++)
   1.302 +		    ramp[i] = (i * fade_level / fade_max) << 8;
   1.303 +		if(SDL_SetGammaRamp(ramp, ramp, ramp) < 0)
   1.304 +		    sdlerr("setting gamma ramp");
   1.305 +
   1.306 +	    } else {
   1.307 +		/* Fade using direct palette manipulation (best) */
   1.308 +		memcpy(cmap, screen->format->palette->colors,
   1.309 +		       boatcols * sizeof(SDL_Color));
   1.310 +		for(i = 0; i < boatcols + 64; i++) {
   1.311 +		    cmap[i].r = cmap[i].r * fade_level / fade_max;
   1.312 +		    cmap[i].g = cmap[i].g * fade_level / fade_max;
   1.313 +		    cmap[i].b = cmap[i].b * fade_level / fade_max;
   1.314 +		}
   1.315 +	    }
   1.316 +	    if(fade_level == fade_max)
   1.317 +		fade_dir = 0;
   1.318 +	}
   1.319 +
   1.320 +	/* pulse the red colour (done after the fade, for a night effect) */
   1.321 +	redphase = frames % 64;
   1.322 +	cmap[red].r = (int)(255 * sin(redphase * M_PI / 63));
   1.323 +
   1.324 +	SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
   1.325 +
   1.326 +	/* update changed areas of the screen */
   1.327 +	SDL_UpdateRects(screen, NBOATS, updates);
   1.328 +	frames++;
   1.329 +    } while(fade_level > 0);
   1.330 +
   1.331 +    printf("%d frames, %.2f fps\n",
   1.332 +	   frames, 1000.0 * frames / (SDL_GetTicks() - start));
   1.333 +    return 0;
   1.334 +}
   1.335 +