source: libcaca/trunk/src/cacaball.c @ 581

Last change on this file since 581 was 581, checked in by Sam Hocevar, 15 years ago
  • Made cacamoir, cacaball and cacaplas buildable into a kernel.
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1/*
2 *  cacaball      metaballs effect for libcaca
3 *  Copyright (c) 2003-2004 Jean-Yves Lamoureux <jylam@lnxscene.org>
4 *                All Rights Reserved
5 *
6 *  $Id: cacaball.c 581 2006-03-10 09:48:33Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14#include "config.h"
15
16#if !defined(__KERNEL__)
17#   include <math.h>
18#   ifndef M_PI
19#       define M_PI 3.14159265358979323846
20#   endif
21#endif
22
23#include "cucul.h"
24#include "caca.h"
25
26/* Virtual buffer size */
27#define XSIZ 256
28#define YSIZ 256
29
30#define METASIZE 100
31#define METABALLS 16
32
33/* Colour index where to crop balls */
34#define CROPBALL 160
35
36static void create_ball(void);
37static void draw_ball(unsigned int, unsigned int);
38
39static unsigned char pixels[XSIZ * YSIZ];
40static unsigned char metaball[METASIZE * METASIZE];
41
42int main(int argc, char **argv)
43{
44    cucul_t *qq; caca_t *kk;
45    unsigned int r[256], g[256], b[256], a[256];
46    float d[METABALLS], di[METABALLS], dj[METABALLS], dk[METABALLS];
47    unsigned int x[METABALLS], y[METABALLS];
48    struct cucul_bitmap *cucul_bitmap;
49    float i = 10.0, j = 17.0, k = 11.0;
50    int p, frame = 0, pause = 0;
51
52    qq = cucul_init();
53    if(!qq)
54        return 1;
55    kk = caca_attach(qq);
56    if(!kk)
57        return 1;
58
59    caca_set_delay(kk, 20000);
60
61    /* Make the palette eatable by libcaca */
62    for(p = 0; p < 256; p++)
63        r[p] = g[p] = b[p] = a[p] = 0x0;
64    r[255] = g[255] = b[255] = 0xfff;
65
66    /* Create a libcucul bitmap smaller than our pixel buffer, so that we
67     * display only the interesting part of it */
68    cucul_bitmap = cucul_create_bitmap(qq, 8, XSIZ - METASIZE, YSIZ - METASIZE,
69                                       XSIZ, 0, 0, 0, 0);
70
71    /* Generate ball sprite */
72    create_ball();
73
74    for(p = 0; p < METABALLS; p++)
75    {
76        d[p] = cucul_rand(0, 100);
77        di[p] = (float)cucul_rand(500, 4000) / 6000.0;
78        dj[p] = (float)cucul_rand(500, 4000) / 6000.0;
79        dk[p] = (float)cucul_rand(500, 4000) / 6000.0;
80    }
81
82    /* Go ! */
83    for(;;)
84    {
85        switch(caca_get_event(kk, CACA_EVENT_KEY_PRESS))
86        {
87            case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
88            case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
89        }
90
91        if(pause)
92            goto paused;
93
94        frame++;
95
96        /* Crop the palette */
97        for(p = CROPBALL; p < 255; p++)
98        {
99            int t1, t2, t3;
100            t1 = p < 0x40 ? 0 : p < 0xc0 ? (p - 0x40) * 0x20 : 0xfff;
101            t2 = p < 0xe0 ? 0 : (p - 0xe0) * 0x80;
102            t3 = p < 0x40 ? p * 0x40 : 0xfff;
103
104            r[p] = (1.0 + sin((double)frame * M_PI / 60)) * t1 / 4
105                 + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t2 / 4
106                 + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t3 / 4;
107            g[p] = (1.0 + sin((double)frame * M_PI / 60)) * t2 / 4
108                 + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t3 / 4
109                 + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t1 / 4;
110            b[p] = (1.0 + sin((double)frame * M_PI / 60)) * t3 / 4
111                 + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t1 / 4
112                 + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t2 / 4;
113        }
114
115        /* Set the palette */
116        cucul_set_bitmap_palette(qq, cucul_bitmap, r, g, b, a);
117
118        /* Silly paths for our balls */
119        for(p = 0; p < METABALLS; p++)
120        {
121            float u = di[p] * i + dj[p] * j + dk[p] * sin(di[p] * k);
122            float v = d[p] + di[p] * j + dj[p] * k + dk[p] * sin(dk[p] * i);
123            u = sin(i + u * 2.1) * (1.0 + sin(u));
124            v = sin(j + v * 1.9) * (1.0 + sin(v));
125            x[p] = (XSIZ - METASIZE) / 2 + u * (XSIZ - METASIZE) / 4;
126            y[p] = (YSIZ - METASIZE) / 2 + v * (YSIZ - METASIZE) / 4;
127        }
128
129        i += 0.011;
130        j += 0.017;
131        k += 0.019;
132
133        memset(pixels, 0, XSIZ * YSIZ);
134
135        /* Here is all the trick. Maybe if you're that
136         * clever you'll understand. */
137        for(p = 0; p < METABALLS; p++)
138            draw_ball(x[p], y[p]);
139
140paused:
141        /* Draw our virtual buffer to screen, letting libcucul resize it */
142        cucul_draw_bitmap(qq, 0, 0,
143                          cucul_get_width(qq) - 1, cucul_get_height(qq) - 1,
144                          cucul_bitmap, pixels + (METASIZE / 2) * (1 + XSIZ));
145        caca_display(kk);
146    }
147
148    /* End, bye folks */
149end:
150    cucul_free_bitmap(qq, cucul_bitmap);
151    caca_detach(kk);
152    cucul_end(qq);
153
154    return 0;
155}
156
157/* Generate ball sprite
158 * You should read the comments, I already wrote that before ... */
159static void create_ball(void)
160{
161    int x, y;
162    float distance;
163
164    for(y = 0; y < METASIZE; y++)
165        for(x = 0; x < METASIZE; x++)
166    {
167        distance = ((METASIZE/2) - x) * ((METASIZE/2) - x)
168                 + ((METASIZE/2) - y) * ((METASIZE/2) - y);
169        distance = sqrt(distance) * 64 / METASIZE;
170        metaball[x + y * METASIZE] = distance > 15 ? 0 : (255 - distance) * 15;
171    }
172}
173
174/* You missed the trick ? */
175static void draw_ball(unsigned int bx, unsigned int by)
176{
177    unsigned int color;
178    unsigned int i, e = 0;
179    unsigned int b = (by * XSIZ) + bx;
180
181    for(i = 0; i < METASIZE * METASIZE; i++)
182    {
183        color = pixels[b] + metaball[i];
184
185        if(color > 255)
186            color = 255;
187
188        pixels[b] = color;
189        if(e == METASIZE)
190        {
191            e = 0;
192            b += XSIZ - METASIZE;
193        }
194        b++;
195        e++;
196    }
197}
198
Note: See TracBrowser for help on using the repository browser.