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

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