Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

520 lignes
12 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_thin_lines(void);
  31. static void demo_circles(void);
  32. static void demo_ellipses(void);
  33. static void demo_triangles(void);
  34. static void demo_outlined_triangles(void);
  35. static void demo_sprites(void);
  36. int force_clipping = 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. break;
  79. case '3':
  80. ee_clear();
  81. demo = demo_thin_lines;
  82. break;
  83. case '4':
  84. ee_clear();
  85. demo = demo_circles;
  86. break;
  87. case '5':
  88. ee_clear();
  89. demo = demo_ellipses;
  90. break;
  91. case '6':
  92. ee_clear();
  93. demo = demo_triangles;
  94. break;
  95. case '7':
  96. ee_clear();
  97. demo = demo_outlined_triangles;
  98. break;
  99. case '8':
  100. ee_clear();
  101. demo = demo_sprites;
  102. break;
  103. }
  104. }
  105. if(demo)
  106. demo();
  107. }
  108. /* Clean up */
  109. ee_free_sprite(sprite);
  110. ee_end();
  111. return 0;
  112. }
  113. static void display_menu(void)
  114. {
  115. int xo = ee_get_width() - 2;
  116. int yo = ee_get_height() - 2;
  117. ee_clear();
  118. ee_color(EE_WHITE);
  119. ee_draw_line(xo, yo, 1, yo, '.');
  120. ee_draw_line(1, yo, 1, 1, ':');
  121. ee_draw_line(xo, 1, xo, yo, ':');
  122. ee_draw_line(1, 1, xo, 1, '.');
  123. ee_goto((xo - strlen("libee demo")) / 2, 3);
  124. ee_putstr("libee demo");
  125. ee_goto((xo - strlen("============")) / 2, 4);
  126. ee_putstr("============");
  127. ee_goto(4, 6);
  128. ee_putstr("0: complete demo");
  129. ee_goto(4, 7);
  130. ee_putstr("1: dots demo");
  131. ee_goto(4, 8);
  132. ee_putstr("2: lines demo");
  133. ee_goto(4, 9);
  134. ee_putstr("3: thin lines demo");
  135. ee_goto(4, 10);
  136. ee_putstr("4: circles demo");
  137. ee_goto(4, 11);
  138. ee_putstr("5: ellipses demo");
  139. ee_goto(4, 12);
  140. ee_putstr("6: triangles demo");
  141. ee_goto(4, 13);
  142. ee_putstr("7: outlined triangles demo");
  143. ee_goto(4, 14);
  144. ee_putstr("8: sprites demo");
  145. ee_goto(4, yo - 2);
  146. ee_putstr("q: quit");
  147. ee_refresh();
  148. }
  149. static void demo_all(void)
  150. {
  151. static int i = 0;
  152. int j, xo, yo, x1, y1, x2, y2, x3, y3;
  153. i++;
  154. ee_clear();
  155. /* Draw the sun */
  156. ee_color(EE_YELLOW);
  157. xo = ee_get_width() / 4;
  158. yo = ee_get_height() / 4 + 5 * sin(0.03*i);
  159. for(j = 0; j < 16; j++)
  160. {
  161. x1 = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
  162. y1 = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
  163. ee_draw_thin_line(xo, yo, x1, y1);
  164. }
  165. ee_color(EE_WHITE);
  166. for(j = 15 + sin(0.03*i) * 8; j--;)
  167. {
  168. ee_draw_circle(xo, yo, j, '#');
  169. }
  170. ee_color(EE_YELLOW);
  171. ee_draw_circle(xo, yo, 15 + sin(0.03*i) * 8, '#');
  172. /* Draw the pyramid */
  173. xo = ee_get_width() * 5 / 8;
  174. yo = 2;
  175. x1 = ee_get_width() / 8 + sin(0.03*i) * 5;
  176. y1 = ee_get_height() / 2 + cos(0.03*i) * 5;
  177. x2 = ee_get_width() - 10 - cos(0.02*i) * 10;
  178. y2 = ee_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
  179. x3 = ee_get_width() / 4 - sin(0.02*i) * 5;
  180. y3 = ee_get_height() * 3 / 4 + cos(0.02*i) * 5;
  181. ee_color(EE_GREEN);
  182. ee_fill_triangle(xo, yo, x2, y2, x1, y1, '%');
  183. ee_color(EE_YELLOW);
  184. ee_draw_thin_line(xo, yo, x2, y2);
  185. ee_draw_thin_line(x2, y2, x1, y1);
  186. ee_draw_thin_line(x1, y1, xo, yo);
  187. ee_color(EE_RED);
  188. ee_fill_triangle(x1, y1, x2, y2, x3, y3, '#');
  189. ee_color(EE_YELLOW);
  190. ee_draw_thin_line(x1, y1, x2, y2);
  191. ee_draw_thin_line(x2, y2, x3, y3);
  192. ee_draw_thin_line(x3, y3, x1, y1);
  193. ee_color(EE_BLUE);
  194. ee_fill_triangle(xo, yo, x2, y2, x3, y3, '%');
  195. ee_color(EE_YELLOW);
  196. ee_draw_thin_line(xo, yo, x2, y2);
  197. ee_draw_thin_line(x2, y2, x3, y3);
  198. ee_draw_thin_line(x3, y3, xo, yo);
  199. /* Draw a background triangle */
  200. x1 = 2;
  201. y1 = 2;
  202. x2 = ee_get_width() - 3;
  203. y2 = ee_get_height() / 2;
  204. x3 = ee_get_width() / 3;
  205. y3 = ee_get_height() - 3;
  206. ee_color(EE_BLUE);
  207. // ee_fill_triangle(x1, y1, x2, y2, x3, y3, '.');
  208. ee_color(EE_CYAN);
  209. ee_draw_thin_line(x1, y1, x3, y3);
  210. ee_draw_thin_line(x2, y2, x1, y1);
  211. ee_draw_thin_line(x3, y3, x2, y2);
  212. xo = ee_get_width() / 2 + cos(0.027*i) * ee_get_width() / 3;
  213. yo = ee_get_height() / 2 - sin(0.027*i) * ee_get_height() / 2;
  214. ee_draw_thin_line(x1, y1, xo, yo);
  215. ee_draw_thin_line(x2, y2, xo, yo);
  216. ee_draw_thin_line(x3, y3, xo, yo);
  217. /* Draw a sprite behind the pyramid */
  218. ee_draw_sprite(xo, yo, sprite);
  219. /* Draw a trail behind the foreground sprite */
  220. for(j = i - 60; j < i; j++)
  221. {
  222. int delta = ee_rand(-5, 5);
  223. ee_color(ee_rand(1, 10));
  224. ee_goto(ee_get_width() / 2
  225. + cos(0.02*j) * (delta + ee_get_width() / 4),
  226. ee_get_height() / 2
  227. + sin(0.02*j) * (delta + ee_get_height() / 3));
  228. ee_putchar('#');
  229. }
  230. /* Draw foreground sprite */
  231. ee_draw_sprite(ee_get_width() / 2 + cos(0.02*i) * ee_get_width() / 4,
  232. ee_get_height() / 2 + sin(0.02*i) * ee_get_height() / 3,
  233. sprite);
  234. ee_refresh();
  235. }
  236. static void demo_dots(void)
  237. {
  238. int xmax = ee_get_width() - 1;
  239. int ymax = ee_get_height() - 1;
  240. int i;
  241. for(i = 1000; i--;)
  242. {
  243. /* Putpixel */
  244. ee_color(ee_rand(1, 10));
  245. ee_goto(ee_rand(0, xmax), ee_rand(0, ymax));
  246. ee_putchar('#');
  247. }
  248. ee_refresh();
  249. }
  250. static void demo_lines(void)
  251. {
  252. int w = ee_get_width();
  253. int h = ee_get_height();
  254. /* Draw lines */
  255. ee_color(ee_rand(1, 10));
  256. if(force_clipping)
  257. {
  258. ee_draw_line(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
  259. ee_rand(- w, 2 * w), ee_rand(- h, 2 * h), '#');
  260. }
  261. else
  262. {
  263. ee_draw_line(ee_rand(0, w - 1), ee_rand(0, h - 1),
  264. ee_rand(0, w - 1), ee_rand(0, h - 1), '#');
  265. }
  266. ee_refresh();
  267. }
  268. static void demo_thin_lines(void)
  269. {
  270. int w = ee_get_width();
  271. int h = ee_get_height();
  272. /* Draw lines */
  273. ee_color(ee_rand(1, 10));
  274. if(force_clipping)
  275. {
  276. ee_draw_thin_line(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
  277. ee_rand(- w, 2 * w), ee_rand(- h, 2 * h));
  278. }
  279. else
  280. {
  281. ee_draw_thin_line(ee_rand(0, w), ee_rand(0, h),
  282. ee_rand(0, w), ee_rand(0, h));
  283. }
  284. ee_refresh();
  285. }
  286. static void demo_circles(void)
  287. {
  288. int w = ee_get_width();
  289. int h = ee_get_height();
  290. /* Draw circles */
  291. if(force_clipping)
  292. {
  293. ee_color(ee_rand(1, 10));
  294. ee_draw_circle(ee_rand(- w, 2 * w),
  295. ee_rand(- h, 2 * h),
  296. ee_rand(0, (w + h) / 2),
  297. '#');
  298. }
  299. else
  300. {
  301. int x = ee_rand(0, w);
  302. int y = ee_rand(0, h);
  303. int r = ee_rand(0, (w + h) / 2);
  304. if(x - r < 0 || x + r >= w || y - r < 0 || y + r >= h)
  305. {
  306. demo_circles();
  307. return;
  308. }
  309. ee_color(ee_rand(1, 10));
  310. ee_draw_circle(x, y, r, '#');
  311. }
  312. ee_refresh();
  313. }
  314. static void demo_ellipses(void)
  315. {
  316. int w = ee_get_width();
  317. int h = ee_get_height();
  318. /* Draw circles */
  319. if(force_clipping)
  320. {
  321. ee_color(ee_rand(1, 10));
  322. ee_draw_ellipse(ee_rand(- w, 2 * w),
  323. ee_rand(- h, 2 * h),
  324. ee_rand(0, w),
  325. ee_rand(0, h),
  326. '#');
  327. }
  328. else
  329. {
  330. int x = ee_rand(0, w);
  331. int y = ee_rand(0, h);
  332. int a = ee_rand(0, w);
  333. int b = ee_rand(0, h);
  334. if(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h)
  335. {
  336. demo_ellipses();
  337. return;
  338. }
  339. ee_color(ee_rand(1, 10));
  340. ee_draw_ellipse(x, y, a, b, '#');
  341. }
  342. ee_refresh();
  343. }
  344. static void demo_triangles(void)
  345. {
  346. int w = ee_get_width();
  347. int h = ee_get_height();
  348. /* Draw lines */
  349. ee_color(ee_rand(1, 10));
  350. if(force_clipping)
  351. {
  352. ee_fill_triangle(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
  353. ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
  354. ee_rand(- w, 2 * w), ee_rand(- h, 2 * h), '#');
  355. }
  356. else
  357. {
  358. ee_fill_triangle(ee_rand(0, w - 1), ee_rand(0, h - 1),
  359. ee_rand(0, w - 1), ee_rand(0, h - 1),
  360. ee_rand(0, w - 1), ee_rand(0, h - 1), '#');
  361. }
  362. ee_refresh();
  363. }
  364. static void demo_outlined_triangles(void)
  365. {
  366. int w = ee_get_width();
  367. int h = ee_get_height();
  368. int x1, y1, x2, y2, x3, y3;
  369. /* Draw lines */
  370. ee_color(ee_rand(1, 10));
  371. if(force_clipping)
  372. {
  373. x1 = ee_rand(- w, 2 * w);
  374. y1 = ee_rand(- h, 2 * h);
  375. x2 = ee_rand(- w, 2 * w);
  376. y2 = ee_rand(- h, 2 * h);
  377. x3 = ee_rand(- w, 2 * w);
  378. y3 = ee_rand(- h, 2 * h);
  379. }
  380. else
  381. {
  382. x1 = ee_rand(0, w - 1);
  383. y1 = ee_rand(0, h - 1);
  384. x2 = ee_rand(0, w - 1);
  385. y2 = ee_rand(0, h - 1);
  386. x3 = ee_rand(0, w - 1);
  387. y3 = ee_rand(0, h - 1);
  388. }
  389. ee_fill_triangle(x1, y1, x2, y2, x3, y3, '#');
  390. ee_color(ee_rand(1, 10));
  391. ee_draw_thin_line(x1, y1, x2, y2);
  392. ee_draw_thin_line(x2, y2, x3, y3);
  393. ee_draw_thin_line(x3, y3, x1, y1);
  394. ee_refresh();
  395. }
  396. static void demo_sprites(void)
  397. {
  398. ee_draw_sprite(ee_rand(0, ee_get_width() - 1),
  399. ee_rand(0, ee_get_height() - 1), sprite);
  400. ee_refresh();
  401. }
  402. static void demo_pyramid(void)
  403. {
  404. static int i = 0;
  405. int xo, yo, x1, y1, x2, y2, x3, y3;
  406. i++;
  407. xo = ee_get_width() * 5 / 8;
  408. yo = 2;
  409. x1 = ee_get_width() / 8 + sin(0.03*i) * 5;
  410. y1 = ee_get_height() / 2 + cos(0.03*i) * 5;
  411. x2 = ee_get_width() - 10 - cos(0.02*i) * 10;
  412. y2 = ee_get_height() - 5 + sin(0.02*i) * 5;
  413. x3 = ee_get_width() / 4 - sin(0.02*i) * 5;
  414. y3 = ee_get_height() + cos(0.02*i) * 5;
  415. ee_clear();
  416. ee_color(EE_GREEN);
  417. ee_fill_triangle(xo, yo, x2, y2, x1, y1, '%');
  418. ee_color(EE_YELLOW);
  419. ee_draw_thin_line(xo, yo, x2, y2);
  420. ee_draw_thin_line(x2, y2, x1, y1);
  421. ee_draw_thin_line(x1, y1, xo, yo);
  422. ee_color(EE_RED);
  423. ee_fill_triangle(x1, y1, x2, y2, x3, y3, '#');
  424. ee_color(EE_YELLOW);
  425. ee_draw_thin_line(x1, y1, x2, y2);
  426. ee_draw_thin_line(x2, y2, x3, y3);
  427. ee_draw_thin_line(x3, y3, x1, y1);
  428. ee_color(EE_BLUE);
  429. ee_fill_triangle(xo, yo, x2, y2, x3, y3, '%');
  430. ee_color(EE_YELLOW);
  431. ee_draw_thin_line(xo, yo, x2, y2);
  432. ee_draw_thin_line(x2, y2, x3, y3);
  433. ee_draw_thin_line(x3, y3, xo, yo);
  434. ee_refresh();
  435. }