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

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