25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

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