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.
 
 
 
 
 
 

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