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.
 
 
 
 
 
 

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