No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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