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.
 
 
 
 
 
 

457 lines
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. if(!sprite)
  55. return 1;
  56. /* Main menu */
  57. display_menu();
  58. caca_refresh();
  59. /* Go ! */
  60. while(!quit)
  61. {
  62. int event = caca_get_event();
  63. if(event && demo)
  64. {
  65. display_menu();
  66. caca_refresh();
  67. demo = NULL;
  68. }
  69. else if(event & CACA_EVENT_KEY_PRESS)
  70. {
  71. handle_key:
  72. switch(event & 0xffff)
  73. {
  74. case 'q':
  75. case 'Q':
  76. demo = NULL;
  77. quit = 1;
  78. break;
  79. case 'o':
  80. case 'O':
  81. outline = (outline + 1) % 3;
  82. display_menu();
  83. break;
  84. case 'b':
  85. case 'B':
  86. bounds = (bounds + 1) % 2;
  87. display_menu();
  88. break;
  89. case 'd':
  90. case 'D':
  91. dithering = (dithering + 1) % 3;
  92. caca_set_dithering(dithering == 0 ? CACA_DITHER_NONE :
  93. dithering == 1 ? CACA_DITHER_ORDERED :
  94. CACA_DITHER_RANDOM);
  95. display_menu();
  96. break;
  97. case 'c':
  98. demo = demo_color;
  99. break;
  100. case 'f':
  101. case 'F':
  102. demo = demo_all;
  103. break;
  104. case '1':
  105. demo = demo_dots;
  106. break;
  107. case '2':
  108. demo = demo_lines;
  109. break;
  110. case '3':
  111. demo = demo_boxes;
  112. break;
  113. case '4':
  114. demo = demo_triangles;
  115. break;
  116. case '5':
  117. demo = demo_ellipses;
  118. break;
  119. case 's':
  120. case 'S':
  121. demo = demo_sprites;
  122. break;
  123. }
  124. if(demo)
  125. caca_clear();
  126. handle_event:
  127. event = caca_get_event();
  128. if(event & CACA_EVENT_KEY_PRESS)
  129. goto handle_key;
  130. else if(event)
  131. goto handle_event;
  132. caca_refresh();
  133. }
  134. else if(event & CACA_EVENT_MOUSE_CLICK)
  135. {
  136. display_menu();
  137. caca_set_color(CACA_COLOR_RED);
  138. caca_putstr((event & 0xff00) >> 8, event & 0xff, "|\\");
  139. caca_refresh();
  140. }
  141. if(demo)
  142. {
  143. demo();
  144. caca_set_color(CACA_COLOR_WHITE);
  145. caca_draw_thin_box(1, 1, caca_get_width() - 2, caca_get_height() - 2);
  146. caca_printf(4, 1, "[%i.%i fps]----",
  147. 1000000 / caca_get_rendertime(),
  148. (10000000 / caca_get_rendertime()) % 10);
  149. caca_refresh();
  150. }
  151. }
  152. /* Clean up */
  153. caca_free_sprite(sprite);
  154. caca_end();
  155. return 0;
  156. }
  157. static void display_menu(void)
  158. {
  159. int xo = caca_get_width() - 2;
  160. int yo = caca_get_height() - 2;
  161. caca_clear();
  162. caca_set_color(CACA_COLOR_WHITE);
  163. caca_draw_thin_box(1, 1, xo, yo);
  164. caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
  165. caca_putstr((xo - strlen("==============")) / 2, 4, "==============");
  166. caca_putstr(4, 6, "demos:");
  167. caca_putstr(4, 7, "'f': full");
  168. caca_putstr(4, 8, "'1': dots");
  169. caca_putstr(4, 9, "'2': lines");
  170. caca_putstr(4, 10, "'3': boxes");
  171. caca_putstr(4, 11, "'4': triangles");
  172. caca_putstr(4, 12, "'5': ellipses");
  173. caca_putstr(4, 13, "'s': sprites");
  174. caca_putstr(4, 14, "'c': color");
  175. caca_putstr(4, 17, "settings:");
  176. caca_printf(4, 18, "'o': outline: %s",
  177. outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
  178. caca_printf(4, 19, "'b': drawing boundaries: %s",
  179. bounds == 0 ? "screen" : "infinite");
  180. caca_printf(4, 20, "'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);
  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);
  202. caca_fill_ellipse(xo, yo, j, j / 2, '#');
  203. caca_set_color(CACA_COLOR_YELLOW);
  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);
  215. caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
  216. caca_set_color(CACA_COLOR_YELLOW);
  217. caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
  218. caca_set_color(CACA_COLOR_RED);
  219. caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  220. caca_set_color(CACA_COLOR_YELLOW);
  221. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  222. caca_set_color(CACA_COLOR_BLUE);
  223. caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
  224. caca_set_color(CACA_COLOR_YELLOW);
  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);
  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));
  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. for(i = 1000; i--;)
  264. {
  265. /* Putpixel */
  266. caca_set_color(caca_rand(0, 15));
  267. caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax), '#');
  268. }
  269. }
  270. static void demo_color(void)
  271. {
  272. int i;
  273. char buf[BUFSIZ];
  274. caca_clear();
  275. for(i = 0; i < 16; i++)
  276. {
  277. sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i));
  278. caca_set_color(CACA_COLOR_WHITE);
  279. caca_putstr(4, i + 3, buf);
  280. caca_set_color(i);
  281. caca_putstr(40, i + 3, "XXXXXXXXXX-XX--X----------");
  282. }
  283. }
  284. static void demo_lines(void)
  285. {
  286. int w = caca_get_width();
  287. int h = caca_get_height();
  288. int xa, ya, xb, yb;
  289. if(bounds)
  290. {
  291. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  292. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  293. }
  294. else
  295. {
  296. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  297. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  298. }
  299. caca_set_color(caca_rand(0, 15));
  300. if(outline > 1)
  301. caca_draw_thin_line(xa, ya, xb, yb);
  302. else
  303. caca_draw_line(xa, ya, xb, yb, '#');
  304. }
  305. static void demo_boxes(void)
  306. {
  307. int w = caca_get_width();
  308. int h = caca_get_height();
  309. int xa, ya, xb, yb;
  310. if(bounds)
  311. {
  312. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  313. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  314. }
  315. else
  316. {
  317. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  318. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  319. }
  320. caca_set_color(caca_rand(0, 15));
  321. caca_fill_box(xa, ya, xb, yb, '#');
  322. caca_set_color(caca_rand(0, 15));
  323. if(outline == 2)
  324. caca_draw_thin_box(xa, ya, xb, yb);
  325. else if(outline == 1)
  326. caca_draw_box(xa, ya, xb, yb, '#');
  327. }
  328. static void demo_ellipses(void)
  329. {
  330. int w = caca_get_width();
  331. int h = caca_get_height();
  332. int x, y, a, b;
  333. if(bounds)
  334. {
  335. x = caca_rand(- w, 2 * w); y = caca_rand(- h, 2 * h);
  336. a = caca_rand(0, w); b = caca_rand(0, h);
  337. }
  338. else
  339. {
  340. do
  341. {
  342. x = caca_rand(0, w); y = caca_rand(0, h);
  343. a = caca_rand(0, w); b = caca_rand(0, h);
  344. } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
  345. }
  346. caca_set_color(caca_rand(0, 15));
  347. caca_fill_ellipse(x, y, a, b, '#');
  348. caca_set_color(caca_rand(0, 15));
  349. if(outline == 2)
  350. caca_draw_thin_ellipse(x, y, a, b);
  351. else if(outline == 1)
  352. caca_draw_ellipse(x, y, a, b, '#');
  353. }
  354. static void demo_triangles(void)
  355. {
  356. int w = caca_get_width();
  357. int h = caca_get_height();
  358. int xa, ya, xb, yb, xc, yc;
  359. if(bounds)
  360. {
  361. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  362. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  363. xc = caca_rand(- w, 2 * w); yc = caca_rand(- h, 2 * h);
  364. }
  365. else
  366. {
  367. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  368. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  369. xc = caca_rand(0, w - 1); yc = caca_rand(0, h - 1);
  370. }
  371. caca_set_color(caca_rand(0, 15));
  372. caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  373. caca_set_color(caca_rand(0, 15));
  374. if(outline == 2)
  375. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  376. else if(outline == 1)
  377. caca_draw_triangle(xa, ya, xb, yb, xc, yc, '#');
  378. }
  379. static void demo_sprites(void)
  380. {
  381. caca_draw_sprite(caca_rand(0, caca_get_width() - 1),
  382. caca_rand(0, caca_get_height() - 1), sprite, 0);
  383. }