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.
 
 
 
 
 

463 lines
12 KiB

  1. /*
  2. * demo demo using libee
  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 modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (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
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include "config.h"
  23. #include <math.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <gdk/gdk.h>
  27. #include <gdk/gdkpixbuf.h>
  28. #include "ee.h"
  29. static void display_menu(void);
  30. static void demo_all(void);
  31. static void demo_color(void);
  32. static void demo_dots(void);
  33. static void demo_lines(void);
  34. static void demo_boxes(void);
  35. static void demo_ellipses(void);
  36. static void demo_triangles(void);
  37. static void demo_sprites(void);
  38. static void demo_blit(void);
  39. int bounds = 0;
  40. int outline = 0;
  41. struct ee_sprite *sprite = NULL;
  42. GdkPixbuf *pixbuf;
  43. char *pixels;
  44. int bufx, bufy, bufpitch;
  45. int main(int argc, char **argv)
  46. {
  47. void (*demo)(void) = NULL;
  48. int quit = 0;
  49. if(ee_init())
  50. {
  51. return 1;
  52. }
  53. ee_set_delay(40000);
  54. /* Initialize data */
  55. sprite = ee_load_sprite("data/barboss.txt");
  56. gdk_init (&argc, &argv);
  57. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/gally4.jpeg", NULL);
  58. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/badge1.jpeg", NULL);
  59. //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/union.png", NULL);
  60. pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/pikachu.jpeg", NULL);
  61. if(!pixbuf) return -2;
  62. pixels = gdk_pixbuf_get_pixels(pixbuf);
  63. bufx = gdk_pixbuf_get_width(pixbuf);
  64. bufy = gdk_pixbuf_get_height(pixbuf);
  65. bufpitch = gdk_pixbuf_get_rowstride(pixbuf);
  66. fprintf(stderr, "bits: %i\n", gdk_pixbuf_get_bits_per_sample(pixbuf));
  67. fprintf(stderr, "w %i, h %i, stride %i\n", bufx, bufy, bufpitch);
  68. /* Main menu */
  69. display_menu();
  70. ee_refresh();
  71. /* Go ! */
  72. while(!quit)
  73. {
  74. char key = ee_get_key();
  75. if(key && demo)
  76. {
  77. display_menu();
  78. ee_refresh();
  79. demo = NULL;
  80. }
  81. else if(key)
  82. {
  83. handle_key:
  84. switch(key)
  85. {
  86. case 'q':
  87. case 'Q':
  88. demo = NULL;
  89. quit = 1;
  90. break;
  91. case 'o':
  92. case 'O':
  93. outline = (outline + 1) % 3;
  94. display_menu();
  95. break;
  96. case 'b':
  97. case 'B':
  98. bounds = (bounds + 1) % 2;
  99. display_menu();
  100. break;
  101. case 'c':
  102. demo = demo_color;
  103. break;
  104. case 'f':
  105. case 'F':
  106. demo = demo_all;
  107. break;
  108. case '1':
  109. demo = demo_dots;
  110. break;
  111. case '2':
  112. demo = demo_lines;
  113. break;
  114. case '3':
  115. demo = demo_boxes;
  116. break;
  117. case '4':
  118. demo = demo_triangles;
  119. break;
  120. case '5':
  121. demo = demo_ellipses;
  122. break;
  123. case 's':
  124. case 'S':
  125. demo = demo_sprites;
  126. break;
  127. case 'i':
  128. case 'I':
  129. demo = demo_blit;
  130. break;
  131. }
  132. if(demo)
  133. ee_clear();
  134. key = ee_get_key();
  135. if(key)
  136. goto handle_key;
  137. ee_refresh();
  138. }
  139. if(demo)
  140. {
  141. demo();
  142. ee_set_color(EE_WHITE);
  143. ee_draw_thin_box(1, 1, ee_get_width() - 2, ee_get_height() - 2);
  144. ee_printf(4, 1, "[%i.%i fps]----",
  145. 1000000 / ee_get_rendertime(),
  146. (10000000 / ee_get_rendertime()) % 10);
  147. ee_refresh();
  148. }
  149. }
  150. /* Clean up */
  151. ee_free_sprite(sprite);
  152. ee_end();
  153. return 0;
  154. }
  155. static void display_menu(void)
  156. {
  157. int xo = ee_get_width() - 2;
  158. int yo = ee_get_height() - 2;
  159. ee_clear();
  160. ee_set_color(EE_WHITE);
  161. ee_draw_thin_box(1, 1, xo, yo);
  162. ee_putstr((xo - strlen("libee demo")) / 2, 3, "libee demo");
  163. ee_putstr((xo - strlen("============")) / 2, 4, "============");
  164. ee_putstr(4, 6, "demos:");
  165. ee_putstr(4, 7, "'f': full");
  166. ee_putstr(4, 8, "'1': dots");
  167. ee_putstr(4, 9, "'2': lines");
  168. ee_putstr(4, 10, "'3': boxes");
  169. ee_putstr(4, 11, "'4': triangles");
  170. ee_putstr(4, 12, "'5': ellipses");
  171. ee_putstr(4, 13, "'s': sprites");
  172. ee_putstr(4, 14, "'c': color");
  173. ee_putstr(4, 15, "'i': image blit");
  174. ee_putstr(4, 17, "settings:");
  175. ee_printf(4, 18, "'o': outline: %s",
  176. outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
  177. ee_printf(4, 19, "'b': drawing boundaries: %s",
  178. bounds == 0 ? "screen" : "infinite");
  179. ee_putstr(4, yo - 2, "'q': quit");
  180. }
  181. static void demo_all(void)
  182. {
  183. static int i = 0;
  184. int j, xo, yo, xa, ya, xb, yb, xc, yc;
  185. i++;
  186. ee_clear();
  187. /* Draw the sun */
  188. ee_set_color(EE_YELLOW);
  189. xo = ee_get_width() / 4;
  190. yo = ee_get_height() / 4 + 5 * sin(0.03*i);
  191. for(j = 0; j < 16; j++)
  192. {
  193. xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
  194. ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
  195. ee_draw_thin_line(xo, yo, xa, ya);
  196. }
  197. j = 15 + sin(0.03*i) * 8;
  198. ee_set_color(EE_WHITE);
  199. ee_fill_ellipse(xo, yo, j, j / 2, '#');
  200. ee_set_color(EE_YELLOW);
  201. ee_draw_ellipse(xo, yo, j, j / 2, '#');
  202. /* Draw the pyramid */
  203. xo = ee_get_width() * 5 / 8;
  204. yo = 2;
  205. xa = ee_get_width() / 8 + sin(0.03*i) * 5;
  206. ya = ee_get_height() / 2 + cos(0.03*i) * 5;
  207. xb = ee_get_width() - 10 - cos(0.02*i) * 10;
  208. yb = ee_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
  209. xc = ee_get_width() / 4 - sin(0.02*i) * 5;
  210. yc = ee_get_height() * 3 / 4 + cos(0.02*i) * 5;
  211. ee_set_color(EE_GREEN);
  212. ee_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
  213. ee_set_color(EE_YELLOW);
  214. ee_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
  215. ee_set_color(EE_RED);
  216. ee_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  217. ee_set_color(EE_YELLOW);
  218. ee_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  219. ee_set_color(EE_BLUE);
  220. ee_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
  221. ee_set_color(EE_YELLOW);
  222. ee_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
  223. /* Draw a background triangle */
  224. xa = 2;
  225. ya = 2;
  226. xb = ee_get_width() - 3;
  227. yb = ee_get_height() / 2;
  228. xc = ee_get_width() / 3;
  229. yc = ee_get_height() - 3;
  230. ee_set_color(EE_CYAN);
  231. ee_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  232. xo = ee_get_width() / 2 + cos(0.027*i) * ee_get_width() / 3;
  233. yo = ee_get_height() / 2 - sin(0.027*i) * ee_get_height() / 2;
  234. ee_draw_thin_line(xa, ya, xo, yo);
  235. ee_draw_thin_line(xb, yb, xo, yo);
  236. ee_draw_thin_line(xc, yc, xo, yo);
  237. /* Draw a sprite on the pyramid */
  238. ee_draw_sprite(xo, yo, sprite, 0);
  239. /* Draw a trail behind the foreground sprite */
  240. for(j = i - 60; j < i; j++)
  241. {
  242. int delta = ee_rand(-5, 5);
  243. ee_set_color(ee_rand(0, 15));
  244. ee_putchar(ee_get_width() / 2
  245. + cos(0.02*j) * (delta + ee_get_width() / 4),
  246. ee_get_height() / 2
  247. + sin(0.02*j) * (delta + ee_get_height() / 3),
  248. '#');
  249. }
  250. /* Draw foreground sprite */
  251. ee_draw_sprite(ee_get_width() / 2 + cos(0.02*i) * ee_get_width() / 4,
  252. ee_get_height() / 2 + sin(0.02*i) * ee_get_height() / 3,
  253. sprite, 0);
  254. }
  255. static void demo_dots(void)
  256. {
  257. int xmax = ee_get_width() - 1;
  258. int ymax = ee_get_height() - 1;
  259. int i;
  260. for(i = 1000; i--;)
  261. {
  262. /* Putpixel */
  263. ee_set_color(ee_rand(0, 15));
  264. ee_putchar(ee_rand(0, xmax), ee_rand(0, ymax), '#');
  265. }
  266. }
  267. static void demo_color(void)
  268. {
  269. int i;
  270. char buf[BUFSIZ];
  271. ee_clear();
  272. for(i = 0; i < 16; i++)
  273. {
  274. sprintf(buf, "'%c': %i (%s)", 'a' + i, i, ee_get_color_name(i));
  275. ee_set_color(EE_WHITE);
  276. ee_putstr(4, i + 3, buf);
  277. ee_set_color(i);
  278. ee_putstr(40, i + 3, "XXXXXXXXXX-XX--X----------");
  279. }
  280. }
  281. static void demo_lines(void)
  282. {
  283. int w = ee_get_width();
  284. int h = ee_get_height();
  285. int xa, ya, xb, yb;
  286. if(bounds)
  287. {
  288. xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h);
  289. xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h);
  290. }
  291. else
  292. {
  293. xa = ee_rand(0, w - 1); ya = ee_rand(0, h - 1);
  294. xb = ee_rand(0, w - 1); yb = ee_rand(0, h - 1);
  295. }
  296. ee_set_color(ee_rand(0, 15));
  297. if(outline > 1)
  298. ee_draw_thin_line(xa, ya, xb, yb);
  299. else
  300. ee_draw_line(xa, ya, xb, yb, '#');
  301. }
  302. static void demo_boxes(void)
  303. {
  304. int w = ee_get_width();
  305. int h = ee_get_height();
  306. int xa, ya, xb, yb;
  307. if(bounds)
  308. {
  309. xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h);
  310. xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h);
  311. }
  312. else
  313. {
  314. xa = ee_rand(0, w - 1); ya = ee_rand(0, h - 1);
  315. xb = ee_rand(0, w - 1); yb = ee_rand(0, h - 1);
  316. }
  317. ee_set_color(ee_rand(0, 15));
  318. ee_fill_box(xa, ya, xb, yb, '#');
  319. ee_set_color(ee_rand(0, 15));
  320. if(outline == 2)
  321. ee_draw_thin_box(xa, ya, xb, yb);
  322. else if(outline == 1)
  323. ee_draw_box(xa, ya, xb, yb, '#');
  324. }
  325. static void demo_ellipses(void)
  326. {
  327. int w = ee_get_width();
  328. int h = ee_get_height();
  329. int x, y, a, b;
  330. if(bounds)
  331. {
  332. x = ee_rand(- w, 2 * w); y = ee_rand(- h, 2 * h);
  333. a = ee_rand(0, w); b = ee_rand(0, h);
  334. }
  335. else
  336. {
  337. do
  338. {
  339. x = ee_rand(0, w); y = ee_rand(0, h);
  340. a = ee_rand(0, w); b = ee_rand(0, h);
  341. } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
  342. }
  343. ee_set_color(ee_rand(0, 15));
  344. ee_fill_ellipse(x, y, a, b, '#');
  345. ee_set_color(ee_rand(0, 15));
  346. if(outline == 2)
  347. ee_draw_thin_ellipse(x, y, a, b);
  348. else if(outline == 1)
  349. ee_draw_ellipse(x, y, a, b, '#');
  350. }
  351. static void demo_triangles(void)
  352. {
  353. int w = ee_get_width();
  354. int h = ee_get_height();
  355. int xa, ya, xb, yb, xc, yc;
  356. if(bounds)
  357. {
  358. xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h);
  359. xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h);
  360. xc = ee_rand(- w, 2 * w); yc = ee_rand(- h, 2 * h);
  361. }
  362. else
  363. {
  364. xa = ee_rand(0, w - 1); ya = ee_rand(0, h - 1);
  365. xb = ee_rand(0, w - 1); yb = ee_rand(0, h - 1);
  366. xc = ee_rand(0, w - 1); yc = ee_rand(0, h - 1);
  367. }
  368. ee_set_color(ee_rand(0, 15));
  369. ee_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
  370. ee_set_color(ee_rand(0, 15));
  371. if(outline == 2)
  372. ee_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
  373. else if(outline == 1)
  374. ee_draw_triangle(xa, ya, xb, yb, xc, yc, '#');
  375. }
  376. static void demo_sprites(void)
  377. {
  378. ee_draw_sprite(ee_rand(0, ee_get_width() - 1),
  379. ee_rand(0, ee_get_height() - 1), sprite, 0);
  380. }
  381. static void demo_blit(void)
  382. {
  383. ee_set_color(EE_LIGHTGRAY);
  384. ee_blit(6, 4, ee_get_width() - 6, ee_get_height() - 4, pixels, bufx, bufy);
  385. }