Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

600 строки
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. #if 0
  32. cucul_sprite_t *sprite = NULL;
  33. #endif
  34. cucul_canvas_t *cv;
  35. caca_display_t *dp;
  36. int main(int argc, char **argv)
  37. {
  38. void (*demo)(void) = NULL;
  39. int quit = 0;
  40. cv = cucul_create_canvas(0, 0);
  41. if(!cv)
  42. return 1;
  43. dp = caca_create_display(cv);
  44. if(!dp)
  45. return 1;
  46. caca_set_delay(dp, 40000);
  47. /* Initialize data */
  48. #if 0
  49. sprite = cucul_load_sprite(DATADIR "/caca.txt");
  50. if(!sprite)
  51. sprite = cucul_load_sprite("caca.txt");
  52. if(!sprite)
  53. sprite = cucul_load_sprite("examples/caca.txt");
  54. #endif
  55. /* Disable cursor */
  56. caca_set_mouse(dp, 0);
  57. /* Main menu */
  58. display_menu();
  59. caca_refresh_display(dp);
  60. /* Go ! */
  61. while(!quit)
  62. {
  63. caca_event_t ev;
  64. int menu = 0, mouse = 0, xmouse = 0, ymouse = 0;
  65. while(caca_get_event(dp, CACA_EVENT_ANY, &ev, 0))
  66. {
  67. if(demo && (ev.type & CACA_EVENT_KEY_PRESS))
  68. {
  69. menu = 1;
  70. demo = NULL;
  71. }
  72. else if(ev.type & CACA_EVENT_KEY_PRESS)
  73. {
  74. switch(ev.data.key.ch)
  75. {
  76. case 'q':
  77. case 'Q':
  78. demo = NULL;
  79. quit = 1;
  80. break;
  81. case 'o':
  82. case 'O':
  83. outline = (outline + 1) % 3;
  84. display_menu();
  85. break;
  86. case 'b':
  87. case 'B':
  88. bounds = (bounds + 1) % 2;
  89. display_menu();
  90. break;
  91. #if 0
  92. case 'd':
  93. case 'D':
  94. dithering = (dithering + 1) % 5;
  95. cucul_set_feature(cv, dithering);
  96. display_menu();
  97. break;
  98. #endif
  99. case 'c':
  100. demo = demo_color;
  101. break;
  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(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_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(cv, CUCUL_COLOR_RED, CUCUL_COLOR_BLACK);
  156. cucul_putstr(cv, xmouse, ymouse, ".");
  157. cucul_putstr(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(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_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_rendertime(dp),
  170. (10000000 / caca_get_rendertime(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(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
  187. cucul_clear_canvas(cv);
  188. cucul_draw_thin_box(cv, 1, 1, xo, yo);
  189. cucul_putstr(cv, (xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
  190. cucul_putstr(cv, (xo - strlen("==============")) / 2, 4, "==============");
  191. cucul_putstr(cv, 4, 6, "demos:");
  192. cucul_putstr(cv, 4, 7, "'f': full");
  193. cucul_putstr(cv, 4, 8, "'1': dots");
  194. cucul_putstr(cv, 4, 9, "'2': lines");
  195. cucul_putstr(cv, 4, 10, "'3': boxes");
  196. cucul_putstr(cv, 4, 11, "'4': triangles");
  197. cucul_putstr(cv, 4, 12, "'5': ellipses");
  198. cucul_putstr(cv, 4, 13, "'c': colour");
  199. cucul_putstr(cv, 4, 14, "'r': render");
  200. #if 0
  201. if(sprite)
  202. cucul_putstr(cv, 4, 15, "'s': sprites");
  203. #endif
  204. cucul_putstr(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_putstr(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(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
  220. cucul_clear_canvas(cv);
  221. /* Draw the sun */
  222. cucul_set_color(cv, CUCUL_COLOR_YELLOW, CUCUL_COLOR_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(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK);
  233. cucul_fill_ellipse(cv, xo, yo, j, j / 2, "#");
  234. cucul_set_color(cv, CUCUL_COLOR_YELLOW, CUCUL_COLOR_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(cv, CUCUL_COLOR_GREEN, CUCUL_COLOR_BLACK);
  246. cucul_fill_triangle(cv, xo, yo, xb, yb, xa, ya, "%");
  247. cucul_set_color(cv, CUCUL_COLOR_YELLOW, CUCUL_COLOR_BLACK);
  248. cucul_draw_thin_triangle(cv, xo, yo, xb, yb, xa, ya);
  249. cucul_set_color(cv, CUCUL_COLOR_RED, CUCUL_COLOR_BLACK);
  250. cucul_fill_triangle(cv, xa, ya, xb, yb, xc, yc, "#");
  251. cucul_set_color(cv, CUCUL_COLOR_YELLOW, CUCUL_COLOR_BLACK);
  252. cucul_draw_thin_triangle(cv, xa, ya, xb, yb, xc, yc);
  253. cucul_set_color(cv, CUCUL_COLOR_BLUE, CUCUL_COLOR_BLACK);
  254. cucul_fill_triangle(cv, xo, yo, xb, yb, xc, yc, "%");
  255. cucul_set_color(cv, CUCUL_COLOR_YELLOW, CUCUL_COLOR_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(cv, CUCUL_COLOR_CYAN, CUCUL_COLOR_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(cv, cucul_rand(0, 16), cucul_rand(0, 16));
  280. cucul_putchar(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(cv, cucul_rand(0, 16), cucul_rand(0, 16));
  306. cucul_putchar(cv, cucul_rand(0, xmax), cucul_rand(0, ymax),
  307. chars[cucul_rand(0, 9)]);
  308. }
  309. }
  310. static void demo_color(void)
  311. {
  312. int i, j;
  313. char buf[BUFSIZ];
  314. cucul_set_color(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
  315. cucul_clear_canvas(cv);
  316. for(i = 0; i < 16; i++)
  317. {
  318. sprintf(buf, "'%c': %i (%s)", 'a' + i, i, cucul_get_color_name(i));
  319. cucul_set_color(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
  320. cucul_putstr(cv, 4, i + (i >= 8 ? 4 : 3), buf);
  321. for(j = 0; j < 16; j++)
  322. {
  323. cucul_set_color(cv, i, j);
  324. cucul_putstr(cv, (j >= 8 ? 41 : 40) + j * 2, i + (i >= 8 ? 4 : 3), "# ");
  325. }
  326. }
  327. }
  328. static void demo_lines(void)
  329. {
  330. int w = cucul_get_canvas_width(cv);
  331. int h = cucul_get_canvas_height(cv);
  332. int xa, ya, xb, yb;
  333. if(bounds)
  334. {
  335. xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h);
  336. xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h);
  337. }
  338. else
  339. {
  340. xa = cucul_rand(0, w); ya = cucul_rand(0, h);
  341. xb = cucul_rand(0, w); yb = cucul_rand(0, h);
  342. }
  343. cucul_set_color(cv, cucul_rand(0, 16), CUCUL_COLOR_BLACK);
  344. if(outline > 1)
  345. cucul_draw_thin_line(cv, xa, ya, xb, yb);
  346. else
  347. cucul_draw_line(cv, xa, ya, xb, yb, "#");
  348. }
  349. static void demo_boxes(void)
  350. {
  351. int w = cucul_get_canvas_width(cv);
  352. int h = cucul_get_canvas_height(cv);
  353. int xa, ya, xb, yb;
  354. if(bounds)
  355. {
  356. xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h);
  357. xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h);
  358. }
  359. else
  360. {
  361. xa = cucul_rand(0, w); ya = cucul_rand(0, h);
  362. xb = cucul_rand(0, w); yb = cucul_rand(0, h);
  363. }
  364. cucul_set_color(cv, cucul_rand(0, 16), cucul_rand(0, 16));
  365. cucul_fill_box(cv, xa, ya, xb, yb, "#");
  366. cucul_set_color(cv, cucul_rand(0, 16), CUCUL_COLOR_BLACK);
  367. if(outline == 2)
  368. cucul_draw_thin_box(cv, xa, ya, xb, yb);
  369. else if(outline == 1)
  370. cucul_draw_box(cv, xa, ya, xb, yb, "#");
  371. }
  372. static void demo_ellipses(void)
  373. {
  374. int w = cucul_get_canvas_width(cv);
  375. int h = cucul_get_canvas_height(cv);
  376. int x, y, a, b;
  377. if(bounds)
  378. {
  379. x = cucul_rand(- w, 2 * w); y = cucul_rand(- h, 2 * h);
  380. a = cucul_rand(0, w); b = cucul_rand(0, h);
  381. }
  382. else
  383. {
  384. do
  385. {
  386. x = cucul_rand(0, w); y = cucul_rand(0, h);
  387. a = cucul_rand(0, w); b = cucul_rand(0, h);
  388. } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
  389. }
  390. cucul_set_color(cv, cucul_rand(0, 16), cucul_rand(0, 16));
  391. cucul_fill_ellipse(cv, x, y, a, b, "#");
  392. cucul_set_color(cv, cucul_rand(0, 16), CUCUL_COLOR_BLACK);
  393. if(outline == 2)
  394. cucul_draw_thin_ellipse(cv, x, y, a, b);
  395. else if(outline == 1)
  396. cucul_draw_ellipse(cv, x, y, a, b, "#");
  397. }
  398. static void demo_triangles(void)
  399. {
  400. int w = cucul_get_canvas_width(cv);
  401. int h = cucul_get_canvas_height(cv);
  402. int xa, ya, xb, yb, xc, yc;
  403. if(bounds)
  404. {
  405. xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h);
  406. xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h);
  407. xc = cucul_rand(- w, 2 * w); yc = cucul_rand(- h, 2 * h);
  408. }
  409. else
  410. {
  411. xa = cucul_rand(0, w); ya = cucul_rand(0, h);
  412. xb = cucul_rand(0, w); yb = cucul_rand(0, h);
  413. xc = cucul_rand(0, w); yc = cucul_rand(0, h);
  414. }
  415. cucul_set_color(cv, cucul_rand(0, 16), cucul_rand(0, 16));
  416. cucul_fill_triangle(cv, xa, ya, xb, yb, xc, yc, "#");
  417. cucul_set_color(cv, cucul_rand(0, 16), CUCUL_COLOR_BLACK);
  418. if(outline == 2)
  419. cucul_draw_thin_triangle(cv, xa, ya, xb, yb, xc, yc);
  420. else if(outline == 1)
  421. cucul_draw_triangle(cv, xa, ya, xb, yb, xc, yc, "#");
  422. }
  423. static void demo_sprites(void)
  424. {
  425. #if 0
  426. cucul_draw_sprite(cv, cucul_rand(0, cucul_get_canvas_width(cv)),
  427. cucul_rand(0, cucul_get_canvas_height(cv)), sprite, 0);
  428. #endif
  429. }
  430. #if 0
  431. static void demo_render(void)
  432. {
  433. cucul_dither_t *dither;
  434. //short buffer[256*256];
  435. //short *dest = buffer;
  436. int buffer[256*256];
  437. int *dest = buffer;
  438. int x, y, z;
  439. static int i = 0;
  440. i = (i + 1) % 512;
  441. z = i < 256 ? i : 511 - i;
  442. for(x = 0; x < 256; x++)
  443. for(y = 0; y < 256; y++)
  444. {
  445. //*dest++ = ((x >> 3) << 11) | ((y >> 2) << 5) | ((z >> 3));
  446. *dest++ = (x << 16) | (y << 8) | (z);
  447. }
  448. cucul_set_dither_invert(dither, 1);
  449. //dither = cucul_create_dither(16, 256, 256, 2 * 256, 0xf800, 0x07e0, 0x001f, 0x0000);
  450. dither = cucul_create_dither(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  451. cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv), cucul_get_canvas_height(cv),
  452. dither, buffer);
  453. cucul_free_dither(dither);
  454. }
  455. #endif
  456. static void draw_circle(int *buffer, int xo, int yo, int r, int mask, int val);
  457. static void demo_render(void)
  458. {
  459. cucul_dither_t *dither;
  460. int buffer[256*256];
  461. int *dest;
  462. int x, y, z, xo, yo;
  463. static int i = 0;
  464. i++;
  465. dest = buffer;
  466. for(x = 0; x < 256; x++)
  467. for(y = 0; y < 256; y++)
  468. {
  469. *dest++ = 0xff000000;
  470. }
  471. /* red */
  472. xo = 128 + 48 * sin(0.02 * i);
  473. yo = 128 + 48 * cos(0.03 * i);
  474. for(z = 0; z < 240; z++)
  475. draw_circle(buffer, xo, yo, z, 0x00ff0000, 200 << 16);
  476. /* green */
  477. xo = 128 + 48 * sin(2 + 0.06 * i);
  478. yo = 128 + 48 * cos(2 + 0.05 * i);
  479. for(z = 0; z < 240; z++)
  480. draw_circle(buffer, xo, yo, z, 0x0000ff00, 200 << 8);
  481. /* blue */
  482. xo = 128 + 48 * sin(1 + 0.04 * i);
  483. yo = 128 + 48 * cos(1 + 0.03 * i);
  484. for(z = 0; z < 240; z++)
  485. draw_circle(buffer, xo, yo, z, 0x000000ff, 200);
  486. dither = cucul_create_dither(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  487. cucul_set_dither_invert(dither, 1);
  488. cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv), cucul_get_canvas_height(cv), dither, (char *)buffer);
  489. cucul_free_dither(dither);
  490. }
  491. static void draw_circle(int *buffer, int x, int y, int r, int mask, int val)
  492. {
  493. int t, dx, dy;
  494. #define POINT(X,Y) \
  495. buffer[(X) + 256 * (Y)] = 0xff000000 | (buffer[(X) + 256 * (Y)] & ~mask) | val;
  496. for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
  497. {
  498. POINT(x - dx / 3, y - dy / 3);
  499. POINT(x + dx / 3, y - dy / 3);
  500. POINT(x - dx / 3, y + dy / 3);
  501. POINT(x + dx / 3, y + dy / 3);
  502. POINT(x - dy / 3, y - dx / 3);
  503. POINT(x + dy / 3, y - dx / 3);
  504. POINT(x - dy / 3, y + dx / 3);
  505. POINT(x + dy / 3, y + dx / 3);
  506. t += t > 0 ? dx - dy-- : dx;
  507. }
  508. }