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.
 
 
 
 
 
 

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