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ů.
 
 
 
 
 
 

365 řádky
11 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@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 X cursor */
  28. caca_set_mouse($dp, 0);
  29. /* Main menu */
  30. display_menu($cv, $dp, $bounds, $outline);
  31. /* Go ! */
  32. $bounds = $outline = $dithering = 0;
  33. $need_refresh = false;
  34. while(!$quit) {
  35. while ($ev = caca_get_event($dp, CACA_EVENT_ANY, 1000)) {
  36. if (caca_get_event_type($ev) & CACA_EVENT_KEY_PRESS) {
  37. switch (caca_get_event_key_ch($ev)) {
  38. case ord('q'):
  39. case ord('Q'):
  40. case CACA_KEY_ESCAPE:
  41. $demo = false;
  42. $quit = 1;
  43. break;
  44. case ord('o'):
  45. case ord('O'):
  46. $outline = ($outline + 1) % 3;
  47. display_menu($cv, $dp, $bounds, $outline);
  48. break;
  49. case ord('b'):
  50. case ord('B'):
  51. $bounds = ($bounds + 1) % 2;
  52. display_menu($cv, $dp, $bounds, $outline);
  53. break;
  54. case ord('d'):
  55. case ord('D'):
  56. $dithering = ($dithering + 1) % 5;
  57. caca_set_feature($cv, $dithering);
  58. display_menu($cv, $dp, $bounds, $outline);
  59. break;
  60. case ord('f'):
  61. case ord('F'):
  62. demo_go($dp, "demo_all", $cv, $bounds, $outline);
  63. break;
  64. case ord('1'):
  65. demo_go($dp, "demo_dots", $cv, $bounds, $outline);
  66. break;
  67. case ord('2'):
  68. demo_go($dp, "demo_lines", $cv, $bounds, $outline);
  69. break;
  70. case ord('3'):
  71. demo_go($dp, "demo_boxes", $cv, $bounds, $outline);
  72. break;
  73. case ord('4'):
  74. demo_go($dp, "demo_triangles", $cv, $bounds, $outline);
  75. break;
  76. case ord('5'):
  77. demo_go($dp, "demo_ellipses", $cv, $bounds, $outline);
  78. break;
  79. case ord('s'):
  80. case ord('S'):
  81. demo_go($dp, "demo_sprites", $cv, $bounds, $outline);
  82. break;
  83. case ord('r'):
  84. case ord('R'):
  85. demo_go($dp, "demo_render", $cv, $bounds, $outline);
  86. break;
  87. }
  88. }
  89. else if(caca_get_event_type($ev) & CACA_EVENT_MOUSE_MOTION) {
  90. $x = caca_get_event_mouse_x($ev);
  91. $y = caca_get_event_mouse_y($ev);
  92. display_menu($cv, $dp, $bounds, $outline, false);
  93. caca_set_color_ansi($cv, CACA_RED, CACA_BLACK);
  94. caca_put_str($cv, $x, $y, ".");
  95. caca_put_str($cv, $x, $y + 1, "|\\");
  96. $need_refresh = true;
  97. }
  98. else if(caca_get_event_type($ev) & CACA_EVENT_RESIZE) {
  99. display_menu($cv, $dp, $bounds, $outline);
  100. }
  101. }
  102. if ($need_refresh) {
  103. caca_refresh_display($dp);
  104. $need_refresh = false;
  105. }
  106. }
  107. }
  108. function display_menu($cv, $dp, $bounds, $outline, $refresh = true) {
  109. $xo = caca_get_canvas_width($cv) - 2;
  110. $yo = caca_get_canvas_height($cv) - 2;
  111. caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
  112. caca_clear_canvas($cv);
  113. caca_draw_thin_box($cv, 1, 1, $xo, $yo);
  114. caca_put_str($cv, ($xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
  115. caca_put_str($cv, ($xo - strlen("==============")) / 2, 4, "==============");
  116. caca_put_str($cv, 4, 6, "demos:");
  117. caca_put_str($cv, 4, 7, "'f': full");
  118. caca_put_str($cv, 4, 8, "'1': dots");
  119. caca_put_str($cv, 4, 9, "'2': lines");
  120. caca_put_str($cv, 4, 10, "'3': boxes");
  121. caca_put_str($cv, 4, 11, "'4': triangles");
  122. caca_put_str($cv, 4, 12, "'5': ellipses");
  123. caca_put_str($cv, 4, 13, "'c': colour");
  124. caca_put_str($cv, 4, 14, "'r': render");
  125. if ($sprite)
  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. main();
  304. ?>