Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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