Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

371 řádky
13 KiB

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