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

577 行
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 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 "common.h"
  15. #if !defined(__KERNEL__)
  16. # include <math.h>
  17. # include <string.h>
  18. # include <stdio.h>
  19. #endif
  20. #include "caca.h"
  21. static void display_menu(void);
  22. static void demo_all(void);
  23. static void demo_dots(void);
  24. static void demo_lines(void);
  25. static void demo_boxes(void);
  26. static void demo_ellipses(void);
  27. static void demo_triangles(void);
  28. /*static void demo_sprites(void);*/
  29. static void demo_render(void);
  30. int bounds = 0;
  31. int outline = 0;
  32. int dithering = 0;
  33. #if 0
  34. cucul_sprite_t *sprite = NULL;
  35. #endif
  36. cucul_canvas_t *cv;
  37. caca_display_t *dp;
  38. int main(int argc, char **argv)
  39. {
  40. void (*demo)(void) = NULL;
  41. int quit = 0;
  42. cv = cucul_create_canvas(0, 0);
  43. if(!cv)
  44. return 1;
  45. dp = caca_create_display(cv);
  46. if(!dp)
  47. return 1;
  48. caca_set_display_time(dp, 40000);
  49. /* Initialize data */
  50. #if 0
  51. sprite = cucul_load_sprite(DATADIR "/caca.txt");
  52. if(!sprite)
  53. sprite = cucul_load_sprite("caca.txt");
  54. if(!sprite)
  55. sprite = cucul_load_sprite("examples/caca.txt");
  56. #endif
  57. /* Disable cursor */
  58. caca_set_mouse(dp, 0);
  59. /* Main menu */
  60. display_menu();
  61. caca_refresh_display(dp);
  62. /* Go ! */
  63. while(!quit)
  64. {
  65. caca_event_t ev;
  66. int menu = 0, mouse = 0, xmouse = 0, ymouse = 0;
  67. while(caca_get_event(dp, CACA_EVENT_ANY, &ev, 0))
  68. {
  69. if(demo && (ev.type & CACA_EVENT_KEY_PRESS))
  70. {
  71. menu = 1;
  72. demo = NULL;
  73. }
  74. else if(ev.type & CACA_EVENT_KEY_PRESS)
  75. {
  76. switch(ev.data.key.ch)
  77. {
  78. case 'q':
  79. case 'Q':
  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. #if 0
  94. case 'd':
  95. case 'D':
  96. dithering = (dithering + 1) % 5;
  97. cucul_set_feature(cv, dithering);
  98. display_menu();
  99. break;
  100. #endif
  101. case 'f':
  102. case 'F':
  103. demo = demo_all;
  104. break;
  105. case '1':
  106. demo = demo_dots;
  107. break;
  108. case '2':
  109. demo = demo_lines;
  110. break;
  111. case '3':
  112. demo = demo_boxes;
  113. break;
  114. case '4':
  115. demo = demo_triangles;
  116. break;
  117. case '5':
  118. demo = demo_ellipses;
  119. break;
  120. #if 0
  121. case 's':
  122. case 'S':
  123. if(sprite)
  124. demo = demo_sprites;
  125. break;
  126. #endif
  127. case 'r':
  128. case 'R':
  129. demo = demo_render;
  130. break;
  131. }
  132. if(demo)
  133. {
  134. cucul_set_color_ansi(cv, CUCUL_LIGHTGRAY, CUCUL_BLACK);
  135. cucul_clear_canvas(cv);
  136. }
  137. }
  138. else if(ev.type & CACA_EVENT_MOUSE_MOTION)
  139. {
  140. mouse = 1;
  141. xmouse = ev.data.mouse.x;
  142. ymouse = ev.data.mouse.y;
  143. }
  144. else if(ev.type & CACA_EVENT_RESIZE)
  145. {
  146. mouse = 1; /* old hack */
  147. }
  148. }
  149. if(menu || (mouse && !demo))
  150. {
  151. display_menu();
  152. if(mouse && !demo)
  153. {
  154. cucul_set_color_ansi(cv, CUCUL_RED, CUCUL_BLACK);
  155. cucul_putstr(cv, xmouse, ymouse, ".");
  156. cucul_putstr(cv, xmouse, ymouse + 1, "|\\");
  157. }
  158. caca_refresh_display(dp);
  159. mouse = menu = 0;
  160. }
  161. if(demo)
  162. {
  163. demo();
  164. cucul_set_color_ansi(cv, CUCUL_LIGHTGRAY, CUCUL_BLACK);
  165. cucul_draw_thin_box(cv, 1, 1, cucul_get_canvas_width(cv) - 2,
  166. cucul_get_canvas_height(cv) - 2);
  167. cucul_printf(cv, 4, 1, "[%i.%i fps]----",
  168. 1000000 / caca_get_display_time(dp),
  169. (10000000 / caca_get_display_time(dp)) % 10);
  170. caca_refresh_display(dp);
  171. }
  172. }
  173. /* Clean up */
  174. #if 0
  175. cucul_free_sprite(sprite);
  176. #endif
  177. caca_free_display(dp);
  178. cucul_free_canvas(cv);
  179. return 0;
  180. }
  181. static void display_menu(void)
  182. {
  183. int xo = cucul_get_canvas_width(cv) - 2;
  184. int yo = cucul_get_canvas_height(cv) - 2;
  185. cucul_set_color_ansi(cv, CUCUL_LIGHTGRAY, CUCUL_BLACK);
  186. cucul_clear_canvas(cv);
  187. cucul_draw_thin_box(cv, 1, 1, xo, yo);
  188. cucul_putstr(cv, (xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
  189. cucul_putstr(cv, (xo - strlen("==============")) / 2, 4, "==============");
  190. cucul_putstr(cv, 4, 6, "demos:");
  191. cucul_putstr(cv, 4, 7, "'f': full");
  192. cucul_putstr(cv, 4, 8, "'1': dots");
  193. cucul_putstr(cv, 4, 9, "'2': lines");
  194. cucul_putstr(cv, 4, 10, "'3': boxes");
  195. cucul_putstr(cv, 4, 11, "'4': triangles");
  196. cucul_putstr(cv, 4, 12, "'5': ellipses");
  197. cucul_putstr(cv, 4, 13, "'c': colour");
  198. cucul_putstr(cv, 4, 14, "'r': render");
  199. #if 0
  200. if(sprite)
  201. cucul_putstr(cv, 4, 15, "'s': sprites");
  202. #endif
  203. cucul_putstr(cv, 4, 16, "settings:");
  204. cucul_printf(cv, 4, 17, "'o': outline: %s",
  205. outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
  206. cucul_printf(cv, 4, 18, "'b': drawing boundaries: %s",
  207. bounds == 0 ? "screen" : "infinite");
  208. //cucul_printf(cv, 4, 19, "'d': dithering (%s)",
  209. // cucul_get_feature_name(dithering));
  210. cucul_putstr(cv, 4, yo - 2, "'q': quit");
  211. caca_refresh_display(dp);
  212. }
  213. static void demo_all(void)
  214. {
  215. static int i = 0;
  216. int j, xo, yo, xa, ya, xb, yb, xc, yc;
  217. i++;
  218. cucul_set_color_ansi(cv, CUCUL_LIGHTGRAY, CUCUL_BLACK);
  219. cucul_clear_canvas(cv);
  220. /* Draw the sun */
  221. cucul_set_color_ansi(cv, CUCUL_YELLOW, CUCUL_BLACK);
  222. xo = cucul_get_canvas_width(cv) / 4;
  223. yo = cucul_get_canvas_height(cv) / 4 + 5 * sin(0.03*i);
  224. for(j = 0; j < 16; j++)
  225. {
  226. xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
  227. ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
  228. cucul_draw_thin_line(cv, xo, yo, xa, ya);
  229. }
  230. j = 15 + sin(0.03*i) * 8;
  231. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
  232. cucul_fill_ellipse(cv, xo, yo, j, j / 2, "#");
  233. cucul_set_color_ansi(cv, CUCUL_YELLOW, CUCUL_BLACK);
  234. cucul_draw_ellipse(cv, xo, yo, j, j / 2, "#");
  235. /* Draw the pyramid */
  236. xo = cucul_get_canvas_width(cv) * 5 / 8;
  237. yo = 2;
  238. xa = cucul_get_canvas_width(cv) / 8 + sin(0.03*i) * 5;
  239. ya = cucul_get_canvas_height(cv) / 2 + cos(0.03*i) * 5;
  240. xb = cucul_get_canvas_width(cv) - 10 - cos(0.02*i) * 10;
  241. yb = cucul_get_canvas_height(cv) * 3 / 4 - 5 + sin(0.02*i) * 5;
  242. xc = cucul_get_canvas_width(cv) / 4 - sin(0.02*i) * 5;
  243. yc = cucul_get_canvas_height(cv) * 3 / 4 + cos(0.02*i) * 5;
  244. cucul_set_color_ansi(cv, CUCUL_GREEN, CUCUL_BLACK);
  245. cucul_fill_triangle(cv, xo, yo, xb, yb, xa, ya, "%");
  246. cucul_set_color_ansi(cv, CUCUL_YELLOW, CUCUL_BLACK);
  247. cucul_draw_thin_triangle(cv, xo, yo, xb, yb, xa, ya);
  248. cucul_set_color_ansi(cv, CUCUL_RED, CUCUL_BLACK);
  249. cucul_fill_triangle(cv, xa, ya, xb, yb, xc, yc, "#");
  250. cucul_set_color_ansi(cv, CUCUL_YELLOW, CUCUL_BLACK);
  251. cucul_draw_thin_triangle(cv, xa, ya, xb, yb, xc, yc);
  252. cucul_set_color_ansi(cv, CUCUL_BLUE, CUCUL_BLACK);
  253. cucul_fill_triangle(cv, xo, yo, xb, yb, xc, yc, "%");
  254. cucul_set_color_ansi(cv, CUCUL_YELLOW, CUCUL_BLACK);
  255. cucul_draw_thin_triangle(cv, xo, yo, xb, yb, xc, yc);
  256. /* Draw a background triangle */
  257. xa = 2;
  258. ya = 2;
  259. xb = cucul_get_canvas_width(cv) - 3;
  260. yb = cucul_get_canvas_height(cv) / 2;
  261. xc = cucul_get_canvas_width(cv) / 3;
  262. yc = cucul_get_canvas_height(cv) - 3;
  263. cucul_set_color_ansi(cv, CUCUL_CYAN, CUCUL_BLACK);
  264. cucul_draw_thin_triangle(cv, xa, ya, xb, yb, xc, yc);
  265. xo = cucul_get_canvas_width(cv) / 2 + cos(0.027*i) * cucul_get_canvas_width(cv) / 3;
  266. yo = cucul_get_canvas_height(cv) / 2 - sin(0.027*i) * cucul_get_canvas_height(cv) / 2;
  267. cucul_draw_thin_line(cv, xa, ya, xo, yo);
  268. cucul_draw_thin_line(cv, xb, yb, xo, yo);
  269. cucul_draw_thin_line(cv, xc, yc, xo, yo);
  270. /* Draw a sprite on the pyramid */
  271. #if 0
  272. cucul_draw_sprite(cv, xo, yo, sprite, 0);
  273. #endif
  274. /* Draw a trail behind the foreground sprite */
  275. for(j = i - 60; j < i; j++)
  276. {
  277. int delta = cucul_rand(-5, 6);
  278. cucul_set_color_ansi(cv, cucul_rand(0, 16), cucul_rand(0, 16));
  279. cucul_putchar(cv, cucul_get_canvas_width(cv) / 2
  280. + cos(0.02*j) * (delta + cucul_get_canvas_width(cv) / 4),
  281. cucul_get_canvas_height(cv) / 2
  282. + sin(0.02*j) * (delta + cucul_get_canvas_height(cv) / 3),
  283. '#');
  284. }
  285. /* Draw foreground sprite */
  286. #if 0
  287. cucul_draw_sprite(cv, cucul_get_canvas_width(cv) / 2 + cos(0.02*i) * cucul_get_canvas_width(cv) / 4,
  288. cucul_get_canvas_height(cv) / 2 + sin(0.02*i) * cucul_get_canvas_height(cv) / 3,
  289. sprite, 0);
  290. #endif
  291. }
  292. static void demo_dots(void)
  293. {
  294. int xmax = cucul_get_canvas_width(cv);
  295. int ymax = cucul_get_canvas_height(cv);
  296. int i;
  297. static char chars[10] =
  298. {
  299. '+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W'
  300. };
  301. for(i = 1000; i--;)
  302. {
  303. /* Putpixel */
  304. cucul_set_color_ansi(cv, cucul_rand(0, 16), cucul_rand(0, 16));
  305. cucul_putchar(cv, cucul_rand(0, xmax), cucul_rand(0, ymax),
  306. chars[cucul_rand(0, 9)]);
  307. }
  308. }
  309. static void demo_lines(void)
  310. {
  311. int w = cucul_get_canvas_width(cv);
  312. int h = cucul_get_canvas_height(cv);
  313. int xa, ya, xb, yb;
  314. if(bounds)
  315. {
  316. xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h);
  317. xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h);
  318. }
  319. else
  320. {
  321. xa = cucul_rand(0, w); ya = cucul_rand(0, h);
  322. xb = cucul_rand(0, w); yb = cucul_rand(0, h);
  323. }
  324. cucul_set_color_ansi(cv, cucul_rand(0, 16), CUCUL_BLACK);
  325. if(outline > 1)
  326. cucul_draw_thin_line(cv, xa, ya, xb, yb);
  327. else
  328. cucul_draw_line(cv, xa, ya, xb, yb, "#");
  329. }
  330. static void demo_boxes(void)
  331. {
  332. int w = cucul_get_canvas_width(cv);
  333. int h = cucul_get_canvas_height(cv);
  334. int xa, ya, xb, yb;
  335. if(bounds)
  336. {
  337. xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h);
  338. xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h);
  339. }
  340. else
  341. {
  342. xa = cucul_rand(0, w); ya = cucul_rand(0, h);
  343. xb = cucul_rand(0, w); yb = cucul_rand(0, h);
  344. }
  345. cucul_set_color_ansi(cv, cucul_rand(0, 16), cucul_rand(0, 16));
  346. cucul_fill_box(cv, xa, ya, xb, yb, "#");
  347. cucul_set_color_ansi(cv, cucul_rand(0, 16), CUCUL_BLACK);
  348. if(outline == 2)
  349. cucul_draw_thin_box(cv, xa, ya, xb, yb);
  350. else if(outline == 1)
  351. cucul_draw_box(cv, xa, ya, xb, yb, "#");
  352. }
  353. static void demo_ellipses(void)
  354. {
  355. int w = cucul_get_canvas_width(cv);
  356. int h = cucul_get_canvas_height(cv);
  357. int x, y, a, b;
  358. if(bounds)
  359. {
  360. x = cucul_rand(- w, 2 * w); y = cucul_rand(- h, 2 * h);
  361. a = cucul_rand(0, w); b = cucul_rand(0, h);
  362. }
  363. else
  364. {
  365. do
  366. {
  367. x = cucul_rand(0, w); y = cucul_rand(0, h);
  368. a = cucul_rand(0, w); b = cucul_rand(0, h);
  369. } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
  370. }
  371. cucul_set_color_ansi(cv, cucul_rand(0, 16), cucul_rand(0, 16));
  372. cucul_fill_ellipse(cv, x, y, a, b, "#");
  373. cucul_set_color_ansi(cv, cucul_rand(0, 16), CUCUL_BLACK);
  374. if(outline == 2)
  375. cucul_draw_thin_ellipse(cv, x, y, a, b);
  376. else if(outline == 1)
  377. cucul_draw_ellipse(cv, x, y, a, b, "#");
  378. }
  379. static void demo_triangles(void)
  380. {
  381. int w = cucul_get_canvas_width(cv);
  382. int h = cucul_get_canvas_height(cv);
  383. int xa, ya, xb, yb, xc, yc;
  384. if(bounds)
  385. {
  386. xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h);
  387. xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h);
  388. xc = cucul_rand(- w, 2 * w); yc = cucul_rand(- h, 2 * h);
  389. }
  390. else
  391. {
  392. xa = cucul_rand(0, w); ya = cucul_rand(0, h);
  393. xb = cucul_rand(0, w); yb = cucul_rand(0, h);
  394. xc = cucul_rand(0, w); yc = cucul_rand(0, h);
  395. }
  396. cucul_set_color_ansi(cv, cucul_rand(0, 16), cucul_rand(0, 16));
  397. cucul_fill_triangle(cv, xa, ya, xb, yb, xc, yc, "#");
  398. cucul_set_color_ansi(cv, cucul_rand(0, 16), CUCUL_BLACK);
  399. if(outline == 2)
  400. cucul_draw_thin_triangle(cv, xa, ya, xb, yb, xc, yc);
  401. else if(outline == 1)
  402. cucul_draw_triangle(cv, xa, ya, xb, yb, xc, yc, "#");
  403. }
  404. #if 0
  405. static void demo_sprites(void)
  406. {
  407. cucul_draw_sprite(cv, cucul_rand(0, cucul_get_canvas_width(cv)),
  408. cucul_rand(0, cucul_get_canvas_height(cv)), sprite, 0);
  409. }
  410. #endif
  411. #if 0
  412. static void demo_render(void)
  413. {
  414. cucul_dither_t *dither;
  415. //short buffer[256*256];
  416. //short *dest = buffer;
  417. int buffer[256*256];
  418. int *dest = buffer;
  419. int x, y, z;
  420. static int i = 0;
  421. i = (i + 1) % 512;
  422. z = i < 256 ? i : 511 - i;
  423. for(x = 0; x < 256; x++)
  424. for(y = 0; y < 256; y++)
  425. {
  426. //*dest++ = ((x >> 3) << 11) | ((y >> 2) << 5) | ((z >> 3));
  427. *dest++ = (x << 16) | (y << 8) | (z);
  428. }
  429. cucul_set_dither_invert(dither, 1);
  430. //dither = cucul_create_dither(16, 256, 256, 2 * 256, 0xf800, 0x07e0, 0x001f, 0x0000);
  431. dither = cucul_create_dither(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  432. cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv), cucul_get_canvas_height(cv),
  433. dither, buffer);
  434. cucul_free_dither(dither);
  435. }
  436. #endif
  437. static void draw_circle(int *buffer, int xo, int yo, int r, int mask, int val);
  438. static void demo_render(void)
  439. {
  440. cucul_dither_t *dither;
  441. int buffer[256*256];
  442. int *dest;
  443. int x, y, z, xo, yo;
  444. static int i = 0;
  445. i++;
  446. dest = buffer;
  447. for(x = 0; x < 256; x++)
  448. for(y = 0; y < 256; y++)
  449. {
  450. *dest++ = 0xff000000;
  451. }
  452. /* red */
  453. xo = 128 + 48 * sin(0.02 * i);
  454. yo = 128 + 48 * cos(0.03 * i);
  455. for(z = 0; z < 240; z++)
  456. draw_circle(buffer, xo, yo, z, 0x00ff0000, 200 << 16);
  457. /* green */
  458. xo = 128 + 48 * sin(2 + 0.06 * i);
  459. yo = 128 + 48 * cos(2 + 0.05 * i);
  460. for(z = 0; z < 240; z++)
  461. draw_circle(buffer, xo, yo, z, 0x0000ff00, 200 << 8);
  462. /* blue */
  463. xo = 128 + 48 * sin(1 + 0.04 * i);
  464. yo = 128 + 48 * cos(1 + 0.03 * i);
  465. for(z = 0; z < 240; z++)
  466. draw_circle(buffer, xo, yo, z, 0x000000ff, 200);
  467. dither = cucul_create_dither(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  468. cucul_set_dither_invert(dither, 1);
  469. cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv), cucul_get_canvas_height(cv), dither, (char *)buffer);
  470. cucul_free_dither(dither);
  471. }
  472. static void draw_circle(int *buffer, int x, int y, int r, int mask, int val)
  473. {
  474. int t, dx, dy;
  475. #define POINT(X,Y) \
  476. buffer[(X) + 256 * (Y)] = 0xff000000 | (buffer[(X) + 256 * (Y)] & ~mask) | val;
  477. for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
  478. {
  479. POINT(x - dx / 3, y - dy / 3);
  480. POINT(x + dx / 3, y - dy / 3);
  481. POINT(x - dx / 3, y + dy / 3);
  482. POINT(x + dx / 3, y + dy / 3);
  483. POINT(x - dy / 3, y - dx / 3);
  484. POINT(x + dy / 3, y - dx / 3);
  485. POINT(x - dy / 3, y + dx / 3);
  486. POINT(x + dy / 3, y + dx / 3);
  487. t += t > 0 ? dx - dy-- : dx;
  488. }
  489. }