Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * demo demo for libcaca version 0.9
  3. * Copyright (c) 2003-2009 Sam Hocevar <sam@hocevar.net>
  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 GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "config.h"
  24. #if !defined(__KERNEL__)
  25. # include <math.h>
  26. # include <string.h>
  27. # include <stdio.h>
  28. #endif
  29. #include "caca0.h"
  30. static void display_menu(void);
  31. static void demo_all(void);
  32. static void demo_color(void);
  33. static void demo_dots(void);
  34. static void demo_lines(void);
  35. static void demo_boxes(void);
  36. static void demo_ellipses(void);
  37. static void demo_triangles(void);
  38. #if 0
  39. static void demo_sprites(void);
  40. #endif
  41. static void demo_render(void);
  42. int bounds = 0;
  43. int outline = 0;
  44. int dithering = 0;
  45. struct caca_sprite *sprite = NULL;
  46. int main(int argc, char **argv)
  47. {
  48. void (*demo)(void) = NULL;
  49. int quit = 0;
  50. if(caca_init())
  51. return 1;
  52. caca_set_delay(40000);
  53. /* Initialize data */
  54. #if 0
  55. sprite = caca_load_sprite("caca.txt");
  56. if(!sprite)
  57. sprite = caca_load_sprite("examples/caca.txt");
  58. #endif
  59. /* Main menu */
  60. display_menu();
  61. caca_refresh();
  62. /* Go ! */
  63. while(!quit)
  64. {
  65. int menu = 0, mouse = 0, xmouse = 0, ymouse = 0;
  66. int event;
  67. while((event = caca_get_event(CACA_EVENT_ANY)))
  68. {
  69. if(demo && (event & CACA_EVENT_KEY_PRESS))
  70. {
  71. menu = 1;
  72. demo = NULL;
  73. }
  74. else if(event & CACA_EVENT_KEY_PRESS)
  75. {
  76. switch(event & 0xffff)
  77. {
  78. case 'q':
  79. case 'Q':
  80. case CACA_KEY_ESCAPE:
  81. demo = NULL;
  82. quit = 1;
  83. break;
  84. case 'o':
  85. case 'O':
  86. outline = (outline + 1) % 3;
  87. display_menu();
  88. caca_refresh();
  89. break;
  90. case 'b':
  91. case 'B':
  92. bounds = (bounds + 1) % 2;
  93. display_menu();
  94. caca_refresh();
  95. break;
  96. case 'd':
  97. case 'D':
  98. dithering = (dithering + 1) % 5;
  99. caca_set_dithering(CACA_DITHERING_NONE + dithering);
  100. display_menu();
  101. caca_refresh();
  102. break;
  103. case 'c':
  104. demo = demo_color;
  105. break;
  106. case 'f':
  107. case 'F':
  108. demo = demo_all;
  109. break;
  110. case '1':
  111. demo = demo_dots;
  112. break;
  113. case '2':
  114. demo = demo_lines;
  115. break;
  116. case '3':
  117. demo = demo_boxes;
  118. break;
  119. case '4':
  120. demo = demo_triangles;
  121. break;
  122. case '5':
  123. demo = demo_ellipses;
  124. break;
  125. #if 0
  126. case 's':
  127. case 'S':
  128. if(sprite)
  129. demo = demo_sprites;
  130. break;
  131. #endif
  132. case 'r':
  133. case 'R':
  134. demo = demo_render;
  135. break;
  136. }
  137. if(demo)
  138. caca_clear();
  139. }
  140. else if(event & CACA_EVENT_MOUSE_MOTION)
  141. {
  142. mouse = 1;
  143. xmouse = (event & 0xfff000) >> 12;
  144. ymouse = event & 0xfff;
  145. }
  146. }
  147. if(menu || (mouse && !demo))
  148. {
  149. display_menu();
  150. if(mouse && !demo)
  151. {
  152. caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
  153. caca_putstr(xmouse, ymouse, "|\\");
  154. }
  155. caca_refresh();
  156. mouse = menu = 0;
  157. }
  158. else if(demo)
  159. {
  160. demo();
  161. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  162. caca_draw_thin_box(1, 1, caca_get_width() - 2, caca_get_height() - 2);
  163. caca_printf(4, 1, "[%i.%i fps]----",
  164. 1000000 / caca_get_rendertime(),
  165. (10000000 / caca_get_rendertime()) % 10);
  166. caca_refresh();
  167. }
  168. }
  169. /* Clean up */
  170. #if 0
  171. caca_free_sprite(sprite);
  172. #endif
  173. caca_end();
  174. return 0;
  175. }
  176. static void display_menu(void)
  177. {
  178. int xo = caca_get_width() - 2;
  179. int yo = caca_get_height() - 2;
  180. caca_clear();
  181. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  182. caca_draw_thin_box(1, 1, xo, yo);
  183. caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
  184. caca_putstr((xo - strlen("==============")) / 2, 4, "==============");
  185. caca_putstr(4, 6, "demos:");
  186. caca_putstr(4, 7, "'f': full");
  187. caca_putstr(4, 8, "'1': dots");
  188. caca_putstr(4, 9, "'2': lines");
  189. caca_putstr(4, 10, "'3': boxes");
  190. caca_putstr(4, 11, "'4': triangles");
  191. caca_putstr(4, 12, "'5': ellipses");
  192. caca_putstr(4, 13, "'c': colour");
  193. caca_putstr(4, 14, "'r': render");
  194. if(sprite)
  195. caca_putstr(4, 15, "'s': sprites");
  196. caca_putstr(4, 16, "settings:");
  197. caca_printf(4, 17, "'o': outline: %s",
  198. outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
  199. caca_printf(4, 18, "'b': drawing boundaries: %s",
  200. bounds == 0 ? "screen" : "infinite");
  201. caca_printf(4, 19, "'d': dithering (%s)",
  202. caca_get_dithering_name(CACA_DITHERING_NONE + dithering));
  203. caca_putstr(4, yo - 2, "'q': quit");
  204. }
  205. static void demo_all(void)
  206. {
  207. static int i = 0;
  208. int j, xo, yo, xa, ya, xb, yb, xc, yc;
  209. i++;
  210. caca_clear();
  211. /* Draw the sun */
  212. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  213. xo = caca_get_width() / 4;
  214. yo = caca_get_height() / 4 + 5 * sin(0.03*i);
  215. for(j = 0; j < 16; j++)
  216. {
  217. xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
  218. ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
  219. caca_draw_thin_line(xo, yo, xa, ya);
  220. }
  221. j = 15 + sin(0.03*i) * 8;
  222. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK);
  223. caca_fill_ellipse(xo, yo, j, j / 2, '#');
  224. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  225. caca_draw_ellipse(xo, yo, j, j / 2, '#');
  226. /* Draw the pyramid */
  227. xo = caca_get_width() * 5 / 8;
  228. yo = 2;
  229. xa = caca_get_width() / 8 + sin(0.03*i) * 5;
  230. ya = caca_get_height() / 2 + cos(0.03*i) * 5;
  231. xb = caca_get_width() - 10 - cos(0.02*i) * 10;
  232. yb = caca_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
  233. xc = caca_get_width() / 4 - sin(0.02*i) * 5;
  234. yc = caca_get_height() * 3 / 4 + cos(0.02*i) * 5;
  235. caca_set_color(CACA_COLOR_GREEN, CACA_COLOR_BLACK);
  236. caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
  237. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  238. caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
  239. caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
  240. caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  241. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  242. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  243. caca_set_color(CACA_COLOR_BLUE, CACA_COLOR_BLACK);
  244. caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
  245. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  246. caca_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
  247. /* Draw a background triangle */
  248. xa = 2;
  249. ya = 2;
  250. xb = caca_get_width() - 3;
  251. yb = caca_get_height() / 2;
  252. xc = caca_get_width() / 3;
  253. yc = caca_get_height() - 3;
  254. caca_set_color(CACA_COLOR_CYAN, CACA_COLOR_BLACK);
  255. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  256. xo = caca_get_width() / 2 + cos(0.027*i) * caca_get_width() / 3;
  257. yo = caca_get_height() / 2 - sin(0.027*i) * caca_get_height() / 2;
  258. caca_draw_thin_line(xa, ya, xo, yo);
  259. caca_draw_thin_line(xb, yb, xo, yo);
  260. caca_draw_thin_line(xc, yc, xo, yo);
  261. /* Draw a sprite on the pyramid */
  262. #if 0
  263. caca_draw_sprite(xo, yo, sprite, 0);
  264. #endif
  265. /* Draw a trail behind the foreground sprite */
  266. for(j = i - 60; j < i; j++)
  267. {
  268. int delta = caca_rand(-5, 5);
  269. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  270. caca_putchar(caca_get_width() / 2
  271. + cos(0.02*j) * (delta + caca_get_width() / 4),
  272. caca_get_height() / 2
  273. + sin(0.02*j) * (delta + caca_get_height() / 3),
  274. '#');
  275. }
  276. /* Draw foreground sprite */
  277. #if 0
  278. caca_draw_sprite(caca_get_width() / 2 + cos(0.02*i) * caca_get_width() / 4,
  279. caca_get_height() / 2 + sin(0.02*i) * caca_get_height() / 3,
  280. sprite, 0);
  281. #endif
  282. }
  283. static void demo_dots(void)
  284. {
  285. int xmax = caca_get_width() - 1;
  286. int ymax = caca_get_height() - 1;
  287. int i;
  288. static char chars[10] =
  289. {
  290. '+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W'
  291. };
  292. for(i = 1000; i--;)
  293. {
  294. /* Putpixel */
  295. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  296. caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax),
  297. chars[caca_rand(0, 9)]);
  298. }
  299. }
  300. static void demo_color(void)
  301. {
  302. int i, j;
  303. char buf[BUFSIZ];
  304. caca_clear();
  305. for(i = 0; i < 16; i++)
  306. {
  307. sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i));
  308. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  309. caca_putstr(4, i + (i >= 8 ? 4 : 3), buf);
  310. for(j = 0; j < 16; j++)
  311. {
  312. caca_set_color(i, j);
  313. caca_putstr((j >= 8 ? 41 : 40) + j * 2, i + (i >= 8 ? 4 : 3), "# ");
  314. }
  315. }
  316. }
  317. static void demo_lines(void)
  318. {
  319. int w = caca_get_width();
  320. int h = caca_get_height();
  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 - 1); ya = caca_rand(0, h - 1);
  330. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  331. }
  332. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  333. if(outline > 1)
  334. caca_draw_thin_line(xa, ya, xb, yb);
  335. else
  336. caca_draw_line(xa, ya, xb, yb, '#');
  337. }
  338. static void demo_boxes(void)
  339. {
  340. int w = caca_get_width();
  341. int h = caca_get_height();
  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 - 1); ya = caca_rand(0, h - 1);
  351. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  352. }
  353. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  354. caca_fill_box(xa, ya, xb, yb, '#');
  355. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  356. if(outline == 2)
  357. caca_draw_thin_box(xa, ya, xb, yb);
  358. else if(outline == 1)
  359. caca_draw_box(xa, ya, xb, yb, '#');
  360. }
  361. static void demo_ellipses(void)
  362. {
  363. int w = caca_get_width();
  364. int h = caca_get_height();
  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(caca_rand(0, 15), caca_rand(0, 15));
  380. caca_fill_ellipse(x, y, a, b, '#');
  381. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  382. if(outline == 2)
  383. caca_draw_thin_ellipse(x, y, a, b);
  384. else if(outline == 1)
  385. caca_draw_ellipse(x, y, a, b, '#');
  386. }
  387. static void demo_triangles(void)
  388. {
  389. int w = caca_get_width();
  390. int h = caca_get_height();
  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 - 1); ya = caca_rand(0, h - 1);
  401. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  402. xc = caca_rand(0, w - 1); yc = caca_rand(0, h - 1);
  403. }
  404. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  405. caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  406. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  407. if(outline == 2)
  408. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  409. else if(outline == 1)
  410. caca_draw_triangle(xa, ya, xb, yb, xc, yc, '#');
  411. }
  412. #if 0
  413. static void demo_sprites(void)
  414. {
  415. caca_draw_sprite(caca_rand(0, caca_get_width() - 1),
  416. caca_rand(0, caca_get_height() - 1), sprite, 0);
  417. }
  418. #endif
  419. #if 0
  420. static void demo_render(void)
  421. {
  422. struct caca_bitmap *bitmap;
  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. //bitmap = caca_create_bitmap(16, 256, 256, 2 * 256, 0xf800, 0x07e0, 0x001f, 0x0000);
  438. bitmap = caca_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  439. caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
  440. bitmap, buffer);
  441. caca_free_bitmap(bitmap);
  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. struct caca_bitmap *bitmap;
  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. bitmap = caca_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  475. caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
  476. bitmap, (char *)buffer);
  477. caca_free_bitmap(bitmap);
  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. }