source: libcaca/trunk/caca-php/examples/demo.php @ 3135

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