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.
 
 
 
 
 
 

582 lines
16 KiB

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