You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

564 rivejä
15 KiB

  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 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. #include <math.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include "caca.h"
  28. static void display_menu(void);
  29. static void demo_all(void);
  30. static void demo_color(void);
  31. static void demo_dots(void);
  32. static void demo_lines(void);
  33. static void demo_boxes(void);
  34. static void demo_ellipses(void);
  35. static void demo_triangles(void);
  36. static void demo_sprites(void);
  37. static void demo_render(void);
  38. int bounds = 0;
  39. int outline = 0;
  40. int dithering = 0;
  41. struct caca_sprite *sprite = NULL;
  42. int main(int argc, char **argv)
  43. {
  44. void (*demo)(void) = NULL;
  45. int quit = 0;
  46. if(caca_init())
  47. return 1;
  48. caca_set_delay(40000);
  49. /* Initialize data */
  50. sprite = caca_load_sprite(DATADIR "/caca.txt");
  51. if(!sprite)
  52. sprite = caca_load_sprite("caca.txt");
  53. if(!sprite)
  54. sprite = caca_load_sprite("examples/caca.txt");
  55. /* Main menu */
  56. display_menu();
  57. caca_refresh();
  58. /* Go ! */
  59. while(!quit)
  60. {
  61. int event = caca_get_event();
  62. if(event && demo)
  63. {
  64. display_menu();
  65. caca_refresh();
  66. demo = NULL;
  67. }
  68. else if(event & CACA_EVENT_KEY_PRESS)
  69. {
  70. handle_key:
  71. switch(event & 0xffff)
  72. {
  73. case 'q':
  74. case 'Q':
  75. demo = NULL;
  76. quit = 1;
  77. break;
  78. case 'o':
  79. case 'O':
  80. outline = (outline + 1) % 3;
  81. display_menu();
  82. break;
  83. case 'b':
  84. case 'B':
  85. bounds = (bounds + 1) % 2;
  86. display_menu();
  87. break;
  88. case 'd':
  89. case 'D':
  90. dithering = (dithering + 1) % 5;
  91. caca_set_dithering(dithering);
  92. display_menu();
  93. break;
  94. case 'c':
  95. demo = demo_color;
  96. break;
  97. case 'f':
  98. case 'F':
  99. demo = demo_all;
  100. break;
  101. case '1':
  102. demo = demo_dots;
  103. break;
  104. case '2':
  105. demo = demo_lines;
  106. break;
  107. case '3':
  108. demo = demo_boxes;
  109. break;
  110. case '4':
  111. demo = demo_triangles;
  112. break;
  113. case '5':
  114. demo = demo_ellipses;
  115. break;
  116. case 's':
  117. case 'S':
  118. if(sprite)
  119. demo = demo_sprites;
  120. break;
  121. case 'r':
  122. case 'R':
  123. demo = demo_render;
  124. break;
  125. }
  126. handle_event:
  127. event = caca_get_event();
  128. if(event & CACA_EVENT_KEY_PRESS)
  129. goto handle_key;
  130. else if(event)
  131. goto handle_event;
  132. caca_refresh();
  133. if(demo)
  134. caca_clear();
  135. }
  136. else if(event & CACA_EVENT_MOUSE_CLICK)
  137. {
  138. display_menu();
  139. caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
  140. caca_putstr((event & 0xff00) >> 8, event & 0xff, "|\\");
  141. caca_refresh();
  142. }
  143. if(demo)
  144. {
  145. demo();
  146. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  147. caca_draw_thin_box(1, 1, caca_get_width() - 2, caca_get_height() - 2);
  148. caca_printf(4, 1, "[%i.%i fps]----",
  149. 1000000 / caca_get_rendertime(),
  150. (10000000 / caca_get_rendertime()) % 10);
  151. caca_refresh();
  152. }
  153. }
  154. /* Clean up */
  155. caca_free_sprite(sprite);
  156. caca_end();
  157. return 0;
  158. }
  159. static void display_menu(void)
  160. {
  161. int xo = caca_get_width() - 2;
  162. int yo = caca_get_height() - 2;
  163. caca_clear();
  164. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  165. caca_draw_thin_box(1, 1, xo, yo);
  166. caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
  167. caca_putstr((xo - strlen("==============")) / 2, 4, "==============");
  168. caca_putstr(4, 6, "demos:");
  169. caca_putstr(4, 7, "'f': full");
  170. caca_putstr(4, 8, "'1': dots");
  171. caca_putstr(4, 9, "'2': lines");
  172. caca_putstr(4, 10, "'3': boxes");
  173. caca_putstr(4, 11, "'4': triangles");
  174. caca_putstr(4, 12, "'5': ellipses");
  175. caca_putstr(4, 13, "'c': colour");
  176. caca_putstr(4, 14, "'r': render");
  177. if(sprite)
  178. caca_putstr(4, 15, "'s': sprites");
  179. caca_putstr(4, 16, "settings:");
  180. caca_printf(4, 17, "'o': outline: %s",
  181. outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
  182. caca_printf(4, 18, "'b': drawing boundaries: %s",
  183. bounds == 0 ? "screen" : "infinite");
  184. caca_printf(4, 19, "'d': dithering (%s)",
  185. caca_get_dithering_name(dithering));
  186. caca_putstr(4, yo - 2, "'q': quit");
  187. }
  188. static void demo_all(void)
  189. {
  190. static int i = 0;
  191. int j, xo, yo, xa, ya, xb, yb, xc, yc;
  192. i++;
  193. caca_clear();
  194. /* Draw the sun */
  195. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  196. xo = caca_get_width() / 4;
  197. yo = caca_get_height() / 4 + 5 * sin(0.03*i);
  198. for(j = 0; j < 16; j++)
  199. {
  200. xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
  201. ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
  202. caca_draw_thin_line(xo, yo, xa, ya);
  203. }
  204. j = 15 + sin(0.03*i) * 8;
  205. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK);
  206. caca_fill_ellipse(xo, yo, j, j / 2, '#');
  207. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  208. caca_draw_ellipse(xo, yo, j, j / 2, '#');
  209. /* Draw the pyramid */
  210. xo = caca_get_width() * 5 / 8;
  211. yo = 2;
  212. xa = caca_get_width() / 8 + sin(0.03*i) * 5;
  213. ya = caca_get_height() / 2 + cos(0.03*i) * 5;
  214. xb = caca_get_width() - 10 - cos(0.02*i) * 10;
  215. yb = caca_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
  216. xc = caca_get_width() / 4 - sin(0.02*i) * 5;
  217. yc = caca_get_height() * 3 / 4 + cos(0.02*i) * 5;
  218. caca_set_color(CACA_COLOR_GREEN, CACA_COLOR_BLACK);
  219. caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
  220. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  221. caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
  222. caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
  223. caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  224. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  225. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  226. caca_set_color(CACA_COLOR_BLUE, CACA_COLOR_BLACK);
  227. caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
  228. caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
  229. caca_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
  230. /* Draw a background triangle */
  231. xa = 2;
  232. ya = 2;
  233. xb = caca_get_width() - 3;
  234. yb = caca_get_height() / 2;
  235. xc = caca_get_width() / 3;
  236. yc = caca_get_height() - 3;
  237. caca_set_color(CACA_COLOR_CYAN, CACA_COLOR_BLACK);
  238. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  239. xo = caca_get_width() / 2 + cos(0.027*i) * caca_get_width() / 3;
  240. yo = caca_get_height() / 2 - sin(0.027*i) * caca_get_height() / 2;
  241. caca_draw_thin_line(xa, ya, xo, yo);
  242. caca_draw_thin_line(xb, yb, xo, yo);
  243. caca_draw_thin_line(xc, yc, xo, yo);
  244. /* Draw a sprite on the pyramid */
  245. caca_draw_sprite(xo, yo, sprite, 0);
  246. /* Draw a trail behind the foreground sprite */
  247. for(j = i - 60; j < i; j++)
  248. {
  249. int delta = caca_rand(-5, 5);
  250. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  251. caca_putchar(caca_get_width() / 2
  252. + cos(0.02*j) * (delta + caca_get_width() / 4),
  253. caca_get_height() / 2
  254. + sin(0.02*j) * (delta + caca_get_height() / 3),
  255. '#');
  256. }
  257. /* Draw foreground sprite */
  258. caca_draw_sprite(caca_get_width() / 2 + cos(0.02*i) * caca_get_width() / 4,
  259. caca_get_height() / 2 + sin(0.02*i) * caca_get_height() / 3,
  260. sprite, 0);
  261. }
  262. static void demo_dots(void)
  263. {
  264. int xmax = caca_get_width() - 1;
  265. int ymax = caca_get_height() - 1;
  266. int i;
  267. static char chars[10] =
  268. {
  269. '+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W'
  270. };
  271. for(i = 1000; i--;)
  272. {
  273. /* Putpixel */
  274. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  275. caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax),
  276. chars[caca_rand(0, 9)]);
  277. }
  278. }
  279. static void demo_color(void)
  280. {
  281. int i, j;
  282. char buf[BUFSIZ];
  283. caca_clear();
  284. for(i = 0; i < 16; i++)
  285. {
  286. sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i));
  287. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  288. caca_putstr(4, i + (i >= 8 ? 4 : 3), buf);
  289. for(j = 0; j < 16; j++)
  290. {
  291. caca_set_color(i, j);
  292. caca_putstr((j >= 8 ? 41 : 40) + j * 2, i + (i >= 8 ? 4 : 3), "# ");
  293. }
  294. }
  295. }
  296. static void demo_lines(void)
  297. {
  298. int w = caca_get_width();
  299. int h = caca_get_height();
  300. int xa, ya, xb, yb;
  301. if(bounds)
  302. {
  303. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  304. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  305. }
  306. else
  307. {
  308. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  309. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  310. }
  311. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  312. if(outline > 1)
  313. caca_draw_thin_line(xa, ya, xb, yb);
  314. else
  315. caca_draw_line(xa, ya, xb, yb, '#');
  316. }
  317. static void demo_boxes(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_rand(0, 15));
  333. caca_fill_box(xa, ya, xb, yb, '#');
  334. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  335. if(outline == 2)
  336. caca_draw_thin_box(xa, ya, xb, yb);
  337. else if(outline == 1)
  338. caca_draw_box(xa, ya, xb, yb, '#');
  339. }
  340. static void demo_ellipses(void)
  341. {
  342. int w = caca_get_width();
  343. int h = caca_get_height();
  344. int x, y, a, b;
  345. if(bounds)
  346. {
  347. x = caca_rand(- w, 2 * w); y = caca_rand(- h, 2 * h);
  348. a = caca_rand(0, w); b = caca_rand(0, h);
  349. }
  350. else
  351. {
  352. do
  353. {
  354. x = caca_rand(0, w); y = caca_rand(0, h);
  355. a = caca_rand(0, w); b = caca_rand(0, h);
  356. } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
  357. }
  358. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  359. caca_fill_ellipse(x, y, a, b, '#');
  360. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  361. if(outline == 2)
  362. caca_draw_thin_ellipse(x, y, a, b);
  363. else if(outline == 1)
  364. caca_draw_ellipse(x, y, a, b, '#');
  365. }
  366. static void demo_triangles(void)
  367. {
  368. int w = caca_get_width();
  369. int h = caca_get_height();
  370. int xa, ya, xb, yb, xc, yc;
  371. if(bounds)
  372. {
  373. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  374. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  375. xc = caca_rand(- w, 2 * w); yc = caca_rand(- h, 2 * h);
  376. }
  377. else
  378. {
  379. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  380. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  381. xc = caca_rand(0, w - 1); yc = caca_rand(0, h - 1);
  382. }
  383. caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
  384. caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  385. caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
  386. if(outline == 2)
  387. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  388. else if(outline == 1)
  389. caca_draw_triangle(xa, ya, xb, yb, xc, yc, '#');
  390. }
  391. static void demo_sprites(void)
  392. {
  393. caca_draw_sprite(caca_rand(0, caca_get_width() - 1),
  394. caca_rand(0, caca_get_height() - 1), sprite, 0);
  395. }
  396. #if 0
  397. static void demo_render(void)
  398. {
  399. struct caca_bitmap *bitmap;
  400. //short buffer[256*256];
  401. //short *dest = buffer;
  402. int buffer[256*256];
  403. int *dest = buffer;
  404. int x, y, z;
  405. static int i = 0;
  406. i = (i + 1) % 512;
  407. z = i < 256 ? i : 511 - i;
  408. for(x = 0; x < 256; x++)
  409. for(y = 0; y < 256; y++)
  410. {
  411. //*dest++ = ((x >> 3) << 11) | ((y >> 2) << 5) | ((z >> 3));
  412. *dest++ = (x << 16) | (y << 8) | (z);
  413. }
  414. //bitmap = caca_create_bitmap(16, 256, 256, 2 * 256, 0xf800, 0x07e0, 0x001f, 0x0000);
  415. bitmap = caca_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  416. caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
  417. bitmap, buffer);
  418. caca_free_bitmap(bitmap);
  419. }
  420. #endif
  421. static void draw_circle(int *buffer, int xo, int yo, int r, int mask, int val);
  422. static void demo_render(void)
  423. {
  424. struct caca_bitmap *bitmap;
  425. int buffer[256*256];
  426. int *dest;
  427. int x, y, z, xo, yo;
  428. static int i = 0;
  429. i++;
  430. dest = buffer;
  431. for(x = 0; x < 256; x++)
  432. for(y = 0; y < 256; y++)
  433. {
  434. *dest++ = 0xff000000;
  435. }
  436. /* red */
  437. xo = 128 + 48 * sin(0.02 * i);
  438. yo = 128 + 48 * cos(0.03 * i);
  439. for(z = 0; z < 240; z++)
  440. draw_circle(buffer, xo, yo, z, 0x00ff0000, 200 << 16);
  441. /* green */
  442. xo = 128 + 48 * sin(2 + 0.06 * i);
  443. yo = 128 + 48 * cos(2 + 0.05 * i);
  444. for(z = 0; z < 240; z++)
  445. draw_circle(buffer, xo, yo, z, 0x0000ff00, 200 << 8);
  446. /* blue */
  447. xo = 128 + 48 * sin(1 + 0.04 * i);
  448. yo = 128 + 48 * cos(1 + 0.03 * i);
  449. for(z = 0; z < 240; z++)
  450. draw_circle(buffer, xo, yo, z, 0x000000ff, 200);
  451. bitmap = caca_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  452. caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
  453. bitmap, (char *)buffer);
  454. caca_free_bitmap(bitmap);
  455. }
  456. static void draw_circle(int *buffer, int x, int y, int r, int mask, int val)
  457. {
  458. int t, dx, dy;
  459. #define POINT(X,Y) \
  460. buffer[(X) + 256 * (Y)] = 0xff000000 | (buffer[(X) + 256 * (Y)] & ~mask) | val;
  461. for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
  462. {
  463. POINT(x - dx / 3, y - dy / 3);
  464. POINT(x + dx / 3, y - dy / 3);
  465. POINT(x - dx / 3, y + dy / 3);
  466. POINT(x + dx / 3, y + dy / 3);
  467. POINT(x - dy / 3, y - dx / 3);
  468. POINT(x + dy / 3, y - dx / 3);
  469. POINT(x - dy / 3, y + dx / 3);
  470. POINT(x + dy / 3, y + dx / 3);
  471. t += t > 0 ? dx - dy-- : dx;
  472. }
  473. }