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

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