1 | #!/usr/bin/php5 |
---|
2 | <?php |
---|
3 | /* |
---|
4 | * demo.php demo for libcaca php binding |
---|
5 | * Copyright (c) 2008 Nicolas Vion <nico@yojik.eu> |
---|
6 | * |
---|
7 | * This file is a Php port of the official libcaca's sample program "demo.c" |
---|
8 | * which is: |
---|
9 | * Copyright (c) 2003 Sam Hocevar <sam@zoy.org> |
---|
10 | * |
---|
11 | * This program is free software. It comes without any warranty, to |
---|
12 | * the extent permitted by applicable law. You can redistribute it |
---|
13 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
14 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
15 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
16 | */ |
---|
17 | |
---|
18 | function main() { |
---|
19 | $cv = caca_create_canvas(0, 0); |
---|
20 | if (!$cv) { |
---|
21 | die("Error while creating canvas\n"); |
---|
22 | } |
---|
23 | |
---|
24 | $dp = caca_create_display($cv); |
---|
25 | if (!$dp) { |
---|
26 | die("Error while attaching canvas to display\n"); |
---|
27 | } |
---|
28 | |
---|
29 | caca_set_display_time($dp, 40000); |
---|
30 | |
---|
31 | /* Disable X cursor */ |
---|
32 | caca_set_mouse($dp, 0); |
---|
33 | |
---|
34 | /* Main menu */ |
---|
35 | display_menu($cv, $dp, $bounds, $outline); |
---|
36 | |
---|
37 | /* Go ! */ |
---|
38 | $bounds = $outline = $dithering = 0; |
---|
39 | $need_refresh = false; |
---|
40 | while(!$quit) { |
---|
41 | while ($ev = caca_get_event($dp, CACA_EVENT_ANY, 1000)) { |
---|
42 | if (caca_get_event_type($ev) & CACA_EVENT_KEY_PRESS) { |
---|
43 | switch (caca_get_event_key_ch($ev)) { |
---|
44 | case ord('q'): |
---|
45 | case ord('Q'): |
---|
46 | case CACA_KEY_ESCAPE: |
---|
47 | $demo = false; |
---|
48 | $quit = 1; |
---|
49 | break; |
---|
50 | case ord('o'): |
---|
51 | case ord('O'): |
---|
52 | $outline = ($outline + 1) % 3; |
---|
53 | display_menu($cv, $dp, $bounds, $outline); |
---|
54 | break; |
---|
55 | case ord('b'): |
---|
56 | case ord('B'): |
---|
57 | $bounds = ($bounds + 1) % 2; |
---|
58 | display_menu($cv, $dp, $bounds, $outline); |
---|
59 | break; |
---|
60 | case ord('d'): |
---|
61 | case ord('D'): |
---|
62 | $dithering = ($dithering + 1) % 5; |
---|
63 | caca_set_feature($cv, $dithering); |
---|
64 | display_menu($cv, $dp, $bounds, $outline); |
---|
65 | break; |
---|
66 | case ord('f'): |
---|
67 | case ord('F'): |
---|
68 | demo_go($dp, "demo_all", $cv, $bounds, $outline); |
---|
69 | break; |
---|
70 | case ord('1'): |
---|
71 | demo_go($dp, "demo_dots", $cv, $bounds, $outline); |
---|
72 | break; |
---|
73 | case ord('2'): |
---|
74 | demo_go($dp, "demo_lines", $cv, $bounds, $outline); |
---|
75 | break; |
---|
76 | case ord('3'): |
---|
77 | demo_go($dp, "demo_boxes", $cv, $bounds, $outline); |
---|
78 | break; |
---|
79 | case ord('4'): |
---|
80 | demo_go($dp, "demo_triangles", $cv, $bounds, $outline); |
---|
81 | break; |
---|
82 | case ord('5'): |
---|
83 | demo_go($dp, "demo_ellipses", $cv, $bounds, $outline); |
---|
84 | break; |
---|
85 | case ord('s'): |
---|
86 | case ord('S'): |
---|
87 | demo_go($dp, "demo_sprites", $cv, $bounds, $outline); |
---|
88 | break; |
---|
89 | case ord('r'): |
---|
90 | case ord('R'): |
---|
91 | demo_go($dp, "demo_render", $cv, $bounds, $outline); |
---|
92 | break; |
---|
93 | } |
---|
94 | } |
---|
95 | else if(caca_get_event_type($ev) & CACA_EVENT_MOUSE_MOTION) { |
---|
96 | $x = caca_get_event_mouse_x($ev); |
---|
97 | $y = caca_get_event_mouse_y($ev); |
---|
98 | display_menu($cv, $dp, $bounds, $outline, false); |
---|
99 | caca_set_color_ansi($cv, CACA_RED, CACA_BLACK); |
---|
100 | caca_put_str($cv, $x, $y, "."); |
---|
101 | caca_put_str($cv, $x, $y + 1, "|\\"); |
---|
102 | $need_refresh = true; |
---|
103 | } |
---|
104 | else if(caca_get_event_type($ev) & CACA_EVENT_RESIZE) { |
---|
105 | display_menu($cv, $dp, $bounds, $outline); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | if ($need_refresh) { |
---|
110 | caca_refresh_display($dp); |
---|
111 | $need_refresh = false; |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | function display_menu($cv, $dp, $bounds, $outline, $refresh = true) { |
---|
117 | $xo = caca_get_canvas_width($cv) - 2; |
---|
118 | $yo = caca_get_canvas_height($cv) - 2; |
---|
119 | |
---|
120 | caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK); |
---|
121 | caca_clear_canvas($cv); |
---|
122 | caca_draw_thin_box($cv, 1, 1, $xo, $yo); |
---|
123 | |
---|
124 | caca_put_str($cv, ($xo - strlen("libcaca demo")) / 2, 3, "libcaca demo"); |
---|
125 | caca_put_str($cv, ($xo - strlen("==============")) / 2, 4, "=============="); |
---|
126 | |
---|
127 | caca_put_str($cv, 4, 6, "demos:"); |
---|
128 | caca_put_str($cv, 4, 7, "'f': full"); |
---|
129 | caca_put_str($cv, 4, 8, "'1': dots"); |
---|
130 | caca_put_str($cv, 4, 9, "'2': lines"); |
---|
131 | caca_put_str($cv, 4, 10, "'3': boxes"); |
---|
132 | caca_put_str($cv, 4, 11, "'4': triangles"); |
---|
133 | caca_put_str($cv, 4, 12, "'5': ellipses"); |
---|
134 | // caca_put_str($cv, 4, 13, "'c': colour"); |
---|
135 | // caca_put_str($cv, 4, 14, "'r': render"); |
---|
136 | |
---|
137 | if ($sprite) |
---|
138 | caca_put_str($cv, 4, 15, "'s': sprites"); |
---|
139 | |
---|
140 | caca_put_str($cv, 4, 16, "settings:"); |
---|
141 | caca_put_str($cv, 4, 17, "'o': outline: ".(($outline == 0) ? "none" : (($outline == 1) ? "solid" : "thin"))); |
---|
142 | caca_put_str($cv, 4, 18, "'b': drawing boundaries: ".(($bounds == 0) ? "screen" : "infinite")); |
---|
143 | caca_put_str($cv, 4, $yo - 2, "'q': quit"); |
---|
144 | |
---|
145 | if ($refresh) |
---|
146 | caca_refresh_display($dp); |
---|
147 | } |
---|
148 | |
---|
149 | function demo_go($dp, $demo, $cv, $bounds, $outline) { |
---|
150 | caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK); |
---|
151 | caca_clear_canvas($cv); |
---|
152 | |
---|
153 | while (!caca_get_event($dp, CACA_EVENT_KEY_PRESS)) { |
---|
154 | if (function_exists($demo)) |
---|
155 | $demo($cv, $bounds, $outline); |
---|
156 | |
---|
157 | caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK); |
---|
158 | caca_draw_thin_box($cv, 1, 1, caca_get_canvas_width($cv) - 2, caca_get_canvas_height($cv) - 2); |
---|
159 | $rate = sprintf("%01.2f", 1000000 / caca_get_display_time($dp)); |
---|
160 | caca_put_str($cv, 4, 1, "[$rate fps]----"); |
---|
161 | caca_refresh_display($dp); |
---|
162 | } |
---|
163 | |
---|
164 | display_menu($cv, $dp, $bounds, $outline); |
---|
165 | caca_refresh_display($dp); |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | function demo_all($cv, $bounds, $outline) { |
---|
170 | global $i; |
---|
171 | |
---|
172 | $i++; |
---|
173 | |
---|
174 | caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK); |
---|
175 | caca_clear_canvas($cv); |
---|
176 | |
---|
177 | /* Draw the sun */ |
---|
178 | caca_set_color_ansi($cv, CACA_YELLOW, CACA_BLACK); |
---|
179 | $xo = caca_get_canvas_width($cv) / 4; |
---|
180 | $yo = caca_get_canvas_height($cv) / 4 + 5 * sin(0.03 * $i); |
---|
181 | |
---|
182 | for ($j = 0; $j < 16; $j++) { |
---|
183 | $xa = $xo - (30 + sin(0.03 * $i) * 8) * sin(0.03 * $i + M_PI * $j / 8); |
---|
184 | $ya = $yo + (15 + sin(0.03 * $i) * 4) * cos(0.03 * $i + M_PI * $j / 8); |
---|
185 | caca_draw_thin_line($cv, $xo, $yo, $xa, $ya); |
---|
186 | } |
---|
187 | |
---|
188 | $j = 15 + sin(0.03 * $i) * 8; |
---|
189 | caca_set_color_ansi($cv, CACA_WHITE, CACA_BLACK); |
---|
190 | caca_fill_ellipse($cv, $xo, $yo, $j, $j / 2, ord('#')); |
---|
191 | caca_set_color_ansi($cv, CACA_YELLOW, CACA_BLACK); |
---|
192 | caca_draw_ellipse($cv, $xo, $yo, $j, $j / 2, ord('#')); |
---|
193 | |
---|
194 | /* Draw the pyramid */ |
---|
195 | $xo = caca_get_canvas_width($cv) * 5 / 8; |
---|
196 | $yo = 2; |
---|
197 | |
---|
198 | $xa = caca_get_canvas_width($cv) / 8 + sin(0.03 * $i) * 5; |
---|
199 | $ya = caca_get_canvas_height($cv) / 2 + cos(0.03 * $i) * 5; |
---|
200 | |
---|
201 | $xb = caca_get_canvas_width($cv) - 10 - cos(0.02 * $i) * 10; |
---|
202 | $yb = caca_get_canvas_height($cv) * 3 / 4 - 5 + sin(0.02 * $i) * 5; |
---|
203 | |
---|
204 | $xc = caca_get_canvas_width($cv) / 4 - sin(0.02 * $i) * 5; |
---|
205 | $yc = caca_get_canvas_height($cv) * 3 / 4 + cos(0.02 * $i) * 5; |
---|
206 | |
---|
207 | caca_set_color_ansi($cv, CACA_GREEN, CACA_BLACK); |
---|
208 | caca_fill_triangle($cv, $xo, $yo, $xb, $yb, $xa, $ya, ord('%')); |
---|
209 | caca_set_color_ansi($cv, CACA_YELLOW, CACA_BLACK); |
---|
210 | caca_draw_thin_triangle($cv, $xo, $yo, $xb, $yb, $xa, $ya); |
---|
211 | |
---|
212 | caca_set_color_ansi($cv, CACA_RED, CACA_BLACK); |
---|
213 | caca_fill_triangle($cv, $xa, $ya, $xb, $yb, $xc, $yc, ord('#')); |
---|
214 | caca_set_color_ansi($cv, CACA_YELLOW, CACA_BLACK); |
---|
215 | caca_draw_thin_triangle($cv, $xa, $ya, $xb, $yb, $xc, $yc); |
---|
216 | |
---|
217 | caca_set_color_ansi($cv, CACA_BLUE, CACA_BLACK); |
---|
218 | caca_fill_triangle($cv, $xo, $yo, $xb, $yb, $xc, $yc, ord('%')); |
---|
219 | caca_set_color_ansi($cv, CACA_YELLOW, CACA_BLACK); |
---|
220 | caca_draw_thin_triangle($cv, $xo, $yo, $xb, $yb, $xc, $yc); |
---|
221 | |
---|
222 | /* Draw a background triangle */ |
---|
223 | $xa = 2; |
---|
224 | $ya = 2; |
---|
225 | |
---|
226 | $xb = caca_get_canvas_width($cv) - 3; |
---|
227 | $yb = caca_get_canvas_height($cv) / 2; |
---|
228 | |
---|
229 | $xc = caca_get_canvas_width($cv) / 3; |
---|
230 | $yc = caca_get_canvas_height($cv) - 3; |
---|
231 | |
---|
232 | caca_set_color_ansi($cv, CACA_CYAN, CACA_BLACK); |
---|
233 | caca_draw_thin_triangle($cv, $xa, $ya, $xb, $yb, $xc, $yc); |
---|
234 | |
---|
235 | $xo = caca_get_canvas_width($cv) / 2 + cos(0.027 * $i) * caca_get_canvas_width($cv) / 3; |
---|
236 | $yo = caca_get_canvas_height($cv) / 2 - sin(0.027 * $i) * caca_get_canvas_height($cv) / 2; |
---|
237 | |
---|
238 | caca_draw_thin_line($cv, $xa, $ya, $xo, $yo); |
---|
239 | caca_draw_thin_line($cv, $xb, $yb, $xo, $yo); |
---|
240 | caca_draw_thin_line($cv, $xc, $yc, $xo, $yo); |
---|
241 | |
---|
242 | /* Draw a trail behind the foreground sprite */ |
---|
243 | for($j = $i - 60; $j < $i; $j++) { |
---|
244 | $delta = caca_rand(-5, 6); |
---|
245 | caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16)); |
---|
246 | caca_put_char($cv, |
---|
247 | caca_get_canvas_width($cv) / 2 + cos(0.02 * $j) * ($delta + caca_get_canvas_width($cv) / 4), |
---|
248 | caca_get_canvas_height($cv) / 2 + sin(0.02 * $j) * ($delta + caca_get_canvas_height($cv) / 3), |
---|
249 | ord('#')); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | function demo_dots($cv, $bounds, $outline) { |
---|
254 | $xmax = caca_get_canvas_width($cv); |
---|
255 | $ymax = caca_get_canvas_height($cv); |
---|
256 | |
---|
257 | $chars = array('+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W'); |
---|
258 | |
---|
259 | for ($i = 1000; $i--;) { |
---|
260 | /* Putpixel */ |
---|
261 | caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16)); |
---|
262 | caca_put_char($cv, caca_rand(0, $xmax), caca_rand(0, $ymax), ord($chars[caca_rand(0, 9)])); |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | function demo_lines($cv, $bounds, $outline) { |
---|
267 | $w = caca_get_canvas_width($cv); |
---|
268 | $h = caca_get_canvas_height($cv); |
---|
269 | |
---|
270 | if ($bounds) { |
---|
271 | $xa = caca_rand(- $w, 2 * $w); $ya = caca_rand(- $h, 2 * $h); |
---|
272 | $xb = caca_rand(- $w, 2 * $w); $yb = caca_rand(- $h, 2 * $h); |
---|
273 | } |
---|
274 | else { |
---|
275 | $xa = caca_rand(0, $w); $ya = caca_rand(0, $h); |
---|
276 | $xb = caca_rand(0, $w); $yb = caca_rand(0, $h); |
---|
277 | } |
---|
278 | |
---|
279 | caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK); |
---|
280 | if ($outline > 1) |
---|
281 | caca_draw_thin_line($cv, $xa, $ya, $xb, $yb); |
---|
282 | else |
---|
283 | caca_draw_line($cv, $xa, $ya, $xb, $yb, ord('#')); |
---|
284 | } |
---|
285 | |
---|
286 | function demo_boxes($cv, $bounds, $outline) { |
---|
287 | $w = caca_get_canvas_width($cv); |
---|
288 | $h = caca_get_canvas_height($cv); |
---|
289 | |
---|
290 | if ($bounds) { |
---|
291 | $xa = caca_rand(- $w, 2 * $w); $ya = caca_rand(- $h, 2 * $h); |
---|
292 | $xb = caca_rand(- $w, 2 * $w); $yb = caca_rand(- $h, 2 * $h); |
---|
293 | } |
---|
294 | else { |
---|
295 | $xa = caca_rand(0, $w); $ya = caca_rand(0, $h); |
---|
296 | $xb = caca_rand(0, $w); $yb = caca_rand(0, $h); |
---|
297 | } |
---|
298 | |
---|
299 | caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16)); |
---|
300 | caca_fill_box($cv, $xa, $ya, $xb, $yb, ord('#')); |
---|
301 | |
---|
302 | caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK); |
---|
303 | if($outline == 2) |
---|
304 | caca_draw_thin_box($cv, $xa, $ya, $xb, $yb); |
---|
305 | else if($outline == 1) |
---|
306 | caca_draw_box($cv, $xa, $ya, $xb, $yb, ord('#')); |
---|
307 | } |
---|
308 | |
---|
309 | function demo_ellipses($cv, $bounds, $outline) { |
---|
310 | $w = caca_get_canvas_width($cv); |
---|
311 | $h = caca_get_canvas_height($cv); |
---|
312 | |
---|
313 | if ($bounds) { |
---|
314 | $x = caca_rand(- $w, 2 * $w); $y = caca_rand(- $h, 2 * $h); |
---|
315 | $a = caca_rand(0, $w); $b = caca_rand(0, $h); |
---|
316 | } |
---|
317 | else { |
---|
318 | do { |
---|
319 | $x = caca_rand(0, $w); $y = caca_rand(0, $h); |
---|
320 | $a = caca_rand(0, $w); $b = caca_rand(0, $h); |
---|
321 | } while ($x - $a < 0 || $x + $a >= $w || $y - $b < 0 || $y + $b >= $h); |
---|
322 | } |
---|
323 | |
---|
324 | caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16)); |
---|
325 | caca_fill_ellipse($cv, $x, $y, $a, $b, ord('#')); |
---|
326 | |
---|
327 | caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK); |
---|
328 | if ($outline == 2) |
---|
329 | caca_draw_thin_ellipse($cv, $x, $y, $a, $b); |
---|
330 | else if ($outline == 1) |
---|
331 | caca_draw_ellipse($cv, $x, $y, $a, $b, ord('#')); |
---|
332 | } |
---|
333 | |
---|
334 | function demo_triangles($cv, $bounds, $outline) { |
---|
335 | $w = caca_get_canvas_width($cv); |
---|
336 | $h = caca_get_canvas_height($cv); |
---|
337 | |
---|
338 | if ($bounds) { |
---|
339 | $xa = caca_rand(- $w, 2 * $w); $ya = caca_rand(- $h, 2 * $h); |
---|
340 | $xb = caca_rand(- $w, 2 * $w); $yb = caca_rand(- $h, 2 * $h); |
---|
341 | $xc = caca_rand(- $w, 2 * $w); $yc = caca_rand(- $h, 2 * $h); |
---|
342 | } |
---|
343 | else { |
---|
344 | $xa = caca_rand(0, $w); $ya = caca_rand(0, $h); |
---|
345 | $xb = caca_rand(0, $w); $yb = caca_rand(0, $h); |
---|
346 | $xc = caca_rand(0, $w); $yc = caca_rand(0, $h); |
---|
347 | } |
---|
348 | |
---|
349 | caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16)); |
---|
350 | caca_fill_triangle($cv, $xa, $ya, $xb, $yb, $xc, $yc, ord('#')); |
---|
351 | |
---|
352 | caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK); |
---|
353 | if ($outline == 2) |
---|
354 | caca_draw_thin_triangle($cv, $xa, $ya, $xb, $yb, $xc, $yc); |
---|
355 | else if ($outline == 1) |
---|
356 | caca_draw_triangle($cv, $xa, $ya, $xb, $yb, $xc, $yc, ord('#')); |
---|
357 | } |
---|
358 | |
---|
359 | function demo_render($cv, $bounds, $outline) { |
---|
360 | } |
---|
361 | |
---|
362 | main(); |
---|
363 | |
---|
364 | ?> |
---|