Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

demo.c 16 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include <math.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include "caca.h"
  18. static void display_menu(void);
  19. static void demo_all(void);
  20. static void demo_color(void);
  21. static void demo_dots(void);
  22. static void demo_lines(void);
  23. static void demo_boxes(void);
  24. static void demo_ellipses(void);
  25. static void demo_triangles(void);
  26. static void demo_sprites(void);
  27. static void demo_render(void);
  28. int bounds = 0;
  29. int outline = 0;
  30. int dithering = 0;
  31. cucul_sprite_t *sprite = NULL;
  32. cucul_t *qq;
  33. caca_t *kk;
  34. int main(int argc, char **argv)
  35. {
  36. void (*demo)(void) = NULL;
  37. int quit = 0;
  38. qq = cucul_create(0, 0);
  39. if(!qq)
  40. return 1;
  41. kk = caca_attach(qq);
  42. if(!kk)
  43. return 1;
  44. caca_set_delay(kk, 40000);
  45. /* Initialize data */
  46. sprite = cucul_load_sprite(DATADIR "/caca.txt");
  47. if(!sprite)
  48. sprite = cucul_load_sprite("caca.txt");
  49. if(!sprite)
  50. sprite = cucul_load_sprite("examples/caca.txt");
  51. /* Disable cursor */
  52. caca_set_mouse(kk, 0);
  53. /* Main menu */
  54. display_menu();
  55. caca_display(kk);
  56. /* Go ! */
  57. while(!quit)
  58. {
  59. caca_event_t ev;
  60. int menu = 0, mouse = 0, xmouse = 0, ymouse = 0;
  61. while(caca_get_event(kk, CACA_EVENT_ANY, &ev, 0))
  62. {
  63. if(demo && (ev.type & CACA_EVENT_KEY_PRESS))
  64. {
  65. menu = 1;
  66. demo = NULL;
  67. }
  68. else if(ev.type & CACA_EVENT_KEY_PRESS)
  69. {
  70. switch(ev.data.key.c)
  71. {
  72. case 'q':
  73. case 'Q':
  74. demo = NULL;
  75. quit = 1;
  76. break;
  77. case 'o':
  78. case 'O':
  79. outline = (outline + 1) % 3;
  80. display_menu();
  81. break;
  82. case 'b':
  83. case 'B':
  84. bounds = (bounds + 1) % 2;
  85. display_menu();
  86. break;
  87. #if 0
  88. case 'd':
  89. case 'D':
  90. dithering = (dithering + 1) % 5;
  91. cucul_set_feature(qq, dithering);
  92. display_menu();
  93. break;
  94. #endif
  95. case 'c':
  96. demo = demo_color;
  97. break;
  98. case 'f':
  99. case 'F':
  100. demo = demo_all;
  101. break;
  102. case '1':
  103. demo = demo_dots;
  104. break;
  105. case '2':
  106. demo = demo_lines;
  107. break;
  108. case '3':
  109. demo = demo_boxes;
  110. break;
  111. case '4':
  112. demo = demo_triangles;
  113. break;
  114. case '5':
  115. demo = demo_ellipses;
  116. break;
  117. case 's':
  118. case 'S':
  119. if(sprite)
  120. demo = demo_sprites;
  121. break;
  122. case 'r':
  123. case 'R':
  124. demo = demo_render;
  125. break;
  126. }
  127. if(demo)
  128. cucul_clear(qq);
  129. }
  130. else if(ev.type & CACA_EVENT_MOUSE_MOTION)
  131. {
  132. mouse = 1;
  133. xmouse = ev.data.mouse.x;
  134. ymouse = ev.data.mouse.y;
  135. }
  136. else if(ev.type & CACA_EVENT_RESIZE)
  137. {
  138. mouse = 1; /* old hack */
  139. }
  140. }
  141. if(menu || (mouse && !demo))
  142. {
  143. display_menu();
  144. if(mouse && !demo)
  145. {
  146. cucul_set_color(qq, CUCUL_COLOR_RED, CUCUL_COLOR_BLACK);
  147. cucul_putstr(qq, xmouse, ymouse, ".");
  148. cucul_putstr(qq, xmouse, ymouse + 1, "|\\");
  149. }
  150. caca_display(kk);
  151. mouse = menu = 0;
  152. }
  153. if(demo)
  154. {
  155. demo();
  156. cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
  157. cucul_draw_thin_box(qq, 1, 1, cucul_get_width(qq) - 2, cucul_get_height(qq) - 2);
  158. cucul_printf(qq, 4, 1, "[%i.%i fps]----",
  159. 1000000 / caca_get_rendertime(kk),
  160. (10000000 / caca_get_rendertime(kk)) % 10);
  161. caca_display(kk);
  162. }
  163. }
  164. /* Clean up */
  165. cucul_free_sprite(sprite);
  166. caca_detach(kk);
  167. cucul_free(qq);
  168. return 0;
  169. }
  170. static void display_menu(void)
  171. {
  172. int xo = cucul_get_width(qq) - 2;
  173. int yo = cucul_get_height(qq) - 2;
  174. cucul_clear(qq);
  175. cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
  176. cucul_draw_thin_box(qq, 1, 1, xo, yo);
  177. cucul_putstr(qq, (xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
  178. cucul_putstr(qq, (xo - strlen("==============")) / 2, 4, "==============");
  179. cucul_putstr(qq, 4, 6, "demos:");
  180. cucul_putstr(qq, 4, 7, "'f': full");
  181. cucul_putstr(qq, 4, 8, "'1': dots");
  182. cucul_putstr(qq, 4, 9, "'2': lines");
  183. cucul_putstr(qq, 4, 10, "'3': boxes");
  184. cucul_putstr(qq, 4, 11, "'4': triangles");
  185. cucul_putstr(qq, 4, 12, "'5': ellipses");
  186. cucul_putstr(qq, 4, 13, "'c': colour");
  187. cucul_putstr(qq, 4, 14, "'r': render");
  188. if(sprite)
  189. cucul_putstr(qq, 4, 15, "'s': sprites");
  190. cucul_putstr(qq, 4, 16, "settings:");
  191. cucul_printf(qq, 4, 17, "'o': outline: %s",
  192. outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
  193. cucul_printf(qq, 4, 18, "'b': drawing boundaries: %s",
  194. bounds == 0 ? "screen" : "infinite");
  195. //cucul_printf(qq, 4, 19, "'d': dithering (%s)",
  196. // cucul_get_feature_name(dithering));
  197. cucul_putstr(qq, 4, yo - 2, "'q': quit");
  198. caca_display(kk);
  199. }
  200. static void demo_all(void)
  201. {
  202. static int i = 0;
  203. int j, xo, yo, xa, ya, xb, yb, xc, yc;
  204. i++;
  205. cucul_clear(qq);
  206. /* Draw the sun */
  207. cucul_set_color(qq, CUCUL_COLOR_YELLOW, CUCUL_COLOR_BLACK);
  208. xo = cucul_get_width(qq) / 4;
  209. yo = cucul_get_height(qq) / 4 + 5 * sin(0.03*i);
  210. for(j = 0; j < 16; j++)
  211. {
  212. xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
  213. ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
  214. cucul_draw_thin_line(qq, xo, yo, xa, ya);
  215. }
  216. j = 15 + sin(0.03*i) * 8;
  217. cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK);
  218. cucul_fill_ellipse(qq, xo, yo, j, j / 2, "#");
  219. cucul_set_color(qq, CUCUL_COLOR_YELLOW, CUCUL_COLOR_BLACK);
  220. cucul_draw_ellipse(qq, xo, yo, j, j / 2, "#");
  221. /* Draw the pyramid */
  222. xo = cucul_get_width(qq) * 5 / 8;
  223. yo = 2;
  224. xa = cucul_get_width(qq) / 8 + sin(0.03*i) * 5;
  225. ya = cucul_get_height(qq) / 2 + cos(0.03*i) * 5;
  226. xb = cucul_get_width(qq) - 10 - cos(0.02*i) * 10;
  227. yb = cucul_get_height(qq) * 3 / 4 - 5 + sin(0.02*i) * 5;
  228. xc = cucul_get_width(qq) / 4 - sin(0.02*i) * 5;
  229. yc = cucul_get_height(qq) * 3 / 4 + cos(0.02*i) * 5;
  230. cucul_set_color(qq, CUCUL_COLOR_GREEN, CUCUL_COLOR_BLACK);
  231. cucul_fill_triangle(qq, xo, yo, xb, yb, xa, ya, "%");
  232. cucul_set_color(qq, CUCUL_COLOR_YELLOW, CUCUL_COLOR_BLACK);
  233. cucul_draw_thin_triangle(qq, xo, yo, xb, yb, xa, ya);
  234. cucul_set_color(qq, CUCUL_COLOR_RED, CUCUL_COLOR_BLACK);
  235. cucul_fill_triangle(qq, xa, ya, xb, yb, xc, yc, "#");
  236. cucul_set_color(qq, CUCUL_COLOR_YELLOW, CUCUL_COLOR_BLACK);
  237. cucul_draw_thin_triangle(qq, xa, ya, xb, yb, xc, yc);
  238. cucul_set_color(qq, CUCUL_COLOR_BLUE, CUCUL_COLOR_BLACK);
  239. cucul_fill_triangle(qq, xo, yo, xb, yb, xc, yc, "%");
  240. cucul_set_color(qq, CUCUL_COLOR_YELLOW, CUCUL_COLOR_BLACK);
  241. cucul_draw_thin_triangle(qq, xo, yo, xb, yb, xc, yc);
  242. /* Draw a background triangle */
  243. xa = 2;
  244. ya = 2;
  245. xb = cucul_get_width(qq) - 3;
  246. yb = cucul_get_height(qq) / 2;
  247. xc = cucul_get_width(qq) / 3;
  248. yc = cucul_get_height(qq) - 3;
  249. cucul_set_color(qq, CUCUL_COLOR_CYAN, CUCUL_COLOR_BLACK);
  250. cucul_draw_thin_triangle(qq, xa, ya, xb, yb, xc, yc);
  251. xo = cucul_get_width(qq) / 2 + cos(0.027*i) * cucul_get_width(qq) / 3;
  252. yo = cucul_get_height(qq) / 2 - sin(0.027*i) * cucul_get_height(qq) / 2;
  253. cucul_draw_thin_line(qq, xa, ya, xo, yo);
  254. cucul_draw_thin_line(qq, xb, yb, xo, yo);
  255. cucul_draw_thin_line(qq, xc, yc, xo, yo);
  256. /* Draw a sprite on the pyramid */
  257. cucul_draw_sprite(qq, xo, yo, sprite, 0);
  258. /* Draw a trail behind the foreground sprite */
  259. for(j = i - 60; j < i; j++)
  260. {
  261. int delta = cucul_rand(-5, 5);
  262. cucul_set_color(qq, cucul_rand(0, 15), cucul_rand(0, 15));
  263. cucul_putchar(qq, cucul_get_width(qq) / 2
  264. + cos(0.02*j) * (delta + cucul_get_width(qq) / 4),
  265. cucul_get_height(qq) / 2
  266. + sin(0.02*j) * (delta + cucul_get_height(qq) / 3),
  267. '#');
  268. }
  269. /* Draw foreground sprite */
  270. cucul_draw_sprite(qq, cucul_get_width(qq) / 2 + cos(0.02*i) * cucul_get_width(qq) / 4,
  271. cucul_get_height(qq) / 2 + sin(0.02*i) * cucul_get_height(qq) / 3,
  272. sprite, 0);
  273. }
  274. static void demo_dots(void)
  275. {
  276. int xmax = cucul_get_width(qq) - 1;
  277. int ymax = cucul_get_height(qq) - 1;
  278. int i;
  279. static char chars[10] =
  280. {
  281. '+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W'
  282. };
  283. for(i = 1000; i--;)
  284. {
  285. /* Putpixel */
  286. cucul_set_color(qq, cucul_rand(0, 15), cucul_rand(0, 15));
  287. cucul_putchar(qq, cucul_rand(0, xmax), cucul_rand(0, ymax),
  288. chars[cucul_rand(0, 9)]);
  289. }
  290. }
  291. static void demo_color(void)
  292. {
  293. int i, j;
  294. char buf[BUFSIZ];
  295. cucul_clear(qq);
  296. for(i = 0; i < 16; i++)
  297. {
  298. sprintf(buf, "'%c': %i (%s)", 'a' + i, i, cucul_get_color_name(i));
  299. cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
  300. cucul_putstr(qq, 4, i + (i >= 8 ? 4 : 3), buf);
  301. for(j = 0; j < 16; j++)
  302. {
  303. cucul_set_color(qq, i, j);
  304. cucul_putstr(qq, (j >= 8 ? 41 : 40) + j * 2, i + (i >= 8 ? 4 : 3), "# ");
  305. }
  306. }
  307. }
  308. static void demo_lines(void)
  309. {
  310. int w = cucul_get_width(qq);
  311. int h = cucul_get_height(qq);
  312. int xa, ya, xb, yb;
  313. if(bounds)
  314. {
  315. xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h);
  316. xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h);
  317. }
  318. else
  319. {
  320. xa = cucul_rand(0, w - 1); ya = cucul_rand(0, h - 1);
  321. xb = cucul_rand(0, w - 1); yb = cucul_rand(0, h - 1);
  322. }
  323. cucul_set_color(qq, cucul_rand(0, 15), CUCUL_COLOR_BLACK);
  324. if(outline > 1)
  325. cucul_draw_thin_line(qq, xa, ya, xb, yb);
  326. else
  327. cucul_draw_line(qq, xa, ya, xb, yb, "#");
  328. }
  329. static void demo_boxes(void)
  330. {
  331. int w = cucul_get_width(qq);
  332. int h = cucul_get_height(qq);
  333. int xa, ya, xb, yb;
  334. if(bounds)
  335. {
  336. xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h);
  337. xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h);
  338. }
  339. else
  340. {
  341. xa = cucul_rand(0, w - 1); ya = cucul_rand(0, h - 1);
  342. xb = cucul_rand(0, w - 1); yb = cucul_rand(0, h - 1);
  343. }
  344. cucul_set_color(qq, cucul_rand(0, 15), cucul_rand(0, 15));
  345. cucul_fill_box(qq, xa, ya, xb, yb, "#");
  346. cucul_set_color(qq, cucul_rand(0, 15), CUCUL_COLOR_BLACK);
  347. if(outline == 2)
  348. cucul_draw_thin_box(qq, xa, ya, xb, yb);
  349. else if(outline == 1)
  350. cucul_draw_box(qq, xa, ya, xb, yb, "#");
  351. }
  352. static void demo_ellipses(void)
  353. {
  354. int w = cucul_get_width(qq);
  355. int h = cucul_get_height(qq);
  356. int x, y, a, b;
  357. if(bounds)
  358. {
  359. x = cucul_rand(- w, 2 * w); y = cucul_rand(- h, 2 * h);
  360. a = cucul_rand(0, w); b = cucul_rand(0, h);
  361. }
  362. else
  363. {
  364. do
  365. {
  366. x = cucul_rand(0, w); y = cucul_rand(0, h);
  367. a = cucul_rand(0, w); b = cucul_rand(0, h);
  368. } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
  369. }
  370. cucul_set_color(qq, cucul_rand(0, 15), cucul_rand(0, 15));
  371. cucul_fill_ellipse(qq, x, y, a, b, "#");
  372. cucul_set_color(qq, cucul_rand(0, 15), CUCUL_COLOR_BLACK);
  373. if(outline == 2)
  374. cucul_draw_thin_ellipse(qq, x, y, a, b);
  375. else if(outline == 1)
  376. cucul_draw_ellipse(qq, x, y, a, b, "#");
  377. }
  378. static void demo_triangles(void)
  379. {
  380. int w = cucul_get_width(qq);
  381. int h = cucul_get_height(qq);
  382. int xa, ya, xb, yb, xc, yc;
  383. if(bounds)
  384. {
  385. xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h);
  386. xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h);
  387. xc = cucul_rand(- w, 2 * w); yc = cucul_rand(- h, 2 * h);
  388. }
  389. else
  390. {
  391. xa = cucul_rand(0, w - 1); ya = cucul_rand(0, h - 1);
  392. xb = cucul_rand(0, w - 1); yb = cucul_rand(0, h - 1);
  393. xc = cucul_rand(0, w - 1); yc = cucul_rand(0, h - 1);
  394. }
  395. cucul_set_color(qq, cucul_rand(0, 15), cucul_rand(0, 15));
  396. cucul_fill_triangle(qq, xa, ya, xb, yb, xc, yc, "#");
  397. cucul_set_color(qq, cucul_rand(0, 15), CUCUL_COLOR_BLACK);
  398. if(outline == 2)
  399. cucul_draw_thin_triangle(qq, xa, ya, xb, yb, xc, yc);
  400. else if(outline == 1)
  401. cucul_draw_triangle(qq, xa, ya, xb, yb, xc, yc, "#");
  402. }
  403. static void demo_sprites(void)
  404. {
  405. cucul_draw_sprite(qq, cucul_rand(0, cucul_get_width(qq) - 1),
  406. cucul_rand(0, cucul_get_height(qq) - 1), sprite, 0);
  407. }
  408. #if 0
  409. static void demo_render(void)
  410. {
  411. cucul_dither_t *dither;
  412. //short buffer[256*256];
  413. //short *dest = buffer;
  414. int buffer[256*256];
  415. int *dest = buffer;
  416. int x, y, z;
  417. static int i = 0;
  418. i = (i + 1) % 512;
  419. z = i < 256 ? i : 511 - i;
  420. for(x = 0; x < 256; x++)
  421. for(y = 0; y < 256; y++)
  422. {
  423. //*dest++ = ((x >> 3) << 11) | ((y >> 2) << 5) | ((z >> 3));
  424. *dest++ = (x << 16) | (y << 8) | (z);
  425. }
  426. cucul_set_dither_invert(dither, 1);
  427. //dither = cucul_create_dither(16, 256, 256, 2 * 256, 0xf800, 0x07e0, 0x001f, 0x0000);
  428. dither = cucul_create_dither(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  429. cucul_dither_bitmap(qq, 0, 0, cucul_get_width(qq) - 1, cucul_get_height(qq) - 1,
  430. dither, buffer);
  431. cucul_free_dither(dither);
  432. }
  433. #endif
  434. static void draw_circle(int *buffer, int xo, int yo, int r, int mask, int val);
  435. static void demo_render(void)
  436. {
  437. cucul_dither_t *dither;
  438. int buffer[256*256];
  439. int *dest;
  440. int x, y, z, xo, yo;
  441. static int i = 0;
  442. i++;
  443. dest = buffer;
  444. for(x = 0; x < 256; x++)
  445. for(y = 0; y < 256; y++)
  446. {
  447. *dest++ = 0xff000000;
  448. }
  449. /* red */
  450. xo = 128 + 48 * sin(0.02 * i);
  451. yo = 128 + 48 * cos(0.03 * i);
  452. for(z = 0; z < 240; z++)
  453. draw_circle(buffer, xo, yo, z, 0x00ff0000, 200 << 16);
  454. /* green */
  455. xo = 128 + 48 * sin(2 + 0.06 * i);
  456. yo = 128 + 48 * cos(2 + 0.05 * i);
  457. for(z = 0; z < 240; z++)
  458. draw_circle(buffer, xo, yo, z, 0x0000ff00, 200 << 8);
  459. /* blue */
  460. xo = 128 + 48 * sin(1 + 0.04 * i);
  461. yo = 128 + 48 * cos(1 + 0.03 * i);
  462. for(z = 0; z < 240; z++)
  463. draw_circle(buffer, xo, yo, z, 0x000000ff, 200);
  464. dither = cucul_create_dither(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  465. cucul_set_dither_invert(dither, 1);
  466. cucul_dither_bitmap(qq, 0, 0, cucul_get_width(qq) - 1, cucul_get_height(qq) - 1, dither, (char *)buffer);
  467. cucul_free_dither(dither);
  468. }
  469. static void draw_circle(int *buffer, int x, int y, int r, int mask, int val)
  470. {
  471. int t, dx, dy;
  472. #define POINT(X,Y) \
  473. buffer[(X) + 256 * (Y)] = 0xff000000 | (buffer[(X) + 256 * (Y)] & ~mask) | val;
  474. for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
  475. {
  476. POINT(x - dx / 3, y - dy / 3);
  477. POINT(x + dx / 3, y - dy / 3);
  478. POINT(x - dx / 3, y + dy / 3);
  479. POINT(x + dx / 3, y + dy / 3);
  480. POINT(x - dy / 3, y - dx / 3);
  481. POINT(x + dy / 3, y - dx / 3);
  482. POINT(x - dy / 3, y + dx / 3);
  483. POINT(x + dy / 3, y + dx / 3);
  484. t += t > 0 ? dx - dy-- : dx;
  485. }
  486. }