source: libcaca/trunk/examples/cacaball.c @ 377

Last change on this file since 377 was 377, checked in by Sam Hocevar, 18 years ago
  • examples/: + Use Space to pause demos, Esc to quit.
  • Property svn:keywords set to Id
File size: 5.8 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 377 2004-11-05 14:44:41Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the GNU Lesser General Public
10 *  License as published by the Free Software Foundation; either
11 *  version 2 of the License, or (at your option) any later version.
12 *
13 *  This program is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 *  Lesser General Public License for more details.
17 *
18 *  You should have received a copy of the GNU Lesser General Public
19 *  License along with this program; if not, write to the Free Software
20 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 *  02111-1307  USA
22 */
23
24#include "config.h"
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <math.h>
30
31#ifndef M_PI
32#   define M_PI 3.14159265358979323846
33#endif
34
35#include "caca.h"
36
37/* Virtual buffer size */
38#define XSIZ 256
39#define YSIZ 256
40
41#define METASIZE 100
42#define METABALLS 16
43
44/* Colour index where to crop balls */
45#define CROPBALL 160
46
47static void create_ball(void);
48static void draw_ball(unsigned int, unsigned int);
49
50static unsigned char pixels[XSIZ * YSIZ];
51static unsigned char metaball[METASIZE * METASIZE];
52
53int main(int argc, char **argv)
54{
55    int r[256], g[256], b[256], a[256];
56    float d[METABALLS], di[METABALLS], dj[METABALLS], dk[METABALLS];
57    unsigned int x[METABALLS], y[METABALLS];
58    struct caca_bitmap *caca_bitmap;
59    float i = 10.0, j = 17.0, k = 11.0;
60    int p, frame = 0, pause = 0;
61
62    if(caca_init())
63        return 1;
64
65    caca_set_delay(20000);
66
67    /* Make the palette eatable by libcaca */
68    for(p = 0; p < 256; p++)
69        r[p] = g[p] = b[p] = a[p] = 0x0;
70    r[255] = g[255] = b[255] = 0xfff;
71
72    /* Create a libcaca bitmap smaller than our pixel buffer, so that we
73     * display only the interesting part of it */
74    caca_bitmap = caca_create_bitmap(8, XSIZ - METASIZE, YSIZ - METASIZE,
75                                     XSIZ, 0, 0, 0, 0);
76
77    /* Generate ball sprite */
78    create_ball();
79
80    for(p = 0; p < METABALLS; p++)
81    {
82        d[p] = caca_rand(0, 100);
83        di[p] = (float)caca_rand(500, 4000) / 6000.0;
84        dj[p] = (float)caca_rand(500, 4000) / 6000.0;
85        dk[p] = (float)caca_rand(500, 4000) / 6000.0;
86    }
87
88    /* Go ! */
89    for(;;)
90    {
91        switch(caca_get_event(CACA_EVENT_KEY_PRESS))
92        {
93            case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
94            case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
95        }
96
97        if(pause)
98            goto paused;
99
100        frame++;
101
102        /* Crop the palette */
103        for(p = CROPBALL; p < 255; p++)
104        {
105            int t1, t2, t3;
106            t1 = p < 0x40 ? 0 : p < 0xc0 ? (p - 0x40) * 0x20 : 0xfff;
107            t2 = p < 0xe0 ? 0 : (p - 0xe0) * 0x80;
108            t3 = p < 0x40 ? p * 0x40 : 0xfff;
109
110            r[p] = (1.0 + sin((double)frame * M_PI / 60)) * t1 / 4
111                 + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t2 / 4
112                 + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t3 / 4;
113            g[p] = (1.0 + sin((double)frame * M_PI / 60)) * t2 / 4
114                 + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t3 / 4
115                 + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t1 / 4;
116            b[p] = (1.0 + sin((double)frame * M_PI / 60)) * t3 / 4
117                 + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t1 / 4
118                 + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t2 / 4;
119        }
120
121        /* Set the palette */
122        caca_set_bitmap_palette(caca_bitmap, r, g, b, a);
123
124        /* Silly paths for our balls */
125        for(p = 0; p < METABALLS; p++)
126        {
127            float u = di[p] * i + dj[p] * j + dk[p] * sin(di[p] * k);
128            float v = d[p] + di[p] * j + dj[p] * k + dk[p] * sin(dk[p] * i);
129            u = sin(i + u * 2.1) * (1.0 + sin(u));
130            v = sin(j + v * 1.9) * (1.0 + sin(v));
131            x[p] = (XSIZ - METASIZE) / 2 + u * (XSIZ - METASIZE) / 4;
132            y[p] = (YSIZ - METASIZE) / 2 + v * (YSIZ - METASIZE) / 4;
133        }
134
135        i += 0.011;
136        j += 0.017;
137        k += 0.019;
138
139        memset(pixels, 0, XSIZ * YSIZ);
140
141        /* Here is all the trick. Maybe if you're that
142         * clever you'll understand. */
143        for(p = 0; p < METABALLS; p++)
144            draw_ball(x[p], y[p]);
145
146paused:
147        /* Draw our virtual buffer to screen, letting libcaca resize it */
148        caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
149                         caca_bitmap, pixels + (METASIZE / 2) * (1 + XSIZ));
150        caca_refresh();
151    }
152
153    /* End, bye folks */
154end:
155    caca_free_bitmap(caca_bitmap);
156    caca_end();
157
158    return 0;
159}
160
161/* Generate ball sprite
162 * You should read the comments, I already wrote that before ... */
163static void create_ball(void)
164{
165    int x, y;
166    float distance;
167
168    for(y = 0; y < METASIZE; y++)
169        for(x = 0; x < METASIZE; x++)
170    {
171        distance = ((METASIZE/2) - x) * ((METASIZE/2) - x)
172                 + ((METASIZE/2) - y) * ((METASIZE/2) - y);
173        distance = sqrt(distance) * 64 / METASIZE;
174        metaball[x + y * METASIZE] = distance > 15 ? 0 : (255 - distance) * 15;
175    }
176}
177
178/* You missed the trick ? */
179static void draw_ball(unsigned int bx, unsigned int by)
180{
181    unsigned int color;
182    unsigned int i, e = 0;
183    unsigned int b = (by * XSIZ) + bx;
184
185    for(i = 0; i < METASIZE * METASIZE; i++)
186    {
187        color = pixels[b] + metaball[i];
188
189        if(color > 255)
190            color = 255;
191
192        pixels[b] = color;
193        if(e == METASIZE)
194        {
195            e = 0;
196            b += XSIZ - METASIZE;
197        }
198        b++;
199        e++;
200    }
201}
202
Note: See TracBrowser for help on using the repository browser.