Just renamed it...
4 * A simple test of runtime palette modification for animation
5 * (using the SDL_SetPalette() API).
13 /* This isn't in the Windows headers */
15 #define M_PI 3.14159265358979323846
28 #define MIN(a, b) ((a) < (b) ? (a) : (b))
31 #define MAX(a, b) ((a) > (b) ? (a) : (b))
35 * wave colours: Made by taking a narrow cross-section of a wave picture
36 * in Gimp, saving in PPM ascii format and formatting with Emacs macros.
38 static SDL_Color wavemap[] = {
39 {0, 2, 103}, {0, 7, 110}, {0, 13, 117}, {0, 19, 125},
40 {0, 25, 133}, {0, 31, 141}, {0, 37, 150}, {0, 43, 158},
41 {0, 49, 166}, {0, 55, 174}, {0, 61, 182}, {0, 67, 190},
42 {0, 73, 198}, {0, 79, 206}, {0, 86, 214}, {0, 96, 220},
43 {5, 105, 224}, {12, 112, 226}, {19, 120, 227}, {26, 128, 229},
44 {33, 135, 230}, {40, 143, 232}, {47, 150, 234}, {54, 158, 236},
45 {61, 165, 238}, {68, 173, 239}, {75, 180, 241}, {82, 188, 242},
46 {89, 195, 244}, {96, 203, 246}, {103, 210, 248}, {112, 218, 250},
47 {124, 224, 250}, {135, 226, 251}, {146, 229, 251}, {156, 231, 252},
48 {167, 233, 252}, {178, 236, 252}, {189, 238, 252}, {200, 240, 252},
49 {211, 242, 252}, {222, 244, 252}, {233, 247, 252}, {242, 249, 252},
50 {237, 250, 252}, {209, 251, 252}, {174, 251, 252}, {138, 252, 252},
51 {102, 251, 252}, {63, 250, 252}, {24, 243, 252}, {7, 225, 252},
52 {4, 203, 252}, {3, 181, 252}, {2, 158, 252}, {1, 136, 251},
53 {0, 111, 248}, {0, 82, 234}, {0, 63, 213}, {0, 50, 192},
54 {0, 39, 172}, {0, 28, 152}, {0, 17, 132}, {0, 7, 114}
57 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
68 fprintf(stderr, "SDL error: %s: %s\n", when, SDL_GetError());
72 /* create a background surface */
74 make_bg(SDL_Surface * screen, int startcol)
78 SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h,
81 sdlerr("creating background surface");
83 /* set the palette to the logical screen palette so that blits
84 won't be translated */
85 SDL_SetColors(bg, screen->format->palette->colors, 0, 256);
87 /* Make a wavy background pattern using colours 0-63 */
88 if (SDL_LockSurface(bg) < 0)
89 sdlerr("locking background");
90 for (i = 0; i < SCRH; i++) {
91 Uint8 *p = (Uint8 *) bg->pixels + i * bg->pitch;
94 for (j = 0; j < SCRW; j++) {
98 v += p[-bg->pitch] + 65 - startcol;
99 p[j] = startcol + (v & 63);
100 d += ((rand() >> 3) % 3) - 1;
103 SDL_UnlockSurface(bg);
108 * Return a surface flipped horisontally. Only works for 8bpp;
109 * extension to arbitrary bitness is left as an exercise for the reader.
112 hflip(SDL_Surface * s)
115 SDL_Surface *z = SDL_CreateRGBSurface(SDL_SWSURFACE, s->w, s->h, 8,
118 SDL_SetColors(z, s->format->palette->colors,
119 0, s->format->palette->ncolors);
120 if (SDL_LockSurface(s) < 0 || SDL_LockSurface(z) < 0)
121 sdlerr("locking flip images");
123 for (i = 0; i < s->h; i++) {
125 Uint8 *from = (Uint8 *) s->pixels + i * s->pitch;
126 Uint8 *to = (Uint8 *) z->pixels + i * z->pitch + s->w - 1;
127 for (j = 0; j < s->w; j++)
131 SDL_UnlockSurface(z);
132 SDL_UnlockSurface(s);
137 main(int argc, char **argv)
142 SDL_Surface *boat[2];
143 unsigned vidflags = 0;
146 int fade_level, fade_dir;
147 int boatcols, frames, i, red;
148 int boatx[NBOATS], boaty[NBOATS], boatdir[NBOATS];
152 if (SDL_Init(SDL_INIT_VIDEO) < 0)
153 sdlerr("initialising SDL");
157 if (strcmp(*argv, "-hw") == 0)
158 vidflags |= SDL_HWSURFACE;
159 else if (strcmp(*argv, "-fullscreen") == 0)
160 vidflags |= SDL_FULLSCREEN;
161 else if (strcmp(*argv, "-nofade") == 0)
163 else if (strcmp(*argv, "-gamma") == 0)
165 else if (strcmp(*argv, "-gammaramp") == 0)
169 "usage: testpalette "
170 " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
175 /* Ask explicitly for 8bpp and a hardware palette */
177 SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE)) == NULL) {
178 fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
179 SCRW, SCRH, SDL_GetError());
183 if (vidflags & SDL_FULLSCREEN)
184 SDL_ShowCursor(SDL_FALSE);
186 if ((boat[0] = SDL_LoadBMP("sail.bmp")) == NULL)
187 sdlerr("loading sail.bmp");
188 /* We've chosen magenta (#ff00ff) as colour key for the boat */
189 SDL_SetColorKey(boat[0], SDL_SRCCOLORKEY | SDL_RLEACCEL,
190 SDL_MapRGB(boat[0]->format, 0xff, 0x00, 0xff));
191 boatcols = boat[0]->format->palette->ncolors;
192 boat[1] = hflip(boat[0]);
193 SDL_SetColorKey(boat[1], SDL_SRCCOLORKEY | SDL_RLEACCEL,
194 SDL_MapRGB(boat[1]->format, 0xff, 0x00, 0xff));
197 * First set the physical screen palette to black, so the user won't
198 * see our initial drawing on the screen.
200 memset(cmap, 0, sizeof(cmap));
201 SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, 256);
204 * Proper palette management is important when playing games with the
205 * colormap. We have divided the palette as follows:
207 * index 0..(boatcols-1): used for the boat
208 * index boatcols..(boatcols+63): used for the waves
210 SDL_SetPalette(screen, SDL_LOGPAL,
211 boat[0]->format->palette->colors, 0, boatcols);
212 SDL_SetPalette(screen, SDL_LOGPAL, wavemap, boatcols, 64);
215 * Now the logical screen palette is set, and will remain unchanged.
216 * The boats already have the same palette so fast blits can be used.
218 memcpy(cmap, screen->format->palette->colors, 256 * sizeof(SDL_Color));
220 /* save the index of the red colour for later */
221 red = SDL_MapRGB(screen->format, 0xff, 0x00, 0x00);
223 bg = make_bg(screen, boatcols); /* make a nice wavy background surface */
225 /* initial screen contents */
226 if (SDL_BlitSurface(bg, NULL, screen, NULL) < 0)
227 sdlerr("blitting background to screen");
228 SDL_Flip(screen); /* actually put the background on screen */
230 /* determine initial boat placements */
231 for (i = 0; i < NBOATS; i++) {
232 boatx[i] = (rand() % (SCRW + boat[0]->w)) - boat[0]->w;
233 boaty[i] = i * (SCRH - boat[0]->h) / (NBOATS - 1);
234 boatdir[i] = ((rand() >> 5) & 1) * 2 - 1;
237 start = SDL_GetTicks();
243 SDL_Rect updates[NBOATS];
247 /* A small event loop: just exit on any key or mouse button event */
248 while (SDL_PollEvent(&e)) {
249 if (e.type == SDL_KEYDOWN || e.type == SDL_QUIT
250 || e.type == SDL_MOUSEBUTTONDOWN) {
258 for (i = 0; i < NBOATS; i++) {
259 int old_x = boatx[i];
260 /* update boat position */
261 boatx[i] += boatdir[i] * SPEED;
262 if (boatx[i] <= -boat[0]->w || boatx[i] >= SCRW)
263 boatdir[i] = -boatdir[i];
265 /* paint over the old boat position */
270 if (SDL_BlitSurface(bg, &r, screen, &r) < 0)
271 sdlerr("blitting background");
273 /* construct update rectangle (bounding box of old and new pos) */
274 updates[i].x = MIN(old_x, boatx[i]);
275 updates[i].y = boaty[i];
276 updates[i].w = boat[0]->w + SPEED;
277 updates[i].h = boat[0]->h;
278 /* clip update rectangle to screen */
279 if (updates[i].x < 0) {
280 updates[i].w += updates[i].x;
283 if (updates[i].x + updates[i].w > SCRW)
284 updates[i].w = SCRW - updates[i].x;
287 for (i = 0; i < NBOATS; i++) {
288 /* paint boat on new position */
291 if (SDL_BlitSurface(boat[(boatdir[i] + 1) / 2], NULL,
293 sdlerr("blitting boat");
296 /* cycle wave palette */
297 for (i = 0; i < 64; i++)
298 cmap[boatcols + ((i + frames) & 63)] = wavemap[i];
301 /* Fade the entire palette in/out */
302 fade_level += fade_dir;
305 /* Fade linearly in gamma level (lousy) */
306 float level = (float) fade_level / fade_max;
307 if (SDL_SetGamma(level, level, level) < 0)
308 sdlerr("setting gamma");
310 } else if (gamma_ramp) {
311 /* Fade using gamma ramp (better) */
313 for (i = 0; i < 256; i++)
314 ramp[i] = (i * fade_level / fade_max) << 8;
315 if (SDL_SetGammaRamp(ramp, ramp, ramp) < 0)
316 sdlerr("setting gamma ramp");
319 /* Fade using direct palette manipulation (best) */
320 memcpy(cmap, screen->format->palette->colors,
321 boatcols * sizeof(SDL_Color));
322 for (i = 0; i < boatcols + 64; i++) {
323 cmap[i].r = cmap[i].r * fade_level / fade_max;
324 cmap[i].g = cmap[i].g * fade_level / fade_max;
325 cmap[i].b = cmap[i].b * fade_level / fade_max;
328 if (fade_level == fade_max)
332 /* pulse the red colour (done after the fade, for a night effect) */
333 redphase = frames % 64;
334 cmap[red].r = (int) (255 * sin(redphase * M_PI / 63));
336 SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
338 /* update changed areas of the screen */
339 SDL_UpdateRects(screen, NBOATS, updates);
342 while (fade_level > 0);
344 printf("%d frames, %.2f fps\n",
345 frames, 1000.0 * frames / (SDL_GetTicks() - start));
347 if (vidflags & SDL_FULLSCREEN)
348 SDL_ShowCursor(SDL_TRUE);