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

Last change on this file since 716 was 716, checked in by Jean-Yves Lamoureux, 14 years ago
  • removed b0rked test. No more pigs, I just commited 500 of them.
  • Property svn:keywords set to Id
File size: 4.7 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 716 2006-04-01 16:11:37Z jylam $
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
130        cucul_blit(qq, 0, 0, qq2, mask);
131
132        cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
133        cucul_putstr(qq, cucul_get_width(qq) - 30, cucul_get_height(qq) - 2,
134                     " -=[ Powered by libcaca ]=- ");
135        caca_display(kk);
136    }
137
138end:
139    cucul_free_bitmap(bitmap);
140    caca_detach(kk);
141    cucul_free(qq);
142
143    return 0;
144}
145
146static void do_plasma(unsigned char *pixels, double x_1, double y_1,
147                      double x_2, double y_2, double x_3, double y_3)
148{
149    unsigned int X1 = x_1 * (TABLEX / 2),
150                 Y1 = y_1 * (TABLEY / 2),
151                 X2 = x_2 * (TABLEX / 2),
152                 Y2 = y_2 * (TABLEY / 2),
153                 X3 = x_3 * (TABLEX / 2),
154                 Y3 = y_3 * (TABLEY / 2);
155    unsigned int y;
156    unsigned char * t1 = table + X1 + Y1 * TABLEX,
157                  * t2 = table + X2 + Y2 * TABLEX,
158                  * t3 = table + X3 + Y3 * TABLEX;
159
160    for(y = 0; y < YSIZ; y++)
161    {
162        unsigned int x;
163        unsigned char * tmp = pixels + y * YSIZ;
164        unsigned int ty = y * TABLEX, tmax = ty + XSIZ;
165        for(x = 0; ty < tmax; ty++, tmp++)
166            tmp[0] = t1[ty] + t2[ty] + t3[ty];
167    }
168}
169
Note: See TracBrowser for help on using the repository browser.