25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

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