25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

568 satır
15 KiB

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