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.
 
 
 
 
 
 

591 lines
16 KiB

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