No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

465 líneas
13 KiB

  1. /*
  2. * demo demo using libcaca
  3. * Copyright (c) 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "config.h"
  24. #include <math.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include "caca.h"
  28. static void display_menu(void);
  29. static void demo_all(void);
  30. static void demo_color(void);
  31. static void demo_dots(void);
  32. static void demo_lines(void);
  33. static void demo_boxes(void);
  34. static void demo_ellipses(void);
  35. static void demo_triangles(void);
  36. static void demo_sprites(void);
  37. int bounds = 0;
  38. int outline = 0;
  39. int dithering = 0;
  40. struct caca_sprite *sprite = NULL;
  41. int main(int argc, char **argv)
  42. {
  43. void (*demo)(void) = NULL;
  44. int quit = 0;
  45. if(caca_init())
  46. return 1;
  47. caca_set_delay(40000);
  48. /* Initialize data */
  49. sprite = caca_load_sprite(DATADIR "/caca.txt");
  50. if(!sprite)
  51. sprite = caca_load_sprite("caca.txt");
  52. if(!sprite)
  53. sprite = caca_load_sprite("examples/caca.txt");
  54. /* Main menu */
  55. display_menu();
  56. caca_refresh();
  57. /* Go ! */
  58. while(!quit)
  59. {
  60. int event = caca_get_event();
  61. if(event && demo)
  62. {
  63. display_menu();
  64. caca_refresh();
  65. demo = NULL;
  66. }
  67. else if(event & CACA_EVENT_KEY_PRESS)
  68. {
  69. handle_key:
  70. switch(event & 0xffff)
  71. {
  72. case 'q':
  73. case 'Q':
  74. demo = NULL;
  75. quit = 1;
  76. break;
  77. case 'o':
  78. case 'O':
  79. outline = (outline + 1) % 3;
  80. display_menu();
  81. break;
  82. case 'b':
  83. case 'B':
  84. bounds = (bounds + 1) % 2;
  85. display_menu();
  86. break;
  87. case 'd':
  88. case 'D':
  89. dithering = (dithering + 1) % 3;
  90. caca_set_dithering(dithering == 0 ? CACA_DITHER_NONE :
  91. dithering == 1 ? CACA_DITHER_ORDERED :
  92. CACA_DITHER_RANDOM);
  93. display_menu();
  94. break;
  95. case 'c':
  96. demo = demo_color;
  97. break;
  98. case 'f':
  99. case 'F':
  100. demo = demo_all;
  101. break;
  102. case '1':
  103. demo = demo_dots;
  104. break;
  105. case '2':
  106. demo = demo_lines;
  107. break;
  108. case '3':
  109. demo = demo_boxes;
  110. break;
  111. case '4':
  112. demo = demo_triangles;
  113. break;
  114. case '5':
  115. demo = demo_ellipses;
  116. break;
  117. case 's':
  118. case 'S':
  119. if(sprite)
  120. demo = demo_sprites;
  121. break;
  122. }
  123. handle_event:
  124. event = caca_get_event();
  125. if(event & CACA_EVENT_KEY_PRESS)
  126. goto handle_key;
  127. else if(event)
  128. goto handle_event;
  129. caca_refresh();
  130. if(demo)
  131. caca_clear();
  132. }
  133. else if(event & CACA_EVENT_MOUSE_CLICK)
  134. {
  135. display_menu();
  136. caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
  137. caca_putstr((event & 0xff00) >> 8, event & 0xff, "|\\");
  138. caca_refresh();
  139. }
  140. if(demo)
  141. {
  142. demo();
  143. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  144. caca_draw_thin_box(1, 1, caca_get_width() - 2, caca_get_height() - 2);
  145. caca_printf(4, 1, "[%i.%i fps]----",
  146. 1000000 / caca_get_rendertime(),
  147. (10000000 / caca_get_rendertime()) % 10);
  148. caca_refresh();
  149. }
  150. }
  151. /* Clean up */
  152. caca_free_sprite(sprite);
  153. caca_end();
  154. return 0;
  155. }
  156. static void display_menu(void)
  157. {
  158. int xo = caca_get_width() - 2;
  159. int yo = caca_get_height() - 2;
  160. caca_clear();
  161. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  162. caca_draw_thin_box(1, 1, xo, yo);
  163. caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
  164. caca_putstr((xo - strlen("==============")) / 2, 4, "==============");
  165. caca_putstr(4, 6, "demos:");
  166. caca_putstr(4, 7, "'f': full");
  167. caca_putstr(4, 8, "'1': dots");
  168. caca_putstr(4, 9, "'2': lines");
  169. caca_putstr(4, 10, "'3': boxes");
  170. caca_putstr(4, 11, "'4': triangles");
  171. caca_putstr(4, 12, "'5': ellipses");
  172. caca_putstr(4, 13, "'c': colour");
  173. if(sprite)
  174. caca_putstr(4, 14, "'s': sprites");
  175. caca_putstr(4, 16, "settings:");
  176. caca_printf(4, 17, "'o': outline: %s",
  177. outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
  178. caca_printf(4, 18, "'b': drawing boundaries: %s",
  179. bounds == 0 ? "screen" : "infinite");
  180. caca_printf(4, 19, "'d': %s dithering",
  181. dithering == 0 ? "no" : dithering == 1 ? "ordered" : "random");
  182. caca_putstr(4, yo - 2, "'q': quit");
  183. }
  184. static void demo_all(void)
  185. {
  186. static int i = 0;
  187. int j, xo, yo, xa, ya, xb, yb, xc, yc;
  188. i++;
  189. caca_clear();
  190. /* Draw the sun */
  191. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  192. xo = caca_get_width() / 4;
  193. yo = caca_get_height() / 4 + 5 * sin(0.03*i);
  194. for(j = 0; j < 16; j++)
  195. {
  196. xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
  197. ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
  198. caca_draw_thin_line(xo, yo, xa, ya);
  199. }
  200. j = 15 + sin(0.03*i) * 8;
  201. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK);
  202. caca_fill_ellipse(xo, yo, j, j / 2, '#');
  203. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  204. caca_draw_ellipse(xo, yo, j, j / 2, '#');
  205. /* Draw the pyramid */
  206. xo = caca_get_width() * 5 / 8;
  207. yo = 2;
  208. xa = caca_get_width() / 8 + sin(0.03*i) * 5;
  209. ya = caca_get_height() / 2 + cos(0.03*i) * 5;
  210. xb = caca_get_width() - 10 - cos(0.02*i) * 10;
  211. yb = caca_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
  212. xc = caca_get_width() / 4 - sin(0.02*i) * 5;
  213. yc = caca_get_height() * 3 / 4 + cos(0.02*i) * 5;
  214. caca_set_color(CACA_COLOR_GREEN, CACA_COLOR_BLACK);
  215. caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
  216. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  217. caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
  218. caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
  219. caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  220. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  221. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  222. caca_set_color(CACA_COLOR_BLUE, CACA_COLOR_BLACK);
  223. caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
  224. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  225. caca_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
  226. /* Draw a background triangle */
  227. xa = 2;
  228. ya = 2;
  229. xb = caca_get_width() - 3;
  230. yb = caca_get_height() / 2;
  231. xc = caca_get_width() / 3;
  232. yc = caca_get_height() - 3;
  233. caca_set_color(CACA_COLOR_CYAN, CACA_COLOR_BLACK);
  234. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  235. xo = caca_get_width() / 2 + cos(0.027*i) * caca_get_width() / 3;
  236. yo = caca_get_height() / 2 - sin(0.027*i) * caca_get_height() / 2;
  237. caca_draw_thin_line(xa, ya, xo, yo);
  238. caca_draw_thin_line(xb, yb, xo, yo);
  239. caca_draw_thin_line(xc, yc, xo, yo);
  240. /* Draw a sprite on the pyramid */
  241. caca_draw_sprite(xo, yo, sprite, 0);
  242. /* Draw a trail behind the foreground sprite */
  243. for(j = i - 60; j < i; j++)
  244. {
  245. int delta = caca_rand(-5, 5);
  246. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  247. caca_putchar(caca_get_width() / 2
  248. + cos(0.02*j) * (delta + caca_get_width() / 4),
  249. caca_get_height() / 2
  250. + sin(0.02*j) * (delta + caca_get_height() / 3),
  251. '#');
  252. }
  253. /* Draw foreground sprite */
  254. caca_draw_sprite(caca_get_width() / 2 + cos(0.02*i) * caca_get_width() / 4,
  255. caca_get_height() / 2 + sin(0.02*i) * caca_get_height() / 3,
  256. sprite, 0);
  257. }
  258. static void demo_dots(void)
  259. {
  260. int xmax = caca_get_width() - 1;
  261. int ymax = caca_get_height() - 1;
  262. int i;
  263. static char chars[10] =
  264. {
  265. '+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W'
  266. };
  267. for(i = 1000; i--;)
  268. {
  269. /* Putpixel */
  270. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  271. caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax),
  272. chars[caca_rand(0, 9)]);
  273. }
  274. }
  275. static void demo_color(void)
  276. {
  277. int i, j;
  278. char buf[BUFSIZ];
  279. caca_clear();
  280. for(i = 0; i < 16; i++)
  281. {
  282. sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i));
  283. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  284. caca_putstr(4, i + (i >= 8 ? 4 : 3), buf);
  285. for(j = 0; j < 16; j++)
  286. {
  287. caca_set_color(i, j);
  288. caca_putstr((j >= 8 ? 41 : 40) + j * 2, i + (i >= 8 ? 4 : 3), "# ");
  289. }
  290. }
  291. }
  292. static void demo_lines(void)
  293. {
  294. int w = caca_get_width();
  295. int h = caca_get_height();
  296. int xa, ya, xb, yb;
  297. if(bounds)
  298. {
  299. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  300. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  301. }
  302. else
  303. {
  304. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  305. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  306. }
  307. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  308. if(outline > 1)
  309. caca_draw_thin_line(xa, ya, xb, yb);
  310. else
  311. caca_draw_line(xa, ya, xb, yb, '#');
  312. }
  313. static void demo_boxes(void)
  314. {
  315. int w = caca_get_width();
  316. int h = caca_get_height();
  317. int xa, ya, xb, yb;
  318. if(bounds)
  319. {
  320. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  321. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  322. }
  323. else
  324. {
  325. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  326. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  327. }
  328. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  329. caca_fill_box(xa, ya, xb, yb, '#');
  330. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  331. if(outline == 2)
  332. caca_draw_thin_box(xa, ya, xb, yb);
  333. else if(outline == 1)
  334. caca_draw_box(xa, ya, xb, yb, '#');
  335. }
  336. static void demo_ellipses(void)
  337. {
  338. int w = caca_get_width();
  339. int h = caca_get_height();
  340. int x, y, a, b;
  341. if(bounds)
  342. {
  343. x = caca_rand(- w, 2 * w); y = caca_rand(- h, 2 * h);
  344. a = caca_rand(0, w); b = caca_rand(0, h);
  345. }
  346. else
  347. {
  348. do
  349. {
  350. x = caca_rand(0, w); y = caca_rand(0, h);
  351. a = caca_rand(0, w); b = caca_rand(0, h);
  352. } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
  353. }
  354. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  355. caca_fill_ellipse(x, y, a, b, '#');
  356. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  357. if(outline == 2)
  358. caca_draw_thin_ellipse(x, y, a, b);
  359. else if(outline == 1)
  360. caca_draw_ellipse(x, y, a, b, '#');
  361. }
  362. static void demo_triangles(void)
  363. {
  364. int w = caca_get_width();
  365. int h = caca_get_height();
  366. int xa, ya, xb, yb, xc, yc;
  367. if(bounds)
  368. {
  369. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  370. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  371. xc = caca_rand(- w, 2 * w); yc = caca_rand(- h, 2 * h);
  372. }
  373. else
  374. {
  375. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  376. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  377. xc = caca_rand(0, w - 1); yc = caca_rand(0, h - 1);
  378. }
  379. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  380. caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  381. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  382. if(outline == 2)
  383. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  384. else if(outline == 1)
  385. caca_draw_triangle(xa, ya, xb, yb, xc, yc, '#');
  386. }
  387. static void demo_sprites(void)
  388. {
  389. caca_draw_sprite(caca_rand(0, caca_get_width() - 1),
  390. caca_rand(0, caca_get_height() - 1), sprite, 0);
  391. }