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.
 
 
 
 
 
 

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