Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

583 рядки
16 KiB

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