選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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