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.
 
 
 
 
 
 

355 line
10 KiB

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