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

Last change on this file since 339 was 339, checked in by Sam Hocevar, 18 years ago
  • examples/cacaballs.c: + Bigger metaballs, less metaballs. + Rotating palette.
  • examples/cacaplas.c: + Slightly decreased speed.
  • examples/cacacirc.c: + New effect.
  • 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 339 2004-01-12 14:25:46Z 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;
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    while(!caca_get_event(CACA_EVENT_KEY_PRESS))
90    {
91        frame++;
92
93        /* Crop the palette */
94        for(p = CROPBALL; p < 255; p++)
95        {
96            int t1, t2, t3;
97            t1 = p < 0x40 ? 0 : p < 0xc0 ? (p - 0x40) * 0x20 : 0xfff;
98            t2 = p < 0xe0 ? 0 : (p - 0xe0) * 0x80;
99            t3 = p < 0x40 ? p * 0x40 : 0xfff;
100
101            r[p] = (1.0 + sin((double)frame * M_PI / 60)) * t1 / 4
102                 + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t2 / 4
103                 + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t3 / 4;
104            g[p] = (1.0 + sin((double)frame * M_PI / 60)) * t2 / 4
105                 + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t3 / 4
106                 + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t1 / 4;
107            b[p] = (1.0 + sin((double)frame * M_PI / 60)) * t3 / 4
108                 + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t1 / 4
109                 + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t2 / 4;
110        }
111
112        /* Set the palette */
113        caca_set_bitmap_palette(caca_bitmap, r, g, b, a);
114
115        /* Silly paths for our balls */
116        for(p = 0; p < METABALLS; p++)
117        {
118            float u = di[p] * i + dj[p] * j + dk[p] * sin(di[p] * k);
119            float v = d[p] + di[p] * j + dj[p] * k + dk[p] * sin(dk[p] * i);
120            u = sin(i + u * 2.1) * (1.0 + sin(u));
121            v = sin(j + v * 1.9) * (1.0 + sin(v));
122            x[p] = (XSIZ - METASIZE) / 2 + u * (XSIZ - METASIZE) / 4;
123            y[p] = (YSIZ - METASIZE) / 2 + v * (YSIZ - METASIZE) / 4;
124        }
125
126        i += 0.011;
127        j += 0.017;
128        k += 0.019;
129
130        memset(pixels, 0, XSIZ * YSIZ);
131
132        /* Here is all the trick. Maybe if you're that
133         * clever you'll understand. */
134        for(p = 0; p < METABALLS; p++)
135            draw_ball(x[p], y[p]);
136
137        /* Draw our virtual buffer to screen, letting libcaca resize it */
138        caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
139                         caca_bitmap, pixels + (METASIZE / 2) * (1 + XSIZ));
140        caca_refresh();
141    }
142
143    /* End, bye folks */
144    caca_end();
145
146    return 0;
147}
148
149/* Generate ball sprite
150 * You should read the comments, I already wrote that before ... */
151static void create_ball(void)
152{
153    int x, y;
154    float distance;
155
156    for(y = 0; y < METASIZE; y++)
157        for(x = 0; x < METASIZE; x++)
158    {
159        distance = ((METASIZE/2) - x) * ((METASIZE/2) - x)
160                 + ((METASIZE/2) - y) * ((METASIZE/2) - y);
161        distance = sqrt(distance) * 64 / METASIZE;
162        metaball[x + y * METASIZE] = distance > 15 ? 0 : (255 - distance) * 15;
163    }
164}
165
166/* You missed the trick ? */
167static void draw_ball(unsigned int bx, unsigned int by)
168{
169    unsigned int color;
170    unsigned int i, e = 0;
171    unsigned int b = (by * XSIZ) + bx;
172
173    for(i = 0; i < METASIZE * METASIZE; i++)
174    {
175        color = pixels[b] + metaball[i];
176
177        if(color > 255)
178            color = 255;
179
180        pixels[b] = color;
181        if(e == METASIZE)
182        {
183            e = 0;
184            b += XSIZ - METASIZE;
185        }
186        b++;
187        e++;
188    }
189}
190
Note: See TracBrowser for help on using the repository browser.