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

583 строки
16 KiB

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