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

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