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