Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
351 lines (307 loc) · 11.1 KB

testpalette.c

File metadata and controls

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