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