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.
 
 
 
 
 
 

293 lines
6.6 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. int clipping = 0;
  33. int main(int argc, char **argv)
  34. {
  35. void (*demo)(void) = NULL;
  36. int quit = 0;
  37. if(ee_init())
  38. {
  39. return 1;
  40. }
  41. display_menu();
  42. /* Go ! */
  43. while(!quit)
  44. {
  45. char key = ee_get_key();
  46. if(key && demo)
  47. {
  48. display_menu();
  49. demo = NULL;
  50. }
  51. else if(key)
  52. {
  53. switch(key)
  54. {
  55. case 'q':
  56. demo = NULL;
  57. quit = 1;
  58. break;
  59. case '1':
  60. ee_clear();
  61. demo = demo_dots;
  62. break;
  63. case '2':
  64. ee_clear();
  65. demo = demo_lines;
  66. break;
  67. case '3':
  68. ee_clear();
  69. demo = demo_thin_lines;
  70. break;
  71. case '4':
  72. ee_clear();
  73. demo = demo_circles;
  74. break;
  75. case '5':
  76. ee_clear();
  77. demo = demo_triangles;
  78. break;
  79. }
  80. }
  81. if(demo)
  82. demo();
  83. }
  84. /* Clean up */
  85. ee_end();
  86. return 0;
  87. }
  88. static void display_menu(void)
  89. {
  90. int xo = ee_get_width() - 2;
  91. int yo = ee_get_height() - 2;
  92. ee_clear();
  93. ee_color(EE_WHITE);
  94. ee_draw_line(xo, yo, 1, yo, '.');
  95. ee_draw_line(1, yo, 1, 1, ':');
  96. ee_draw_line(xo, 1, xo, yo, ':');
  97. ee_draw_line(1, 1, xo, 1, '.');
  98. ee_goto((xo - strlen("libee demo")) / 2, 3);
  99. ee_putstr("libee demo");
  100. ee_goto((xo - strlen("============")) / 2, 4);
  101. ee_putstr("============");
  102. ee_goto(4, 6);
  103. ee_putstr("1: dots demo");
  104. ee_goto(4, 7);
  105. ee_putstr("2: lines demo");
  106. ee_goto(4, 8);
  107. ee_putstr("3: thin lines demo");
  108. ee_goto(4, 9);
  109. ee_putstr("4: circles demo");
  110. ee_goto(4, 10);
  111. ee_putstr("5: triangles demo");
  112. ee_goto(4, yo - 2);
  113. ee_putstr("q: quit");
  114. ee_refresh();
  115. }
  116. static void demo_dots(void)
  117. {
  118. int xmax = ee_get_width() - 1;
  119. int ymax = ee_get_height() - 1;
  120. int i;
  121. for(i = 1000; i--;)
  122. {
  123. /* Putpixel */
  124. ee_color(ee_rand(1, 10));
  125. ee_goto(ee_rand(0, xmax), ee_rand(0, ymax));
  126. ee_putchar('#');
  127. }
  128. ee_refresh();
  129. }
  130. static void demo_lines(void)
  131. {
  132. int w = ee_get_width();
  133. int h = ee_get_height();
  134. /* Draw lines */
  135. ee_color(ee_rand(1, 10));
  136. if(clipping)
  137. {
  138. ee_draw_line(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
  139. ee_rand(- w, 2 * w), ee_rand(- h, 2 * h), '#');
  140. }
  141. else
  142. {
  143. ee_draw_line(ee_rand(0, w - 1), ee_rand(0, h - 1),
  144. ee_rand(0, w - 1), ee_rand(0, h - 1), '#');
  145. }
  146. ee_refresh();
  147. }
  148. static void demo_thin_lines(void)
  149. {
  150. int w = ee_get_width();
  151. int h = ee_get_height();
  152. /* Draw lines */
  153. ee_color(ee_rand(1, 10));
  154. if(clipping)
  155. {
  156. ee_draw_thin_line(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
  157. ee_rand(- w, 2 * w), ee_rand(- h, 2 * h));
  158. }
  159. else
  160. {
  161. ee_draw_thin_line(ee_rand(0, w), ee_rand(0, h),
  162. ee_rand(0, w), ee_rand(0, h));
  163. }
  164. ee_refresh();
  165. }
  166. static void demo_circles(void)
  167. {
  168. int w = ee_get_width();
  169. int h = ee_get_height();
  170. /* Draw circles */
  171. if(clipping)
  172. {
  173. ee_color(ee_rand(1, 10));
  174. ee_draw_circle(ee_rand(- w, 2 * w),
  175. ee_rand(- h, 2 * h),
  176. ee_rand(0, (w + h) / 2),
  177. '#');
  178. }
  179. else
  180. {
  181. int x = ee_rand(0, w);
  182. int y = ee_rand(0, h);
  183. int r = ee_rand(0, (w + h) / 2);
  184. if(x - r < 0 || x + r >= w || y - r < 0 || y + r >= h)
  185. {
  186. demo_circles();
  187. return;
  188. }
  189. ee_color(ee_rand(1, 10));
  190. ee_draw_circle(x, y, r, '#');
  191. }
  192. ee_refresh();
  193. }
  194. static void demo_triangles(void)
  195. {
  196. int w = ee_get_width();
  197. int h = ee_get_height();
  198. /* Draw lines */
  199. ee_color(ee_rand(1, 10));
  200. if(clipping)
  201. {
  202. ee_fill_triangle(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
  203. ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
  204. ee_rand(- w, 2 * w), ee_rand(- h, 2 * h), '#');
  205. }
  206. else
  207. {
  208. ee_fill_triangle(ee_rand(0, w - 1), ee_rand(0, h - 1),
  209. ee_rand(0, w - 1), ee_rand(0, h - 1),
  210. ee_rand(0, w - 1), ee_rand(0, h - 1), '#');
  211. }
  212. ee_refresh();
  213. }
  214. static void demo_pyramid(void)
  215. {
  216. static int i = 0;
  217. int xo, yo, x1, y1, x2, y2, x3, y3;
  218. i++;
  219. xo = ee_get_width() * 5 / 8;
  220. yo = 2;
  221. x1 = ee_get_width() / 8 + sin(0.03*i) * 5;
  222. y1 = ee_get_height() / 2 + cos(0.03*i) * 5;
  223. x2 = ee_get_width() - 10 - cos(0.02*i) * 10;
  224. y2 = ee_get_height() - 5 + sin(0.02*i) * 5;
  225. x3 = ee_get_width() / 4 - sin(0.02*i) * 5;
  226. y3 = ee_get_height() + cos(0.02*i) * 5;
  227. ee_clear();
  228. ee_color(EE_GREEN);
  229. ee_fill_triangle(xo, yo, x2, y2, x1, y1, '%');
  230. ee_color(EE_YELLOW);
  231. ee_draw_thin_line(xo, yo, x2, y2);
  232. ee_draw_thin_line(x2, y2, x1, y1);
  233. ee_draw_thin_line(x1, y1, xo, yo);
  234. ee_color(EE_RED);
  235. ee_fill_triangle(x1, y1, x2, y2, x3, y3, '#');
  236. ee_color(EE_YELLOW);
  237. ee_draw_thin_line(x1, y1, x2, y2);
  238. ee_draw_thin_line(x2, y2, x3, y3);
  239. ee_draw_thin_line(x3, y3, x1, y1);
  240. ee_color(EE_BLUE);
  241. ee_fill_triangle(xo, yo, x2, y2, x3, y3, '%');
  242. ee_color(EE_YELLOW);
  243. ee_draw_thin_line(xo, yo, x2, y2);
  244. ee_draw_thin_line(x2, y2, x3, y3);
  245. ee_draw_thin_line(x3, y3, xo, yo);
  246. ee_refresh();
  247. }