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.
 
 
 
 
 
 

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