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.
 
 
 
 
 
 

513 lines
14 KiB

  1. /*
  2. * demo demo using 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. #if 0
  28. #include <gdk/gdk.h>
  29. #include <gdk/gdkpixbuf.h>
  30. #endif
  31. #include "caca.h"
  32. static void display_menu(void);
  33. static void demo_all(void);
  34. static void demo_color(void);
  35. static void demo_dots(void);
  36. static void demo_lines(void);
  37. static void demo_boxes(void);
  38. static void demo_ellipses(void);
  39. static void demo_triangles(void);
  40. static void demo_sprites(void);
  41. #if 0
  42. static void demo_blit(void);
  43. #endif
  44. int bounds = 0;
  45. int outline = 0;
  46. int dithering = 0;
  47. struct caca_sprite *sprite = NULL;
  48. #if 0
  49. GdkPixbuf *pixbuf;
  50. char *pixels;
  51. int bufx, bufy, bufpitch;
  52. #endif
  53. int main(int argc, char **argv)
  54. {
  55. void (*demo)(void) = NULL;
  56. int quit = 0;
  57. if(caca_init())
  58. {
  59. return 1;
  60. }
  61. caca_set_delay(40000);
  62. /* Initialize data */
  63. sprite = caca_load_sprite(DATADIR "/caca.txt");
  64. if(!sprite)
  65. sprite = caca_load_sprite("caca.txt");
  66. if(!sprite)
  67. sprite = caca_load_sprite("examples/caca.txt");
  68. #if 0
  69. gdk_init (&argc, &argv);
  70. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/gally4.jpeg", NULL);
  71. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/badge1.jpeg", NULL);
  72. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/union.png", NULL);
  73. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/pikachu.jpeg", NULL);
  74. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/gradient.png", NULL);
  75. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/beastie.png", NULL);
  76. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/stitch.jpg", NULL);
  77. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/caca.jpg", NULL);
  78. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/dranac.jpeg", NULL);
  79. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/artwork/aboire.png", NULL);
  80. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/web/sam.zoy.org/artwork/goret.png", NULL);
  81. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/lilkim02.jpg", NULL);
  82. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/etw.bmp", NULL);
  83. pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/lena_std.png", NULL);
  84. if(!pixbuf) return -2;
  85. pixels = gdk_pixbuf_get_pixels(pixbuf);
  86. bufx = gdk_pixbuf_get_width(pixbuf);
  87. bufy = gdk_pixbuf_get_height(pixbuf);
  88. bufpitch = gdk_pixbuf_get_rowstride(pixbuf);
  89. fprintf(stderr, "bits: %i\n", gdk_pixbuf_get_bits_per_sample(pixbuf));
  90. fprintf(stderr, "w %i, h %i, stride %i\n", bufx, bufy, bufpitch);
  91. #endif
  92. /* Main menu */
  93. display_menu();
  94. caca_refresh();
  95. /* Go ! */
  96. while(!quit)
  97. {
  98. int event = caca_get_event();
  99. if(event && demo)
  100. {
  101. display_menu();
  102. caca_refresh();
  103. demo = NULL;
  104. }
  105. else if(event & CACA_EVENT_KEY_PRESS)
  106. {
  107. handle_key:
  108. switch(event & 0xffff)
  109. {
  110. case 'q':
  111. case 'Q':
  112. demo = NULL;
  113. quit = 1;
  114. break;
  115. case 'o':
  116. case 'O':
  117. outline = (outline + 1) % 3;
  118. display_menu();
  119. break;
  120. case 'b':
  121. case 'B':
  122. bounds = (bounds + 1) % 2;
  123. display_menu();
  124. break;
  125. case 'd':
  126. case 'D':
  127. dithering = (dithering + 1) % 3;
  128. caca_set_dithering(dithering == 0 ? CACA_DITHER_NONE :
  129. dithering == 1 ? CACA_DITHER_ORDERED :
  130. CACA_DITHER_RANDOM);
  131. display_menu();
  132. break;
  133. case 'c':
  134. demo = demo_color;
  135. break;
  136. case 'f':
  137. case 'F':
  138. demo = demo_all;
  139. break;
  140. case '1':
  141. demo = demo_dots;
  142. break;
  143. case '2':
  144. demo = demo_lines;
  145. break;
  146. case '3':
  147. demo = demo_boxes;
  148. break;
  149. case '4':
  150. demo = demo_triangles;
  151. break;
  152. case '5':
  153. demo = demo_ellipses;
  154. break;
  155. case 's':
  156. case 'S':
  157. demo = demo_sprites;
  158. break;
  159. #if 0
  160. case 'i':
  161. case 'I':
  162. demo = demo_blit;
  163. break;
  164. #endif
  165. }
  166. if(demo)
  167. caca_clear();
  168. handle_event:
  169. event = caca_get_event();
  170. if(event & CACA_EVENT_KEY_PRESS)
  171. goto handle_key;
  172. else if(event)
  173. goto handle_event;
  174. caca_refresh();
  175. }
  176. else if(event & CACA_EVENT_MOUSE_CLICK)
  177. {
  178. display_menu();
  179. caca_set_color(CACA_COLOR_RED);
  180. caca_putstr((event & 0xff00) >> 8, event & 0xff, "|\\");
  181. caca_refresh();
  182. }
  183. if(demo)
  184. {
  185. demo();
  186. caca_set_color(CACA_COLOR_WHITE);
  187. caca_draw_thin_box(1, 1, caca_get_width() - 2, caca_get_height() - 2);
  188. caca_printf(4, 1, "[%i.%i fps]----",
  189. 1000000 / caca_get_rendertime(),
  190. (10000000 / caca_get_rendertime()) % 10);
  191. caca_refresh();
  192. }
  193. }
  194. /* Clean up */
  195. caca_free_sprite(sprite);
  196. caca_end();
  197. return 0;
  198. }
  199. static void display_menu(void)
  200. {
  201. int xo = caca_get_width() - 2;
  202. int yo = caca_get_height() - 2;
  203. caca_clear();
  204. caca_set_color(CACA_COLOR_WHITE);
  205. caca_draw_thin_box(1, 1, xo, yo);
  206. caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
  207. caca_putstr((xo - strlen("==============")) / 2, 4, "==============");
  208. caca_putstr(4, 6, "demos:");
  209. caca_putstr(4, 7, "'f': full");
  210. caca_putstr(4, 8, "'1': dots");
  211. caca_putstr(4, 9, "'2': lines");
  212. caca_putstr(4, 10, "'3': boxes");
  213. caca_putstr(4, 11, "'4': triangles");
  214. caca_putstr(4, 12, "'5': ellipses");
  215. caca_putstr(4, 13, "'s': sprites");
  216. caca_putstr(4, 14, "'c': color");
  217. #if 0
  218. caca_putstr(4, 15, "'i': image blit");
  219. #endif
  220. caca_putstr(4, 17, "settings:");
  221. caca_printf(4, 18, "'o': outline: %s",
  222. outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
  223. caca_printf(4, 19, "'b': drawing boundaries: %s",
  224. bounds == 0 ? "screen" : "infinite");
  225. caca_printf(4, 20, "'d': %s dithering",
  226. dithering == 0 ? "no" : dithering == 1 ? "ordered" : "random");
  227. caca_putstr(4, yo - 2, "'q': quit");
  228. }
  229. static void demo_all(void)
  230. {
  231. static int i = 0;
  232. int j, xo, yo, xa, ya, xb, yb, xc, yc;
  233. i++;
  234. caca_clear();
  235. /* Draw the sun */
  236. caca_set_color(CACA_COLOR_YELLOW);
  237. xo = caca_get_width() / 4;
  238. yo = caca_get_height() / 4 + 5 * sin(0.03*i);
  239. for(j = 0; j < 16; j++)
  240. {
  241. xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
  242. ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
  243. caca_draw_thin_line(xo, yo, xa, ya);
  244. }
  245. j = 15 + sin(0.03*i) * 8;
  246. caca_set_color(CACA_COLOR_WHITE);
  247. caca_fill_ellipse(xo, yo, j, j / 2, '#');
  248. caca_set_color(CACA_COLOR_YELLOW);
  249. caca_draw_ellipse(xo, yo, j, j / 2, '#');
  250. /* Draw the pyramid */
  251. xo = caca_get_width() * 5 / 8;
  252. yo = 2;
  253. xa = caca_get_width() / 8 + sin(0.03*i) * 5;
  254. ya = caca_get_height() / 2 + cos(0.03*i) * 5;
  255. xb = caca_get_width() - 10 - cos(0.02*i) * 10;
  256. yb = caca_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
  257. xc = caca_get_width() / 4 - sin(0.02*i) * 5;
  258. yc = caca_get_height() * 3 / 4 + cos(0.02*i) * 5;
  259. caca_set_color(CACA_COLOR_GREEN);
  260. caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
  261. caca_set_color(CACA_COLOR_YELLOW);
  262. caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
  263. caca_set_color(CACA_COLOR_RED);
  264. caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  265. caca_set_color(CACA_COLOR_YELLOW);
  266. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  267. caca_set_color(CACA_COLOR_BLUE);
  268. caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
  269. caca_set_color(CACA_COLOR_YELLOW);
  270. caca_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
  271. /* Draw a background triangle */
  272. xa = 2;
  273. ya = 2;
  274. xb = caca_get_width() - 3;
  275. yb = caca_get_height() / 2;
  276. xc = caca_get_width() / 3;
  277. yc = caca_get_height() - 3;
  278. caca_set_color(CACA_COLOR_CYAN);
  279. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  280. xo = caca_get_width() / 2 + cos(0.027*i) * caca_get_width() / 3;
  281. yo = caca_get_height() / 2 - sin(0.027*i) * caca_get_height() / 2;
  282. caca_draw_thin_line(xa, ya, xo, yo);
  283. caca_draw_thin_line(xb, yb, xo, yo);
  284. caca_draw_thin_line(xc, yc, xo, yo);
  285. /* Draw a sprite on the pyramid */
  286. caca_draw_sprite(xo, yo, sprite, 0);
  287. /* Draw a trail behind the foreground sprite */
  288. for(j = i - 60; j < i; j++)
  289. {
  290. int delta = caca_rand(-5, 5);
  291. caca_set_color(caca_rand(0, 15));
  292. caca_putchar(caca_get_width() / 2
  293. + cos(0.02*j) * (delta + caca_get_width() / 4),
  294. caca_get_height() / 2
  295. + sin(0.02*j) * (delta + caca_get_height() / 3),
  296. '#');
  297. }
  298. /* Draw foreground sprite */
  299. caca_draw_sprite(caca_get_width() / 2 + cos(0.02*i) * caca_get_width() / 4,
  300. caca_get_height() / 2 + sin(0.02*i) * caca_get_height() / 3,
  301. sprite, 0);
  302. }
  303. static void demo_dots(void)
  304. {
  305. int xmax = caca_get_width() - 1;
  306. int ymax = caca_get_height() - 1;
  307. int i;
  308. for(i = 1000; i--;)
  309. {
  310. /* Putpixel */
  311. caca_set_color(caca_rand(0, 15));
  312. caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax), '#');
  313. }
  314. }
  315. static void demo_color(void)
  316. {
  317. int i;
  318. char buf[BUFSIZ];
  319. caca_clear();
  320. for(i = 0; i < 16; i++)
  321. {
  322. sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i));
  323. caca_set_color(CACA_COLOR_WHITE);
  324. caca_putstr(4, i + 3, buf);
  325. caca_set_color(i);
  326. caca_putstr(40, i + 3, "XXXXXXXXXX-XX--X----------");
  327. }
  328. }
  329. static void demo_lines(void)
  330. {
  331. int w = caca_get_width();
  332. int h = caca_get_height();
  333. int xa, ya, xb, yb;
  334. if(bounds)
  335. {
  336. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  337. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  338. }
  339. else
  340. {
  341. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  342. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  343. }
  344. caca_set_color(caca_rand(0, 15));
  345. if(outline > 1)
  346. caca_draw_thin_line(xa, ya, xb, yb);
  347. else
  348. caca_draw_line(xa, ya, xb, yb, '#');
  349. }
  350. static void demo_boxes(void)
  351. {
  352. int w = caca_get_width();
  353. int h = caca_get_height();
  354. int xa, ya, xb, yb;
  355. if(bounds)
  356. {
  357. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  358. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  359. }
  360. else
  361. {
  362. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  363. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  364. }
  365. caca_set_color(caca_rand(0, 15));
  366. caca_fill_box(xa, ya, xb, yb, '#');
  367. caca_set_color(caca_rand(0, 15));
  368. if(outline == 2)
  369. caca_draw_thin_box(xa, ya, xb, yb);
  370. else if(outline == 1)
  371. caca_draw_box(xa, ya, xb, yb, '#');
  372. }
  373. static void demo_ellipses(void)
  374. {
  375. int w = caca_get_width();
  376. int h = caca_get_height();
  377. int x, y, a, b;
  378. if(bounds)
  379. {
  380. x = caca_rand(- w, 2 * w); y = caca_rand(- h, 2 * h);
  381. a = caca_rand(0, w); b = caca_rand(0, h);
  382. }
  383. else
  384. {
  385. do
  386. {
  387. x = caca_rand(0, w); y = caca_rand(0, h);
  388. a = caca_rand(0, w); b = caca_rand(0, h);
  389. } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
  390. }
  391. caca_set_color(caca_rand(0, 15));
  392. caca_fill_ellipse(x, y, a, b, '#');
  393. caca_set_color(caca_rand(0, 15));
  394. if(outline == 2)
  395. caca_draw_thin_ellipse(x, y, a, b);
  396. else if(outline == 1)
  397. caca_draw_ellipse(x, y, a, b, '#');
  398. }
  399. static void demo_triangles(void)
  400. {
  401. int w = caca_get_width();
  402. int h = caca_get_height();
  403. int xa, ya, xb, yb, xc, yc;
  404. if(bounds)
  405. {
  406. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  407. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  408. xc = caca_rand(- w, 2 * w); yc = caca_rand(- h, 2 * h);
  409. }
  410. else
  411. {
  412. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  413. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  414. xc = caca_rand(0, w - 1); yc = caca_rand(0, h - 1);
  415. }
  416. caca_set_color(caca_rand(0, 15));
  417. caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  418. caca_set_color(caca_rand(0, 15));
  419. if(outline == 2)
  420. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  421. else if(outline == 1)
  422. caca_draw_triangle(xa, ya, xb, yb, xc, yc, '#');
  423. }
  424. static void demo_sprites(void)
  425. {
  426. caca_draw_sprite(caca_rand(0, caca_get_width() - 1),
  427. caca_rand(0, caca_get_height() - 1), sprite, 0);
  428. }
  429. #if 0
  430. static void demo_blit(void)
  431. {
  432. caca_blit(6, 4, caca_get_width() - 6, caca_get_height() - 4,
  433. pixels, bufx, bufy);
  434. }
  435. #endif