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

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