source: libcaca/trunk/src/cacademo.c @ 2049

Last change on this file since 2049 was 2049, checked in by Sam Hocevar, 16 years ago
  • Made the caca_event_t structure opaque and created a whole bunch of functions to access its real data. This is a big API change that will break your software, sorry :(
  • Property svn:keywords set to Id
File size: 27.1 KB
Line 
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 2049 2007-11-25 11:11:54Z sam $
9 *
10 *  This program is free software. It comes without any warranty, to
11 *  the extent permitted by applicable law. You can redistribute it
12 *  and/or modify it under the terms of the Do What The Fuck You Want
13 *  To Public License, Version 2, as published by Sam Hocevar. See
14 *  http://sam.zoy.org/wtfpl/COPYING for more details.
15 */
16
17#include "config.h"
18#include "common.h"
19
20#if !defined(__KERNEL__)
21#   include <stdio.h>
22#   include <stdlib.h>
23#   include <string.h>
24#   include <math.h>
25#   ifndef M_PI
26#       define M_PI 3.14159265358979323846
27#   endif
28#endif
29
30#include "cucul.h"
31#include "caca.h"
32
33enum action { PREPARE, INIT, UPDATE, RENDER, FREE };
34
35void transition(cucul_canvas_t *, int, int);
36void plasma(enum action, cucul_canvas_t *);
37void metaballs(enum action, cucul_canvas_t *);
38void moire(enum action, cucul_canvas_t *);
39void langton(enum action, cucul_canvas_t *);
40void matrix(enum action, cucul_canvas_t *);
41void rotozoom(enum action, cucul_canvas_t *);
42
43void (*fn[])(enum action, cucul_canvas_t *) =
44{
45    plasma,
46    metaballs,
47    moire,
48    /*langton,*/
49    matrix,
50    rotozoom,
51};
52#define DEMOS (sizeof(fn)/sizeof(*fn))
53
54#define DEMO_FRAMES cucul_rand(500, 1000)
55#define TRANSITION_FRAMES 40
56
57#define TRANSITION_COUNT  5
58#define TRANSITION_CIRCLE 0
59#define TRANSITION_STAR   1
60#define TRANSITION_SQUARE 2
61#define TRANSITION_VLINES 3
62#define TRANSITION_HLINES 4
63
64/* Common macros for dither-based demos */
65#define XSIZ 256
66#define YSIZ 256
67
68/* Global variables */
69static int frame = 0;
70
71int main(int argc, char **argv)
72{
73    static caca_display_t *dp;
74    static cucul_canvas_t *frontcv, *backcv, *mask;
75
76    int demo, next = -1, pause = 0, next_transition = DEMO_FRAMES;
77    unsigned int i;
78    int tmode = cucul_rand(0, TRANSITION_COUNT);
79
80    /* Set up two canvases, a mask, and attach a display to the front one */
81    frontcv = cucul_create_canvas(0, 0);
82    backcv = cucul_create_canvas(0, 0);
83    mask = cucul_create_canvas(0, 0);
84
85    dp = caca_create_display(frontcv);
86    if(!dp)
87        return 1;
88
89    cucul_set_canvas_size(backcv, cucul_get_canvas_width(frontcv),
90                                  cucul_get_canvas_height(frontcv));
91    cucul_set_canvas_size(mask, cucul_get_canvas_width(frontcv),
92                                cucul_get_canvas_height(frontcv));
93
94    caca_set_display_time(dp, 20000);
95
96    /* Initialise all demos' lookup tables */
97    for(i = 0; i < DEMOS; i++)
98        fn[i](PREPARE, frontcv);
99
100    /* Choose a demo at random */
101    demo = cucul_rand(0, DEMOS);
102    fn[demo](INIT, frontcv);
103
104    for(;;)
105    {
106        /* Handle events */
107        caca_event_t ev;
108        while(caca_get_event(dp, CACA_EVENT_KEY_PRESS
109                                  | CACA_EVENT_QUIT, &ev, 0))
110        {
111            if(caca_get_event_type(&ev) == CACA_EVENT_QUIT)
112                goto end;
113
114            switch(caca_get_event_key_ch(&ev))
115            {
116                case CACA_KEY_ESCAPE:
117                case CACA_KEY_CTRL_C:
118                case CACA_KEY_CTRL_Z:
119                    goto end;
120                case ' ':
121                    pause = !pause;
122                    break;
123                case '\r':
124                    if(next == -1)
125                        next_transition = frame;
126                    break;
127            }
128        }
129
130        /* Resize the spare canvas, just in case the main one changed */
131        cucul_set_canvas_size(backcv, cucul_get_canvas_width(frontcv),
132                                      cucul_get_canvas_height(frontcv));
133        cucul_set_canvas_size(mask, cucul_get_canvas_width(frontcv),
134                                    cucul_get_canvas_height(frontcv));
135
136        if(pause)
137            goto paused;
138
139        /* Update demo's data */
140        fn[demo](UPDATE, frontcv);
141
142        /* Handle transitions */
143        if(frame == next_transition)
144        {
145            next = cucul_rand(0, DEMOS);
146            if(next == demo)
147                next = (next + 1) % DEMOS;
148            fn[next](INIT, backcv);
149        }
150        else if(frame == next_transition + TRANSITION_FRAMES)
151        {
152            fn[demo](FREE, frontcv);
153            demo = next;
154            next = -1;
155            next_transition = frame + DEMO_FRAMES;
156            tmode = cucul_rand(0, TRANSITION_COUNT);
157        }
158
159        if(next != -1)
160            fn[next](UPDATE, backcv);
161
162        frame++;
163paused:
164        /* Render main demo's canvas */
165        fn[demo](RENDER, frontcv);
166
167        /* If a transition is on its way, render it */
168        if(next != -1)
169        {
170            fn[next](RENDER, backcv);
171            cucul_set_color_ansi(mask, CUCUL_LIGHTGRAY, CUCUL_BLACK);
172            cucul_clear_canvas(mask);
173            cucul_set_color_ansi(mask, CUCUL_WHITE, CUCUL_WHITE);
174            transition(mask, tmode,
175                       100 * (frame - next_transition) / TRANSITION_FRAMES);
176            cucul_blit(frontcv, 0, 0, backcv, mask);
177        }
178
179        cucul_set_color_ansi(frontcv, CUCUL_WHITE, CUCUL_BLUE);
180        if(frame < 100)
181            cucul_put_str(frontcv, cucul_get_canvas_width(frontcv) - 30,
182                                   cucul_get_canvas_height(frontcv) - 2,
183                                   " -=[ Powered by libcaca ]=- ");
184        caca_refresh_display(dp);
185    }
186end:
187    if(next != -1)
188        fn[next](FREE, frontcv);
189    fn[demo](FREE, frontcv);
190
191    caca_free_display(dp);
192    cucul_free_canvas(mask);
193    cucul_free_canvas(backcv);
194    cucul_free_canvas(frontcv);
195
196    return 0;
197}
198
199/* Transitions */
200void transition(cucul_canvas_t *mask, int tmode, int completed)
201{
202    static float const star[] =
203    {
204         0.000000, -1.000000,
205         0.308000, -0.349000,
206         0.992000, -0.244000,
207         0.500000,  0.266000,
208         0.632000,  0.998000,
209         0.008000,  0.659000,
210        -0.601000,  0.995000,
211        -0.496000,  0.275000,
212        -0.997000, -0.244000,
213        -0.313000, -0.349000
214    };
215    static float star_rot[sizeof(star)/sizeof(*star)];
216
217
218    static float const square[] =
219    {
220        -1, -1,
221        1, -1,
222        1, 1,
223        -1, 1
224    };
225    static float square_rot[sizeof(square)/sizeof(*square)];
226
227    float mulx = 0.0075f * completed * cucul_get_canvas_width(mask);
228    float muly = 0.0075f * completed * cucul_get_canvas_height(mask);
229    int w2 = cucul_get_canvas_width(mask) / 2;
230    int h2 = cucul_get_canvas_height(mask) / 2;
231    float angle = (0.0075f * completed * 360) * 3.14 / 180, x, y;
232    unsigned int i;
233
234    switch(tmode)
235    {
236        case TRANSITION_SQUARE:
237            /* Compute rotated coordinates */
238            for(i = 0; i < (sizeof(square) / sizeof(*square)) / 2; i++)
239            {
240                x = square[i * 2];
241                y = square[i * 2 + 1];
242
243                square_rot[i * 2] = x * cos(angle) - y * sin(angle);
244                square_rot[i * 2 + 1] = y * cos(angle) + x * sin(angle);
245            }
246
247            mulx *= 1.8;
248            muly *= 1.8;
249            cucul_fill_triangle(mask,
250                                square_rot[0*2] * mulx + w2, square_rot[0*2+1] * muly + h2, \
251                                square_rot[1*2] * mulx + w2, square_rot[1*2+1] * muly + h2, \
252                                square_rot[2*2] * mulx + w2, square_rot[2*2+1] * muly + h2, '#');
253            cucul_fill_triangle(mask,
254                                square_rot[0*2] * mulx + w2, square_rot[0*2+1] * muly + h2, \
255                                square_rot[2*2] * mulx + w2, square_rot[2*2+1] * muly + h2, \
256                                square_rot[3*2] * mulx + w2, square_rot[3*2+1] * muly + h2, '#');
257            break;
258
259
260        case TRANSITION_STAR:
261            /* Compute rotated coordinates */
262            for(i = 0; i < (sizeof(star) / sizeof(*star)) / 2; i++)
263            {
264                x = star[i * 2];
265                y = star[i * 2 + 1];
266
267                star_rot[i * 2] = x * cos(angle) - y * sin(angle);
268                star_rot[i * 2 + 1] = y * cos(angle) + x * sin(angle);
269            }
270
271            mulx *= 1.8;
272            muly *= 1.8;
273
274#define DO_TRI(a, b, c) \
275    cucul_fill_triangle(mask, \
276        star_rot[(a)*2] * mulx + w2, star_rot[(a)*2+1] * muly + h2, \
277        star_rot[(b)*2] * mulx + w2, star_rot[(b)*2+1] * muly + h2, \
278        star_rot[(c)*2] * mulx + w2, star_rot[(c)*2+1] * muly + h2, '#')
279            DO_TRI(0, 1, 9);
280            DO_TRI(1, 2, 3);
281            DO_TRI(3, 4, 5);
282            DO_TRI(5, 6, 7);
283            DO_TRI(7, 8, 9);
284            DO_TRI(9, 1, 5);
285            DO_TRI(9, 5, 7);
286            DO_TRI(1, 3, 5);
287            break;
288
289        case TRANSITION_CIRCLE:
290            cucul_fill_ellipse(mask, w2, h2, mulx, muly, '#');
291            break;
292
293        case TRANSITION_VLINES:
294            for(i = 0; i < 8; i++)
295            {
296                int w = cucul_get_canvas_width(mask);
297                int h = cucul_get_canvas_height(mask);
298                int z = ((i & 1) ? h : -h) * (100 - completed) / 100;
299
300                cucul_fill_box(mask, i * w / 8, z, (i + 1) * w / 8, z + h, '#');
301            }
302            break;
303
304        case TRANSITION_HLINES:
305            for(i = 0; i < 6; i++)
306            {
307                int w = cucul_get_canvas_width(mask);
308                int h = cucul_get_canvas_height(mask);
309                int z = ((i & 1) ? w : -w) * (100 - completed) / 100;
310
311                cucul_fill_box(mask, z, i * h / 6, z + w, (i + 1) * h / 6, '#');
312            }
313            break;
314    }
315}
316
317/* The plasma effect */
318#define TABLEX (XSIZ * 2)
319#define TABLEY (YSIZ * 2)
320static uint8_t table[TABLEX * TABLEY];
321
322static void do_plasma(uint8_t *,
323                      double, double, double, double, double, double);
324
325void plasma(enum action action, cucul_canvas_t *cv)
326{
327    static cucul_dither_t *dither;
328    static uint8_t *screen;
329    static unsigned int red[256], green[256], blue[256], alpha[256];
330    static double r[3], R[6];
331
332    int i, x, y;
333
334    switch(action)
335    {
336    case PREPARE:
337        /* Fill various tables */
338        for(i = 0 ; i < 256; i++)
339            red[i] = green[i] = blue[i] = alpha[i] = 0;
340
341        for(i = 0; i < 3; i++)
342            r[i] = (double)(cucul_rand(1, 1000)) / 60000 * M_PI;
343
344        for(i = 0; i < 6; i++)
345            R[i] = (double)(cucul_rand(1, 1000)) / 10000;
346
347        for(y = 0 ; y < TABLEY ; y++)
348            for(x = 0 ; x < TABLEX ; x++)
349        {
350            double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2))
351                                  + (y - (TABLEX / 2)) * (y - (TABLEX / 2))))
352                          * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY)));
353
354            table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6;
355        }
356        break;
357
358    case INIT:
359        screen = malloc(XSIZ * YSIZ * sizeof(uint8_t));
360        dither = cucul_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
361        break;
362
363    case UPDATE:
364        for(i = 0 ; i < 256; i++)
365        {
366            double z = ((double)i) / 256 * 6 * M_PI;
367
368            red[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
369            blue[i] = (1.0 + cos(z + r[0] * (frame + 100))) / 2 * 0xfff;
370            green[i] = (1.0 + cos(z + r[2] * (frame + 200))) / 2 * 0xfff;
371        }
372
373        /* Set the palette */
374        cucul_set_dither_palette(dither, red, green, blue, alpha);
375
376        do_plasma(screen,
377                  (1.0 + sin(((double)frame) * R[0])) / 2,
378                  (1.0 + sin(((double)frame) * R[1])) / 2,
379                  (1.0 + sin(((double)frame) * R[2])) / 2,
380                  (1.0 + sin(((double)frame) * R[3])) / 2,
381                  (1.0 + sin(((double)frame) * R[4])) / 2,
382                  (1.0 + sin(((double)frame) * R[5])) / 2);
383        break;
384
385    case RENDER:
386        cucul_dither_bitmap(cv, 0, 0,
387                            cucul_get_canvas_width(cv),
388                            cucul_get_canvas_height(cv),
389                            dither, screen);
390        break;
391
392    case FREE:
393        free(screen);
394        cucul_free_dither(dither);
395        break;
396    }
397}
398
399static void do_plasma(uint8_t *pixels, double x_1, double y_1,
400                      double x_2, double y_2, double x_3, double y_3)
401{
402    unsigned int X1 = x_1 * (TABLEX / 2),
403                 Y1 = y_1 * (TABLEY / 2),
404                 X2 = x_2 * (TABLEX / 2),
405                 Y2 = y_2 * (TABLEY / 2),
406                 X3 = x_3 * (TABLEX / 2),
407                 Y3 = y_3 * (TABLEY / 2);
408    unsigned int y;
409    uint8_t * t1 = table + X1 + Y1 * TABLEX,
410            * t2 = table + X2 + Y2 * TABLEX,
411            * t3 = table + X3 + Y3 * TABLEX;
412
413    for(y = 0; y < YSIZ; y++)
414    {
415        unsigned int x;
416        uint8_t * tmp = pixels + y * YSIZ;
417        unsigned int ty = y * TABLEX, tmax = ty + XSIZ;
418        for(x = 0; ty < tmax; ty++, tmp++)
419            tmp[0] = t1[ty] + t2[ty] + t3[ty];
420    }
421}
422
423/* The metaball effect */
424#define METASIZE (XSIZ/2)
425#define METABALLS 12
426#define CROPBALL 200 /* Colour index where to crop balls */
427static uint8_t metaball[METASIZE * METASIZE];
428
429static void create_ball(void);
430static void draw_ball(uint8_t *, unsigned int, unsigned int);
431
432void metaballs(enum action action, cucul_canvas_t *cv)
433{
434    static cucul_dither_t *cucul_dither;
435    static uint8_t *screen;
436    static unsigned int r[256], g[256], b[256], a[256];
437    static float dd[METABALLS], di[METABALLS], dj[METABALLS], dk[METABALLS];
438    static unsigned int x[METABALLS], y[METABALLS];
439    static float i = 10.0, j = 17.0, k = 11.0;
440    static double offset[360 + 80];
441    static unsigned int angleoff;
442
443    int n, angle;
444
445    switch(action)
446    {
447    case PREPARE:
448        /* Make the palette eatable by libcaca */
449        for(n = 0; n < 256; n++)
450            r[n] = g[n] = b[n] = a[n] = 0x0;
451        r[255] = g[255] = b[255] = 0xfff;
452
453        /* Generate ball sprite */
454        create_ball();
455
456        for(n = 0; n < METABALLS; n++)
457        {
458            dd[n] = cucul_rand(0, 100);
459            di[n] = (float)cucul_rand(500, 4000) / 6000.0;
460            dj[n] = (float)cucul_rand(500, 4000) / 6000.0;
461            dk[n] = (float)cucul_rand(500, 4000) / 6000.0;
462        }
463
464        angleoff = cucul_rand(0, 360);
465
466        for(n = 0; n < 360 + 80; n++)
467            offset[n] = 1.0 + sin((double)(n * M_PI / 60));
468        break;
469
470    case INIT:
471        screen = malloc(XSIZ * YSIZ * sizeof(uint8_t));
472        /* Create a libcucul dither smaller than our pixel buffer, so that we
473         * display only the interesting part of it */
474        cucul_dither = cucul_create_dither(8, XSIZ - METASIZE, YSIZ - METASIZE,
475                                           XSIZ, 0, 0, 0, 0);
476        break;
477
478    case UPDATE:
479        angle = (frame + angleoff) % 360;
480
481        /* Crop the palette */
482        for(n = CROPBALL; n < 255; n++)
483        {
484            int t1, t2, t3;
485            double c1 = offset[angle];
486            double c2 = offset[angle + 40];
487            double c3 = offset[angle + 80];
488
489            t1 = n < 0x40 ? 0 : n < 0xc0 ? (n - 0x40) * 0x20 : 0xfff;
490            t2 = n < 0xe0 ? 0 : (n - 0xe0) * 0x80;
491            t3 = n < 0x40 ? n * 0x40 : 0xfff;
492
493            r[n] = (c1 * t1 + c2 * t2 + c3 * t3) / 4;
494            g[n] = (c1 * t2 + c2 * t3 + c3 * t1) / 4;
495            b[n] = (c1 * t3 + c2 * t1 + c3 * t2) / 4;
496        }
497
498        /* Set the palette */
499        cucul_set_dither_palette(cucul_dither, r, g, b, a);
500
501        /* Silly paths for our balls */
502        for(n = 0; n < METABALLS; n++)
503        {
504            float u = di[n] * i + dj[n] * j + dk[n] * sin(di[n] * k);
505            float v = dd[n] + di[n] * j + dj[n] * k + dk[n] * sin(dk[n] * i);
506            u = sin(i + u * 2.1) * (1.0 + sin(u));
507            v = sin(j + v * 1.9) * (1.0 + sin(v));
508            x[n] = (XSIZ - METASIZE) / 2 + u * (XSIZ - METASIZE) / 4;
509            y[n] = (YSIZ - METASIZE) / 2 + v * (YSIZ - METASIZE) / 4;
510        }
511
512        i += 0.011;
513        j += 0.017;
514        k += 0.019;
515
516        memset(screen, 0, XSIZ * YSIZ);
517
518        for(n = 0; n < METABALLS; n++)
519            draw_ball(screen, x[n], y[n]);
520        break;
521
522    case RENDER:
523        cucul_dither_bitmap(cv, 0, 0,
524                          cucul_get_canvas_width(cv),
525                          cucul_get_canvas_height(cv),
526                          cucul_dither, screen + (METASIZE / 2) * (1 + XSIZ));
527        break;
528
529    case FREE:
530        free(screen);
531        cucul_free_dither(cucul_dither);
532        break;
533    }
534}
535
536static void create_ball(void)
537{
538    int x, y;
539    float distance;
540
541    for(y = 0; y < METASIZE; y++)
542        for(x = 0; x < METASIZE; x++)
543    {
544        distance = ((METASIZE/2) - x) * ((METASIZE/2) - x)
545                 + ((METASIZE/2) - y) * ((METASIZE/2) - y);
546        distance = sqrt(distance) * 64 / METASIZE;
547        metaball[x + y * METASIZE] = distance > 15 ? 0 : (255 - distance) * 15;
548    }
549}
550
551static void draw_ball(uint8_t *screen, unsigned int bx, unsigned int by)
552{
553    unsigned int color;
554    unsigned int i, e = 0;
555    unsigned int b = (by * XSIZ) + bx;
556
557    for(i = 0; i < METASIZE * METASIZE; i++)
558    {
559        color = screen[b] + metaball[i];
560
561        if(color > 255)
562            color = 255;
563
564        screen[b] = color;
565        if(e == METASIZE)
566        {
567            e = 0;
568            b += XSIZ - METASIZE;
569        }
570        b++;
571        e++;
572    }
573}
574
575/* The moiré effect */
576#define DISCSIZ (XSIZ*2)
577#define DISCTHICKNESS (XSIZ*15/40)
578static uint8_t disc[DISCSIZ * DISCSIZ];
579
580static void put_disc(uint8_t *, int, int);
581static void draw_line(int, int, char);
582
583void moire(enum action action, cucul_canvas_t *cv)
584{
585    static cucul_dither_t *dither;
586    static uint8_t *screen;
587    static float d[6];
588    static unsigned int red[256], green[256], blue[256], alpha[256];
589
590    int i, x, y;
591
592    switch(action)
593    {
594    case PREPARE:
595        /* Fill various tables */
596        for(i = 0 ; i < 256; i++)
597            red[i] = green[i] = blue[i] = alpha[i] = 0;
598
599        for(i = 0; i < 6; i++)
600            d[i] = ((float)cucul_rand(50, 70)) / 1000.0;
601
602        red[0] = green[0] = blue[0] = 0x777;
603        red[1] = green[1] = blue[1] = 0xfff;
604
605        /* Fill the circle */
606        for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS)
607        {
608            int t, dx, dy;
609
610            for(t = 0, dx = 0, dy = i; dx <= dy; dx++)
611            {
612                draw_line(dx / 3, dy / 3, (i / DISCTHICKNESS) % 2);
613                draw_line(dy / 3, dx / 3, (i / DISCTHICKNESS) % 2);
614
615                t += t > 0 ? dx - dy-- : dx;
616            }
617        }
618
619        break;
620
621    case INIT:
622        screen = malloc(XSIZ * YSIZ * sizeof(uint8_t));
623        dither = cucul_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
624        break;
625
626    case UPDATE:
627        memset(screen, 0, XSIZ * YSIZ);
628
629        /* Set the palette */
630        red[0] = 0.5 * (1 + sin(d[0] * (frame + 1000))) * 0xfff;
631        green[0] = 0.5 * (1 + cos(d[1] * frame)) * 0xfff;
632        blue[0] = 0.5 * (1 + cos(d[2] * (frame + 3000))) * 0xfff;
633
634        red[1] = 0.5 * (1 + sin(d[3] * (frame + 2000))) * 0xfff;
635        green[1] = 0.5 * (1 + cos(d[4] * frame + 5.0)) * 0xfff;
636        blue[1] = 0.5 * (1 + cos(d[5] * (frame + 4000))) * 0xfff;
637
638        cucul_set_dither_palette(dither, red, green, blue, alpha);
639
640        /* Draw circles */
641        x = cos(d[0] * (frame + 1000)) * 128.0 + (XSIZ / 2);
642        y = sin(0.11 * frame) * 128.0 + (YSIZ / 2);
643        put_disc(screen, x, y);
644
645        x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2);
646        y = sin(d[1] * (frame + 2000)) * 64.0 + (YSIZ / 2);
647        put_disc(screen, x, y);
648        break;
649
650    case RENDER:
651        cucul_dither_bitmap(cv, 0, 0,
652                            cucul_get_canvas_width(cv),
653                            cucul_get_canvas_height(cv),
654                            dither, screen);
655        break;
656
657    case FREE:
658        free(screen);
659        cucul_free_dither(dither);
660        break;
661    }
662}
663
664static void put_disc(uint8_t *screen, int x, int y)
665{
666    char *src = ((char*)disc) + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ;
667    int i, j;
668
669    for(j = 0; j < YSIZ; j++)
670        for(i = 0; i < XSIZ; i++)
671    {
672        screen[i + XSIZ * j] ^= src[i + DISCSIZ * j];
673    }
674}
675
676static void draw_line(int x, int y, char color)
677{
678    if(x == 0 || y == 0 || y > DISCSIZ / 2)
679        return;
680
681    if(x > DISCSIZ / 2)
682        x = DISCSIZ / 2;
683
684    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y),
685           color, 2 * x - 1);
686    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1),
687           color, 2 * x - 1);
688}
689
690/* Langton ant effect */
691#define ANTS 15
692#define ITER 2
693
694void langton(enum action action, cucul_canvas_t *cv)
695{
696    static char gradient[] =
697    {
698        ' ', ' ', '.', '.', ':', ':', 'x', 'x',
699        'X', 'X', '&', '&', 'W', 'W', '@', '@',
700    };
701    static int steps[][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
702    static uint8_t *screen;
703    static int width, height;
704    static int ax[ANTS], ay[ANTS], dir[ANTS];
705
706    int i, a, x, y;
707
708    switch(action)
709    {
710    case PREPARE:
711        width = cucul_get_canvas_width(cv);
712        height = cucul_get_canvas_height(cv);
713        for(i = 0; i < ANTS; i++)
714        {
715            ax[i] = cucul_rand(0, width);
716            ay[i] = cucul_rand(0, height);
717            dir[i] = cucul_rand(0, 4);
718        }
719        break;
720
721    case INIT:
722        screen = malloc(width * height);
723        memset(screen, 0, width * height);
724        break;
725
726    case UPDATE:
727        for(i = 0; i < ITER; i++)
728        {
729            for(x = 0; x < width * height; x++)
730            {
731                uint8_t p = screen[x];
732                if((p & 0x0f) > 1)
733                    screen[x] = p - 1;
734            }
735
736            for(a = 0; a < ANTS; a++)
737            {
738                uint8_t p = screen[ax[a] + width * ay[a]];
739
740                if(p & 0x0f)
741                {
742                    dir[a] = (dir[a] + 1) % 4;
743                    screen[ax[a] + width * ay[a]] = a << 4;
744                }
745                else
746                {
747                    dir[a] = (dir[a] + 3) % 4;
748                    screen[ax[a] + width * ay[a]] = (a << 4) | 0x0f;
749                }
750                ax[a] = (width + ax[a] + steps[dir[a]][0]) % width;
751                ay[a] = (height + ay[a] + steps[dir[a]][1]) % height;
752            }
753        }
754        break;
755
756    case RENDER:
757        for(y = 0; y < height; y++)
758        {
759            for(x = 0; x < width; x++)
760            {
761                uint8_t p = screen[x + width * y];
762
763                if(p & 0x0f)
764                    cucul_set_color_ansi(cv, CUCUL_WHITE, p >> 4);
765                else
766                    cucul_set_color_ansi(cv, CUCUL_BLACK, CUCUL_BLACK);
767                cucul_put_char(cv, x, y, gradient[p & 0x0f]);
768            }
769        }
770        break;
771
772    case FREE:
773        free(screen);
774        break;
775    }
776}
777
778/* Matrix effect */
779#define MAXDROPS 500
780#define MINLEN 15
781#define MAXLEN 30
782
783void matrix(enum action action, cucul_canvas_t *cv)
784{
785    static struct drop
786    {
787        int x, y, speed, len;
788        char str[MAXLEN];
789    }
790    drop[MAXDROPS];
791
792    int w, h, i, j;
793
794    switch(action)
795    {
796    case PREPARE:
797        for(i = 0; i < MAXDROPS; i++)
798        {
799            drop[i].x = cucul_rand(0, 1000);
800            drop[i].y = cucul_rand(0, 1000);
801            drop[i].speed = 5 + cucul_rand(0, 30);
802            drop[i].len = MINLEN + cucul_rand(0, (MAXLEN - MINLEN));
803            for(j = 0; j < MAXLEN; j++)
804                drop[i].str[j] = cucul_rand('0', 'z');
805        }
806        break;
807
808    case INIT:
809        break;
810
811    case UPDATE:
812        w = cucul_get_canvas_width(cv);
813        h = cucul_get_canvas_height(cv);
814
815        for(i = 0; i < MAXDROPS && i < (w * h / 32); i++)
816        {
817            drop[i].y += drop[i].speed;
818            if(drop[i].y > 1000)
819            {
820                drop[i].y -= 1000;
821                drop[i].x = cucul_rand(0, 1000);
822            }
823        }
824        break;
825
826    case RENDER:
827        w = cucul_get_canvas_width(cv);
828        h = cucul_get_canvas_height(cv);
829
830        cucul_set_color_ansi(cv, CUCUL_BLACK, CUCUL_BLACK);
831        cucul_clear_canvas(cv);
832
833        for(i = 0; i < MAXDROPS && i < (w * h / 32); i++)
834        {
835            int x, y;
836
837            x = drop[i].x * w / 1000 / 2 * 2;
838            y = drop[i].y * (h + MAXLEN) / 1000;
839
840            for(j = 0; j < drop[i].len; j++)
841            {
842                unsigned int fg;
843
844                if(j < 2)
845                    fg = CUCUL_WHITE;
846                else if(j < drop[i].len / 4)
847                    fg = CUCUL_LIGHTGREEN;
848                else if(j < drop[i].len * 4 / 5)
849                    fg = CUCUL_GREEN;
850                else
851                    fg = CUCUL_DARKGRAY;
852                cucul_set_color_ansi(cv, fg, CUCUL_BLACK);
853
854                cucul_put_char(cv, x, y - j,
855                               drop[i].str[(y - j) % drop[i].len]);
856            }
857        }
858        break;
859
860    case FREE:
861        break;
862    }
863}
864
865/* Rotozoom effect */
866#define TEXTURE_SIZE 256
867#define TABLE_SIZE 65536
868
869/* 24:8 Fixed point stuff */
870#define PRECISION 8
871
872#define FMUL(a, b) (((a)*(b))>>PRECISION)
873#define TOFIX(d)   ((int)( (d)*(double)(1<<PRECISION) ))
874#define TOINT(a)   (a>>PRECISION);
875
876#include "texture.h"
877
878void rotozoom(enum action action, cucul_canvas_t *canvas)
879{
880    static uint32_t screen[XSIZ * YSIZ];
881    static int cos_tab[TABLE_SIZE], sin_tab[TABLE_SIZE];
882    static int y_tab[TEXTURE_SIZE];
883
884    static cucul_dither_t *dither;
885    static uint32_t *texture;
886    uint32_t *p;
887    static int alphaF, tF;
888    int scaleF;
889
890    /* register is quite a bad idea on CISC, but not on RISC */
891    register unsigned int x, y;
892    register unsigned int xxF, yyF, uF, vF, uF_, vF_;
893    register unsigned int vu, vv;
894
895    switch(action)
896    {
897    case PREPARE:
898        for(x = 0; x < TABLE_SIZE; x++)
899        {
900            cos_tab[x] = TOFIX(cos(x * (360.0f / (float)TABLE_SIZE)));
901            sin_tab[x] = TOFIX(sin(x * (360.0f / (float)TABLE_SIZE)));
902        }
903        for(x = 0; x < TEXTURE_SIZE; x++)
904            y_tab[x] = x * TEXTURE_SIZE; /* start of lines offsets */
905        /* FIXME: this may be an invalid cast */
906        texture = (uint32_t *)textureByte;
907        break;
908
909    case INIT:
910        dither = cucul_create_dither(32, XSIZ, YSIZ, XSIZ * 4,
911                                     0x00FF0000,
912                                     0x0000FF00,
913                                     0x000000FF,
914                                     0x00000000);
915        break;
916
917    case UPDATE:
918        alphaF += 4;
919        tF     += 3;
920        scaleF = FMUL(sin_tab[tF & 0xFFFF], TOFIX(3)) + (TOFIX(4));
921        xxF    = FMUL(cos_tab[(alphaF) & 0xFFFF], scaleF);
922        yyF    = FMUL(sin_tab[(alphaF) & 0xFFFF], scaleF);
923        uF  = vF  = 0;
924        uF_ = vF_ = 0;
925        p = screen;
926
927        for(y = YSIZ; y--;)
928        {
929            for(x = XSIZ; x--;)
930            {
931                uF += xxF;
932                vF += yyF;
933
934                vu = TOINT(uF);
935                vv = TOINT(vF);
936                vu &= 0xFF;       /* ARM doesn't like    */
937                vv &= 0xFF;       /* chars as local vars */
938
939                *p++ = texture[vu + y_tab[vv]];
940            }
941
942            uF = uF_ -= yyF;
943            vF = vF_ += xxF;
944        }
945        break;
946
947    case RENDER:
948        cucul_dither_bitmap(canvas, 0, 0,
949                            cucul_get_canvas_width(canvas),
950                            cucul_get_canvas_height(canvas),
951                            dither, screen);
952        break;
953
954    case FREE:
955        cucul_free_dither(dither);
956        break;
957    }
958}
959
Note: See TracBrowser for help on using the repository browser.