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