1 | /* |
---|
2 | * cacademo various demo effects for libcaca |
---|
3 | * Copyright (c) 1998 Michele Bini <mibin@tin.it> |
---|
4 | * 2003-2006 Jean-Yves Lamoureux <jylam@lnxscene.org> |
---|
5 | * 2004-2006 Sam Hocevar <sam@zoy.org> |
---|
6 | * All Rights Reserved |
---|
7 | * |
---|
8 | * $Id: cacademo.c 1226 2006-10-24 07:07:47Z sam $ |
---|
9 | * |
---|
10 | * This program is free software; you can redistribute it and/or |
---|
11 | * modify it under the terms of the Do What The Fuck You Want To |
---|
12 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
13 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
14 | */ |
---|
15 | |
---|
16 | #include "config.h" |
---|
17 | #include "common.h" |
---|
18 | |
---|
19 | #if !defined(__KERNEL__) |
---|
20 | # include <stdio.h> |
---|
21 | # include <stdlib.h> |
---|
22 | # include <string.h> |
---|
23 | # include <math.h> |
---|
24 | # ifndef M_PI |
---|
25 | # define M_PI 3.14159265358979323846 |
---|
26 | # endif |
---|
27 | #endif |
---|
28 | |
---|
29 | #include "cucul.h" |
---|
30 | #include "caca.h" |
---|
31 | |
---|
32 | enum action { PREPARE, INIT, UPDATE, RENDER, FREE }; |
---|
33 | |
---|
34 | void transition(cucul_canvas_t *, int, int); |
---|
35 | void plasma(enum action, cucul_canvas_t *); |
---|
36 | void metaballs(enum action, cucul_canvas_t *); |
---|
37 | void moire(enum action, cucul_canvas_t *); |
---|
38 | void langton(enum action, cucul_canvas_t *); |
---|
39 | void matrix(enum action, cucul_canvas_t *); |
---|
40 | |
---|
41 | void (*fn[])(enum action, cucul_canvas_t *) = |
---|
42 | { |
---|
43 | plasma, |
---|
44 | metaballs, |
---|
45 | moire, |
---|
46 | //langton, |
---|
47 | matrix, |
---|
48 | }; |
---|
49 | #define DEMOS (sizeof(fn)/sizeof(*fn)) |
---|
50 | |
---|
51 | #define DEMO_FRAMES 1000 |
---|
52 | #define TRANSITION_FRAMES 40 |
---|
53 | |
---|
54 | #define TRANSITION_COUNT 2 |
---|
55 | #define TRANSITION_CIRCLE 0 |
---|
56 | #define TRANSITION_STAR 1 |
---|
57 | |
---|
58 | |
---|
59 | /* Common macros for dither-based demos */ |
---|
60 | #define XSIZ 256 |
---|
61 | #define YSIZ 256 |
---|
62 | |
---|
63 | /* Global variables */ |
---|
64 | static int frame = 0; |
---|
65 | |
---|
66 | int main(int argc, char **argv) |
---|
67 | { |
---|
68 | static caca_display_t *dp; |
---|
69 | static cucul_canvas_t *frontcv, *backcv, *mask; |
---|
70 | |
---|
71 | int demo, next = -1, pause = 0, next_transition = DEMO_FRAMES; |
---|
72 | unsigned int i; |
---|
73 | int tmode = cucul_rand(0, TRANSITION_COUNT); |
---|
74 | |
---|
75 | /* Set up two canvases, a mask, and attach a display to the front one */ |
---|
76 | frontcv = cucul_create_canvas(0, 0); |
---|
77 | backcv = cucul_create_canvas(0, 0); |
---|
78 | mask = cucul_create_canvas(0, 0); |
---|
79 | |
---|
80 | dp = caca_create_display(frontcv); |
---|
81 | if(!dp) |
---|
82 | return 1; |
---|
83 | |
---|
84 | cucul_set_canvas_size(backcv, cucul_get_canvas_width(frontcv), |
---|
85 | cucul_get_canvas_height(frontcv)); |
---|
86 | cucul_set_canvas_size(mask, cucul_get_canvas_width(frontcv), |
---|
87 | cucul_get_canvas_height(frontcv)); |
---|
88 | |
---|
89 | caca_set_display_time(dp, 20000); |
---|
90 | |
---|
91 | /* Initialise all demos' lookup tables */ |
---|
92 | for(i = 0; i < DEMOS; i++) |
---|
93 | fn[i](PREPARE, frontcv); |
---|
94 | |
---|
95 | /* Choose a demo at random */ |
---|
96 | demo = cucul_rand(0, DEMOS); |
---|
97 | fn[demo](INIT, frontcv); |
---|
98 | |
---|
99 | for(;;) |
---|
100 | { |
---|
101 | /* Handle events */ |
---|
102 | caca_event_t ev; |
---|
103 | while(caca_get_event(dp, CACA_EVENT_KEY_PRESS |
---|
104 | | CACA_EVENT_QUIT, &ev, 0)) |
---|
105 | { |
---|
106 | if(ev.type == CACA_EVENT_QUIT) |
---|
107 | goto end; |
---|
108 | |
---|
109 | switch(ev.data.key.ch) |
---|
110 | { |
---|
111 | case CACA_KEY_ESCAPE: |
---|
112 | goto end; |
---|
113 | case ' ': |
---|
114 | pause = !pause; |
---|
115 | break; |
---|
116 | case '\r': |
---|
117 | if(next == -1) |
---|
118 | next_transition = frame; |
---|
119 | break; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | /* Resize the spare canvas, just in case the main one changed */ |
---|
124 | cucul_set_canvas_size(backcv, cucul_get_canvas_width(frontcv), |
---|
125 | cucul_get_canvas_height(frontcv)); |
---|
126 | cucul_set_canvas_size(mask, cucul_get_canvas_width(frontcv), |
---|
127 | cucul_get_canvas_height(frontcv)); |
---|
128 | |
---|
129 | if(pause) |
---|
130 | goto paused; |
---|
131 | |
---|
132 | /* Update demo's data */ |
---|
133 | fn[demo](UPDATE, frontcv); |
---|
134 | |
---|
135 | /* Handle transitions */ |
---|
136 | if(frame == next_transition) |
---|
137 | { |
---|
138 | next = cucul_rand(0, DEMOS); |
---|
139 | if(next == demo) |
---|
140 | next = (next + 1) % DEMOS; |
---|
141 | fn[next](INIT, backcv); |
---|
142 | } |
---|
143 | else if(frame == next_transition + TRANSITION_FRAMES) |
---|
144 | { |
---|
145 | fn[demo](FREE, frontcv); |
---|
146 | demo = next; |
---|
147 | next = -1; |
---|
148 | next_transition = frame + DEMO_FRAMES; |
---|
149 | tmode = cucul_rand(0, TRANSITION_COUNT); |
---|
150 | } |
---|
151 | |
---|
152 | if(next != -1) |
---|
153 | fn[next](UPDATE, backcv); |
---|
154 | |
---|
155 | frame++; |
---|
156 | paused: |
---|
157 | /* Render main demo's canvas */ |
---|
158 | fn[demo](RENDER, frontcv); |
---|
159 | |
---|
160 | /* If a transition is on its way, render it */ |
---|
161 | if(next != -1) |
---|
162 | { |
---|
163 | fn[next](RENDER, backcv); |
---|
164 | cucul_set_color(mask, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK); |
---|
165 | cucul_clear_canvas(mask); |
---|
166 | cucul_set_color(mask, CUCUL_COLOR_WHITE, CUCUL_COLOR_WHITE); |
---|
167 | transition(mask, tmode, |
---|
168 | 100 * (frame - next_transition) / TRANSITION_FRAMES); |
---|
169 | cucul_blit(frontcv, 0, 0, backcv, mask); |
---|
170 | } |
---|
171 | |
---|
172 | cucul_set_color(frontcv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
173 | if(frame < 100) |
---|
174 | cucul_putstr(frontcv, cucul_get_canvas_width(frontcv) - 30, |
---|
175 | cucul_get_canvas_height(frontcv) - 2, |
---|
176 | " -=[ Powered by libcaca ]=- "); |
---|
177 | caca_refresh_display(dp); |
---|
178 | } |
---|
179 | end: |
---|
180 | if(next != -1) |
---|
181 | fn[next](FREE, frontcv); |
---|
182 | fn[demo](FREE, frontcv); |
---|
183 | |
---|
184 | caca_free_display(dp); |
---|
185 | cucul_free_canvas(mask); |
---|
186 | cucul_free_canvas(backcv); |
---|
187 | cucul_free_canvas(frontcv); |
---|
188 | |
---|
189 | return 0; |
---|
190 | } |
---|
191 | |
---|
192 | /* Transitions */ |
---|
193 | void transition(cucul_canvas_t *mask, int tmode, int completed) |
---|
194 | { |
---|
195 | static float const star[] = |
---|
196 | { |
---|
197 | 0.000000, -1.000000, |
---|
198 | 0.308000, -0.349000, |
---|
199 | 0.992000, -0.244000, |
---|
200 | 0.500000, 0.266000, |
---|
201 | 0.632000, 0.998000, |
---|
202 | 0.008000, 0.659000, |
---|
203 | -0.601000, 0.995000, |
---|
204 | -0.496000, 0.275000, |
---|
205 | -0.997000, -0.244000, |
---|
206 | -0.313000, -0.349000 |
---|
207 | }; |
---|
208 | static float star_rot[sizeof(star)/sizeof(*star)]; |
---|
209 | |
---|
210 | float mulx = 0.0075f * completed * cucul_get_canvas_width(mask); |
---|
211 | float muly = 0.0075f * completed * cucul_get_canvas_height(mask); |
---|
212 | int w2 = cucul_get_canvas_width(mask) / 2; |
---|
213 | int h2 = cucul_get_canvas_height(mask) / 2; |
---|
214 | float angle = (0.0075f * completed * 360) * 3.14 / 180, x, y; |
---|
215 | unsigned int i; |
---|
216 | |
---|
217 | switch(tmode) |
---|
218 | { |
---|
219 | case TRANSITION_STAR: |
---|
220 | /* Compute rotated coordinates */ |
---|
221 | for(i = 0; i < (sizeof(star) / sizeof(*star)) / 2; i++) |
---|
222 | { |
---|
223 | x = star[i * 2]; |
---|
224 | y = star[i * 2 + 1]; |
---|
225 | |
---|
226 | star_rot[i * 2] = x * cos(angle) - y * sin(angle); |
---|
227 | star_rot[i * 2 + 1] = y * cos(angle) + x * sin(angle); |
---|
228 | } |
---|
229 | |
---|
230 | mulx *= 1.8; |
---|
231 | muly *= 1.8; |
---|
232 | |
---|
233 | #define DO_TRI(a, b, c) \ |
---|
234 | cucul_fill_triangle(mask, \ |
---|
235 | star_rot[(a)*2] * mulx + w2, star_rot[(a)*2+1] * muly + h2, \ |
---|
236 | star_rot[(b)*2] * mulx + w2, star_rot[(b)*2+1] * muly + h2, \ |
---|
237 | star_rot[(c)*2] * mulx + w2, star_rot[(c)*2+1] * muly + h2, "#") |
---|
238 | DO_TRI(0, 1, 9); |
---|
239 | DO_TRI(1, 2, 3); |
---|
240 | DO_TRI(3, 4, 5); |
---|
241 | DO_TRI(5, 6, 7); |
---|
242 | DO_TRI(7, 8, 9); |
---|
243 | DO_TRI(9, 1, 5); |
---|
244 | DO_TRI(9, 5, 7); |
---|
245 | DO_TRI(1, 3, 5); |
---|
246 | break; |
---|
247 | |
---|
248 | case TRANSITION_CIRCLE: |
---|
249 | cucul_fill_ellipse(mask, w2, h2, mulx, muly, "#"); |
---|
250 | break; |
---|
251 | |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | /* The plasma effect */ |
---|
256 | #define TABLEX (XSIZ * 2) |
---|
257 | #define TABLEY (YSIZ * 2) |
---|
258 | static uint8_t table[TABLEX * TABLEY]; |
---|
259 | |
---|
260 | static void do_plasma(uint8_t *, |
---|
261 | double, double, double, double, double, double); |
---|
262 | |
---|
263 | void plasma(enum action action, cucul_canvas_t *cv) |
---|
264 | { |
---|
265 | static cucul_dither_t *dither; |
---|
266 | static uint8_t *screen; |
---|
267 | static unsigned int red[256], green[256], blue[256], alpha[256]; |
---|
268 | static double r[3], R[6]; |
---|
269 | |
---|
270 | int i, x, y; |
---|
271 | |
---|
272 | switch(action) |
---|
273 | { |
---|
274 | case PREPARE: |
---|
275 | /* Fill various tables */ |
---|
276 | for(i = 0 ; i < 256; i++) |
---|
277 | red[i] = green[i] = blue[i] = alpha[i] = 0; |
---|
278 | |
---|
279 | for(i = 0; i < 3; i++) |
---|
280 | r[i] = (double)(cucul_rand(1, 1000)) / 60000 * M_PI; |
---|
281 | |
---|
282 | for(i = 0; i < 6; i++) |
---|
283 | R[i] = (double)(cucul_rand(1, 1000)) / 10000; |
---|
284 | |
---|
285 | for(y = 0 ; y < TABLEY ; y++) |
---|
286 | for(x = 0 ; x < TABLEX ; x++) |
---|
287 | { |
---|
288 | double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2)) |
---|
289 | + (y - (TABLEX / 2)) * (y - (TABLEX / 2)))) |
---|
290 | * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY))); |
---|
291 | |
---|
292 | table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6; |
---|
293 | } |
---|
294 | break; |
---|
295 | |
---|
296 | case INIT: |
---|
297 | screen = malloc(XSIZ * YSIZ * sizeof(uint8_t)); |
---|
298 | dither = cucul_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0); |
---|
299 | break; |
---|
300 | |
---|
301 | case UPDATE: |
---|
302 | for(i = 0 ; i < 256; i++) |
---|
303 | { |
---|
304 | double z = ((double)i) / 256 * 6 * M_PI; |
---|
305 | |
---|
306 | red[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff; |
---|
307 | blue[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff; |
---|
308 | green[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff; |
---|
309 | } |
---|
310 | |
---|
311 | /* Set the palette */ |
---|
312 | cucul_set_dither_palette(dither, red, green, blue, alpha); |
---|
313 | |
---|
314 | do_plasma(screen, |
---|
315 | (1.0 + sin(((double)frame) * R[0])) / 2, |
---|
316 | (1.0 + sin(((double)frame) * R[1])) / 2, |
---|
317 | (1.0 + sin(((double)frame) * R[2])) / 2, |
---|
318 | (1.0 + sin(((double)frame) * R[3])) / 2, |
---|
319 | (1.0 + sin(((double)frame) * R[4])) / 2, |
---|
320 | (1.0 + sin(((double)frame) * R[5])) / 2); |
---|
321 | break; |
---|
322 | |
---|
323 | case RENDER: |
---|
324 | cucul_dither_bitmap(cv, 0, 0, |
---|
325 | cucul_get_canvas_width(cv), |
---|
326 | cucul_get_canvas_height(cv), |
---|
327 | dither, screen); |
---|
328 | break; |
---|
329 | |
---|
330 | case FREE: |
---|
331 | free(screen); |
---|
332 | cucul_free_dither(dither); |
---|
333 | break; |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | static void do_plasma(uint8_t *pixels, double x_1, double y_1, |
---|
338 | double x_2, double y_2, double x_3, double y_3) |
---|
339 | { |
---|
340 | unsigned int X1 = x_1 * (TABLEX / 2), |
---|
341 | Y1 = y_1 * (TABLEY / 2), |
---|
342 | X2 = x_2 * (TABLEX / 2), |
---|
343 | Y2 = y_2 * (TABLEY / 2), |
---|
344 | X3 = x_3 * (TABLEX / 2), |
---|
345 | Y3 = y_3 * (TABLEY / 2); |
---|
346 | unsigned int y; |
---|
347 | uint8_t * t1 = table + X1 + Y1 * TABLEX, |
---|
348 | * t2 = table + X2 + Y2 * TABLEX, |
---|
349 | * t3 = table + X3 + Y3 * TABLEX; |
---|
350 | |
---|
351 | for(y = 0; y < YSIZ; y++) |
---|
352 | { |
---|
353 | unsigned int x; |
---|
354 | uint8_t * tmp = pixels + y * YSIZ; |
---|
355 | unsigned int ty = y * TABLEX, tmax = ty + XSIZ; |
---|
356 | for(x = 0; ty < tmax; ty++, tmp++) |
---|
357 | tmp[0] = t1[ty] + t2[ty] + t3[ty]; |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | /* The metaball effect */ |
---|
362 | #define METASIZE (XSIZ/2) |
---|
363 | #define METABALLS 12 |
---|
364 | #define CROPBALL 200 /* Colour index where to crop balls */ |
---|
365 | static uint8_t metaball[METASIZE * METASIZE]; |
---|
366 | |
---|
367 | static void create_ball(void); |
---|
368 | static void draw_ball(uint8_t *, unsigned int, unsigned int); |
---|
369 | |
---|
370 | void metaballs(enum action action, cucul_canvas_t *cv) |
---|
371 | { |
---|
372 | static cucul_dither_t *cucul_dither; |
---|
373 | static uint8_t *screen; |
---|
374 | static unsigned int r[256], g[256], b[256], a[256]; |
---|
375 | static float dd[METABALLS], di[METABALLS], dj[METABALLS], dk[METABALLS]; |
---|
376 | static unsigned int x[METABALLS], y[METABALLS]; |
---|
377 | static float i = 10.0, j = 17.0, k = 11.0; |
---|
378 | static double offset[360 + 80]; |
---|
379 | |
---|
380 | int n, angle; |
---|
381 | |
---|
382 | switch(action) |
---|
383 | { |
---|
384 | case PREPARE: |
---|
385 | /* Make the palette eatable by libcaca */ |
---|
386 | for(n = 0; n < 256; n++) |
---|
387 | r[n] = g[n] = b[n] = a[n] = 0x0; |
---|
388 | r[255] = g[255] = b[255] = 0xfff; |
---|
389 | |
---|
390 | /* Generate ball sprite */ |
---|
391 | create_ball(); |
---|
392 | |
---|
393 | for(n = 0; n < METABALLS; n++) |
---|
394 | { |
---|
395 | dd[n] = cucul_rand(0, 100); |
---|
396 | di[n] = (float)cucul_rand(500, 4000) / 6000.0; |
---|
397 | dj[n] = (float)cucul_rand(500, 4000) / 6000.0; |
---|
398 | dk[n] = (float)cucul_rand(500, 4000) / 6000.0; |
---|
399 | } |
---|
400 | |
---|
401 | for(n = 0; n < 360 + 80; n++) |
---|
402 | offset[n] = 1.0 + sin((double)(n * M_PI / 60)); |
---|
403 | break; |
---|
404 | |
---|
405 | case INIT: |
---|
406 | screen = malloc(XSIZ * YSIZ * sizeof(uint8_t)); |
---|
407 | /* Create a libcucul dither smaller than our pixel buffer, so that we |
---|
408 | * display only the interesting part of it */ |
---|
409 | cucul_dither = cucul_create_dither(8, XSIZ - METASIZE, YSIZ - METASIZE, |
---|
410 | XSIZ, 0, 0, 0, 0); |
---|
411 | break; |
---|
412 | |
---|
413 | case UPDATE: |
---|
414 | angle = frame % 360; |
---|
415 | |
---|
416 | /* Crop the palette */ |
---|
417 | for(n = CROPBALL; n < 255; n++) |
---|
418 | { |
---|
419 | int t1, t2, t3; |
---|
420 | double c1 = offset[angle]; |
---|
421 | double c2 = offset[angle + 40]; |
---|
422 | double c3 = offset[angle + 80]; |
---|
423 | |
---|
424 | t1 = n < 0x40 ? 0 : n < 0xc0 ? (n - 0x40) * 0x20 : 0xfff; |
---|
425 | t2 = n < 0xe0 ? 0 : (n - 0xe0) * 0x80; |
---|
426 | t3 = n < 0x40 ? n * 0x40 : 0xfff; |
---|
427 | |
---|
428 | r[n] = (c1 * t1 + c2 * t2 + c3 * t3) / 4; |
---|
429 | g[n] = (c1 * t2 + c2 * t3 + c3 * t1) / 4; |
---|
430 | b[n] = (c1 * t3 + c2 * t1 + c3 * t2) / 4; |
---|
431 | } |
---|
432 | |
---|
433 | /* Set the palette */ |
---|
434 | cucul_set_dither_palette(cucul_dither, r, g, b, a); |
---|
435 | |
---|
436 | /* Silly paths for our balls */ |
---|
437 | for(n = 0; n < METABALLS; n++) |
---|
438 | { |
---|
439 | float u = di[n] * i + dj[n] * j + dk[n] * sin(di[n] * k); |
---|
440 | float v = dd[n] + di[n] * j + dj[n] * k + dk[n] * sin(dk[n] * i); |
---|
441 | u = sin(i + u * 2.1) * (1.0 + sin(u)); |
---|
442 | v = sin(j + v * 1.9) * (1.0 + sin(v)); |
---|
443 | x[n] = (XSIZ - METASIZE) / 2 + u * (XSIZ - METASIZE) / 4; |
---|
444 | y[n] = (YSIZ - METASIZE) / 2 + v * (YSIZ - METASIZE) / 4; |
---|
445 | } |
---|
446 | |
---|
447 | i += 0.011; |
---|
448 | j += 0.017; |
---|
449 | k += 0.019; |
---|
450 | |
---|
451 | memset(screen, 0, XSIZ * YSIZ); |
---|
452 | |
---|
453 | for(n = 0; n < METABALLS; n++) |
---|
454 | draw_ball(screen, x[n], y[n]); |
---|
455 | break; |
---|
456 | |
---|
457 | case RENDER: |
---|
458 | cucul_dither_bitmap(cv, 0, 0, |
---|
459 | cucul_get_canvas_width(cv), |
---|
460 | cucul_get_canvas_height(cv), |
---|
461 | cucul_dither, screen + (METASIZE / 2) * (1 + XSIZ)); |
---|
462 | break; |
---|
463 | |
---|
464 | case FREE: |
---|
465 | free(screen); |
---|
466 | cucul_free_dither(cucul_dither); |
---|
467 | break; |
---|
468 | } |
---|
469 | } |
---|
470 | |
---|
471 | static void create_ball(void) |
---|
472 | { |
---|
473 | int x, y; |
---|
474 | float distance; |
---|
475 | |
---|
476 | for(y = 0; y < METASIZE; y++) |
---|
477 | for(x = 0; x < METASIZE; x++) |
---|
478 | { |
---|
479 | distance = ((METASIZE/2) - x) * ((METASIZE/2) - x) |
---|
480 | + ((METASIZE/2) - y) * ((METASIZE/2) - y); |
---|
481 | distance = sqrt(distance) * 64 / METASIZE; |
---|
482 | metaball[x + y * METASIZE] = distance > 15 ? 0 : (255 - distance) * 15; |
---|
483 | } |
---|
484 | } |
---|
485 | |
---|
486 | static void draw_ball(uint8_t *screen, unsigned int bx, unsigned int by) |
---|
487 | { |
---|
488 | unsigned int color; |
---|
489 | unsigned int i, e = 0; |
---|
490 | unsigned int b = (by * XSIZ) + bx; |
---|
491 | |
---|
492 | for(i = 0; i < METASIZE * METASIZE; i++) |
---|
493 | { |
---|
494 | color = screen[b] + metaball[i]; |
---|
495 | |
---|
496 | if(color > 255) |
---|
497 | color = 255; |
---|
498 | |
---|
499 | screen[b] = color; |
---|
500 | if(e == METASIZE) |
---|
501 | { |
---|
502 | e = 0; |
---|
503 | b += XSIZ - METASIZE; |
---|
504 | } |
---|
505 | b++; |
---|
506 | e++; |
---|
507 | } |
---|
508 | } |
---|
509 | |
---|
510 | /* The moiré effect */ |
---|
511 | #define DISCSIZ (XSIZ*2) |
---|
512 | #define DISCTHICKNESS (XSIZ*15/40) |
---|
513 | static uint8_t disc[DISCSIZ * DISCSIZ]; |
---|
514 | |
---|
515 | static void put_disc(uint8_t *, int, int); |
---|
516 | static void draw_line(int, int, char); |
---|
517 | |
---|
518 | void moire(enum action action, cucul_canvas_t *cv) |
---|
519 | { |
---|
520 | static cucul_dither_t *dither; |
---|
521 | static uint8_t *screen; |
---|
522 | static unsigned int red[256], green[256], blue[256], alpha[256]; |
---|
523 | |
---|
524 | int i, x, y; |
---|
525 | |
---|
526 | switch(action) |
---|
527 | { |
---|
528 | case PREPARE: |
---|
529 | /* Fill various tables */ |
---|
530 | for(i = 0 ; i < 256; i++) |
---|
531 | red[i] = green[i] = blue[i] = alpha[i] = 0; |
---|
532 | |
---|
533 | red[0] = green[0] = blue[0] = 0x777; |
---|
534 | red[1] = green[1] = blue[1] = 0xfff; |
---|
535 | |
---|
536 | /* Fill the circle */ |
---|
537 | for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS) |
---|
538 | { |
---|
539 | int t, dx, dy; |
---|
540 | |
---|
541 | for(t = 0, dx = 0, dy = i; dx <= dy; dx++) |
---|
542 | { |
---|
543 | draw_line(dx / 3, dy / 3, (i / DISCTHICKNESS) % 2); |
---|
544 | draw_line(dy / 3, dx / 3, (i / DISCTHICKNESS) % 2); |
---|
545 | |
---|
546 | t += t > 0 ? dx - dy-- : dx; |
---|
547 | } |
---|
548 | } |
---|
549 | |
---|
550 | break; |
---|
551 | |
---|
552 | case INIT: |
---|
553 | screen = malloc(XSIZ * YSIZ * sizeof(uint8_t)); |
---|
554 | dither = cucul_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0); |
---|
555 | break; |
---|
556 | |
---|
557 | case UPDATE: |
---|
558 | memset(screen, 0, XSIZ * YSIZ); |
---|
559 | |
---|
560 | /* Set the palette */ |
---|
561 | red[0] = 0.5 * (1 + sin(0.05 * frame)) * 0xfff; |
---|
562 | green[0] = 0.5 * (1 + cos(0.07 * frame)) * 0xfff; |
---|
563 | blue[0] = 0.5 * (1 + cos(0.06 * frame)) * 0xfff; |
---|
564 | |
---|
565 | red[1] = 0.5 * (1 + sin(0.07 * frame + 5.0)) * 0xfff; |
---|
566 | green[1] = 0.5 * (1 + cos(0.06 * frame + 5.0)) * 0xfff; |
---|
567 | blue[1] = 0.5 * (1 + cos(0.05 * frame + 5.0)) * 0xfff; |
---|
568 | |
---|
569 | cucul_set_dither_palette(dither, red, green, blue, alpha); |
---|
570 | |
---|
571 | /* Draw circles */ |
---|
572 | x = cos(0.07 * frame + 5.0) * 128.0 + (XSIZ / 2); |
---|
573 | y = sin(0.11 * frame) * 128.0 + (YSIZ / 2); |
---|
574 | put_disc(screen, x, y); |
---|
575 | |
---|
576 | x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2); |
---|
577 | y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2); |
---|
578 | put_disc(screen, x, y); |
---|
579 | break; |
---|
580 | |
---|
581 | case RENDER: |
---|
582 | cucul_dither_bitmap(cv, 0, 0, |
---|
583 | cucul_get_canvas_width(cv), |
---|
584 | cucul_get_canvas_height(cv), |
---|
585 | dither, screen); |
---|
586 | break; |
---|
587 | |
---|
588 | case FREE: |
---|
589 | free(screen); |
---|
590 | cucul_free_dither(dither); |
---|
591 | break; |
---|
592 | } |
---|
593 | } |
---|
594 | |
---|
595 | static void put_disc(uint8_t *screen, int x, int y) |
---|
596 | { |
---|
597 | char *src = ((char*)disc) + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ; |
---|
598 | int i, j; |
---|
599 | |
---|
600 | for(j = 0; j < YSIZ; j++) |
---|
601 | for(i = 0; i < XSIZ; i++) |
---|
602 | { |
---|
603 | screen[i + XSIZ * j] ^= src[i + DISCSIZ * j]; |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | static void draw_line(int x, int y, char color) |
---|
608 | { |
---|
609 | if(x == 0 || y == 0 || y > DISCSIZ / 2) |
---|
610 | return; |
---|
611 | |
---|
612 | if(x > DISCSIZ / 2) |
---|
613 | x = DISCSIZ / 2; |
---|
614 | |
---|
615 | memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y), |
---|
616 | color, 2 * x - 1); |
---|
617 | memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1), |
---|
618 | color, 2 * x - 1); |
---|
619 | } |
---|
620 | |
---|
621 | /* Langton ant effect */ |
---|
622 | #define ANTS 15 |
---|
623 | #define ITER 2 |
---|
624 | |
---|
625 | void langton(enum action action, cucul_canvas_t *cv) |
---|
626 | { |
---|
627 | static char gradient[] = |
---|
628 | { |
---|
629 | ' ', ' ', '.', '.', ':', ':', 'x', 'x', |
---|
630 | 'X', 'X', '&', '&', 'W', 'W', '@', '@', |
---|
631 | }; |
---|
632 | static int steps[][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; |
---|
633 | static uint8_t *screen; |
---|
634 | static int width, height; |
---|
635 | static int ax[ANTS], ay[ANTS], dir[ANTS]; |
---|
636 | |
---|
637 | int i, a, x, y; |
---|
638 | |
---|
639 | switch(action) |
---|
640 | { |
---|
641 | case PREPARE: |
---|
642 | width = cucul_get_canvas_width(cv); |
---|
643 | height = cucul_get_canvas_height(cv); |
---|
644 | for(i = 0; i < ANTS; i++) |
---|
645 | { |
---|
646 | ax[i] = cucul_rand(0, width); |
---|
647 | ay[i] = cucul_rand(0, height); |
---|
648 | dir[i] = cucul_rand(0, 4); |
---|
649 | } |
---|
650 | break; |
---|
651 | |
---|
652 | case INIT: |
---|
653 | screen = malloc(width * height); |
---|
654 | memset(screen, 0, width * height); |
---|
655 | break; |
---|
656 | |
---|
657 | case UPDATE: |
---|
658 | for(i = 0; i < ITER; i++) |
---|
659 | { |
---|
660 | for(x = 0; x < width * height; x++) |
---|
661 | { |
---|
662 | uint8_t p = screen[x]; |
---|
663 | if((p & 0x0f) > 1) |
---|
664 | screen[x] = p - 1; |
---|
665 | } |
---|
666 | |
---|
667 | for(a = 0; a < ANTS; a++) |
---|
668 | { |
---|
669 | uint8_t p = screen[ax[a] + width * ay[a]]; |
---|
670 | |
---|
671 | if(p & 0x0f) |
---|
672 | { |
---|
673 | dir[a] = (dir[a] + 1) % 4; |
---|
674 | screen[ax[a] + width * ay[a]] = a << 4; |
---|
675 | } |
---|
676 | else |
---|
677 | { |
---|
678 | dir[a] = (dir[a] + 3) % 4; |
---|
679 | screen[ax[a] + width * ay[a]] = (a << 4) | 0x0f; |
---|
680 | } |
---|
681 | ax[a] = (width + ax[a] + steps[dir[a]][0]) % width; |
---|
682 | ay[a] = (height + ay[a] + steps[dir[a]][1]) % height; |
---|
683 | } |
---|
684 | } |
---|
685 | break; |
---|
686 | |
---|
687 | case RENDER: |
---|
688 | for(y = 0; y < height; y++) |
---|
689 | { |
---|
690 | for(x = 0; x < width; x++) |
---|
691 | { |
---|
692 | uint8_t p = screen[x + width * y]; |
---|
693 | |
---|
694 | if(p & 0x0f) |
---|
695 | cucul_set_color(cv, CUCUL_COLOR_WHITE, p >> 4); |
---|
696 | else |
---|
697 | cucul_set_color(cv, CUCUL_COLOR_BLACK, CUCUL_COLOR_BLACK); |
---|
698 | cucul_putchar(cv, x, y, gradient[p & 0x0f]); |
---|
699 | } |
---|
700 | } |
---|
701 | break; |
---|
702 | |
---|
703 | case FREE: |
---|
704 | free(screen); |
---|
705 | break; |
---|
706 | } |
---|
707 | } |
---|
708 | |
---|
709 | /* Matrix effect */ |
---|
710 | #define MAXDROPS 500 |
---|
711 | #define MINLEN 15 |
---|
712 | #define MAXLEN 30 |
---|
713 | |
---|
714 | void matrix(enum action action, cucul_canvas_t *cv) |
---|
715 | { |
---|
716 | static struct drop |
---|
717 | { |
---|
718 | int x, y, speed, len; |
---|
719 | char str[MAXLEN]; |
---|
720 | } |
---|
721 | drop[MAXDROPS]; |
---|
722 | |
---|
723 | int w, h, i, j; |
---|
724 | |
---|
725 | switch(action) |
---|
726 | { |
---|
727 | case PREPARE: |
---|
728 | for(i = 0; i < MAXDROPS; i++) |
---|
729 | { |
---|
730 | drop[i].x = cucul_rand(0, 1000); |
---|
731 | drop[i].y = cucul_rand(0, 1000); |
---|
732 | drop[i].speed = 5 + cucul_rand(0, 30); |
---|
733 | drop[i].len = MINLEN + cucul_rand(0, (MAXLEN - MINLEN)); |
---|
734 | for(j = 0; j < MAXLEN; j++) |
---|
735 | drop[i].str[j] = cucul_rand('0', 'z'); |
---|
736 | } |
---|
737 | break; |
---|
738 | |
---|
739 | case INIT: |
---|
740 | break; |
---|
741 | |
---|
742 | case UPDATE: |
---|
743 | w = cucul_get_canvas_width(cv); |
---|
744 | h = cucul_get_canvas_height(cv); |
---|
745 | |
---|
746 | for(i = 0; i < MAXDROPS && i < (w * h / 32); i++) |
---|
747 | { |
---|
748 | drop[i].y += drop[i].speed; |
---|
749 | if(drop[i].y > 1000) |
---|
750 | { |
---|
751 | drop[i].y -= 1000; |
---|
752 | drop[i].x = cucul_rand(0, 1000); |
---|
753 | } |
---|
754 | } |
---|
755 | break; |
---|
756 | |
---|
757 | case RENDER: |
---|
758 | w = cucul_get_canvas_width(cv); |
---|
759 | h = cucul_get_canvas_height(cv); |
---|
760 | |
---|
761 | cucul_set_color(cv, CUCUL_COLOR_BLACK, CUCUL_COLOR_BLACK); |
---|
762 | cucul_clear_canvas(cv); |
---|
763 | |
---|
764 | for(i = 0; i < MAXDROPS && i < (w * h / 32); i++) |
---|
765 | { |
---|
766 | int x, y; |
---|
767 | |
---|
768 | x = drop[i].x * w / 1000 / 2 * 2; |
---|
769 | y = drop[i].y * (h + MAXLEN) / 1000; |
---|
770 | |
---|
771 | for(j = 0; j < drop[i].len; j++) |
---|
772 | { |
---|
773 | unsigned int fg; |
---|
774 | |
---|
775 | if(j < 2) |
---|
776 | fg = CUCUL_COLOR_WHITE; |
---|
777 | else if(j < drop[i].len / 4) |
---|
778 | fg = CUCUL_COLOR_LIGHTGREEN; |
---|
779 | else if(j < drop[i].len * 4 / 5) |
---|
780 | fg = CUCUL_COLOR_GREEN; |
---|
781 | else |
---|
782 | fg = CUCUL_COLOR_DARKGRAY; |
---|
783 | cucul_set_color(cv, fg, CUCUL_COLOR_BLACK); |
---|
784 | |
---|
785 | cucul_putchar(cv, x, y - j, |
---|
786 | drop[i].str[(y - j) % drop[i].len]); |
---|
787 | } |
---|
788 | } |
---|
789 | break; |
---|
790 | |
---|
791 | case FREE: |
---|
792 | break; |
---|
793 | } |
---|
794 | } |
---|
795 | |
---|
796 | |
---|