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

Last change on this file since 905 was 859, checked in by Sam Hocevar, 15 years ago
  • Removed duplicate uint*_t defines from *_internal.h and included common.h in all .c files that needed it.
  • Property svn:keywords set to Id
File size: 5.0 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 859 2006-04-24 20:35:59Z 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#include "common.h"
17
18#if !defined(__KERNEL__)
19#   include <stdio.h>
20#   include <math.h>
21#   ifndef M_PI
22#       define M_PI 3.14159265358979323846
23#   endif
24#endif
25
26#include "cucul.h"
27#include "caca.h"
28
29/* Virtual buffer size */
30#define XSIZ 256
31#define YSIZ 256
32
33#define TABLEX (XSIZ * 2)
34#define TABLEY (YSIZ * 2)
35
36static unsigned char screen[XSIZ * YSIZ];
37static unsigned char table[TABLEX * TABLEY];
38
39static void do_plasma(unsigned char *,
40                      double, double, double, double, double, double);
41
42int main (int argc, char **argv)
43{
44    cucul_canvas_t *cv, *c2, *mask; caca_display_t *dp;
45    unsigned int red[256], green[256], blue[256], alpha[256];
46    double r[3], R[6];
47    cucul_dither_t *dither;
48    int i, x, y, frame = 0, pause = 0;
49
50    cv = cucul_create_canvas(0, 0);
51    if(!cv)
52        return 1;
53    dp = caca_create_display(cv);
54    if(!dp)
55        return 1;
56
57    caca_set_delay(dp, 20000);
58
59    c2 = cucul_create_canvas(cucul_get_canvas_width(cv),
60                             cucul_get_canvas_height(cv));
61    mask = cucul_create_canvas(cucul_get_canvas_width(cv),
62                               cucul_get_canvas_height(cv));
63
64    /* Fill various tables */
65    for(i = 0 ; i < 256; i++)
66        red[i] = green[i] = blue[i] = alpha[i] = 0;
67
68    for(i = 0; i < 3; i++)
69        r[i] = (double)(cucul_rand(1, 1000)) / 60000 * M_PI;
70
71    for(i = 0; i < 6; i++)
72        R[i] = (double)(cucul_rand(1, 1000)) / 10000;
73
74    for(y = 0 ; y < TABLEY ; y++)
75        for(x = 0 ; x < TABLEX ; x++)
76    {
77        double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2))
78                              + (y - (TABLEX / 2)) * (y - (TABLEX / 2))))
79                      * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY)));
80
81        table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6;
82    }
83
84    /* Create a libcucul dither */
85    dither = cucul_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
86
87    /* Main loop */
88    for(;;)
89    {
90        caca_event_t ev;
91        if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0))
92        {
93            switch(ev.data.key.ch)
94            {
95                case CACA_KEY_ESCAPE: goto end;
96                case ' ': pause = !pause;
97            }
98        }
99
100        if(pause)
101            goto paused;
102
103        for(i = 0 ; i < 256; i++)
104        {
105            double z = ((double)i) / 256 * 6 * M_PI;
106
107            red[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
108            blue[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff;
109            green[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff;
110        }
111
112        /* Set the palette */
113        cucul_set_dither_palette(dither, red, green, blue, alpha);
114
115        do_plasma(screen,
116                  (1.0 + sin(((double)frame) * R[0])) / 2,
117                  (1.0 + sin(((double)frame) * R[1])) / 2,
118                  (1.0 + sin(((double)frame) * R[2])) / 2,
119                  (1.0 + sin(((double)frame) * R[3])) / 2,
120                  (1.0 + sin(((double)frame) * R[4])) / 2,
121                  (1.0 + sin(((double)frame) * R[5])) / 2);
122        frame++;
123
124paused:
125        cucul_dither_bitmap(cv, 0, 0,
126                            cucul_get_canvas_width(cv),
127                            cucul_get_canvas_height(cv),
128                            dither, screen);
129
130        cucul_blit(c2, 0, 0, cv, NULL);
131        cucul_invert(c2);
132
133
134        cucul_blit(cv, 0, 0, c2, mask);
135
136        cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
137        cucul_putstr(cv, cucul_get_canvas_width(cv) - 30,
138                         cucul_get_canvas_height(cv) - 2,
139                         " -=[ Powered by libcaca ]=- ");
140        caca_refresh_display(dp);
141    }
142
143end:
144    cucul_free_dither(dither);
145    caca_free_display(dp);
146    cucul_free_canvas(cv);
147
148    return 0;
149}
150
151static void do_plasma(unsigned char *pixels, double x_1, double y_1,
152                      double x_2, double y_2, double x_3, double y_3)
153{
154    unsigned int X1 = x_1 * (TABLEX / 2),
155                 Y1 = y_1 * (TABLEY / 2),
156                 X2 = x_2 * (TABLEX / 2),
157                 Y2 = y_2 * (TABLEY / 2),
158                 X3 = x_3 * (TABLEX / 2),
159                 Y3 = y_3 * (TABLEY / 2);
160    unsigned int y;
161    unsigned char * t1 = table + X1 + Y1 * TABLEX,
162                  * t2 = table + X2 + Y2 * TABLEX,
163                  * t3 = table + X3 + Y3 * TABLEX;
164
165    for(y = 0; y < YSIZ; y++)
166    {
167        unsigned int x;
168        unsigned char * tmp = pixels + y * YSIZ;
169        unsigned int ty = y * TABLEX, tmax = ty + XSIZ;
170        for(x = 0; ty < tmax; ty++, tmp++)
171            tmp[0] = t1[ty] + t2[ty] + t3[ty];
172    }
173}
174
Note: See TracBrowser for help on using the repository browser.