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.
 
 
 
 
 
 

501 satır
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. #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. else if(event & CACA_EVENT_MOUSE_CLICK)
  167. {
  168. display_menu();
  169. caca_set_color(CACA_COLOR_RED);
  170. caca_putstr((event & 0xff00) >> 8, event & 0xff, "|\\");
  171. caca_refresh();
  172. }
  173. if(demo)
  174. {
  175. demo();
  176. caca_set_color(CACA_COLOR_WHITE);
  177. caca_draw_thin_box(1, 1, caca_get_width() - 2, caca_get_height() - 2);
  178. caca_printf(4, 1, "[%i.%i fps]----",
  179. 1000000 / caca_get_rendertime(),
  180. (10000000 / caca_get_rendertime()) % 10);
  181. caca_refresh();
  182. }
  183. }
  184. /* Clean up */
  185. caca_free_sprite(sprite);
  186. caca_end();
  187. return 0;
  188. }
  189. static void display_menu(void)
  190. {
  191. int xo = caca_get_width() - 2;
  192. int yo = caca_get_height() - 2;
  193. caca_clear();
  194. caca_set_color(CACA_COLOR_WHITE);
  195. caca_draw_thin_box(1, 1, xo, yo);
  196. caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
  197. caca_putstr((xo - strlen("==============")) / 2, 4, "==============");
  198. caca_putstr(4, 6, "demos:");
  199. caca_putstr(4, 7, "'f': full");
  200. caca_putstr(4, 8, "'1': dots");
  201. caca_putstr(4, 9, "'2': lines");
  202. caca_putstr(4, 10, "'3': boxes");
  203. caca_putstr(4, 11, "'4': triangles");
  204. caca_putstr(4, 12, "'5': ellipses");
  205. caca_putstr(4, 13, "'s': sprites");
  206. caca_putstr(4, 14, "'c': color");
  207. caca_putstr(4, 15, "'i': image blit");
  208. caca_putstr(4, 17, "settings:");
  209. caca_printf(4, 18, "'o': outline: %s",
  210. outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
  211. caca_printf(4, 19, "'b': drawing boundaries: %s",
  212. bounds == 0 ? "screen" : "infinite");
  213. caca_printf(4, 20, "'d': %s dithering",
  214. dithering == 0 ? "no" : dithering == 1 ? "ordered" : "random");
  215. caca_putstr(4, yo - 2, "'q': quit");
  216. }
  217. static void demo_all(void)
  218. {
  219. static int i = 0;
  220. int j, xo, yo, xa, ya, xb, yb, xc, yc;
  221. i++;
  222. caca_clear();
  223. /* Draw the sun */
  224. caca_set_color(CACA_COLOR_YELLOW);
  225. xo = caca_get_width() / 4;
  226. yo = caca_get_height() / 4 + 5 * sin(0.03*i);
  227. for(j = 0; j < 16; j++)
  228. {
  229. xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
  230. ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
  231. caca_draw_thin_line(xo, yo, xa, ya);
  232. }
  233. j = 15 + sin(0.03*i) * 8;
  234. caca_set_color(CACA_COLOR_WHITE);
  235. caca_fill_ellipse(xo, yo, j, j / 2, '#');
  236. caca_set_color(CACA_COLOR_YELLOW);
  237. caca_draw_ellipse(xo, yo, j, j / 2, '#');
  238. /* Draw the pyramid */
  239. xo = caca_get_width() * 5 / 8;
  240. yo = 2;
  241. xa = caca_get_width() / 8 + sin(0.03*i) * 5;
  242. ya = caca_get_height() / 2 + cos(0.03*i) * 5;
  243. xb = caca_get_width() - 10 - cos(0.02*i) * 10;
  244. yb = caca_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
  245. xc = caca_get_width() / 4 - sin(0.02*i) * 5;
  246. yc = caca_get_height() * 3 / 4 + cos(0.02*i) * 5;
  247. caca_set_color(CACA_COLOR_GREEN);
  248. caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
  249. caca_set_color(CACA_COLOR_YELLOW);
  250. caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
  251. caca_set_color(CACA_COLOR_RED);
  252. caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  253. caca_set_color(CACA_COLOR_YELLOW);
  254. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  255. caca_set_color(CACA_COLOR_BLUE);
  256. caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
  257. caca_set_color(CACA_COLOR_YELLOW);
  258. caca_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
  259. /* Draw a background triangle */
  260. xa = 2;
  261. ya = 2;
  262. xb = caca_get_width() - 3;
  263. yb = caca_get_height() / 2;
  264. xc = caca_get_width() / 3;
  265. yc = caca_get_height() - 3;
  266. caca_set_color(CACA_COLOR_CYAN);
  267. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  268. xo = caca_get_width() / 2 + cos(0.027*i) * caca_get_width() / 3;
  269. yo = caca_get_height() / 2 - sin(0.027*i) * caca_get_height() / 2;
  270. caca_draw_thin_line(xa, ya, xo, yo);
  271. caca_draw_thin_line(xb, yb, xo, yo);
  272. caca_draw_thin_line(xc, yc, xo, yo);
  273. /* Draw a sprite on the pyramid */
  274. caca_draw_sprite(xo, yo, sprite, 0);
  275. /* Draw a trail behind the foreground sprite */
  276. for(j = i - 60; j < i; j++)
  277. {
  278. int delta = caca_rand(-5, 5);
  279. caca_set_color(caca_rand(0, 15));
  280. caca_putchar(caca_get_width() / 2
  281. + cos(0.02*j) * (delta + caca_get_width() / 4),
  282. caca_get_height() / 2
  283. + sin(0.02*j) * (delta + caca_get_height() / 3),
  284. '#');
  285. }
  286. /* Draw foreground sprite */
  287. caca_draw_sprite(caca_get_width() / 2 + cos(0.02*i) * caca_get_width() / 4,
  288. caca_get_height() / 2 + sin(0.02*i) * caca_get_height() / 3,
  289. sprite, 0);
  290. }
  291. static void demo_dots(void)
  292. {
  293. int xmax = caca_get_width() - 1;
  294. int ymax = caca_get_height() - 1;
  295. int i;
  296. for(i = 1000; i--;)
  297. {
  298. /* Putpixel */
  299. caca_set_color(caca_rand(0, 15));
  300. caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax), '#');
  301. }
  302. }
  303. static void demo_color(void)
  304. {
  305. int i;
  306. char buf[BUFSIZ];
  307. caca_clear();
  308. for(i = 0; i < 16; i++)
  309. {
  310. sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i));
  311. caca_set_color(CACA_COLOR_WHITE);
  312. caca_putstr(4, i + 3, buf);
  313. caca_set_color(i);
  314. caca_putstr(40, i + 3, "XXXXXXXXXX-XX--X----------");
  315. }
  316. }
  317. static void demo_lines(void)
  318. {
  319. int w = caca_get_width();
  320. int h = caca_get_height();
  321. int xa, ya, xb, yb;
  322. if(bounds)
  323. {
  324. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  325. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  326. }
  327. else
  328. {
  329. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  330. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  331. }
  332. caca_set_color(caca_rand(0, 15));
  333. if(outline > 1)
  334. caca_draw_thin_line(xa, ya, xb, yb);
  335. else
  336. caca_draw_line(xa, ya, xb, yb, '#');
  337. }
  338. static void demo_boxes(void)
  339. {
  340. int w = caca_get_width();
  341. int h = caca_get_height();
  342. int xa, ya, xb, yb;
  343. if(bounds)
  344. {
  345. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  346. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  347. }
  348. else
  349. {
  350. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  351. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  352. }
  353. caca_set_color(caca_rand(0, 15));
  354. caca_fill_box(xa, ya, xb, yb, '#');
  355. caca_set_color(caca_rand(0, 15));
  356. if(outline == 2)
  357. caca_draw_thin_box(xa, ya, xb, yb);
  358. else if(outline == 1)
  359. caca_draw_box(xa, ya, xb, yb, '#');
  360. }
  361. static void demo_ellipses(void)
  362. {
  363. int w = caca_get_width();
  364. int h = caca_get_height();
  365. int x, y, a, b;
  366. if(bounds)
  367. {
  368. x = caca_rand(- w, 2 * w); y = caca_rand(- h, 2 * h);
  369. a = caca_rand(0, w); b = caca_rand(0, h);
  370. }
  371. else
  372. {
  373. do
  374. {
  375. x = caca_rand(0, w); y = caca_rand(0, h);
  376. a = caca_rand(0, w); b = caca_rand(0, h);
  377. } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
  378. }
  379. caca_set_color(caca_rand(0, 15));
  380. caca_fill_ellipse(x, y, a, b, '#');
  381. caca_set_color(caca_rand(0, 15));
  382. if(outline == 2)
  383. caca_draw_thin_ellipse(x, y, a, b);
  384. else if(outline == 1)
  385. caca_draw_ellipse(x, y, a, b, '#');
  386. }
  387. static void demo_triangles(void)
  388. {
  389. int w = caca_get_width();
  390. int h = caca_get_height();
  391. int xa, ya, xb, yb, xc, yc;
  392. if(bounds)
  393. {
  394. xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
  395. xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
  396. xc = caca_rand(- w, 2 * w); yc = caca_rand(- h, 2 * h);
  397. }
  398. else
  399. {
  400. xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
  401. xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
  402. xc = caca_rand(0, w - 1); yc = caca_rand(0, h - 1);
  403. }
  404. caca_set_color(caca_rand(0, 15));
  405. caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  406. caca_set_color(caca_rand(0, 15));
  407. if(outline == 2)
  408. caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  409. else if(outline == 1)
  410. caca_draw_triangle(xa, ya, xb, yb, xc, yc, '#');
  411. }
  412. static void demo_sprites(void)
  413. {
  414. caca_draw_sprite(caca_rand(0, caca_get_width() - 1),
  415. caca_rand(0, caca_get_height() - 1), sprite, 0);
  416. }
  417. static void demo_blit(void)
  418. {
  419. #if 0
  420. caca_blit(6, 4, caca_get_width() - 6, caca_get_height() - 4,
  421. pixels, bufx, bufy);
  422. #endif
  423. }