You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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