Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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