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 681 2006-03-23 18:36: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 | |
---|
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 | |
---|
34 | static unsigned char screen[XSIZ * YSIZ]; |
---|
35 | static unsigned char table[TABLEX * TABLEY]; |
---|
36 | |
---|
37 | static void do_plasma(unsigned char *, |
---|
38 | double, double, double, double, double, double); |
---|
39 | |
---|
40 | int 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_create(0, 0); |
---|
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(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0); |
---|
79 | |
---|
80 | /* Main loop */ |
---|
81 | for(;;) |
---|
82 | { |
---|
83 | struct caca_event ev; |
---|
84 | if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev)) |
---|
85 | { |
---|
86 | switch(ev.data.key.c) |
---|
87 | { |
---|
88 | case CACA_KEY_ESCAPE: goto end; |
---|
89 | case ' ': pause = !pause; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | if(pause) |
---|
94 | goto paused; |
---|
95 | |
---|
96 | for(i = 0 ; i < 256; i++) |
---|
97 | { |
---|
98 | double z = ((double)i) / 256 * 6 * M_PI; |
---|
99 | |
---|
100 | red[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff; |
---|
101 | blue[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff; |
---|
102 | green[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff; |
---|
103 | } |
---|
104 | |
---|
105 | /* Set the palette */ |
---|
106 | cucul_set_bitmap_palette(bitmap, red, green, blue, alpha); |
---|
107 | |
---|
108 | do_plasma(screen, |
---|
109 | (1.0 + sin(((double)frame) * R[0])) / 2, |
---|
110 | (1.0 + sin(((double)frame) * R[1])) / 2, |
---|
111 | (1.0 + sin(((double)frame) * R[2])) / 2, |
---|
112 | (1.0 + sin(((double)frame) * R[3])) / 2, |
---|
113 | (1.0 + sin(((double)frame) * R[4])) / 2, |
---|
114 | (1.0 + sin(((double)frame) * R[5])) / 2); |
---|
115 | frame++; |
---|
116 | |
---|
117 | paused: |
---|
118 | cucul_draw_bitmap(qq, 0, 0, |
---|
119 | cucul_get_width(qq) - 1, cucul_get_height(qq) - 1, |
---|
120 | bitmap, screen); |
---|
121 | cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
122 | cucul_putstr(qq, cucul_get_width(qq) - 30, cucul_get_height(qq) - 2, |
---|
123 | " -=[ Powered by libcaca ]=- "); |
---|
124 | caca_display(kk); |
---|
125 | } |
---|
126 | |
---|
127 | end: |
---|
128 | cucul_free_bitmap(bitmap); |
---|
129 | caca_detach(kk); |
---|
130 | cucul_free(qq); |
---|
131 | |
---|
132 | return 0; |
---|
133 | } |
---|
134 | |
---|
135 | static void do_plasma(unsigned char *pixels, double x_1, double y_1, |
---|
136 | double x_2, double y_2, double x_3, double y_3) |
---|
137 | { |
---|
138 | unsigned int X1 = x_1 * (TABLEX / 2), |
---|
139 | Y1 = y_1 * (TABLEY / 2), |
---|
140 | X2 = x_2 * (TABLEX / 2), |
---|
141 | Y2 = y_2 * (TABLEY / 2), |
---|
142 | X3 = x_3 * (TABLEX / 2), |
---|
143 | Y3 = y_3 * (TABLEY / 2); |
---|
144 | unsigned int y; |
---|
145 | unsigned char * t1 = table + X1 + Y1 * TABLEX, |
---|
146 | * t2 = table + X2 + Y2 * TABLEX, |
---|
147 | * t3 = table + X3 + Y3 * TABLEX; |
---|
148 | |
---|
149 | for(y = 0; y < YSIZ; y++) |
---|
150 | { |
---|
151 | unsigned int x; |
---|
152 | unsigned char * tmp = pixels + y * YSIZ; |
---|
153 | unsigned int ty = y * TABLEX, tmax = ty + XSIZ; |
---|
154 | for(x = 0; ty < tmax; ty++, tmp++) |
---|
155 | tmp[0] = t1[ty] + t2[ty] + t3[ty]; |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|