1 | /* |
---|
2 | * cacamoir moiré circles effect for libcaca |
---|
3 | * Copyright (c) 2004 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: cacamoir.c 581 2006-03-10 09:48:33Z sam $ |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | #include "config.h" |
---|
15 | |
---|
16 | #if !defined(__KERNEL__) |
---|
17 | # include <math.h> |
---|
18 | # include <string.h> |
---|
19 | #endif |
---|
20 | |
---|
21 | #include "cucul.h" |
---|
22 | #include "caca.h" |
---|
23 | |
---|
24 | /* Virtual buffer size */ |
---|
25 | #define XSIZ 256 |
---|
26 | #define YSIZ 256 |
---|
27 | |
---|
28 | #define DISCSIZ 512 |
---|
29 | #define DISCTHICKNESS 64 |
---|
30 | |
---|
31 | static unsigned char screen[XSIZ * YSIZ]; |
---|
32 | static unsigned char disc[DISCSIZ * DISCSIZ]; |
---|
33 | |
---|
34 | static void put_disc(int, int); |
---|
35 | static void draw_disc(int, char); |
---|
36 | static void draw_line(int, int, char); |
---|
37 | |
---|
38 | int main (int argc, char **argv) |
---|
39 | { |
---|
40 | cucul_t *qq; caca_t *kk; |
---|
41 | unsigned int red[256], green[256], blue[256], alpha[256]; |
---|
42 | struct cucul_bitmap *bitmap; |
---|
43 | int i, x, y, frame = 0, pause = 0; |
---|
44 | |
---|
45 | qq = cucul_init(); |
---|
46 | if(!qq) |
---|
47 | return 1; |
---|
48 | kk = caca_attach(qq); |
---|
49 | if(!kk) |
---|
50 | return 1; |
---|
51 | |
---|
52 | caca_set_delay(kk, 20000); |
---|
53 | |
---|
54 | /* Fill various tables */ |
---|
55 | for(i = 0 ; i < 256; i++) |
---|
56 | red[i] = green[i] = blue[i] = alpha[i] = 0; |
---|
57 | |
---|
58 | red[0] = green[0] = blue[0] = 0x777; |
---|
59 | red[1] = green[1] = blue[1] = 0xfff; |
---|
60 | |
---|
61 | /* Fill the circle */ |
---|
62 | for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS) |
---|
63 | draw_disc(i, (i / DISCTHICKNESS) % 2); |
---|
64 | |
---|
65 | /* Create a libcucul bitmap */ |
---|
66 | bitmap = cucul_create_bitmap(qq, 8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0); |
---|
67 | |
---|
68 | /* Main loop */ |
---|
69 | for(;;) |
---|
70 | { |
---|
71 | switch(caca_get_event(kk, CACA_EVENT_KEY_PRESS)) |
---|
72 | { |
---|
73 | case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end; |
---|
74 | case CACA_EVENT_KEY_PRESS | ' ': pause = !pause; |
---|
75 | } |
---|
76 | |
---|
77 | if(pause) |
---|
78 | goto paused; |
---|
79 | |
---|
80 | memset(screen, 0, XSIZ * YSIZ); |
---|
81 | |
---|
82 | /* Set the palette */ |
---|
83 | red[0] = 0.5 * (1 + sin(0.05 * frame)) * 0xfff; |
---|
84 | green[0] = 0.5 * (1 + cos(0.07 * frame)) * 0xfff; |
---|
85 | blue[0] = 0.5 * (1 + cos(0.06 * frame)) * 0xfff; |
---|
86 | |
---|
87 | red[1] = 0.5 * (1 + sin(0.07 * frame + 5.0)) * 0xfff; |
---|
88 | green[1] = 0.5 * (1 + cos(0.06 * frame + 5.0)) * 0xfff; |
---|
89 | blue[1] = 0.5 * (1 + cos(0.05 * frame + 5.0)) * 0xfff; |
---|
90 | |
---|
91 | cucul_set_bitmap_palette(qq, bitmap, red, green, blue, alpha); |
---|
92 | |
---|
93 | /* Draw circles */ |
---|
94 | x = cos(0.07 * frame + 5.0) * 128.0 + (XSIZ / 2); |
---|
95 | y = sin(0.11 * frame) * 128.0 + (YSIZ / 2); |
---|
96 | put_disc(x, y); |
---|
97 | |
---|
98 | x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2); |
---|
99 | y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2); |
---|
100 | put_disc(x, y); |
---|
101 | |
---|
102 | frame++; |
---|
103 | |
---|
104 | paused: |
---|
105 | cucul_draw_bitmap(qq, 0, 0, |
---|
106 | cucul_get_width(qq) - 1, cucul_get_height(qq) - 1, |
---|
107 | bitmap, screen); |
---|
108 | caca_display(kk); |
---|
109 | } |
---|
110 | |
---|
111 | end: |
---|
112 | cucul_free_bitmap(qq, bitmap); |
---|
113 | caca_detach(kk); |
---|
114 | cucul_end(qq); |
---|
115 | |
---|
116 | return 0; |
---|
117 | } |
---|
118 | |
---|
119 | static void put_disc(int x, int y) |
---|
120 | { |
---|
121 | char *src = ((char*)disc) + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ; |
---|
122 | int i, j; |
---|
123 | |
---|
124 | for(j = 0; j < YSIZ; j++) |
---|
125 | for(i = 0; i < XSIZ; i++) |
---|
126 | { |
---|
127 | screen[i + XSIZ * j] ^= src[i + DISCSIZ * j]; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | static void draw_disc(int r, char color) |
---|
132 | { |
---|
133 | int t, dx, dy; |
---|
134 | |
---|
135 | for(t = 0, dx = 0, dy = r; dx <= dy; dx++) |
---|
136 | { |
---|
137 | draw_line(dx / 3, dy / 3, color); |
---|
138 | draw_line(dy / 3, dx / 3, color); |
---|
139 | |
---|
140 | t += t > 0 ? dx - dy-- : dx; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | static void draw_line(int x, int y, char color) |
---|
145 | { |
---|
146 | if(x == 0 || y == 0 || y > DISCSIZ / 2) |
---|
147 | return; |
---|
148 | |
---|
149 | if(x > DISCSIZ / 2) |
---|
150 | x = DISCSIZ / 2; |
---|
151 | |
---|
152 | memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y), |
---|
153 | color, 2 * x - 1); |
---|
154 | memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1), |
---|
155 | color, 2 * x - 1); |
---|
156 | } |
---|
157 | |
---|