25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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