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.
 
 
 
 
 
 

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