Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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