1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_framerate.h>
#include <SDL_rotozoom.h>
#include <SDL_gfxPrimitives.h>
//#define EFFECT_FIRE_COLORKEYED
#define NARRAY(ARRAY) \
(sizeof(ARRAY) / sizeof((ARRAY)[0]))
#define SDL_MAYBE_LOCK(SURFACE) do { \
if (SDL_MUSTLOCK(SURFACE)) \
SDL_LockSurface(SURFACE); \
} while (0)
#define SDL_MAYBE_UNLOCK(SURFACE) do { \
if (SDL_MUSTLOCK(SURFACE)) \
SDL_UnlockSurface(SURFACE); \
} while (0)
#define SDL_ERROR(FMT, ...) do { \
fprintf(stderr, "%s(%d): " FMT ": %s\n", \
__FILE__, __LINE__, ##__VA_ARGS__, SDL_GetError()); \
} while (0)
#define SDL_IMAGE_ERROR(FMT, ...) do { \
fprintf(stderr, "%s(%d): " FMT ": %s\n", \
__FILE__, __LINE__, ##__VA_ARGS__, IMG_GetError()); \
} while (0)
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define FRAMERATE 20 /* Hz */
static SDL_Surface *screen;
enum Effects {
EFFECT_FIRE,
EFFECT_BLOPS
};
static enum Effects currentEffect = EFFECT_FIRE;
static SDL_Color colors[256];
#define FIRE_DECAY(C) \
((C) > 0 ? (C) - 1 : 0)
static inline SDL_Surface *
effect_fire_init(void)
{
Uint32 flags = SDL_HWSURFACE;
SDL_Surface *surface;
#ifdef EFFECT_FIRE_COLORKEYED
surface = SDL_CreateRGBSurface(flags, screen->w, screen->h, 8,
0, 0, 0, 0);
#else
flags |= SDL_SRCALPHA;
surface = SDL_CreateRGBSurface(flags, screen->w, screen->h, 32,
0x000000FF, 0x0000FF00,
0x00FF0000, 0xFF000000);
#endif
if (surface == NULL) {
SDL_ERROR("SDL_CreateRGBSurface");
exit(EXIT_FAILURE);
}
#ifdef EFFECT_FIRE_COLORKEYED
if (SDL_SetColorKey(surface, SDL_SRCCOLORKEY, 0)) {
SDL_ERROR("SDL_SetColorKey");
SDL_FreeSurface(surface);
exit(EXIT_FAILURE);
}
#endif
memset(colors, 0, sizeof(colors));
for (int i = 0; i < 32; i++) {
#ifdef EFFECT_FIRE_COLORKEYED
/* black to blue, 32 values */
colors[i].b = i << 1;
#else
/* transparent to blue, 32 values */
colors[i].b = 255;
colors[i].unused = i << 3;
#endif
/* blue to red, 32 values */
colors[i + 32].r = i << 3;
colors[i + 32].b = 64 - (i << 1);
colors[i + 32].unused = SDL_ALPHA_OPAQUE;
/*red to yellow, 32 values */
colors[i + 64].r = 255;
colors[i + 64].g = i << 3;
colors[i + 64].unused = SDL_ALPHA_OPAQUE;
/* yellow to white, 162 */
colors[i + 96].r = 255;
colors[i + 96].g = 255;
colors[i + 96].b = i << 2;
colors[i + 96].unused = SDL_ALPHA_OPAQUE;
colors[i + 128].r = 255;
colors[i + 128].g = 255;
colors[i + 128].b = 64 + (i << 2);
colors[i + 128].unused = SDL_ALPHA_OPAQUE;
colors[i + 160].r = 255;
colors[i + 160].g = 255;
colors[i + 160].b = 128 + (i << 2);
colors[i + 160].unused = SDL_ALPHA_OPAQUE;
colors[i + 192].r = 255;
colors[i + 192].g = 255;
colors[i + 192].b = 192 + i;
colors[i + 192].unused = SDL_ALPHA_OPAQUE;
colors[i + 224].r = 255;
colors[i + 224].g = 255;
colors[i + 224].b = 224 + i;
colors[i + 224].unused = SDL_ALPHA_OPAQUE;
}
#ifdef EFFECT_FIRE_COLORKEYED
SDL_SetPalette(surface, SDL_LOGPAL | SDL_PHYSPAL, colors,
0, NARRAY(colors));
#else
SDL_FillRect(surface, NULL,
SDL_MapRGBA(surface->format,
colors[0].r, colors[0].g,
colors[0].b, colors[0].unused));
#endif
return surface;
}
#ifdef EFFECT_FIRE_COLORKEYED
static inline Uint8
effect_fire_get_color(SDL_Surface *surface, int x, int y)
{
return ((Uint8 *)surface->pixels)[surface->w*y + x];
}
static inline void
effect_fire_set_color(SDL_Surface *surface, int x, int y, Uint8 color)
{
((Uint8 *)surface->pixels)[surface->w*y + x] = color;
}
#else
static inline Uint8
effect_fire_get_color(SDL_Surface *surface, int x, int y)
{
SDL_Color color;
/* alpha-channel surface */
SDL_GetRGBA(((Uint32 *)surface->pixels)[surface->w*y + x],
surface->format, &color.r, &color.g, &color.b, &color.unused);
for (Uint8 i = 0; i < NARRAY(colors); i++)
if (!memcmp(colors + i, &color, sizeof(color)))
return i;
/* shouldn't be reached */
return 0;
}
static inline void
effect_fire_set_color(SDL_Surface *surface, int x, int y, Uint8 color)
{
((Uint32 *)surface->pixels)[surface->w*y + x] =
SDL_MapRGBA(surface->format,
colors[color].r, colors[color].g,
colors[color].b, colors[color].unused);
}
#endif
static inline void
effect_fire_update(SDL_Surface *surface)
{
int mouse_x, mouse_y;
SDL_MAYBE_LOCK(surface);
if (currentEffect == EFFECT_FIRE &&
(SDL_GetMouseState(&mouse_x, &mouse_y) & SDL_BUTTON(1)))
effect_fire_set_color(surface, mouse_x, mouse_y,
(float)rand()/RAND_MAX > .3 ? 255 : 0);
for (int y = surface->h - 1; y > 0; y--) {
Uint16 acc;
Uint8 color;
/* right edge */
acc = (Uint16)effect_fire_get_color(surface, surface->w - 2, y)
+ effect_fire_get_color(surface, surface->w - 1, y);
color = effect_fire_get_color(surface, surface->w - 1, y - 1);
if (acc/2 > color)
color = (Uint8)((acc + color)/3);
effect_fire_set_color(surface, surface->w - 1, y - 1,
FIRE_DECAY(color));
/* center */
for (int x = surface->w - 2; x > 0; x--) {
acc = (Uint16)effect_fire_get_color(surface, x - 1, y)
+ effect_fire_get_color(surface, x, y)
+ effect_fire_get_color(surface, x + 1, y);
color = effect_fire_get_color(surface, x, y - 1);
if (acc/3 > color)
color = (Uint8)((acc + color)/4);
effect_fire_set_color(surface, x, y - 1, FIRE_DECAY(color));
}
/* left edge */
acc = (Uint16)effect_fire_get_color(surface, 0, y)
+ effect_fire_get_color(surface, 1, y);
color = effect_fire_get_color(surface, 0, y - 1);
if (acc/2 > color)
color = (Uint8)((acc + color)/3);
effect_fire_set_color(surface, 0, y - 1, FIRE_DECAY(color));
}
SDL_MAYBE_UNLOCK(surface);
}
struct EffectBlop {
struct EffectBlop *next;
Sint16 x;
Sint16 y;
Sint16 radius;
SDL_Color color;
Uint8 alpha;
};
static struct EffectBlop effect_blops_list = {NULL};
static void
effect_blops_create(Uint16 x, Uint16 y, SDL_Color color)
{
struct EffectBlop *tail = &effect_blops_list;
while (tail->next != NULL)
tail = tail->next;
tail->next = malloc(sizeof(struct EffectBlop));
tail = tail->next;
tail->next = NULL;
tail->x = (Sint16)x;
tail->y = (Sint16)y;
tail->radius = 1;
tail->color = color;
tail->alpha = SDL_ALPHA_OPAQUE;
}
static inline void
effect_blops_update(void)
{
struct EffectBlop *next;
for (struct EffectBlop *cur = &effect_blops_list;
cur != NULL && cur->next != NULL;
cur = next) {
next = cur->next;
filledCircleRGBA(screen, next->x, next->y, next->radius,
next->color.r, next->color.g,
next->color.b, next->alpha);
if (next->radius > 1)
aacircleRGBA(screen, next->x, next->y, next->radius,
next->color.r, next->color.g,
next->color.b, next->alpha);
next->radius++;
if (next->alpha > 10)
next->alpha -= 10;
else
next->alpha = 0;
if (next->alpha == SDL_ALPHA_TRANSPARENT) {
cur->next = next->next;
free(next);
next = cur;
}
}
}
static inline void
process_events(void)
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_F11:
if (!SDL_WM_ToggleFullScreen(screen)) {
SDL_ERROR("SDL_WM_ToggleFullScreen");
exit(EXIT_FAILURE);
}
break;
case SDLK_m:
SDL_ShowCursor(!SDL_ShowCursor(SDL_QUERY));
break;
case SDLK_ESCAPE:
exit(EXIT_SUCCESS);
case SDLK_f:
currentEffect = EFFECT_FIRE;
break;
case SDLK_b:
currentEffect = EFFECT_BLOPS;
break;
default:
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
if (currentEffect == EFFECT_BLOPS)
effect_blops_create(event.button.x, event.button.y,
event.button.button == SDL_BUTTON_LEFT
? (SDL_Color){0, 0, 255}
: (SDL_Color){255, 0, 0});
break;
case SDL_MOUSEMOTION:
if ((event.motion.state & (SDL_BUTTON(1) | SDL_BUTTON(3))) &&
currentEffect == EFFECT_BLOPS)
effect_blops_create(event.motion.x, event.motion.y,
event.motion.state & SDL_BUTTON(1)
? (SDL_Color){0, 0, 255}
: (SDL_Color){255, 0, 0});
break;
case SDL_QUIT:
exit(EXIT_SUCCESS);
default:
break;
}
}
}
int
main(int argc, char **argv)
{
FPSmanager fpsm;
SDL_Surface *fire_surface;
SDL_Surface *image_surface;
if (SDL_Init(SDL_INIT_VIDEO)) {
SDL_ERROR("SDL_Init");
return EXIT_FAILURE;
}
atexit(SDL_Quit);
SDL_WM_SetCaption("Effect Pad", NULL);
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32,
SDL_HWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL) {
SDL_ERROR("SDL_SetVideoMode");
return EXIT_FAILURE;
}
SDL_initFramerate(&fpsm);
SDL_setFramerate(&fpsm, FRAMERATE);
image_surface = IMG_Load("image_1.jpg");
if (image_surface == NULL) {
SDL_IMAGE_ERROR("IMG_Load");
return EXIT_FAILURE;
}
if (image_surface->w != screen->w || image_surface->h != screen->h) {
SDL_Surface *new;
new = zoomSurface(image_surface,
(double)screen->w/image_surface->w,
(double)screen->h/image_surface->h,
SMOOTHING_ON);
SDL_FreeSurface(image_surface);
image_surface = new;
}
fire_surface = effect_fire_init();
for (;;) {
process_events();
SDL_FillRect(screen, NULL,
SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(image_surface, NULL, screen, NULL);
effect_fire_update(fire_surface);
SDL_BlitSurface(fire_surface, NULL, screen, NULL);
effect_blops_update();
SDL_Flip(screen);
SDL_framerateDelay(&fpsm);
}
/* never reached */
return EXIT_FAILURE;
}
|