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

Last change on this file since 710 was 710, checked in by Sam Hocevar, 14 years ago
  • Merged caca_wait_event() into caca_get_event() and added a timeout parameter to do what both functions did before, and even more.
  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1/*
2 *  cacaplas      plasma effect for libcaca
3 *  Copyright (c) 2004 Sam Hocevar <sam@zoy.org>
4 *                1998 Michele Bini <mibin@tin.it>
5 *                All Rights Reserved
6 *
7 *  $Id: cacaplas.c 710 2006-04-01 14:44:09Z 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_t *qq, *qq2, *mask; caca_t *kk;
44    unsigned int red[256], green[256], blue[256], alpha[256];
45    double r[3], R[6];
46    struct cucul_bitmap *bitmap;
47    int i, x, y, frame = 0, pause = 0;
48
49    qq = cucul_create(0, 0);
50    if(!qq)
51        return 1;
52    kk = caca_attach(qq);
53    if(!kk)
54        return 1;
55
56    caca_set_delay(kk, 20000);
57
58    qq2 = cucul_create(cucul_get_width(qq), cucul_get_height(qq));
59    mask = cucul_create(cucul_get_width(qq), cucul_get_height(qq));
60
61    /* Fill various tables */
62    for(i = 0 ; i < 256; i++)
63        red[i] = green[i] = blue[i] = alpha[i] = 0;
64
65    for(i = 0; i < 3; i++)
66        r[i] = (double)(cucul_rand(1, 1000)) / 60000 * M_PI;
67
68    for(i = 0; i < 6; i++)
69        R[i] = (double)(cucul_rand(1, 1000)) / 10000;
70
71    for(y = 0 ; y < TABLEY ; y++)
72        for(x = 0 ; x < TABLEX ; x++)
73    {
74        double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2))
75                              + (y - (TABLEX / 2)) * (y - (TABLEX / 2))))
76                      * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY)));
77
78        table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6;
79    }
80
81    /* Create a libcucul bitmap */
82    bitmap = cucul_create_bitmap(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
83
84    /* Main loop */
85    for(;;)
86    {
87        struct caca_event ev;
88        if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0))
89        {
90            switch(ev.data.key.c)
91            {
92                case CACA_KEY_ESCAPE: goto end;
93                case ' ': pause = !pause;
94            }
95        }
96
97        if(pause)
98            goto paused;
99
100        for(i = 0 ; i < 256; i++)
101        {
102            double z = ((double)i) / 256 * 6 * M_PI;
103
104            red[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
105            blue[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff;
106            green[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff;
107        }
108
109        /* Set the palette */
110        cucul_set_bitmap_palette(bitmap, red, green, blue, alpha);
111
112        do_plasma(screen,
113                  (1.0 + sin(((double)frame) * R[0])) / 2,
114                  (1.0 + sin(((double)frame) * R[1])) / 2,
115                  (1.0 + sin(((double)frame) * R[2])) / 2,
116                  (1.0 + sin(((double)frame) * R[3])) / 2,
117                  (1.0 + sin(((double)frame) * R[4])) / 2,
118                  (1.0 + sin(((double)frame) * R[5])) / 2);
119        frame++;
120
121paused:
122        cucul_draw_bitmap(qq, 0, 0,
123                          cucul_get_width(qq) - 1, cucul_get_height(qq) - 1,
124                          bitmap, screen);
125
126        cucul_blit(qq2, 0, 0, qq, NULL);
127        cucul_invert(qq2);
128
129        cucul_clear(mask);
130        cucul_set_color(mask, CUCUL_COLOR_WHITE, CUCUL_COLOR_WHITE);
131        cucul_fill_ellipse(mask, (1.0 + 0.7 * sin(0.05 * (float)frame))
132                                   * 0.5 * cucul_get_width(mask),
133                                 (1.0 + 0.7 * cos(0.05 * (float)frame))
134                                   * 0.5 * cucul_get_height(mask),
135                                 cucul_get_width(mask) / 3,
136                                 cucul_get_height(mask) / 3, "#");
137
138        cucul_blit(qq, 0, 0, qq2, mask);
139
140        cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
141        cucul_putstr(qq, cucul_get_width(qq) - 30, cucul_get_height(qq) - 2,
142                     " -=[ Powered by libcaca ]=- ");
143        caca_display(kk);
144    }
145
146end:
147    cucul_free_bitmap(bitmap);
148    caca_detach(kk);
149    cucul_free(qq);
150
151    return 0;
152}
153
154static void do_plasma(unsigned char *pixels, double x_1, double y_1,
155                      double x_2, double y_2, double x_3, double y_3)
156{
157    unsigned int X1 = x_1 * (TABLEX / 2),
158                 Y1 = y_1 * (TABLEY / 2),
159                 X2 = x_2 * (TABLEX / 2),
160                 Y2 = y_2 * (TABLEY / 2),
161                 X3 = x_3 * (TABLEX / 2),
162                 Y3 = y_3 * (TABLEY / 2);
163    unsigned int y;
164    unsigned char * t1 = table + X1 + Y1 * TABLEX,
165                  * t2 = table + X2 + Y2 * TABLEX,
166                  * t3 = table + X3 + Y3 * TABLEX;
167
168    for(y = 0; y < YSIZ; y++)
169    {
170        unsigned int x;
171        unsigned char * tmp = pixels + y * YSIZ;
172        unsigned int ty = y * TABLEX, tmax = ty + XSIZ;
173        for(x = 0; ty < tmax; ty++, tmp++)
174            tmp[0] = t1[ty] + t2[ty] + t3[ty];
175    }
176}
177
Note: See TracBrowser for help on using the repository browser.