您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

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