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.
 
 
 
 
 

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