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.
 
 
 
 
 
 

608 rivejä
18 KiB

  1. /*
  2. * demo demo for 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 Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include "common.h"
  15. #if !defined(__KERNEL__)
  16. # include <math.h>
  17. # include <string.h>
  18. # include <stdio.h>
  19. #endif
  20. #include "caca.h"
  21. static void display_menu(void);
  22. static void demo_all(void);
  23. static void demo_color(void);
  24. static void demo_dots(void);
  25. static void demo_lines(void);
  26. static void demo_boxes(void);
  27. static void demo_ellipses(void);
  28. static void demo_triangles(void);
  29. /*static void demo_sprites(void);*/
  30. static void demo_render(void);
  31. int bounds = 0;
  32. int outline = 0;
  33. int dithering = 0;
  34. #if 0
  35. cucul_sprite_t *sprite = NULL;
  36. #endif
  37. cucul_canvas_t *cv;
  38. caca_display_t *dp;
  39. int main(int argc, char **argv)
  40. {
  41. void (*demo)(void) = NULL;
  42. int quit = 0;
  43. cv = cucul_create_canvas(0, 0);
  44. if(!cv)
  45. return 1;
  46. dp = caca_create_display(cv);
  47. if(!dp)
  48. return 1;
  49. caca_set_display_time(dp, 40000);
  50. /* Initialize data */
  51. #if 0
  52. sprite = cucul_load_sprite(DATADIR "/caca.txt");
  53. if(!sprite)
  54. sprite = cucul_load_sprite("caca.txt");
  55. if(!sprite)
  56. sprite = cucul_load_sprite("examples/caca.txt");
  57. #endif
  58. /* Disable cursor */
  59. caca_set_mouse(dp, 0);
  60. /* Main menu */
  61. display_menu();
  62. caca_refresh_display(dp);
  63. /* Go ! */
  64. while(!quit)
  65. {
  66. caca_event_t ev;
  67. int menu = 0, mouse = 0, xmouse = 0, ymouse = 0;
  68. while(caca_get_event(dp, CACA_EVENT_ANY, &ev, 0))
  69. {
  70. if(demo && (ev.type & CACA_EVENT_KEY_PRESS))
  71. {
  72. menu = 1;
  73. demo = NULL;
  74. }
  75. else if(ev.type & CACA_EVENT_KEY_PRESS)
  76. {
  77. switch(ev.data.key.ch)
  78. {
  79. case 'q':
  80. case 'Q':
  81. demo = NULL;
  82. quit = 1;
  83. break;
  84. case 'o':
  85. case 'O':
  86. outline = (outline + 1) % 3;
  87. display_menu();
  88. break;
  89. case 'b':
  90. case 'B':
  91. bounds = (bounds + 1) % 2;
  92. display_menu();
  93. break;
  94. #if 0
  95. case 'd':
  96. case 'D':
  97. dithering = (dithering + 1) % 5;
  98. cucul_set_feature(cv, dithering);
  99. display_menu();
  100. break;
  101. #endif
  102. case 'c':
  103. demo = demo_color;
  104. break;
  105. case 'f':
  106. case 'F':
  107. demo = demo_all;
  108. break;
  109. case '1':
  110. demo = demo_dots;
  111. break;
  112. case '2':
  113. demo = demo_lines;
  114. break;
  115. case '3':
  116. demo = demo_boxes;
  117. break;
  118. case '4':
  119. demo = demo_triangles;
  120. break;
  121. case '5':
  122. demo = demo_ellipses;
  123. break;
  124. #if 0
  125. case 's':
  126. case 'S':
  127. if(sprite)
  128. demo = demo_sprites;
  129. break;
  130. #endif
  131. case 'r':
  132. case 'R':
  133. demo = demo_render;
  134. break;
  135. }
  136. if(demo)
  137. {
  138. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_LIGHTGRAY,
  139. CUCUL_BLACK));
  140. cucul_clear_canvas(cv);
  141. }
  142. }
  143. else if(ev.type & CACA_EVENT_MOUSE_MOTION)
  144. {
  145. mouse = 1;
  146. xmouse = ev.data.mouse.x;
  147. ymouse = ev.data.mouse.y;
  148. }
  149. else if(ev.type & CACA_EVENT_RESIZE)
  150. {
  151. mouse = 1; /* old hack */
  152. }
  153. }
  154. if(menu || (mouse && !demo))
  155. {
  156. display_menu();
  157. if(mouse && !demo)
  158. {
  159. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_RED, CUCUL_BLACK));
  160. cucul_putstr(cv, xmouse, ymouse, ".");
  161. cucul_putstr(cv, xmouse, ymouse + 1, "|\\");
  162. }
  163. caca_refresh_display(dp);
  164. mouse = menu = 0;
  165. }
  166. if(demo)
  167. {
  168. demo();
  169. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_LIGHTGRAY,
  170. CUCUL_BLACK));
  171. cucul_draw_thin_box(cv, 1, 1, cucul_get_canvas_width(cv) - 2,
  172. cucul_get_canvas_height(cv) - 2);
  173. cucul_printf(cv, 4, 1, "[%i.%i fps]----",
  174. 1000000 / caca_get_display_time(dp),
  175. (10000000 / caca_get_display_time(dp)) % 10);
  176. caca_refresh_display(dp);
  177. }
  178. }
  179. /* Clean up */
  180. #if 0
  181. cucul_free_sprite(sprite);
  182. #endif
  183. caca_free_display(dp);
  184. cucul_free_canvas(cv);
  185. return 0;
  186. }
  187. static void display_menu(void)
  188. {
  189. int xo = cucul_get_canvas_width(cv) - 2;
  190. int yo = cucul_get_canvas_height(cv) - 2;
  191. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_LIGHTGRAY, CUCUL_BLACK));
  192. cucul_clear_canvas(cv);
  193. cucul_draw_thin_box(cv, 1, 1, xo, yo);
  194. cucul_putstr(cv, (xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
  195. cucul_putstr(cv, (xo - strlen("==============")) / 2, 4, "==============");
  196. cucul_putstr(cv, 4, 6, "demos:");
  197. cucul_putstr(cv, 4, 7, "'f': full");
  198. cucul_putstr(cv, 4, 8, "'1': dots");
  199. cucul_putstr(cv, 4, 9, "'2': lines");
  200. cucul_putstr(cv, 4, 10, "'3': boxes");
  201. cucul_putstr(cv, 4, 11, "'4': triangles");
  202. cucul_putstr(cv, 4, 12, "'5': ellipses");
  203. cucul_putstr(cv, 4, 13, "'c': colour");
  204. cucul_putstr(cv, 4, 14, "'r': render");
  205. #if 0
  206. if(sprite)
  207. cucul_putstr(cv, 4, 15, "'s': sprites");
  208. #endif
  209. cucul_putstr(cv, 4, 16, "settings:");
  210. cucul_printf(cv, 4, 17, "'o': outline: %s",
  211. outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
  212. cucul_printf(cv, 4, 18, "'b': drawing boundaries: %s",
  213. bounds == 0 ? "screen" : "infinite");
  214. //cucul_printf(cv, 4, 19, "'d': dithering (%s)",
  215. // cucul_get_feature_name(dithering));
  216. cucul_putstr(cv, 4, yo - 2, "'q': quit");
  217. caca_refresh_display(dp);
  218. }
  219. static void demo_all(void)
  220. {
  221. static int i = 0;
  222. int j, xo, yo, xa, ya, xb, yb, xc, yc;
  223. i++;
  224. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_LIGHTGRAY, CUCUL_BLACK));
  225. cucul_clear_canvas(cv);
  226. /* Draw the sun */
  227. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_YELLOW, CUCUL_BLACK));
  228. xo = cucul_get_canvas_width(cv) / 4;
  229. yo = cucul_get_canvas_height(cv) / 4 + 5 * sin(0.03*i);
  230. for(j = 0; j < 16; j++)
  231. {
  232. xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
  233. ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
  234. cucul_draw_thin_line(cv, xo, yo, xa, ya);
  235. }
  236. j = 15 + sin(0.03*i) * 8;
  237. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_WHITE, CUCUL_BLACK));
  238. cucul_fill_ellipse(cv, xo, yo, j, j / 2, "#");
  239. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_YELLOW, CUCUL_BLACK));
  240. cucul_draw_ellipse(cv, xo, yo, j, j / 2, "#");
  241. /* Draw the pyramid */
  242. xo = cucul_get_canvas_width(cv) * 5 / 8;
  243. yo = 2;
  244. xa = cucul_get_canvas_width(cv) / 8 + sin(0.03*i) * 5;
  245. ya = cucul_get_canvas_height(cv) / 2 + cos(0.03*i) * 5;
  246. xb = cucul_get_canvas_width(cv) - 10 - cos(0.02*i) * 10;
  247. yb = cucul_get_canvas_height(cv) * 3 / 4 - 5 + sin(0.02*i) * 5;
  248. xc = cucul_get_canvas_width(cv) / 4 - sin(0.02*i) * 5;
  249. yc = cucul_get_canvas_height(cv) * 3 / 4 + cos(0.02*i) * 5;
  250. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_GREEN, CUCUL_BLACK));
  251. cucul_fill_triangle(cv, xo, yo, xb, yb, xa, ya, "%");
  252. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_YELLOW, CUCUL_BLACK));
  253. cucul_draw_thin_triangle(cv, xo, yo, xb, yb, xa, ya);
  254. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_RED, CUCUL_BLACK));
  255. cucul_fill_triangle(cv, xa, ya, xb, yb, xc, yc, "#");
  256. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_YELLOW, CUCUL_BLACK));
  257. cucul_draw_thin_triangle(cv, xa, ya, xb, yb, xc, yc);
  258. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_BLUE, CUCUL_BLACK));
  259. cucul_fill_triangle(cv, xo, yo, xb, yb, xc, yc, "%");
  260. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_YELLOW, CUCUL_BLACK));
  261. cucul_draw_thin_triangle(cv, xo, yo, xb, yb, xc, yc);
  262. /* Draw a background triangle */
  263. xa = 2;
  264. ya = 2;
  265. xb = cucul_get_canvas_width(cv) - 3;
  266. yb = cucul_get_canvas_height(cv) / 2;
  267. xc = cucul_get_canvas_width(cv) / 3;
  268. yc = cucul_get_canvas_height(cv) - 3;
  269. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_CYAN, CUCUL_BLACK));
  270. cucul_draw_thin_triangle(cv, xa, ya, xb, yb, xc, yc);
  271. xo = cucul_get_canvas_width(cv) / 2 + cos(0.027*i) * cucul_get_canvas_width(cv) / 3;
  272. yo = cucul_get_canvas_height(cv) / 2 - sin(0.027*i) * cucul_get_canvas_height(cv) / 2;
  273. cucul_draw_thin_line(cv, xa, ya, xo, yo);
  274. cucul_draw_thin_line(cv, xb, yb, xo, yo);
  275. cucul_draw_thin_line(cv, xc, yc, xo, yo);
  276. /* Draw a sprite on the pyramid */
  277. #if 0
  278. cucul_draw_sprite(cv, xo, yo, sprite, 0);
  279. #endif
  280. /* Draw a trail behind the foreground sprite */
  281. for(j = i - 60; j < i; j++)
  282. {
  283. int delta = cucul_rand(-5, 6);
  284. cucul_set_attr(cv, cucul_ansi_to_attr(cucul_rand(0, 16),
  285. cucul_rand(0, 16)));
  286. cucul_putchar(cv, cucul_get_canvas_width(cv) / 2
  287. + cos(0.02*j) * (delta + cucul_get_canvas_width(cv) / 4),
  288. cucul_get_canvas_height(cv) / 2
  289. + sin(0.02*j) * (delta + cucul_get_canvas_height(cv) / 3),
  290. '#');
  291. }
  292. /* Draw foreground sprite */
  293. #if 0
  294. cucul_draw_sprite(cv, cucul_get_canvas_width(cv) / 2 + cos(0.02*i) * cucul_get_canvas_width(cv) / 4,
  295. cucul_get_canvas_height(cv) / 2 + sin(0.02*i) * cucul_get_canvas_height(cv) / 3,
  296. sprite, 0);
  297. #endif
  298. }
  299. static void demo_dots(void)
  300. {
  301. int xmax = cucul_get_canvas_width(cv);
  302. int ymax = cucul_get_canvas_height(cv);
  303. int i;
  304. static char chars[10] =
  305. {
  306. '+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W'
  307. };
  308. for(i = 1000; i--;)
  309. {
  310. /* Putpixel */
  311. cucul_set_attr(cv, cucul_ansi_to_attr(cucul_rand(0, 16),
  312. cucul_rand(0, 16)));
  313. cucul_putchar(cv, cucul_rand(0, xmax), cucul_rand(0, ymax),
  314. chars[cucul_rand(0, 9)]);
  315. }
  316. }
  317. static void demo_color(void)
  318. {
  319. int i, j;
  320. char buf[BUFSIZ];
  321. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_LIGHTGRAY, CUCUL_BLACK));
  322. cucul_clear_canvas(cv);
  323. for(i = 0; i < 16; i++)
  324. {
  325. sprintf(buf, "'%c': %i (%s)", 'a' + i, i, cucul_get_color_name(i));
  326. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_LIGHTGRAY, CUCUL_BLACK));
  327. cucul_putstr(cv, 4, i + (i >= 8 ? 4 : 3), buf);
  328. for(j = 0; j < 16; j++)
  329. {
  330. cucul_set_attr(cv, cucul_ansi_to_attr(i, j));
  331. cucul_putstr(cv, (j >= 8 ? 41 : 40) + j * 2, i + (i >= 8 ? 4 : 3), "# ");
  332. }
  333. }
  334. }
  335. static void demo_lines(void)
  336. {
  337. int w = cucul_get_canvas_width(cv);
  338. int h = cucul_get_canvas_height(cv);
  339. int xa, ya, xb, yb;
  340. if(bounds)
  341. {
  342. xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h);
  343. xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h);
  344. }
  345. else
  346. {
  347. xa = cucul_rand(0, w); ya = cucul_rand(0, h);
  348. xb = cucul_rand(0, w); yb = cucul_rand(0, h);
  349. }
  350. cucul_set_attr(cv, cucul_ansi_to_attr(cucul_rand(0, 16), CUCUL_BLACK));
  351. if(outline > 1)
  352. cucul_draw_thin_line(cv, xa, ya, xb, yb);
  353. else
  354. cucul_draw_line(cv, xa, ya, xb, yb, "#");
  355. }
  356. static void demo_boxes(void)
  357. {
  358. int w = cucul_get_canvas_width(cv);
  359. int h = cucul_get_canvas_height(cv);
  360. int xa, ya, xb, yb;
  361. if(bounds)
  362. {
  363. xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h);
  364. xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h);
  365. }
  366. else
  367. {
  368. xa = cucul_rand(0, w); ya = cucul_rand(0, h);
  369. xb = cucul_rand(0, w); yb = cucul_rand(0, h);
  370. }
  371. cucul_set_attr(cv, cucul_ansi_to_attr(cucul_rand(0, 16),
  372. cucul_rand(0, 16)));
  373. cucul_fill_box(cv, xa, ya, xb, yb, "#");
  374. cucul_set_attr(cv, cucul_ansi_to_attr(cucul_rand(0, 16), CUCUL_BLACK));
  375. if(outline == 2)
  376. cucul_draw_thin_box(cv, xa, ya, xb, yb);
  377. else if(outline == 1)
  378. cucul_draw_box(cv, xa, ya, xb, yb, "#");
  379. }
  380. static void demo_ellipses(void)
  381. {
  382. int w = cucul_get_canvas_width(cv);
  383. int h = cucul_get_canvas_height(cv);
  384. int x, y, a, b;
  385. if(bounds)
  386. {
  387. x = cucul_rand(- w, 2 * w); y = cucul_rand(- h, 2 * h);
  388. a = cucul_rand(0, w); b = cucul_rand(0, h);
  389. }
  390. else
  391. {
  392. do
  393. {
  394. x = cucul_rand(0, w); y = cucul_rand(0, h);
  395. a = cucul_rand(0, w); b = cucul_rand(0, h);
  396. } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
  397. }
  398. cucul_set_attr(cv, cucul_ansi_to_attr(cucul_rand(0, 16),
  399. cucul_rand(0, 16)));
  400. cucul_fill_ellipse(cv, x, y, a, b, "#");
  401. cucul_set_attr(cv, cucul_ansi_to_attr(cucul_rand(0, 16), CUCUL_BLACK));
  402. if(outline == 2)
  403. cucul_draw_thin_ellipse(cv, x, y, a, b);
  404. else if(outline == 1)
  405. cucul_draw_ellipse(cv, x, y, a, b, "#");
  406. }
  407. static void demo_triangles(void)
  408. {
  409. int w = cucul_get_canvas_width(cv);
  410. int h = cucul_get_canvas_height(cv);
  411. int xa, ya, xb, yb, xc, yc;
  412. if(bounds)
  413. {
  414. xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h);
  415. xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h);
  416. xc = cucul_rand(- w, 2 * w); yc = cucul_rand(- h, 2 * h);
  417. }
  418. else
  419. {
  420. xa = cucul_rand(0, w); ya = cucul_rand(0, h);
  421. xb = cucul_rand(0, w); yb = cucul_rand(0, h);
  422. xc = cucul_rand(0, w); yc = cucul_rand(0, h);
  423. }
  424. cucul_set_attr(cv, cucul_ansi_to_attr(cucul_rand(0, 16),
  425. cucul_rand(0, 16)));
  426. cucul_fill_triangle(cv, xa, ya, xb, yb, xc, yc, "#");
  427. cucul_set_attr(cv, cucul_ansi_to_attr(cucul_rand(0, 16), CUCUL_BLACK));
  428. if(outline == 2)
  429. cucul_draw_thin_triangle(cv, xa, ya, xb, yb, xc, yc);
  430. else if(outline == 1)
  431. cucul_draw_triangle(cv, xa, ya, xb, yb, xc, yc, "#");
  432. }
  433. #if 0
  434. static void demo_sprites(void)
  435. {
  436. cucul_draw_sprite(cv, cucul_rand(0, cucul_get_canvas_width(cv)),
  437. cucul_rand(0, cucul_get_canvas_height(cv)), sprite, 0);
  438. }
  439. #endif
  440. #if 0
  441. static void demo_render(void)
  442. {
  443. cucul_dither_t *dither;
  444. //short buffer[256*256];
  445. //short *dest = buffer;
  446. int buffer[256*256];
  447. int *dest = buffer;
  448. int x, y, z;
  449. static int i = 0;
  450. i = (i + 1) % 512;
  451. z = i < 256 ? i : 511 - i;
  452. for(x = 0; x < 256; x++)
  453. for(y = 0; y < 256; y++)
  454. {
  455. //*dest++ = ((x >> 3) << 11) | ((y >> 2) << 5) | ((z >> 3));
  456. *dest++ = (x << 16) | (y << 8) | (z);
  457. }
  458. cucul_set_dither_invert(dither, 1);
  459. //dither = cucul_create_dither(16, 256, 256, 2 * 256, 0xf800, 0x07e0, 0x001f, 0x0000);
  460. dither = cucul_create_dither(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  461. cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv), cucul_get_canvas_height(cv),
  462. dither, buffer);
  463. cucul_free_dither(dither);
  464. }
  465. #endif
  466. static void draw_circle(int *buffer, int xo, int yo, int r, int mask, int val);
  467. static void demo_render(void)
  468. {
  469. cucul_dither_t *dither;
  470. int buffer[256*256];
  471. int *dest;
  472. int x, y, z, xo, yo;
  473. static int i = 0;
  474. i++;
  475. dest = buffer;
  476. for(x = 0; x < 256; x++)
  477. for(y = 0; y < 256; y++)
  478. {
  479. *dest++ = 0xff000000;
  480. }
  481. /* red */
  482. xo = 128 + 48 * sin(0.02 * i);
  483. yo = 128 + 48 * cos(0.03 * i);
  484. for(z = 0; z < 240; z++)
  485. draw_circle(buffer, xo, yo, z, 0x00ff0000, 200 << 16);
  486. /* green */
  487. xo = 128 + 48 * sin(2 + 0.06 * i);
  488. yo = 128 + 48 * cos(2 + 0.05 * i);
  489. for(z = 0; z < 240; z++)
  490. draw_circle(buffer, xo, yo, z, 0x0000ff00, 200 << 8);
  491. /* blue */
  492. xo = 128 + 48 * sin(1 + 0.04 * i);
  493. yo = 128 + 48 * cos(1 + 0.03 * i);
  494. for(z = 0; z < 240; z++)
  495. draw_circle(buffer, xo, yo, z, 0x000000ff, 200);
  496. dither = cucul_create_dither(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  497. cucul_set_dither_invert(dither, 1);
  498. cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv), cucul_get_canvas_height(cv), dither, (char *)buffer);
  499. cucul_free_dither(dither);
  500. }
  501. static void draw_circle(int *buffer, int x, int y, int r, int mask, int val)
  502. {
  503. int t, dx, dy;
  504. #define POINT(X,Y) \
  505. buffer[(X) + 256 * (Y)] = 0xff000000 | (buffer[(X) + 256 * (Y)] & ~mask) | val;
  506. for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
  507. {
  508. POINT(x - dx / 3, y - dy / 3);
  509. POINT(x + dx / 3, y - dy / 3);
  510. POINT(x - dx / 3, y + dy / 3);
  511. POINT(x + dx / 3, y + dy / 3);
  512. POINT(x - dy / 3, y - dx / 3);
  513. POINT(x + dy / 3, y - dx / 3);
  514. POINT(x - dy / 3, y + dx / 3);
  515. POINT(x + dy / 3, y + dx / 3);
  516. t += t > 0 ? dx - dy-- : dx;
  517. }
  518. }