source: libcaca/trunk/examples/cacaplas.c @ 333

Last change on this file since 333 was 333, checked in by Sam Hocevar, 19 years ago
  • configure.ac: + Check for -lm.
  • examples/cacaplas.c: + New demo, cacaplas, that displays a colour plasma.
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1/*
2 *  cacaplas      plasma effect for libcaca
3 *  Copyright (c) 2004 Sam Hocevar <sam@zoy.org>
4 *                1998 Bini Michele <mibin@tin.it>
5 *                All Rights Reserved
6 *
7 *  $Id: cacaplas.c 333 2004-01-10 19:53:14Z sam $
8 *
9 *  This program is free software; you can redistribute it and/or
10 *  modify it under the terms of the GNU Lesser General Public
11 *  License as published by the Free Software Foundation; either
12 *  version 2 of the License, or (at your option) any later version.
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 *  Lesser General Public License for more details.
18 *
19 *  You should have received a copy of the GNU Lesser General Public
20 *  License along with this program; if not, write to the Free Software
21 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 *  02111-1307  USA
23 */
24
25#include "config.h"
26
27#include <math.h>
28
29#ifndef M_PI
30#   define M_PI 3.14159265358979323846
31#endif
32
33#include "caca.h"
34
35/* Virtual buffer size */
36#define XSIZ 256
37#define YSIZ 256
38
39#define TABLEX (XSIZ * 2)
40#define TABLEY (YSIZ * 2)
41
42static unsigned char screen[XSIZ * YSIZ];
43static unsigned char table[TABLEX * TABLEY];
44
45static void do_plasma(unsigned char *,
46                      double, double, double, double, double, double);
47
48int main (int argc, char **argv)
49{
50    int red[256], green[256], blue[256], alpha[256];
51    double r[3], R[6];
52    struct caca_bitmap *bitmap;
53    int i, x, y, frame;
54
55    if(caca_init() < 0)
56        return 1;
57
58    caca_set_delay(10000);
59
60    /* Fill various tables */
61    for(i = 0 ; i < 256; i++)
62        red[i] = green[i] = blue[i] = alpha[i] = 0;
63
64    for(i = 0; i < 3; i++)
65        r[i] = (double)(caca_rand(1, 1000)) / 30000 * M_PI;
66
67    for(i = 0; i < 6; i++)
68        R[i] = (double)(caca_rand(1, 1000)) / 5000;
69
70    for(y = 0 ; y < TABLEY ; y++)
71        for(x = 0 ; x < TABLEX ; x++)
72    {
73        double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2))
74                              + (y - (TABLEX / 2)) * (y - (TABLEX / 2))))
75                      * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY)));
76
77        table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6;
78    }
79
80    /* Create a libcaca bitmap */
81    bitmap = caca_create_bitmap(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
82
83    /* Main loop */
84    for(frame = 0; !caca_get_event(CACA_EVENT_KEY_PRESS); frame++)
85    {
86        for(i = 0 ; i < 256; i++)
87        {
88            double z = ((double)i) / 256 * 6 * M_PI;
89
90            red[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff;
91            green[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
92            blue[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff;
93        }
94
95        /* Set the palette */
96        caca_set_bitmap_palette(bitmap, red, green, blue, alpha);
97
98        do_plasma(screen,
99                  (1.0 + sin(((double)frame) * R[0])) / 2,
100                  (1.0 + sin(((double)frame) * R[1])) / 2,
101                  (1.0 + sin(((double)frame) * R[2])) / 2,
102                  (1.0 + sin(((double)frame) * R[3])) / 2,
103                  (1.0 + sin(((double)frame) * R[4])) / 2,
104                  (1.0 + sin(((double)frame) * R[5])) / 2);
105
106        caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
107                         bitmap, screen);
108        caca_refresh();
109    }
110
111    caca_free_bitmap(bitmap);
112    caca_end();
113
114    return 0;
115}
116
117static void do_plasma(unsigned char *pixels, double x_1, double y_1,
118                      double x_2, double y_2, double x_3, double y_3)
119{
120    unsigned int X1 = x_1 * (TABLEX / 2),
121                 Y1 = y_1 * (TABLEY / 2),
122                 X2 = x_2 * (TABLEX / 2),
123                 Y2 = y_2 * (TABLEY / 2),
124                 X3 = x_3 * (TABLEX / 2),
125                 Y3 = y_3 * (TABLEY / 2);
126    unsigned int y;
127    unsigned char * t1 = table + X1 + Y1 * TABLEX,
128                  * t2 = table + X2 + Y2 * TABLEX,
129                  * t3 = table + X3 + Y3 * TABLEX;
130
131    for(y = 0; y < YSIZ; y++)
132    {
133        unsigned int x;
134        unsigned char * tmp = pixels + y * YSIZ;
135        unsigned int ty = y * TABLEX, tmax = ty + XSIZ;
136        for(x = 0; ty < tmax; ty++, tmp++)
137            tmp[0] = t1[ty] + t2[ty] + t3[ty];
138    }
139}
140
Note: See TracBrowser for help on using the repository browser.