Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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(DATADIR "/caca.txt");
  58. if(!sprite)
  59. sprite = caca_load_sprite("caca.txt");
  60. if(!sprite)
  61. sprite = caca_load_sprite("examples/caca.txt");
  62. #endif
  63. /* Disable cursor */
  64. caca_set_mouse(dp, 0);
  65. /* Main menu */
  66. display_menu();
  67. caca_refresh_display(dp);
  68. /* Go ! */
  69. while(!quit)
  70. {
  71. caca_event_t ev;
  72. int menu = 0, mouse = 0, xmouse = 0, ymouse = 0;
  73. while(caca_get_event(dp, CACA_EVENT_ANY, &ev, 0))
  74. {
  75. if(demo && (caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS))
  76. {
  77. menu = 1;
  78. demo = NULL;
  79. }
  80. else if(caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
  81. {
  82. switch(caca_get_event_key_ch(&ev))
  83. {
  84. case 'q':
  85. case 'Q':
  86. case CACA_KEY_ESCAPE:
  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. caca_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. caca_set_color_ansi(cv, CACA_LIGHTGRAY, CACA_BLACK);
  142. caca_clear_canvas(cv);
  143. }
  144. }
  145. else if(caca_get_event_type(&ev) & CACA_EVENT_MOUSE_MOTION)
  146. {
  147. mouse = 1;
  148. xmouse = caca_get_event_mouse_x(&ev);
  149. ymouse = caca_get_event_mouse_y(&ev);
  150. }
  151. else if(caca_get_event_type(&ev) & 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. caca_set_color_ansi(cv, CACA_RED, CACA_BLACK);
  162. caca_put_str(cv, xmouse, ymouse, ".");
  163. caca_put_str(cv, xmouse, ymouse + 1, "|\\");
  164. }
  165. caca_refresh_display(dp);
  166. mouse = menu = 0;
  167. }
  168. if(demo)
  169. {
  170. demo();
  171. caca_set_color_ansi(cv, CACA_LIGHTGRAY, CACA_BLACK);
  172. caca_draw_thin_box(cv, 1, 1, caca_get_canvas_width(cv) - 2,
  173. caca_get_canvas_height(cv) - 2);
  174. caca_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. caca_free_sprite(sprite);
  183. #endif
  184. caca_free_display(dp);
  185. caca_free_canvas(cv);
  186. return 0;
  187. }
  188. static void display_menu(void)
  189. {
  190. int xo = caca_get_canvas_width(cv) - 2;
  191. int yo = caca_get_canvas_height(cv) - 2;
  192. caca_set_color_ansi(cv, CACA_LIGHTGRAY, CACA_BLACK);
  193. caca_clear_canvas(cv);
  194. caca_draw_thin_box(cv, 1, 1, xo, yo);
  195. caca_put_str(cv, (xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
  196. caca_put_str(cv, (xo - strlen("==============")) / 2, 4, "==============");
  197. caca_put_str(cv, 4, 6, "demos:");
  198. caca_put_str(cv, 4, 7, "'f': full");
  199. caca_put_str(cv, 4, 8, "'1': dots");
  200. caca_put_str(cv, 4, 9, "'2': lines");
  201. caca_put_str(cv, 4, 10, "'3': boxes");
  202. caca_put_str(cv, 4, 11, "'4': triangles");
  203. caca_put_str(cv, 4, 12, "'5': ellipses");
  204. caca_put_str(cv, 4, 13, "'c': colour");
  205. caca_put_str(cv, 4, 14, "'r': render");
  206. #if 0
  207. if(sprite)
  208. caca_put_str(cv, 4, 15, "'s': sprites");
  209. #endif
  210. caca_put_str(cv, 4, 16, "settings:");
  211. caca_printf(cv, 4, 17, "'o': outline: %s",
  212. outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
  213. caca_printf(cv, 4, 18, "'b': drawing boundaries: %s",
  214. bounds == 0 ? "screen" : "infinite");
  215. //caca_printf(cv, 4, 19, "'d': dithering (%s)",
  216. // caca_get_feature_name(dithering));
  217. caca_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. caca_set_color_ansi(cv, CACA_LIGHTGRAY, CACA_BLACK);
  226. caca_clear_canvas(cv);
  227. /* Draw the sun */
  228. caca_set_color_ansi(cv, CACA_YELLOW, CACA_BLACK);
  229. xo = caca_get_canvas_width(cv) / 4;
  230. yo = caca_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. caca_draw_thin_line(cv, xo, yo, xa, ya);
  236. }
  237. j = 15 + sin(0.03*i) * 8;
  238. caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK);
  239. caca_fill_ellipse(cv, xo, yo, j, j / 2, '#');
  240. caca_set_color_ansi(cv, CACA_YELLOW, CACA_BLACK);
  241. caca_draw_ellipse(cv, xo, yo, j, j / 2, '#');
  242. /* Draw the pyramid */
  243. xo = caca_get_canvas_width(cv) * 5 / 8;
  244. yo = 2;
  245. xa = caca_get_canvas_width(cv) / 8 + sin(0.03*i) * 5;
  246. ya = caca_get_canvas_height(cv) / 2 + cos(0.03*i) * 5;
  247. xb = caca_get_canvas_width(cv) - 10 - cos(0.02*i) * 10;
  248. yb = caca_get_canvas_height(cv) * 3 / 4 - 5 + sin(0.02*i) * 5;
  249. xc = caca_get_canvas_width(cv) / 4 - sin(0.02*i) * 5;
  250. yc = caca_get_canvas_height(cv) * 3 / 4 + cos(0.02*i) * 5;
  251. caca_set_color_ansi(cv, CACA_GREEN, CACA_BLACK);
  252. caca_fill_triangle(cv, xo, yo, xb, yb, xa, ya, '%');
  253. caca_set_color_ansi(cv, CACA_YELLOW, CACA_BLACK);
  254. caca_draw_thin_triangle(cv, xo, yo, xb, yb, xa, ya);
  255. caca_set_color_ansi(cv, CACA_RED, CACA_BLACK);
  256. caca_fill_triangle(cv, xa, ya, xb, yb, xc, yc, '#');
  257. caca_set_color_ansi(cv, CACA_YELLOW, CACA_BLACK);
  258. caca_draw_thin_triangle(cv, xa, ya, xb, yb, xc, yc);
  259. caca_set_color_ansi(cv, CACA_BLUE, CACA_BLACK);
  260. caca_fill_triangle(cv, xo, yo, xb, yb, xc, yc, '%');
  261. caca_set_color_ansi(cv, CACA_YELLOW, CACA_BLACK);
  262. caca_draw_thin_triangle(cv, xo, yo, xb, yb, xc, yc);
  263. /* Draw a background triangle */
  264. xa = 2;
  265. ya = 2;
  266. xb = caca_get_canvas_width(cv) - 3;
  267. yb = caca_get_canvas_height(cv) / 2;
  268. xc = caca_get_canvas_width(cv) / 3;
  269. yc = caca_get_canvas_height(cv) - 3;
  270. caca_set_color_ansi(cv, CACA_CYAN, CACA_BLACK);
  271. caca_draw_thin_triangle(cv, xa, ya, xb, yb, xc, yc);
  272. xo = caca_get_canvas_width(cv) / 2 + cos(0.027*i) * caca_get_canvas_width(cv) / 3;
  273. yo = caca_get_canvas_height(cv) / 2 - sin(0.027*i) * caca_get_canvas_height(cv) / 2;
  274. caca_draw_thin_line(cv, xa, ya, xo, yo);
  275. caca_draw_thin_line(cv, xb, yb, xo, yo);
  276. caca_draw_thin_line(cv, xc, yc, xo, yo);
  277. /* Draw a sprite on the pyramid */
  278. #if 0
  279. caca_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 = caca_rand(-5, 6);
  285. caca_set_color_ansi(cv, caca_rand(0, 16), caca_rand(0, 16));
  286. caca_put_char(cv, caca_get_canvas_width(cv) / 2
  287. + cos(0.02*j) * (delta + caca_get_canvas_width(cv) / 4),
  288. caca_get_canvas_height(cv) / 2
  289. + sin(0.02*j) * (delta + caca_get_canvas_height(cv) / 3),
  290. '#');
  291. }
  292. /* Draw foreground sprite */
  293. #if 0
  294. caca_draw_sprite(cv, caca_get_canvas_width(cv) / 2 + cos(0.02*i) * caca_get_canvas_width(cv) / 4,
  295. caca_get_canvas_height(cv) / 2 + sin(0.02*i) * caca_get_canvas_height(cv) / 3,
  296. sprite, 0);
  297. #endif
  298. }
  299. static void demo_dots(void)
  300. {
  301. int xmax = caca_get_canvas_width(cv);
  302. int ymax = caca_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. caca_set_color_ansi(cv, caca_rand(0, 16), caca_rand(0, 16));
  312. caca_put_char(cv, caca_rand(0, xmax), caca_rand(0, ymax),
  313. chars[caca_rand(0, 9)]);
  314. }
  315. }
  316. static void demo_lines(void)
  317. {
  318. int w = caca_get_canvas_width(cv);
  319. int h = caca_get_canvas_height(cv);
  320. int xa, ya, xb, yb;
  321. if(bounds)
  322. {
  323. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  324. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  325. }
  326. else
  327. {
  328. xa = caca_rand(0, w); ya = caca_rand(0, h);
  329. xb = caca_rand(0, w); yb = caca_rand(0, h);
  330. }
  331. caca_set_color_ansi(cv, caca_rand(0, 16), CACA_BLACK);
  332. if(outline > 1)
  333. caca_draw_thin_line(cv, xa, ya, xb, yb);
  334. else
  335. caca_draw_line(cv, xa, ya, xb, yb, '#');
  336. }
  337. static void demo_boxes(void)
  338. {
  339. int w = caca_get_canvas_width(cv);
  340. int h = caca_get_canvas_height(cv);
  341. int xa, ya, xb, yb;
  342. if(bounds)
  343. {
  344. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  345. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  346. }
  347. else
  348. {
  349. xa = caca_rand(0, w); ya = caca_rand(0, h);
  350. xb = caca_rand(0, w); yb = caca_rand(0, h);
  351. }
  352. caca_set_color_ansi(cv, caca_rand(0, 16), caca_rand(0, 16));
  353. caca_fill_box(cv, xa, ya, xb, yb, '#');
  354. caca_set_color_ansi(cv, caca_rand(0, 16), CACA_BLACK);
  355. if(outline == 2)
  356. caca_draw_thin_box(cv, xa, ya, xb, yb);
  357. else if(outline == 1)
  358. caca_draw_box(cv, xa, ya, xb, yb, '#');
  359. }
  360. static void demo_ellipses(void)
  361. {
  362. int w = caca_get_canvas_width(cv);
  363. int h = caca_get_canvas_height(cv);
  364. int x, y, a, b;
  365. if(bounds)
  366. {
  367. x = caca_rand(- w, 2 * w); y = caca_rand(- h, 2 * h);
  368. a = caca_rand(0, w); b = caca_rand(0, h);
  369. }
  370. else
  371. {
  372. do
  373. {
  374. x = caca_rand(0, w); y = caca_rand(0, h);
  375. a = caca_rand(0, w); b = caca_rand(0, h);
  376. } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
  377. }
  378. caca_set_color_ansi(cv, caca_rand(0, 16), caca_rand(0, 16));
  379. caca_fill_ellipse(cv, x, y, a, b, '#');
  380. caca_set_color_ansi(cv, caca_rand(0, 16), CACA_BLACK);
  381. if(outline == 2)
  382. caca_draw_thin_ellipse(cv, x, y, a, b);
  383. else if(outline == 1)
  384. caca_draw_ellipse(cv, x, y, a, b, '#');
  385. }
  386. static void demo_triangles(void)
  387. {
  388. int w = caca_get_canvas_width(cv);
  389. int h = caca_get_canvas_height(cv);
  390. int xa, ya, xb, yb, xc, yc;
  391. if(bounds)
  392. {
  393. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  394. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  395. xc = caca_rand(- w, 2 * w); yc = caca_rand(- h, 2 * h);
  396. }
  397. else
  398. {
  399. xa = caca_rand(0, w); ya = caca_rand(0, h);
  400. xb = caca_rand(0, w); yb = caca_rand(0, h);
  401. xc = caca_rand(0, w); yc = caca_rand(0, h);
  402. }
  403. caca_set_color_ansi(cv, caca_rand(0, 16), caca_rand(0, 16));
  404. caca_fill_triangle(cv, xa, ya, xb, yb, xc, yc, '#');
  405. caca_set_color_ansi(cv, caca_rand(0, 16), CACA_BLACK);
  406. if(outline == 2)
  407. caca_draw_thin_triangle(cv, xa, ya, xb, yb, xc, yc);
  408. else if(outline == 1)
  409. caca_draw_triangle(cv, xa, ya, xb, yb, xc, yc, '#');
  410. }
  411. #if 0
  412. static void demo_sprites(void)
  413. {
  414. caca_draw_sprite(cv, caca_rand(0, caca_get_canvas_width(cv)),
  415. caca_rand(0, caca_get_canvas_height(cv)), sprite, 0);
  416. }
  417. #endif
  418. #if 0
  419. static void demo_render(void)
  420. {
  421. caca_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. caca_set_dither_invert(dither, 1);
  437. //dither = caca_create_dither(16, 256, 256, 2 * 256, 0xf800, 0x07e0, 0x001f, 0x0000);
  438. dither = caca_create_dither(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  439. caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv), caca_get_canvas_height(cv),
  440. dither, buffer);
  441. caca_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. caca_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 = caca_create_dither(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  475. caca_set_dither_gamma(dither, -1.0);
  476. caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv), caca_get_canvas_height(cv), dither, (char *)buffer);
  477. caca_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. }