Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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