source: libcaca/trunk/src/cacaplas.c @ 816

Last change on this file since 816 was 816, checked in by Sam Hocevar, 15 years ago
  • Changed the cucul_dither_bitmap behaviour so that the 4 coordinates are now x/y/width/height instead of x1/y1/x2/y2. Avoids unnecessary +1/-1 computations.
  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1/*
2 *  cacaplas      plasma effect for libcaca
3 *  Copyright (c) 1998 Michele Bini <mibin@tin.it>
4 *                2004 Sam Hocevar <sam@zoy.org>
5 *                All Rights Reserved
6 *
7 *  $Id: cacaplas.c 816 2006-04-18 16:50:56Z sam $
8 *
9 *  This program is free software; you can redistribute it and/or
10 *  modify it under the terms of the Do What The Fuck You Want To
11 *  Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15#include "config.h"
16
17#if !defined(__KERNEL__)
18#   include <stdio.h>
19#   include <math.h>
20#   ifndef M_PI
21#       define M_PI 3.14159265358979323846
22#   endif
23#endif
24
25#include "cucul.h"
26#include "caca.h"
27
28/* Virtual buffer size */
29#define XSIZ 256
30#define YSIZ 256
31
32#define TABLEX (XSIZ * 2)
33#define TABLEY (YSIZ * 2)
34
35static unsigned char screen[XSIZ * YSIZ];
36static unsigned char table[TABLEX * TABLEY];
37
38static void do_plasma(unsigned char *,
39                      double, double, double, double, double, double);
40
41int main (int argc, char **argv)
42{
43    cucul_canvas_t *cv, *c2, *mask; caca_display_t *dp;
44    unsigned int red[256], green[256], blue[256], alpha[256];
45    double r[3], R[6];
46    cucul_dither_t *dither;
47    int i, x, y, frame = 0, pause = 0;
48
49    cv = cucul_create_canvas(0, 0);
50    if(!cv)
51        return 1;
52    dp = caca_attach(cv);
53    if(!dp)
54        return 1;
55
56    caca_set_delay(dp, 20000);
57
58    c2 = cucul_create_canvas(cucul_get_canvas_width(cv),
59                             cucul_get_canvas_height(cv));
60    mask = cucul_create_canvas(cucul_get_canvas_width(cv),
61                               cucul_get_canvas_height(cv));
62
63    /* Fill various tables */
64    for(i = 0 ; i < 256; i++)
65        red[i] = green[i] = blue[i] = alpha[i] = 0;
66
67    for(i = 0; i < 3; i++)
68        r[i] = (double)(cucul_rand(1, 1000)) / 60000 * M_PI;
69
70    for(i = 0; i < 6; i++)
71        R[i] = (double)(cucul_rand(1, 1000)) / 10000;
72
73    for(y = 0 ; y < TABLEY ; y++)
74        for(x = 0 ; x < TABLEX ; x++)
75    {
76        double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2))
77                              + (y - (TABLEX / 2)) * (y - (TABLEX / 2))))
78                      * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY)));
79
80        table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6;
81    }
82
83    /* Create a libcucul dither */
84    dither = cucul_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
85
86    /* Main loop */
87    for(;;)
88    {
89        caca_event_t ev;
90        if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0))
91        {
92            switch(ev.data.key.ch)
93            {
94                case CACA_KEY_ESCAPE: goto end;
95                case ' ': pause = !pause;
96            }
97        }
98
99        if(pause)
100            goto paused;
101
102        for(i = 0 ; i < 256; i++)
103        {
104            double z = ((double)i) / 256 * 6 * M_PI;
105
106            red[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
107            blue[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff;
108            green[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff;
109        }
110
111        /* Set the palette */
112        cucul_set_dither_palette(dither, red, green, blue, alpha);
113
114        do_plasma(screen,
115                  (1.0 + sin(((double)frame) * R[0])) / 2,
116                  (1.0 + sin(((double)frame) * R[1])) / 2,
117                  (1.0 + sin(((double)frame) * R[2])) / 2,
118                  (1.0 + sin(((double)frame) * R[3])) / 2,
119                  (1.0 + sin(((double)frame) * R[4])) / 2,
120                  (1.0 + sin(((double)frame) * R[5])) / 2);
121        frame++;
122
123paused:
124        cucul_dither_bitmap(cv, 0, 0,
125                            cucul_get_canvas_width(cv),
126                            cucul_get_canvas_height(cv),
127                            dither, screen);
128
129        cucul_blit(c2, 0, 0, cv, NULL);
130        cucul_invert(c2);
131
132
133        cucul_blit(cv, 0, 0, c2, mask);
134
135        cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
136        cucul_putstr(cv, cucul_get_canvas_width(cv) - 30,
137                         cucul_get_canvas_height(cv) - 2,
138                         " -=[ Powered by libcaca ]=- ");
139        caca_display(dp);
140    }
141
142end:
143    cucul_free_dither(dither);
144    caca_detach(dp);
145    cucul_free_canvas(cv);
146
147    return 0;
148}
149
150static void do_plasma(unsigned char *pixels, double x_1, double y_1,
151                      double x_2, double y_2, double x_3, double y_3)
152{
153    unsigned int X1 = x_1 * (TABLEX / 2),
154                 Y1 = y_1 * (TABLEY / 2),
155                 X2 = x_2 * (TABLEX / 2),
156                 Y2 = y_2 * (TABLEY / 2),
157                 X3 = x_3 * (TABLEX / 2),
158                 Y3 = y_3 * (TABLEY / 2);
159    unsigned int y;
160    unsigned char * t1 = table + X1 + Y1 * TABLEX,
161                  * t2 = table + X2 + Y2 * TABLEX,
162                  * t3 = table + X3 + Y3 * TABLEX;
163
164    for(y = 0; y < YSIZ; y++)
165    {
166        unsigned int x;
167        unsigned char * tmp = pixels + y * YSIZ;
168        unsigned int ty = y * TABLEX, tmax = ty + XSIZ;
169        for(x = 0; ty < tmax; ty++, tmp++)
170            tmp[0] = t1[ty] + t2[ty] + t3[ty];
171    }
172}
173
Note: See TracBrowser for help on using the repository browser.