Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

463 wiersze
12 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) % 5;
  90. caca_set_dithering(dithering);
  91. display_menu();
  92. break;
  93. case 'c':
  94. demo = demo_color;
  95. break;
  96. case 'f':
  97. case 'F':
  98. demo = demo_all;
  99. break;
  100. case '1':
  101. demo = demo_dots;
  102. break;
  103. case '2':
  104. demo = demo_lines;
  105. break;
  106. case '3':
  107. demo = demo_boxes;
  108. break;
  109. case '4':
  110. demo = demo_triangles;
  111. break;
  112. case '5':
  113. demo = demo_ellipses;
  114. break;
  115. case 's':
  116. case 'S':
  117. if(sprite)
  118. demo = demo_sprites;
  119. break;
  120. }
  121. handle_event:
  122. event = caca_get_event();
  123. if(event & CACA_EVENT_KEY_PRESS)
  124. goto handle_key;
  125. else if(event)
  126. goto handle_event;
  127. caca_refresh();
  128. if(demo)
  129. caca_clear();
  130. }
  131. else if(event & CACA_EVENT_MOUSE_CLICK)
  132. {
  133. display_menu();
  134. caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
  135. caca_putstr((event & 0xff00) >> 8, event & 0xff, "|\\");
  136. caca_refresh();
  137. }
  138. if(demo)
  139. {
  140. demo();
  141. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  142. caca_draw_thin_box(1, 1, caca_get_width() - 2, caca_get_height() - 2);
  143. caca_printf(4, 1, "[%i.%i fps]----",
  144. 1000000 / caca_get_rendertime(),
  145. (10000000 / caca_get_rendertime()) % 10);
  146. caca_refresh();
  147. }
  148. }
  149. /* Clean up */
  150. caca_free_sprite(sprite);
  151. caca_end();
  152. return 0;
  153. }
  154. static void display_menu(void)
  155. {
  156. int xo = caca_get_width() - 2;
  157. int yo = caca_get_height() - 2;
  158. caca_clear();
  159. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  160. caca_draw_thin_box(1, 1, xo, yo);
  161. caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
  162. caca_putstr((xo - strlen("==============")) / 2, 4, "==============");
  163. caca_putstr(4, 6, "demos:");
  164. caca_putstr(4, 7, "'f': full");
  165. caca_putstr(4, 8, "'1': dots");
  166. caca_putstr(4, 9, "'2': lines");
  167. caca_putstr(4, 10, "'3': boxes");
  168. caca_putstr(4, 11, "'4': triangles");
  169. caca_putstr(4, 12, "'5': ellipses");
  170. caca_putstr(4, 13, "'c': colour");
  171. if(sprite)
  172. caca_putstr(4, 14, "'s': sprites");
  173. caca_putstr(4, 16, "settings:");
  174. caca_printf(4, 17, "'o': outline: %s",
  175. outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
  176. caca_printf(4, 18, "'b': drawing boundaries: %s",
  177. bounds == 0 ? "screen" : "infinite");
  178. caca_printf(4, 19, "'d': dithering (%s)",
  179. caca_get_dithering_name(dithering));
  180. caca_putstr(4, yo - 2, "'q': quit");
  181. }
  182. static void demo_all(void)
  183. {
  184. static int i = 0;
  185. int j, xo, yo, xa, ya, xb, yb, xc, yc;
  186. i++;
  187. caca_clear();
  188. /* Draw the sun */
  189. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  190. xo = caca_get_width() / 4;
  191. yo = caca_get_height() / 4 + 5 * sin(0.03*i);
  192. for(j = 0; j < 16; j++)
  193. {
  194. xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
  195. ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
  196. caca_draw_thin_line(xo, yo, xa, ya);
  197. }
  198. j = 15 + sin(0.03*i) * 8;
  199. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK);
  200. caca_fill_ellipse(xo, yo, j, j / 2, '#');
  201. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  202. caca_draw_ellipse(xo, yo, j, j / 2, '#');
  203. /* Draw the pyramid */
  204. xo = caca_get_width() * 5 / 8;
  205. yo = 2;
  206. xa = caca_get_width() / 8 + sin(0.03*i) * 5;
  207. ya = caca_get_height() / 2 + cos(0.03*i) * 5;
  208. xb = caca_get_width() - 10 - cos(0.02*i) * 10;
  209. yb = caca_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
  210. xc = caca_get_width() / 4 - sin(0.02*i) * 5;
  211. yc = caca_get_height() * 3 / 4 + cos(0.02*i) * 5;
  212. caca_set_color(CACA_COLOR_GREEN, CACA_COLOR_BLACK);
  213. caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
  214. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  215. caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
  216. caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
  217. caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  218. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  219. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  220. caca_set_color(CACA_COLOR_BLUE, CACA_COLOR_BLACK);
  221. caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
  222. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  223. caca_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
  224. /* Draw a background triangle */
  225. xa = 2;
  226. ya = 2;
  227. xb = caca_get_width() - 3;
  228. yb = caca_get_height() / 2;
  229. xc = caca_get_width() / 3;
  230. yc = caca_get_height() - 3;
  231. caca_set_color(CACA_COLOR_CYAN, CACA_COLOR_BLACK);
  232. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  233. xo = caca_get_width() / 2 + cos(0.027*i) * caca_get_width() / 3;
  234. yo = caca_get_height() / 2 - sin(0.027*i) * caca_get_height() / 2;
  235. caca_draw_thin_line(xa, ya, xo, yo);
  236. caca_draw_thin_line(xb, yb, xo, yo);
  237. caca_draw_thin_line(xc, yc, xo, yo);
  238. /* Draw a sprite on the pyramid */
  239. caca_draw_sprite(xo, yo, sprite, 0);
  240. /* Draw a trail behind the foreground sprite */
  241. for(j = i - 60; j < i; j++)
  242. {
  243. int delta = caca_rand(-5, 5);
  244. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  245. caca_putchar(caca_get_width() / 2
  246. + cos(0.02*j) * (delta + caca_get_width() / 4),
  247. caca_get_height() / 2
  248. + sin(0.02*j) * (delta + caca_get_height() / 3),
  249. '#');
  250. }
  251. /* Draw foreground sprite */
  252. caca_draw_sprite(caca_get_width() / 2 + cos(0.02*i) * caca_get_width() / 4,
  253. caca_get_height() / 2 + sin(0.02*i) * caca_get_height() / 3,
  254. sprite, 0);
  255. }
  256. static void demo_dots(void)
  257. {
  258. int xmax = caca_get_width() - 1;
  259. int ymax = caca_get_height() - 1;
  260. int i;
  261. static char chars[10] =
  262. {
  263. '+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W'
  264. };
  265. for(i = 1000; i--;)
  266. {
  267. /* Putpixel */
  268. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  269. caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax),
  270. chars[caca_rand(0, 9)]);
  271. }
  272. }
  273. static void demo_color(void)
  274. {
  275. int i, j;
  276. char buf[BUFSIZ];
  277. caca_clear();
  278. for(i = 0; i < 16; i++)
  279. {
  280. sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i));
  281. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  282. caca_putstr(4, i + (i >= 8 ? 4 : 3), buf);
  283. for(j = 0; j < 16; j++)
  284. {
  285. caca_set_color(i, j);
  286. caca_putstr((j >= 8 ? 41 : 40) + j * 2, i + (i >= 8 ? 4 : 3), "# ");
  287. }
  288. }
  289. }
  290. static void demo_lines(void)
  291. {
  292. int w = caca_get_width();
  293. int h = caca_get_height();
  294. int xa, ya, xb, yb;
  295. if(bounds)
  296. {
  297. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  298. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  299. }
  300. else
  301. {
  302. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  303. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  304. }
  305. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  306. if(outline > 1)
  307. caca_draw_thin_line(xa, ya, xb, yb);
  308. else
  309. caca_draw_line(xa, ya, xb, yb, '#');
  310. }
  311. static void demo_boxes(void)
  312. {
  313. int w = caca_get_width();
  314. int h = caca_get_height();
  315. int xa, ya, xb, yb;
  316. if(bounds)
  317. {
  318. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  319. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  320. }
  321. else
  322. {
  323. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  324. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  325. }
  326. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  327. caca_fill_box(xa, ya, xb, yb, '#');
  328. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  329. if(outline == 2)
  330. caca_draw_thin_box(xa, ya, xb, yb);
  331. else if(outline == 1)
  332. caca_draw_box(xa, ya, xb, yb, '#');
  333. }
  334. static void demo_ellipses(void)
  335. {
  336. int w = caca_get_width();
  337. int h = caca_get_height();
  338. int x, y, a, b;
  339. if(bounds)
  340. {
  341. x = caca_rand(- w, 2 * w); y = caca_rand(- h, 2 * h);
  342. a = caca_rand(0, w); b = caca_rand(0, h);
  343. }
  344. else
  345. {
  346. do
  347. {
  348. x = caca_rand(0, w); y = caca_rand(0, h);
  349. a = caca_rand(0, w); b = caca_rand(0, h);
  350. } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
  351. }
  352. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  353. caca_fill_ellipse(x, y, a, b, '#');
  354. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  355. if(outline == 2)
  356. caca_draw_thin_ellipse(x, y, a, b);
  357. else if(outline == 1)
  358. caca_draw_ellipse(x, y, a, b, '#');
  359. }
  360. static void demo_triangles(void)
  361. {
  362. int w = caca_get_width();
  363. int h = caca_get_height();
  364. int xa, ya, xb, yb, xc, yc;
  365. if(bounds)
  366. {
  367. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  368. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  369. xc = caca_rand(- w, 2 * w); yc = caca_rand(- h, 2 * h);
  370. }
  371. else
  372. {
  373. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  374. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  375. xc = caca_rand(0, w - 1); yc = caca_rand(0, h - 1);
  376. }
  377. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  378. caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  379. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  380. if(outline == 2)
  381. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  382. else if(outline == 1)
  383. caca_draw_triangle(xa, ya, xb, yb, xc, yc, '#');
  384. }
  385. static void demo_sprites(void)
  386. {
  387. caca_draw_sprite(caca_rand(0, caca_get_width() - 1),
  388. caca_rand(0, caca_get_height() - 1), sprite, 0);
  389. }