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 811 2006-04-18 15:11:25Z 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 | |
---|
35 | static unsigned char screen[XSIZ * YSIZ]; |
---|
36 | static unsigned char table[TABLEX * TABLEY]; |
---|
37 | |
---|
38 | static void do_plasma(unsigned char *, |
---|
39 | double, double, double, double, double, double); |
---|
40 | |
---|
41 | int 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(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(cucul_get_width(cv), cucul_get_height(cv)); |
---|
59 | mask = cucul_create(cucul_get_width(cv), cucul_get_height(cv)); |
---|
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 dither */ |
---|
82 | dither = cucul_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0); |
---|
83 | |
---|
84 | /* Main loop */ |
---|
85 | for(;;) |
---|
86 | { |
---|
87 | caca_event_t ev; |
---|
88 | if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0)) |
---|
89 | { |
---|
90 | switch(ev.data.key.ch) |
---|
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_dither_palette(dither, 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 | |
---|
121 | paused: |
---|
122 | cucul_dither_bitmap(cv, 0, 0, |
---|
123 | cucul_get_width(cv) - 1, cucul_get_height(cv) - 1, |
---|
124 | dither, screen); |
---|
125 | |
---|
126 | cucul_blit(c2, 0, 0, cv, NULL); |
---|
127 | cucul_invert(c2); |
---|
128 | |
---|
129 | |
---|
130 | cucul_blit(cv, 0, 0, c2, mask); |
---|
131 | |
---|
132 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
133 | cucul_putstr(cv, cucul_get_width(cv) - 30, cucul_get_height(cv) - 2, |
---|
134 | " -=[ Powered by libcaca ]=- "); |
---|
135 | caca_display(dp); |
---|
136 | } |
---|
137 | |
---|
138 | end: |
---|
139 | cucul_free_dither(dither); |
---|
140 | caca_detach(dp); |
---|
141 | cucul_free(cv); |
---|
142 | |
---|
143 | return 0; |
---|
144 | } |
---|
145 | |
---|
146 | static 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 | |
---|