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

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