test/testpalette.c
changeset 1895 c121d94672cb
parent 1815 871090feb7ad
child 2669 e27bdcc80744
     1.1 --- a/test/testpalette.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/test/testpalette.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -36,91 +36,96 @@
     1.4   * in Gimp, saving in PPM ascii format and formatting with Emacs macros.
     1.5   */
     1.6  static SDL_Color wavemap[] = {
     1.7 -    {0,2,103}, {0,7,110}, {0,13,117}, {0,19,125},
     1.8 -    {0,25,133}, {0,31,141}, {0,37,150}, {0,43,158},
     1.9 -    {0,49,166}, {0,55,174}, {0,61,182}, {0,67,190},
    1.10 -    {0,73,198}, {0,79,206}, {0,86,214}, {0,96,220},
    1.11 -    {5,105,224}, {12,112,226}, {19,120,227}, {26,128,229},
    1.12 -    {33,135,230}, {40,143,232}, {47,150,234}, {54,158,236},
    1.13 -    {61,165,238}, {68,173,239}, {75,180,241}, {82,188,242},
    1.14 -    {89,195,244}, {96,203,246}, {103,210,248}, {112,218,250},
    1.15 -    {124,224,250}, {135,226,251}, {146,229,251}, {156,231,252},
    1.16 -    {167,233,252}, {178,236,252}, {189,238,252}, {200,240,252},
    1.17 -    {211,242,252}, {222,244,252}, {233,247,252}, {242,249,252},
    1.18 -    {237,250,252}, {209,251,252}, {174,251,252}, {138,252,252},
    1.19 -    {102,251,252}, {63,250,252}, {24,243,252}, {7,225,252},
    1.20 -    {4,203,252}, {3,181,252}, {2,158,252}, {1,136,251},
    1.21 -    {0,111,248}, {0,82,234}, {0,63,213}, {0,50,192},
    1.22 -    {0,39,172}, {0,28,152}, {0,17,132}, {0,7,114}
    1.23 +    {0, 2, 103}, {0, 7, 110}, {0, 13, 117}, {0, 19, 125},
    1.24 +    {0, 25, 133}, {0, 31, 141}, {0, 37, 150}, {0, 43, 158},
    1.25 +    {0, 49, 166}, {0, 55, 174}, {0, 61, 182}, {0, 67, 190},
    1.26 +    {0, 73, 198}, {0, 79, 206}, {0, 86, 214}, {0, 96, 220},
    1.27 +    {5, 105, 224}, {12, 112, 226}, {19, 120, 227}, {26, 128, 229},
    1.28 +    {33, 135, 230}, {40, 143, 232}, {47, 150, 234}, {54, 158, 236},
    1.29 +    {61, 165, 238}, {68, 173, 239}, {75, 180, 241}, {82, 188, 242},
    1.30 +    {89, 195, 244}, {96, 203, 246}, {103, 210, 248}, {112, 218, 250},
    1.31 +    {124, 224, 250}, {135, 226, 251}, {146, 229, 251}, {156, 231, 252},
    1.32 +    {167, 233, 252}, {178, 236, 252}, {189, 238, 252}, {200, 240, 252},
    1.33 +    {211, 242, 252}, {222, 244, 252}, {233, 247, 252}, {242, 249, 252},
    1.34 +    {237, 250, 252}, {209, 251, 252}, {174, 251, 252}, {138, 252, 252},
    1.35 +    {102, 251, 252}, {63, 250, 252}, {24, 243, 252}, {7, 225, 252},
    1.36 +    {4, 203, 252}, {3, 181, 252}, {2, 158, 252}, {1, 136, 251},
    1.37 +    {0, 111, 248}, {0, 82, 234}, {0, 63, 213}, {0, 50, 192},
    1.38 +    {0, 39, 172}, {0, 28, 152}, {0, 17, 132}, {0, 7, 114}
    1.39  };
    1.40  
    1.41  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.42 -static void quit(int rc)
    1.43 +static void
    1.44 +quit(int rc)
    1.45  {
    1.46 -	SDL_Quit();
    1.47 -	exit(rc);
    1.48 +    SDL_Quit();
    1.49 +    exit(rc);
    1.50  }
    1.51  
    1.52 -static void sdlerr(char *when)
    1.53 +static void
    1.54 +sdlerr(char *when)
    1.55  {
    1.56      fprintf(stderr, "SDL error: %s: %s\n", when, SDL_GetError());
    1.57      quit(1);
    1.58  }
    1.59  
    1.60  /* create a background surface */
    1.61 -static SDL_Surface *make_bg(SDL_Surface *screen, int startcol)
    1.62 +static SDL_Surface *
    1.63 +make_bg(SDL_Surface * screen, int startcol)
    1.64  {
    1.65      int i;
    1.66 -    SDL_Surface *bg = SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h,
    1.67 -					   8, 0, 0, 0, 0);
    1.68 -    if(!bg)
    1.69 -	sdlerr("creating background surface");
    1.70 +    SDL_Surface *bg =
    1.71 +        SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h,
    1.72 +                             8, 0, 0, 0, 0);
    1.73 +    if (!bg)
    1.74 +        sdlerr("creating background surface");
    1.75  
    1.76      /* set the palette to the logical screen palette so that blits
    1.77         won't be translated */
    1.78      SDL_SetColors(bg, screen->format->palette->colors, 0, 256);
    1.79  
    1.80      /* Make a wavy background pattern using colours 0-63 */
    1.81 -    if(SDL_LockSurface(bg) < 0)
    1.82 -	sdlerr("locking background");
    1.83 -    for(i = 0; i < SCRH; i++) {
    1.84 -	Uint8 *p = (Uint8 *)bg->pixels + i * bg->pitch;
    1.85 -	int j, d;
    1.86 -	d = 0;
    1.87 -	for(j = 0; j < SCRW; j++) {
    1.88 -	    int v = MAX(d, -2);
    1.89 -	    v = MIN(v, 2);
    1.90 -	    if(i > 0)
    1.91 -		v += p[-bg->pitch] + 65 - startcol;
    1.92 -	    p[j] = startcol + (v & 63);
    1.93 -	    d += ((rand() >> 3) % 3) - 1;
    1.94 -	}
    1.95 +    if (SDL_LockSurface(bg) < 0)
    1.96 +        sdlerr("locking background");
    1.97 +    for (i = 0; i < SCRH; i++) {
    1.98 +        Uint8 *p = (Uint8 *) bg->pixels + i * bg->pitch;
    1.99 +        int j, d;
   1.100 +        d = 0;
   1.101 +        for (j = 0; j < SCRW; j++) {
   1.102 +            int v = MAX(d, -2);
   1.103 +            v = MIN(v, 2);
   1.104 +            if (i > 0)
   1.105 +                v += p[-bg->pitch] + 65 - startcol;
   1.106 +            p[j] = startcol + (v & 63);
   1.107 +            d += ((rand() >> 3) % 3) - 1;
   1.108 +        }
   1.109      }
   1.110      SDL_UnlockSurface(bg);
   1.111 -    return(bg);
   1.112 +    return (bg);
   1.113  }
   1.114  
   1.115  /*
   1.116   * Return a surface flipped horisontally. Only works for 8bpp;
   1.117   * extension to arbitrary bitness is left as an exercise for the reader.
   1.118   */
   1.119 -static SDL_Surface *hflip(SDL_Surface *s)
   1.120 +static SDL_Surface *
   1.121 +hflip(SDL_Surface * s)
   1.122  {
   1.123      int i;
   1.124      SDL_Surface *z = SDL_CreateRGBSurface(SDL_SWSURFACE, s->w, s->h, 8,
   1.125 -					  0, 0, 0, 0);
   1.126 +                                          0, 0, 0, 0);
   1.127      /* copy palette */
   1.128      SDL_SetColors(z, s->format->palette->colors,
   1.129 -		  0, s->format->palette->ncolors);
   1.130 -    if(SDL_LockSurface(s) < 0 || SDL_LockSurface(z) < 0)
   1.131 -	sdlerr("locking flip images");
   1.132 +                  0, s->format->palette->ncolors);
   1.133 +    if (SDL_LockSurface(s) < 0 || SDL_LockSurface(z) < 0)
   1.134 +        sdlerr("locking flip images");
   1.135  
   1.136 -    for(i = 0; i < s->h; i++) {
   1.137 -	int j;
   1.138 -	Uint8 *from = (Uint8 *)s->pixels + i * s->pitch;
   1.139 -	Uint8 *to = (Uint8 *)z->pixels + i * z->pitch + s->w - 1;
   1.140 -	for(j = 0; j < s->w; j++)
   1.141 -	    to[-j] = from[j];
   1.142 +    for (i = 0; i < s->h; i++) {
   1.143 +        int j;
   1.144 +        Uint8 *from = (Uint8 *) s->pixels + i * s->pitch;
   1.145 +        Uint8 *to = (Uint8 *) z->pixels + i * z->pitch + s->w - 1;
   1.146 +        for (j = 0; j < s->w; j++)
   1.147 +            to[-j] = from[j];
   1.148      }
   1.149  
   1.150      SDL_UnlockSurface(z);
   1.151 @@ -128,7 +133,8 @@
   1.152      return z;
   1.153  }
   1.154  
   1.155 -int main(int argc, char **argv)
   1.156 +int
   1.157 +main(int argc, char **argv)
   1.158  {
   1.159      SDL_Color cmap[256];
   1.160      SDL_Surface *screen;
   1.161 @@ -143,47 +149,49 @@
   1.162      int gamma_fade = 0;
   1.163      int gamma_ramp = 0;
   1.164  
   1.165 -    if(SDL_Init(SDL_INIT_VIDEO) < 0)
   1.166 -	sdlerr("initialising SDL");
   1.167 +    if (SDL_Init(SDL_INIT_VIDEO) < 0)
   1.168 +        sdlerr("initialising SDL");
   1.169  
   1.170 -    while(--argc) {
   1.171 -	++argv;
   1.172 -	if(strcmp(*argv, "-hw") == 0)
   1.173 -	    vidflags |= SDL_HWSURFACE;
   1.174 -	else if(strcmp(*argv, "-fullscreen") == 0)
   1.175 -	    vidflags |= SDL_FULLSCREEN;
   1.176 -	else if(strcmp(*argv, "-nofade") == 0)
   1.177 -	    fade_max = 1;
   1.178 -	else if(strcmp(*argv, "-gamma") == 0)
   1.179 -	    gamma_fade = 1;
   1.180 -	else if(strcmp(*argv, "-gammaramp") == 0)
   1.181 -	    gamma_ramp = 1;
   1.182 -	else {
   1.183 -	    fprintf(stderr,
   1.184 -		    "usage: testpalette "
   1.185 -		    " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
   1.186 -	    quit(1);
   1.187 -	}
   1.188 +    while (--argc) {
   1.189 +        ++argv;
   1.190 +        if (strcmp(*argv, "-hw") == 0)
   1.191 +            vidflags |= SDL_HWSURFACE;
   1.192 +        else if (strcmp(*argv, "-fullscreen") == 0)
   1.193 +            vidflags |= SDL_FULLSCREEN;
   1.194 +        else if (strcmp(*argv, "-nofade") == 0)
   1.195 +            fade_max = 1;
   1.196 +        else if (strcmp(*argv, "-gamma") == 0)
   1.197 +            gamma_fade = 1;
   1.198 +        else if (strcmp(*argv, "-gammaramp") == 0)
   1.199 +            gamma_ramp = 1;
   1.200 +        else {
   1.201 +            fprintf(stderr,
   1.202 +                    "usage: testpalette "
   1.203 +                    " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
   1.204 +            quit(1);
   1.205 +        }
   1.206      }
   1.207  
   1.208      /* Ask explicitly for 8bpp and a hardware palette */
   1.209 -    if((screen = SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE)) == NULL) {
   1.210 -	fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
   1.211 -		SCRW, SCRH, SDL_GetError());
   1.212 -	quit(1);
   1.213 +    if ((screen =
   1.214 +         SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE)) == NULL) {
   1.215 +        fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
   1.216 +                SCRW, SCRH, SDL_GetError());
   1.217 +        quit(1);
   1.218      }
   1.219  
   1.220 -    if (vidflags & SDL_FULLSCREEN) SDL_ShowCursor (SDL_FALSE);
   1.221 +    if (vidflags & SDL_FULLSCREEN)
   1.222 +        SDL_ShowCursor(SDL_FALSE);
   1.223  
   1.224 -    if((boat[0] = SDL_LoadBMP("sail.bmp")) == NULL)
   1.225 -	sdlerr("loading sail.bmp");
   1.226 +    if ((boat[0] = SDL_LoadBMP("sail.bmp")) == NULL)
   1.227 +        sdlerr("loading sail.bmp");
   1.228      /* We've chosen magenta (#ff00ff) as colour key for the boat */
   1.229      SDL_SetColorKey(boat[0], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   1.230 -		    SDL_MapRGB(boat[0]->format, 0xff, 0x00, 0xff));
   1.231 +                    SDL_MapRGB(boat[0]->format, 0xff, 0x00, 0xff));
   1.232      boatcols = boat[0]->format->palette->ncolors;
   1.233      boat[1] = hflip(boat[0]);
   1.234      SDL_SetColorKey(boat[1], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   1.235 -		    SDL_MapRGB(boat[1]->format, 0xff, 0x00, 0xff));
   1.236 +                    SDL_MapRGB(boat[1]->format, 0xff, 0x00, 0xff));
   1.237  
   1.238      /*
   1.239       * First set the physical screen palette to black, so the user won't
   1.240 @@ -196,11 +204,11 @@
   1.241       * Proper palette management is important when playing games with the
   1.242       * colormap. We have divided the palette as follows:
   1.243       *
   1.244 -     * index 0..(boatcols-1):		used for the boat
   1.245 -     * index boatcols..(boatcols+63):	used for the waves
   1.246 +     * index 0..(boatcols-1):           used for the boat
   1.247 +     * index boatcols..(boatcols+63):   used for the waves
   1.248       */
   1.249      SDL_SetPalette(screen, SDL_LOGPAL,
   1.250 -		   boat[0]->format->palette->colors, 0, boatcols);
   1.251 +                   boat[0]->format->palette->colors, 0, boatcols);
   1.252      SDL_SetPalette(screen, SDL_LOGPAL, wavemap, boatcols, 64);
   1.253  
   1.254      /*
   1.255 @@ -212,18 +220,18 @@
   1.256      /* save the index of the red colour for later */
   1.257      red = SDL_MapRGB(screen->format, 0xff, 0x00, 0x00);
   1.258  
   1.259 -    bg = make_bg(screen, boatcols); /* make a nice wavy background surface */
   1.260 +    bg = make_bg(screen, boatcols);     /* make a nice wavy background surface */
   1.261  
   1.262      /* initial screen contents */
   1.263 -    if(SDL_BlitSurface(bg, NULL, screen, NULL) < 0)
   1.264 -	sdlerr("blitting background to screen");
   1.265 -    SDL_Flip(screen);		/* actually put the background on screen */
   1.266 +    if (SDL_BlitSurface(bg, NULL, screen, NULL) < 0)
   1.267 +        sdlerr("blitting background to screen");
   1.268 +    SDL_Flip(screen);           /* actually put the background on screen */
   1.269  
   1.270      /* determine initial boat placements */
   1.271 -    for(i = 0; i < NBOATS; i++) {
   1.272 -	boatx[i] = (rand() % (SCRW + boat[0]->w)) - boat[0]->w;
   1.273 -	boaty[i] = i * (SCRH - boat[0]->h) / (NBOATS - 1);
   1.274 -	boatdir[i] = ((rand() >> 5) & 1) * 2 - 1;
   1.275 +    for (i = 0; i < NBOATS; i++) {
   1.276 +        boatx[i] = (rand() % (SCRW + boat[0]->w)) - boat[0]->w;
   1.277 +        boaty[i] = i * (SCRH - boat[0]->h) / (NBOATS - 1);
   1.278 +        boatdir[i] = ((rand() >> 5) & 1) * 2 - 1;
   1.279      }
   1.280  
   1.281      start = SDL_GetTicks();
   1.282 @@ -231,112 +239,113 @@
   1.283      fade_dir = 1;
   1.284      fade_level = 0;
   1.285      do {
   1.286 -	SDL_Event e;
   1.287 -	SDL_Rect updates[NBOATS];
   1.288 -	SDL_Rect r;
   1.289 -	int redphase;
   1.290 +        SDL_Event e;
   1.291 +        SDL_Rect updates[NBOATS];
   1.292 +        SDL_Rect r;
   1.293 +        int redphase;
   1.294  
   1.295 -	/* A small event loop: just exit on any key or mouse button event */
   1.296 -	while(SDL_PollEvent(&e)) {
   1.297 -	    if(e.type == SDL_KEYDOWN || e.type == SDL_QUIT
   1.298 -	       || e.type == SDL_MOUSEBUTTONDOWN) {
   1.299 -		if(fade_dir < 0)
   1.300 -		    fade_level = 0;
   1.301 -		fade_dir = -1;
   1.302 -	    }
   1.303 -	}
   1.304 +        /* A small event loop: just exit on any key or mouse button event */
   1.305 +        while (SDL_PollEvent(&e)) {
   1.306 +            if (e.type == SDL_KEYDOWN || e.type == SDL_QUIT
   1.307 +                || e.type == SDL_MOUSEBUTTONDOWN) {
   1.308 +                if (fade_dir < 0)
   1.309 +                    fade_level = 0;
   1.310 +                fade_dir = -1;
   1.311 +            }
   1.312 +        }
   1.313  
   1.314 -	/* move boats */
   1.315 -	for(i = 0; i < NBOATS; i++) {
   1.316 -	    int old_x = boatx[i];
   1.317 -	    /* update boat position */
   1.318 -	    boatx[i] += boatdir[i] * SPEED;
   1.319 -	    if(boatx[i] <= -boat[0]->w || boatx[i] >= SCRW)
   1.320 -		boatdir[i] = -boatdir[i];
   1.321 +        /* move boats */
   1.322 +        for (i = 0; i < NBOATS; i++) {
   1.323 +            int old_x = boatx[i];
   1.324 +            /* update boat position */
   1.325 +            boatx[i] += boatdir[i] * SPEED;
   1.326 +            if (boatx[i] <= -boat[0]->w || boatx[i] >= SCRW)
   1.327 +                boatdir[i] = -boatdir[i];
   1.328  
   1.329 -	    /* paint over the old boat position */
   1.330 -	    r.x = old_x;
   1.331 -	    r.y = boaty[i];
   1.332 -	    r.w = boat[0]->w;
   1.333 -	    r.h = boat[0]->h;
   1.334 -	    if(SDL_BlitSurface(bg, &r, screen, &r) < 0)
   1.335 -		sdlerr("blitting background");
   1.336 +            /* paint over the old boat position */
   1.337 +            r.x = old_x;
   1.338 +            r.y = boaty[i];
   1.339 +            r.w = boat[0]->w;
   1.340 +            r.h = boat[0]->h;
   1.341 +            if (SDL_BlitSurface(bg, &r, screen, &r) < 0)
   1.342 +                sdlerr("blitting background");
   1.343  
   1.344 -	    /* construct update rectangle (bounding box of old and new pos) */
   1.345 -	    updates[i].x = MIN(old_x, boatx[i]);
   1.346 -	    updates[i].y = boaty[i];
   1.347 -	    updates[i].w = boat[0]->w + SPEED;
   1.348 -	    updates[i].h = boat[0]->h;
   1.349 -	    /* clip update rectangle to screen */
   1.350 -	    if(updates[i].x < 0) {
   1.351 -		updates[i].w += updates[i].x;
   1.352 -		updates[i].x = 0;
   1.353 -	    }
   1.354 -	    if(updates[i].x + updates[i].w > SCRW)
   1.355 -		updates[i].w = SCRW - updates[i].x;
   1.356 -	}
   1.357 +            /* construct update rectangle (bounding box of old and new pos) */
   1.358 +            updates[i].x = MIN(old_x, boatx[i]);
   1.359 +            updates[i].y = boaty[i];
   1.360 +            updates[i].w = boat[0]->w + SPEED;
   1.361 +            updates[i].h = boat[0]->h;
   1.362 +            /* clip update rectangle to screen */
   1.363 +            if (updates[i].x < 0) {
   1.364 +                updates[i].w += updates[i].x;
   1.365 +                updates[i].x = 0;
   1.366 +            }
   1.367 +            if (updates[i].x + updates[i].w > SCRW)
   1.368 +                updates[i].w = SCRW - updates[i].x;
   1.369 +        }
   1.370  
   1.371 -	for(i = 0; i < NBOATS; i++) {
   1.372 -	    /* paint boat on new position */
   1.373 -	    r.x = boatx[i];
   1.374 -	    r.y = boaty[i];
   1.375 -	    if(SDL_BlitSurface(boat[(boatdir[i] + 1) / 2], NULL,
   1.376 -			       screen, &r) < 0)
   1.377 -		sdlerr("blitting boat");
   1.378 -	}
   1.379 +        for (i = 0; i < NBOATS; i++) {
   1.380 +            /* paint boat on new position */
   1.381 +            r.x = boatx[i];
   1.382 +            r.y = boaty[i];
   1.383 +            if (SDL_BlitSurface(boat[(boatdir[i] + 1) / 2], NULL,
   1.384 +                                screen, &r) < 0)
   1.385 +                sdlerr("blitting boat");
   1.386 +        }
   1.387  
   1.388 -	/* cycle wave palette */
   1.389 -	for(i = 0; i < 64; i++)
   1.390 -	    cmap[boatcols + ((i + frames) & 63)] = wavemap[i];
   1.391 +        /* cycle wave palette */
   1.392 +        for (i = 0; i < 64; i++)
   1.393 +            cmap[boatcols + ((i + frames) & 63)] = wavemap[i];
   1.394  
   1.395 -	if(fade_dir) {
   1.396 -	    /* Fade the entire palette in/out */
   1.397 -	    fade_level += fade_dir;
   1.398 +        if (fade_dir) {
   1.399 +            /* Fade the entire palette in/out */
   1.400 +            fade_level += fade_dir;
   1.401  
   1.402 -	    if(gamma_fade) {
   1.403 -		/* Fade linearly in gamma level (lousy) */
   1.404 -		float level = (float)fade_level / fade_max;
   1.405 -		if(SDL_SetGamma(level, level, level) < 0)
   1.406 -		    sdlerr("setting gamma");
   1.407 +            if (gamma_fade) {
   1.408 +                /* Fade linearly in gamma level (lousy) */
   1.409 +                float level = (float) fade_level / fade_max;
   1.410 +                if (SDL_SetGamma(level, level, level) < 0)
   1.411 +                    sdlerr("setting gamma");
   1.412  
   1.413 -	    } else if(gamma_ramp) {
   1.414 -		/* Fade using gamma ramp (better) */
   1.415 -		Uint16 ramp[256];
   1.416 -		for(i = 0; i < 256; i++)
   1.417 -		    ramp[i] = (i * fade_level / fade_max) << 8;
   1.418 -		if(SDL_SetGammaRamp(ramp, ramp, ramp) < 0)
   1.419 -		    sdlerr("setting gamma ramp");
   1.420 +            } else if (gamma_ramp) {
   1.421 +                /* Fade using gamma ramp (better) */
   1.422 +                Uint16 ramp[256];
   1.423 +                for (i = 0; i < 256; i++)
   1.424 +                    ramp[i] = (i * fade_level / fade_max) << 8;
   1.425 +                if (SDL_SetGammaRamp(ramp, ramp, ramp) < 0)
   1.426 +                    sdlerr("setting gamma ramp");
   1.427  
   1.428 -	    } else {
   1.429 -		/* Fade using direct palette manipulation (best) */
   1.430 -		memcpy(cmap, screen->format->palette->colors,
   1.431 -		       boatcols * sizeof(SDL_Color));
   1.432 -		for(i = 0; i < boatcols + 64; i++) {
   1.433 -		    cmap[i].r = cmap[i].r * fade_level / fade_max;
   1.434 -		    cmap[i].g = cmap[i].g * fade_level / fade_max;
   1.435 -		    cmap[i].b = cmap[i].b * fade_level / fade_max;
   1.436 -		}
   1.437 -	    }
   1.438 -	    if(fade_level == fade_max)
   1.439 -		fade_dir = 0;
   1.440 -	}
   1.441 +            } else {
   1.442 +                /* Fade using direct palette manipulation (best) */
   1.443 +                memcpy(cmap, screen->format->palette->colors,
   1.444 +                       boatcols * sizeof(SDL_Color));
   1.445 +                for (i = 0; i < boatcols + 64; i++) {
   1.446 +                    cmap[i].r = cmap[i].r * fade_level / fade_max;
   1.447 +                    cmap[i].g = cmap[i].g * fade_level / fade_max;
   1.448 +                    cmap[i].b = cmap[i].b * fade_level / fade_max;
   1.449 +                }
   1.450 +            }
   1.451 +            if (fade_level == fade_max)
   1.452 +                fade_dir = 0;
   1.453 +        }
   1.454  
   1.455 -	/* pulse the red colour (done after the fade, for a night effect) */
   1.456 -	redphase = frames % 64;
   1.457 -	cmap[red].r = (int)(255 * sin(redphase * M_PI / 63));
   1.458 +        /* pulse the red colour (done after the fade, for a night effect) */
   1.459 +        redphase = frames % 64;
   1.460 +        cmap[red].r = (int) (255 * sin(redphase * M_PI / 63));
   1.461  
   1.462 -	SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
   1.463 +        SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
   1.464  
   1.465 -	/* update changed areas of the screen */
   1.466 -	SDL_UpdateRects(screen, NBOATS, updates);
   1.467 -	frames++;
   1.468 -    } while(fade_level > 0);
   1.469 +        /* update changed areas of the screen */
   1.470 +        SDL_UpdateRects(screen, NBOATS, updates);
   1.471 +        frames++;
   1.472 +    }
   1.473 +    while (fade_level > 0);
   1.474  
   1.475      printf("%d frames, %.2f fps\n",
   1.476 -	   frames, 1000.0 * frames / (SDL_GetTicks() - start));
   1.477 +           frames, 1000.0 * frames / (SDL_GetTicks() - start));
   1.478  
   1.479 -    if (vidflags & SDL_FULLSCREEN) SDL_ShowCursor (SDL_TRUE);
   1.480 +    if (vidflags & SDL_FULLSCREEN)
   1.481 +        SDL_ShowCursor(SDL_TRUE);
   1.482      SDL_Quit();
   1.483      return 0;
   1.484  }
   1.485 -