You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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