您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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