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.

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