1 | #!/usr/bin/php5 |
---|
2 | <? |
---|
3 | |
---|
4 | function main() { |
---|
5 | $cv = caca_create_canvas(0, 0); |
---|
6 | if (!$cv) { |
---|
7 | die("Error while creating canvas\n"); |
---|
8 | } |
---|
9 | |
---|
10 | $dp = caca_create_display($cv); |
---|
11 | if (!$dp) { |
---|
12 | die("Error while attaching canvas to display\n"); |
---|
13 | } |
---|
14 | |
---|
15 | display_menu($cv, $dp); |
---|
16 | |
---|
17 | caca_set_display_time($dp, 40000); |
---|
18 | |
---|
19 | /* Disable cursor */ |
---|
20 | caca_set_mouse($dp, 0); |
---|
21 | |
---|
22 | /* Main menu */ |
---|
23 | display_menu($cv, $dp); |
---|
24 | caca_refresh_display($dp); |
---|
25 | |
---|
26 | |
---|
27 | /* Go ! */ |
---|
28 | $bounds = $outline = $dithering = 0; |
---|
29 | |
---|
30 | $ev = caca_create_event(); |
---|
31 | while(!$quit) { |
---|
32 | $menu = $mouse = $xmouse = $ymouse = 0; |
---|
33 | |
---|
34 | while (caca_get_event($dp, CACA_EVENT_ANY, $ev, 0)) { |
---|
35 | if ($demo and (caca_get_event_type($ev) & CACA_EVENT_KEY_PRESS)) { |
---|
36 | $menu = 1; |
---|
37 | $demo = false; |
---|
38 | } |
---|
39 | else if (caca_get_event_type($ev) & CACA_EVENT_KEY_PRESS) { |
---|
40 | switch (caca_get_event_key_ch($ev)) { |
---|
41 | case ord('q'): |
---|
42 | case ord('Q'): |
---|
43 | case CACA_KEY_ESCAPE: |
---|
44 | $demo = false; |
---|
45 | $quit = 1; |
---|
46 | break; |
---|
47 | case ord('o'): |
---|
48 | case ord('O'): |
---|
49 | $outline = ($outline + 1) % 3; |
---|
50 | display_menu($cv, $dp); |
---|
51 | break; |
---|
52 | case ord('b'): |
---|
53 | case ord('B'): |
---|
54 | $bounds = ($bounds + 1) % 2; |
---|
55 | display_menu($cv, $dp); |
---|
56 | break; |
---|
57 | case ord('d'): |
---|
58 | case ord('D'): |
---|
59 | $dithering = ($dithering + 1) % 5; |
---|
60 | caca_set_feature($cv, $dithering); |
---|
61 | display_menu($cv, $dp); |
---|
62 | break; |
---|
63 | case ord('f'): |
---|
64 | case ord('F'): |
---|
65 | $demo = "demo_all"; |
---|
66 | break; |
---|
67 | case ord('1'): |
---|
68 | $demo = "demo_dots"; |
---|
69 | break; |
---|
70 | case ord('2'): |
---|
71 | $demo = "demo_lines"; |
---|
72 | break; |
---|
73 | case ord('3'): |
---|
74 | $demo = "demo_boxes"; |
---|
75 | break; |
---|
76 | case ord('4'): |
---|
77 | $demo = "demo_triangles"; |
---|
78 | break; |
---|
79 | case ord('5'): |
---|
80 | $demo = "demo_ellipses"; |
---|
81 | break; |
---|
82 | case ord('s'): |
---|
83 | case ord('S'): |
---|
84 | $demo = "demo_sprites"; |
---|
85 | break; |
---|
86 | case ord('r'): |
---|
87 | case ord('R'): |
---|
88 | $demo = "demo_render"; |
---|
89 | break; |
---|
90 | } |
---|
91 | |
---|
92 | if ($demo) { |
---|
93 | caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK); |
---|
94 | caca_clear_canvas($cv); |
---|
95 | } |
---|
96 | } |
---|
97 | else if(caca_get_event_type($ev) & CACA_EVENT_MOUSE_MOTION) { |
---|
98 | $mouse = 1; |
---|
99 | $xmouse = caca_get_event_mouse_x($ev); |
---|
100 | $ymouse = caca_get_event_mouse_y($ev); |
---|
101 | } |
---|
102 | else if(caca_get_event_type($ev) & CACA_EVENT_RESIZE) { |
---|
103 | $mouse = 1; /* old hack */ |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | if ($menu || ($mouse && !$demo)) { |
---|
108 | display_menu($cv, $dp); |
---|
109 | if ($mouse && !$demo) { |
---|
110 | caca_set_color_ansi($cv, CACA_RED, CACA_BLACK); |
---|
111 | caca_put_str($cv, $xmouse, $ymouse,"."); |
---|
112 | caca_put_str($cv, $xmouse, $ymouse + 1, "|\\"); |
---|
113 | } |
---|
114 | caca_refresh_display($dp); |
---|
115 | $mouse = $menu = 0; |
---|
116 | } |
---|
117 | |
---|
118 | if ($demo) { |
---|
119 | $demo($cv, $bounds, $outline, $dithering); |
---|
120 | |
---|
121 | caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK); |
---|
122 | caca_draw_thin_box($cv, 1, 1, caca_get_canvas_width($cv) - 2, caca_get_canvas_height($cv) - 2); |
---|
123 | $rate = (1000000 / caca_get_display_time($dp)).".".((10000000 / caca_get_display_time($dp)) % 10); |
---|
124 | caca_put_str($cv, 4, 1, "[$rate fps]----"); |
---|
125 | caca_refresh_display($dp); |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | function display_menu($cv, $dp) { |
---|
131 | $xo = caca_get_canvas_width($cv) - 2; |
---|
132 | $yo = caca_get_canvas_height($cv) - 2; |
---|
133 | |
---|
134 | caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK); |
---|
135 | caca_clear_canvas($cv); |
---|
136 | caca_draw_thin_box($cv, 1, 1, $xo, $yo); |
---|
137 | |
---|
138 | caca_put_str($cv, ($xo - strlen("libcaca demo")) / 2, 3, "libcaca demo"); |
---|
139 | caca_put_str($cv, ($xo - strlen("==============")) / 2, 4, "=============="); |
---|
140 | |
---|
141 | caca_put_str($cv, 4, 6, "demos:"); |
---|
142 | caca_put_str($cv, 4, 7, "'f': full"); |
---|
143 | caca_put_str($cv, 4, 8, "'1': dots"); |
---|
144 | caca_put_str($cv, 4, 9, "'2': lines"); |
---|
145 | caca_put_str($cv, 4, 10, "'3': boxes"); |
---|
146 | caca_put_str($cv, 4, 11, "'4': triangles"); |
---|
147 | caca_put_str($cv, 4, 12, "'5': ellipses"); |
---|
148 | caca_put_str($cv, 4, 13, "'c': colour"); |
---|
149 | caca_put_str($cv, 4, 14, "'r': render"); |
---|
150 | |
---|
151 | if ($sprite) |
---|
152 | caca_put_str($cv, 4, 15, "'s': sprites"); |
---|
153 | |
---|
154 | caca_put_str($cv, 4, 16, "settings:"); |
---|
155 | caca_put_str($cv, 4, 17, "'o': outline: ".((outline == 0) ? "none" : ((outline == 1) ? "solid" : "thin"))); |
---|
156 | caca_put_str($cv, 4, 18, "'b': drawing boundaries: ".((bounds == 0) ? "screen" : "infinite")); |
---|
157 | caca_put_str($cv, 4, $yo - 2, "'q': quit"); |
---|
158 | |
---|
159 | caca_refresh_display($dp); |
---|
160 | } |
---|
161 | |
---|
162 | function demo_dots($cv, $bounds, $outline, $dithering) { |
---|
163 | $xmax = caca_get_canvas_width($cv); |
---|
164 | $ymax = caca_get_canvas_height($cv); |
---|
165 | |
---|
166 | $chars = array('+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W'); |
---|
167 | |
---|
168 | for ($i = 1000; $i--;) { |
---|
169 | /* Putpixel */ |
---|
170 | caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16)); |
---|
171 | caca_put_char($cv, caca_rand(0, $xmax), caca_rand(0, $ymax), $chars[caca_rand(0, 9)]); |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | function demo_lines($cv, $bounds, $outline, $dithering) { |
---|
176 | $w = caca_get_canvas_width($cv); |
---|
177 | $h = caca_get_canvas_height($cv); |
---|
178 | |
---|
179 | if ($bounds) { |
---|
180 | $xa = caca_rand(- $w, 2 * $w); $ya = caca_rand(- $h, 2 * $h); |
---|
181 | $xb = caca_rand(- $w, 2 * $w); $yb = caca_rand(- $h, 2 * $h); |
---|
182 | } |
---|
183 | else { |
---|
184 | $xa = caca_rand(0, $w); $ya = caca_rand(0, $h); |
---|
185 | $xb = caca_rand(0, $w); $yb = caca_rand(0, $h); |
---|
186 | } |
---|
187 | |
---|
188 | caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK); |
---|
189 | if ($outline > 1) |
---|
190 | caca_draw_thin_line($cv, $xa, $ya, $xb, $yb); |
---|
191 | else |
---|
192 | caca_draw_line($cv, $xa, $ya, $xb, $yb, '#'); |
---|
193 | } |
---|
194 | |
---|
195 | function demo_boxes($cv, $bounds, $outline, $dithering) { |
---|
196 | $w = caca_get_canvas_width($cv); |
---|
197 | $h = caca_get_canvas_height($cv); |
---|
198 | |
---|
199 | if ($bounds) { |
---|
200 | $xa = caca_rand(- $w, 2 * $w); $ya = caca_rand(- $h, 2 * $h); |
---|
201 | $xb = caca_rand(- $w, 2 * $w); $yb = caca_rand(- $h, 2 * $h); |
---|
202 | } |
---|
203 | else { |
---|
204 | $xa = caca_rand(0, $w); $ya = caca_rand(0, $h); |
---|
205 | $xb = caca_rand(0, $w); $yb = caca_rand(0, $h); |
---|
206 | } |
---|
207 | |
---|
208 | caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16)); |
---|
209 | caca_fill_box($cv, $xa, $ya, $xb, $yb, '#'); |
---|
210 | |
---|
211 | caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK); |
---|
212 | if($outline == 2) |
---|
213 | caca_draw_thin_box($cv, $xa, $ya, $xb, $yb); |
---|
214 | else if($outline == 1) |
---|
215 | caca_draw_box($cv, $xa, $ya, $xb, $yb, '#'); |
---|
216 | } |
---|
217 | |
---|
218 | function demo_ellipses($cv, $bounds, $outline, $dithering) { |
---|
219 | $w = caca_get_canvas_width($cv); |
---|
220 | $h = caca_get_canvas_height($cv); |
---|
221 | |
---|
222 | if ($bounds) { |
---|
223 | $x = caca_rand(- $w, 2 * $w); $y = caca_rand(- $h, 2 * $h); |
---|
224 | $a = caca_rand(0, $w); $b = caca_rand(0, $h); |
---|
225 | } |
---|
226 | else { |
---|
227 | do { |
---|
228 | $x = caca_rand(0, $w); $y = caca_rand(0, $h); |
---|
229 | $a = caca_rand(0, $w); $b = caca_rand(0, $h); |
---|
230 | } while ($x - $a < 0 || $x + $a >= $w || $y - $b < 0 || $y + $b >= $h); |
---|
231 | } |
---|
232 | |
---|
233 | caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16)); |
---|
234 | caca_fill_ellipse($cv, $x, $y, $a, $b, '#'); |
---|
235 | |
---|
236 | caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK); |
---|
237 | if ($outline == 2) |
---|
238 | caca_draw_thin_ellipse($cv, $x, $y, $a, $b); |
---|
239 | else if ($outline == 1) |
---|
240 | caca_draw_ellipse($cv, $x, $y, $a, $b, '#'); |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | |
---|
245 | main(); |
---|