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

246 řádky
6.4 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. display_menu($cv, $dp);
  13. caca_set_display_time($dp, 40000);
  14. /* Disable cursor */
  15. caca_set_mouse($dp, 0);
  16. /* Main menu */
  17. display_menu($cv, $dp);
  18. caca_refresh_display($dp);
  19. /* Go ! */
  20. $bounds = $outline = $dithering = 0;
  21. $ev = caca_create_event();
  22. while(!$quit) {
  23. $menu = $mouse = $xmouse = $ymouse = 0;
  24. while (caca_get_event($dp, CACA_EVENT_ANY, $ev, 0)) {
  25. if ($demo and (caca_get_event_type($ev) & CACA_EVENT_KEY_PRESS)) {
  26. $menu = 1;
  27. $demo = false;
  28. }
  29. else if (caca_get_event_type($ev) & CACA_EVENT_KEY_PRESS) {
  30. switch (caca_get_event_key_ch($ev)) {
  31. case ord('q'):
  32. case ord('Q'):
  33. case CACA_KEY_ESCAPE:
  34. $demo = false;
  35. $quit = 1;
  36. break;
  37. case ord('o'):
  38. case ord('O'):
  39. $outline = ($outline + 1) % 3;
  40. display_menu($cv, $dp);
  41. break;
  42. case ord('b'):
  43. case ord('B'):
  44. $bounds = ($bounds + 1) % 2;
  45. display_menu($cv, $dp);
  46. break;
  47. case ord('d'):
  48. case ord('D'):
  49. $dithering = ($dithering + 1) % 5;
  50. caca_set_feature($cv, $dithering);
  51. display_menu($cv, $dp);
  52. break;
  53. case ord('f'):
  54. case ord('F'):
  55. $demo = "demo_all";
  56. break;
  57. case ord('1'):
  58. $demo = "demo_dots";
  59. break;
  60. case ord('2'):
  61. $demo = "demo_lines";
  62. break;
  63. case ord('3'):
  64. $demo = "demo_boxes";
  65. break;
  66. case ord('4'):
  67. $demo = "demo_triangles";
  68. break;
  69. case ord('5'):
  70. $demo = "demo_ellipses";
  71. break;
  72. case ord('s'):
  73. case ord('S'):
  74. $demo = "demo_sprites";
  75. break;
  76. case ord('r'):
  77. case ord('R'):
  78. $demo = "demo_render";
  79. break;
  80. }
  81. if ($demo) {
  82. caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
  83. caca_clear_canvas($cv);
  84. }
  85. }
  86. else if(caca_get_event_type($ev) & CACA_EVENT_MOUSE_MOTION) {
  87. $mouse = 1;
  88. $xmouse = caca_get_event_mouse_x($ev);
  89. $ymouse = caca_get_event_mouse_y($ev);
  90. }
  91. else if(caca_get_event_type($ev) & CACA_EVENT_RESIZE) {
  92. $mouse = 1; /* old hack */
  93. }
  94. }
  95. if ($menu || ($mouse && !$demo)) {
  96. display_menu($cv, $dp);
  97. if ($mouse && !$demo) {
  98. caca_set_color_ansi($cv, CACA_RED, CACA_BLACK);
  99. caca_put_str($cv, $xmouse, $ymouse,".");
  100. caca_put_str($cv, $xmouse, $ymouse + 1, "|\\");
  101. }
  102. caca_refresh_display($dp);
  103. $mouse = $menu = 0;
  104. }
  105. if ($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 = (1000000 / caca_get_display_time($dp)).".".((10000000 / caca_get_display_time($dp)) % 10);
  110. caca_put_str($cv, 4, 1, "[$rate fps]----");
  111. caca_refresh_display($dp);
  112. }
  113. }
  114. }
  115. function display_menu($cv, $dp) {
  116. $xo = caca_get_canvas_width($cv) - 2;
  117. $yo = caca_get_canvas_height($cv) - 2;
  118. caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
  119. caca_clear_canvas($cv);
  120. caca_draw_thin_box($cv, 1, 1, $xo, $yo);
  121. caca_put_str($cv, ($xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
  122. caca_put_str($cv, ($xo - strlen("==============")) / 2, 4, "==============");
  123. caca_put_str($cv, 4, 6, "demos:");
  124. caca_put_str($cv, 4, 7, "'f': full");
  125. caca_put_str($cv, 4, 8, "'1': dots");
  126. caca_put_str($cv, 4, 9, "'2': lines");
  127. caca_put_str($cv, 4, 10, "'3': boxes");
  128. caca_put_str($cv, 4, 11, "'4': triangles");
  129. caca_put_str($cv, 4, 12, "'5': ellipses");
  130. caca_put_str($cv, 4, 13, "'c': colour");
  131. caca_put_str($cv, 4, 14, "'r': render");
  132. if ($sprite)
  133. caca_put_str($cv, 4, 15, "'s': sprites");
  134. caca_put_str($cv, 4, 16, "settings:");
  135. caca_put_str($cv, 4, 17, "'o': outline: ".((outline == 0) ? "none" : ((outline == 1) ? "solid" : "thin")));
  136. caca_put_str($cv, 4, 18, "'b': drawing boundaries: ".((bounds == 0) ? "screen" : "infinite"));
  137. caca_put_str($cv, 4, $yo - 2, "'q': quit");
  138. caca_refresh_display($dp);
  139. }
  140. function demo_dots($cv, $bounds, $outline, $dithering) {
  141. $xmax = caca_get_canvas_width($cv);
  142. $ymax = caca_get_canvas_height($cv);
  143. $chars = array('+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W');
  144. for ($i = 1000; $i--;) {
  145. /* Putpixel */
  146. caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16));
  147. caca_put_char($cv, caca_rand(0, $xmax), caca_rand(0, $ymax), $chars[caca_rand(0, 9)]);
  148. }
  149. }
  150. function demo_lines($cv, $bounds, $outline, $dithering) {
  151. $w = caca_get_canvas_width($cv);
  152. $h = caca_get_canvas_height($cv);
  153. if ($bounds) {
  154. $xa = caca_rand(- $w, 2 * $w); $ya = caca_rand(- $h, 2 * $h);
  155. $xb = caca_rand(- $w, 2 * $w); $yb = caca_rand(- $h, 2 * $h);
  156. }
  157. else {
  158. $xa = caca_rand(0, $w); $ya = caca_rand(0, $h);
  159. $xb = caca_rand(0, $w); $yb = caca_rand(0, $h);
  160. }
  161. caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK);
  162. if ($outline > 1)
  163. caca_draw_thin_line($cv, $xa, $ya, $xb, $yb);
  164. else
  165. caca_draw_line($cv, $xa, $ya, $xb, $yb, '#');
  166. }
  167. function demo_boxes($cv, $bounds, $outline, $dithering) {
  168. $w = caca_get_canvas_width($cv);
  169. $h = caca_get_canvas_height($cv);
  170. if ($bounds) {
  171. $xa = caca_rand(- $w, 2 * $w); $ya = caca_rand(- $h, 2 * $h);
  172. $xb = caca_rand(- $w, 2 * $w); $yb = caca_rand(- $h, 2 * $h);
  173. }
  174. else {
  175. $xa = caca_rand(0, $w); $ya = caca_rand(0, $h);
  176. $xb = caca_rand(0, $w); $yb = caca_rand(0, $h);
  177. }
  178. caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16));
  179. caca_fill_box($cv, $xa, $ya, $xb, $yb, '#');
  180. caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK);
  181. if($outline == 2)
  182. caca_draw_thin_box($cv, $xa, $ya, $xb, $yb);
  183. else if($outline == 1)
  184. caca_draw_box($cv, $xa, $ya, $xb, $yb, '#');
  185. }
  186. function demo_ellipses($cv, $bounds, $outline, $dithering) {
  187. $w = caca_get_canvas_width($cv);
  188. $h = caca_get_canvas_height($cv);
  189. if ($bounds) {
  190. $x = caca_rand(- $w, 2 * $w); $y = caca_rand(- $h, 2 * $h);
  191. $a = caca_rand(0, $w); $b = caca_rand(0, $h);
  192. }
  193. else {
  194. do {
  195. $x = caca_rand(0, $w); $y = caca_rand(0, $h);
  196. $a = caca_rand(0, $w); $b = caca_rand(0, $h);
  197. } while ($x - $a < 0 || $x + $a >= $w || $y - $b < 0 || $y + $b >= $h);
  198. }
  199. caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16));
  200. caca_fill_ellipse($cv, $x, $y, $a, $b, '#');
  201. caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK);
  202. if ($outline == 2)
  203. caca_draw_thin_ellipse($cv, $x, $y, $a, $b);
  204. else if ($outline == 1)
  205. caca_draw_ellipse($cv, $x, $y, $a, $b, '#');
  206. }
  207. main();