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

Last change on this file since 655 was 655, checked in by Sam Hocevar, 14 years ago
  • Changed the cacaplas colours. Yeah, I do what I want.
  • Property svn:keywords set to Id
File size: 4.4 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 655 2006-03-21 10:52:24Z 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 <math.h>
19#   ifndef M_PI
20#       define M_PI 3.14159265358979323846
21#   endif
22#endif
23
24#include "cucul.h"
25#include "caca.h"
26
27/* Virtual buffer size */
28#define XSIZ 256
29#define YSIZ 256
30
31#define TABLEX (XSIZ * 2)
32#define TABLEY (YSIZ * 2)
33
34static unsigned char screen[XSIZ * YSIZ];
35static unsigned char table[TABLEX * TABLEY];
36
37static void do_plasma(unsigned char *,
38                      double, double, double, double, double, double);
39
40int main (int argc, char **argv)
41{
42    cucul_t *qq; caca_t *kk;
43    unsigned int red[256], green[256], blue[256], alpha[256];
44    double r[3], R[6];
45    struct cucul_bitmap *bitmap;
46    int i, x, y, frame = 0, pause = 0;
47
48    qq = cucul_init();
49    if(!qq)
50        return 1;
51    kk = caca_attach(qq);
52    if(!kk)
53        return 1;
54
55    caca_set_delay(kk, 20000);
56
57    /* Fill various tables */
58    for(i = 0 ; i < 256; i++)
59        red[i] = green[i] = blue[i] = alpha[i] = 0;
60
61    for(i = 0; i < 3; i++)
62        r[i] = (double)(cucul_rand(1, 1000)) / 60000 * M_PI;
63
64    for(i = 0; i < 6; i++)
65        R[i] = (double)(cucul_rand(1, 1000)) / 10000;
66
67    for(y = 0 ; y < TABLEY ; y++)
68        for(x = 0 ; x < TABLEX ; x++)
69    {
70        double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2))
71                              + (y - (TABLEX / 2)) * (y - (TABLEX / 2))))
72                      * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY)));
73
74        table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6;
75    }
76
77    /* Create a libcucul bitmap */
78    bitmap = cucul_create_bitmap(qq, 8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
79
80    /* Main loop */
81    for(;;)
82    {
83        switch(caca_get_event(kk, CACA_EVENT_KEY_PRESS))
84        {
85            case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
86            case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
87        }
88
89        if(pause)
90            goto paused;
91
92        for(i = 0 ; i < 256; i++)
93        {
94            double z = ((double)i) / 256 * 6 * M_PI;
95
96            red[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
97            blue[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff;
98            green[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff;
99        }
100
101        /* Set the palette */
102        cucul_set_bitmap_palette(bitmap, red, green, blue, alpha);
103
104        do_plasma(screen,
105                  (1.0 + sin(((double)frame) * R[0])) / 2,
106                  (1.0 + sin(((double)frame) * R[1])) / 2,
107                  (1.0 + sin(((double)frame) * R[2])) / 2,
108                  (1.0 + sin(((double)frame) * R[3])) / 2,
109                  (1.0 + sin(((double)frame) * R[4])) / 2,
110                  (1.0 + sin(((double)frame) * R[5])) / 2);
111        frame++;
112
113paused:
114        cucul_draw_bitmap(qq, 0, 0,
115                          cucul_get_width(qq) - 1, cucul_get_height(qq) - 1,
116                          bitmap, screen);
117        cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
118        cucul_putstr(qq, cucul_get_width(qq) - 30, cucul_get_height(qq) - 2,
119                     " -=[ Powered by libcaca ]=- ");
120        caca_display(kk);
121    }
122
123end:
124    cucul_free_bitmap(bitmap);
125    caca_detach(kk);
126    cucul_end(qq);
127
128    return 0;
129}
130
131static void do_plasma(unsigned char *pixels, double x_1, double y_1,
132                      double x_2, double y_2, double x_3, double y_3)
133{
134    unsigned int X1 = x_1 * (TABLEX / 2),
135                 Y1 = y_1 * (TABLEY / 2),
136                 X2 = x_2 * (TABLEX / 2),
137                 Y2 = y_2 * (TABLEY / 2),
138                 X3 = x_3 * (TABLEX / 2),
139                 Y3 = y_3 * (TABLEY / 2);
140    unsigned int y;
141    unsigned char * t1 = table + X1 + Y1 * TABLEX,
142                  * t2 = table + X2 + Y2 * TABLEX,
143                  * t3 = table + X3 + Y3 * TABLEX;
144
145    for(y = 0; y < YSIZ; y++)
146    {
147        unsigned int x;
148        unsigned char * tmp = pixels + y * YSIZ;
149        unsigned int ty = y * TABLEX, tmax = ty + XSIZ;
150        for(x = 0; ty < tmax; ty++, tmp++)
151            tmp[0] = t1[ty] + t2[ty] + t3[ty];
152    }
153}
154
Note: See TracBrowser for help on using the repository browser.