1 | /* |
---|
2 | * demo demo using libee |
---|
3 | * Copyright (c) 2003 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: demo.c 159 2003-11-12 21:18:50Z sam $ |
---|
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 | |
---|
23 | #include "config.h" |
---|
24 | |
---|
25 | #include <math.h> |
---|
26 | #include <string.h> |
---|
27 | #include <stdio.h> |
---|
28 | |
---|
29 | #include "ee.h" |
---|
30 | |
---|
31 | static void display_menu(void); |
---|
32 | |
---|
33 | static void demo_all(void); |
---|
34 | |
---|
35 | static void demo_color(void); |
---|
36 | static void demo_dots(void); |
---|
37 | static void demo_lines(void); |
---|
38 | static void demo_boxes(void); |
---|
39 | static void demo_ellipses(void); |
---|
40 | static void demo_triangles(void); |
---|
41 | static void demo_sprites(void); |
---|
42 | |
---|
43 | int force_clipping = 0; |
---|
44 | int outline = 0; |
---|
45 | int thin = 0; |
---|
46 | struct ee_sprite *sprite = NULL; |
---|
47 | |
---|
48 | int main(int argc, char **argv) |
---|
49 | { |
---|
50 | void (*demo)(void) = NULL; |
---|
51 | int quit = 0; |
---|
52 | |
---|
53 | if(ee_init()) |
---|
54 | { |
---|
55 | return 1; |
---|
56 | } |
---|
57 | |
---|
58 | /* Initialize data */ |
---|
59 | sprite = ee_load_sprite("data/bar_boss"); |
---|
60 | |
---|
61 | /* Main menu */ |
---|
62 | display_menu(); |
---|
63 | |
---|
64 | /* Go ! */ |
---|
65 | while(!quit) |
---|
66 | { |
---|
67 | char key = ee_get_key(); |
---|
68 | if(key && demo) |
---|
69 | { |
---|
70 | display_menu(); |
---|
71 | demo = NULL; |
---|
72 | } |
---|
73 | else if(key) |
---|
74 | { |
---|
75 | switch(key) |
---|
76 | { |
---|
77 | case 'q': |
---|
78 | demo = NULL; |
---|
79 | quit = 1; |
---|
80 | break; |
---|
81 | case 'c': |
---|
82 | ee_clear(); |
---|
83 | demo = demo_color; |
---|
84 | break; |
---|
85 | case '0': |
---|
86 | ee_clear(); |
---|
87 | demo = demo_all; |
---|
88 | break; |
---|
89 | case '1': |
---|
90 | ee_clear(); |
---|
91 | demo = demo_dots; |
---|
92 | break; |
---|
93 | case '2': |
---|
94 | ee_clear(); |
---|
95 | demo = demo_lines; |
---|
96 | thin = 0; |
---|
97 | break; |
---|
98 | case '3': |
---|
99 | ee_clear(); |
---|
100 | demo = demo_lines; |
---|
101 | thin = 1; |
---|
102 | break; |
---|
103 | case '4': |
---|
104 | ee_clear(); |
---|
105 | demo = demo_boxes; |
---|
106 | outline = 0; |
---|
107 | break; |
---|
108 | case '5': |
---|
109 | ee_clear(); |
---|
110 | demo = demo_boxes; |
---|
111 | outline = 1; |
---|
112 | break; |
---|
113 | case '6': |
---|
114 | ee_clear(); |
---|
115 | demo = demo_ellipses; |
---|
116 | break; |
---|
117 | case '7': |
---|
118 | ee_clear(); |
---|
119 | demo = demo_triangles; |
---|
120 | outline = 0; |
---|
121 | break; |
---|
122 | case '8': |
---|
123 | ee_clear(); |
---|
124 | demo = demo_triangles; |
---|
125 | outline = 1; |
---|
126 | break; |
---|
127 | case '9': |
---|
128 | ee_clear(); |
---|
129 | demo = demo_sprites; |
---|
130 | break; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | if(demo) |
---|
135 | demo(); |
---|
136 | } |
---|
137 | |
---|
138 | /* Clean up */ |
---|
139 | ee_free_sprite(sprite); |
---|
140 | ee_end(); |
---|
141 | |
---|
142 | return 0; |
---|
143 | } |
---|
144 | |
---|
145 | static void display_menu(void) |
---|
146 | { |
---|
147 | int xo = ee_get_width() - 2; |
---|
148 | int yo = ee_get_height() - 2; |
---|
149 | |
---|
150 | ee_clear(); |
---|
151 | ee_set_color(EE_WHITE); |
---|
152 | ee_draw_thin_box(1, 1, xo, yo); |
---|
153 | |
---|
154 | ee_putstr((xo - strlen("libee demo")) / 2, 3, "libee demo"); |
---|
155 | ee_putstr((xo - strlen("============")) / 2, 4, "============"); |
---|
156 | |
---|
157 | ee_putstr(4, 6, "0: complete demo"); |
---|
158 | ee_putstr(4, 7, "1: dots demo"); |
---|
159 | ee_putstr(4, 8, "2: lines demo"); |
---|
160 | ee_putstr(4, 9, "3: thin lines demo"); |
---|
161 | ee_putstr(4, 10, "4: boxes demo"); |
---|
162 | ee_putstr(4, 11, "5: outlined boxes demo"); |
---|
163 | ee_putstr(4, 12, "6: ellipses demo"); |
---|
164 | ee_putstr(4, 13, "7: triangles demo"); |
---|
165 | ee_putstr(4, 14, "8: outlined triangles demo"); |
---|
166 | ee_putstr(4, 15, "9: sprites demo"); |
---|
167 | |
---|
168 | ee_putstr(4, yo - 2, "q: quit"); |
---|
169 | |
---|
170 | ee_refresh(); |
---|
171 | } |
---|
172 | |
---|
173 | static void demo_all(void) |
---|
174 | { |
---|
175 | static int i = 0; |
---|
176 | |
---|
177 | int j, xo, yo, xa, ya, xb, yb, xc, yc; |
---|
178 | |
---|
179 | i++; |
---|
180 | |
---|
181 | ee_clear(); |
---|
182 | |
---|
183 | /* Draw the sun */ |
---|
184 | ee_set_color(EE_YELLOW); |
---|
185 | xo = ee_get_width() / 4; |
---|
186 | yo = ee_get_height() / 4 + 5 * sin(0.03*i); |
---|
187 | |
---|
188 | for(j = 0; j < 16; j++) |
---|
189 | { |
---|
190 | xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8); |
---|
191 | ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8); |
---|
192 | ee_draw_thin_line(xo, yo, xa, ya); |
---|
193 | } |
---|
194 | |
---|
195 | j = 15 + sin(0.03*i) * 8; |
---|
196 | ee_set_color(EE_WHITE); |
---|
197 | ee_fill_ellipse(xo, yo, j, j / 2, '#'); |
---|
198 | ee_set_color(EE_YELLOW); |
---|
199 | ee_draw_ellipse(xo, yo, j, j / 2, '#'); |
---|
200 | |
---|
201 | /* Draw the pyramid */ |
---|
202 | xo = ee_get_width() * 5 / 8; |
---|
203 | yo = 2; |
---|
204 | |
---|
205 | xa = ee_get_width() / 8 + sin(0.03*i) * 5; |
---|
206 | ya = ee_get_height() / 2 + cos(0.03*i) * 5; |
---|
207 | |
---|
208 | xb = ee_get_width() - 10 - cos(0.02*i) * 10; |
---|
209 | yb = ee_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5; |
---|
210 | |
---|
211 | xc = ee_get_width() / 4 - sin(0.02*i) * 5; |
---|
212 | yc = ee_get_height() * 3 / 4 + cos(0.02*i) * 5; |
---|
213 | |
---|
214 | ee_set_color(EE_GREEN); |
---|
215 | ee_fill_triangle(xo, yo, xb, yb, xa, ya, '%'); |
---|
216 | ee_set_color(EE_YELLOW); |
---|
217 | ee_draw_thin_triangle(xo, yo, xb, yb, xa, ya); |
---|
218 | |
---|
219 | ee_set_color(EE_RED); |
---|
220 | ee_fill_triangle(xa, ya, xb, yb, xc, yc, '#'); |
---|
221 | ee_set_color(EE_YELLOW); |
---|
222 | ee_draw_thin_triangle(xa, ya, xb, yb, xc, yc); |
---|
223 | |
---|
224 | ee_set_color(EE_BLUE); |
---|
225 | ee_fill_triangle(xo, yo, xb, yb, xc, yc, '%'); |
---|
226 | ee_set_color(EE_YELLOW); |
---|
227 | ee_draw_thin_triangle(xo, yo, xb, yb, xc, yc); |
---|
228 | |
---|
229 | /* Draw a background triangle */ |
---|
230 | xa = 2; |
---|
231 | ya = 2; |
---|
232 | |
---|
233 | xb = ee_get_width() - 3; |
---|
234 | yb = ee_get_height() / 2; |
---|
235 | |
---|
236 | xc = ee_get_width() / 3; |
---|
237 | yc = ee_get_height() - 3; |
---|
238 | |
---|
239 | ee_set_color(EE_CYAN); |
---|
240 | ee_draw_thin_triangle(xa, ya, xb, yb, xc, yc); |
---|
241 | |
---|
242 | xo = ee_get_width() / 2 + cos(0.027*i) * ee_get_width() / 3; |
---|
243 | yo = ee_get_height() / 2 - sin(0.027*i) * ee_get_height() / 2; |
---|
244 | |
---|
245 | ee_draw_thin_line(xa, ya, xo, yo); |
---|
246 | ee_draw_thin_line(xb, yb, xo, yo); |
---|
247 | ee_draw_thin_line(xc, yc, xo, yo); |
---|
248 | |
---|
249 | /* Draw a sprite on the pyramid */ |
---|
250 | ee_draw_sprite(xo, yo, sprite, 0); |
---|
251 | |
---|
252 | /* Draw a trail behind the foreground sprite */ |
---|
253 | for(j = i - 60; j < i; j++) |
---|
254 | { |
---|
255 | int delta = ee_rand(-5, 5); |
---|
256 | ee_set_color(ee_rand(0, 15)); |
---|
257 | ee_putchar(ee_get_width() / 2 |
---|
258 | + cos(0.02*j) * (delta + ee_get_width() / 4), |
---|
259 | ee_get_height() / 2 |
---|
260 | + sin(0.02*j) * (delta + ee_get_height() / 3), |
---|
261 | '#'); |
---|
262 | } |
---|
263 | |
---|
264 | /* Draw foreground sprite */ |
---|
265 | ee_draw_sprite(ee_get_width() / 2 + cos(0.02*i) * ee_get_width() / 4, |
---|
266 | ee_get_height() / 2 + sin(0.02*i) * ee_get_height() / 3, |
---|
267 | sprite, 0); |
---|
268 | |
---|
269 | ee_refresh(); |
---|
270 | } |
---|
271 | |
---|
272 | static void demo_dots(void) |
---|
273 | { |
---|
274 | int xmax = ee_get_width() - 1; |
---|
275 | int ymax = ee_get_height() - 1; |
---|
276 | int i; |
---|
277 | |
---|
278 | for(i = 1000; i--;) |
---|
279 | { |
---|
280 | /* Putpixel */ |
---|
281 | ee_set_color(ee_rand(0, 15)); |
---|
282 | ee_putchar(ee_rand(0, xmax), ee_rand(0, ymax), '#'); |
---|
283 | } |
---|
284 | ee_refresh(); |
---|
285 | } |
---|
286 | |
---|
287 | static void demo_color(void) |
---|
288 | { |
---|
289 | int i; |
---|
290 | char buf[BUFSIZ]; |
---|
291 | |
---|
292 | ee_clear(); |
---|
293 | for(i = 0; i < 16; i++) |
---|
294 | { |
---|
295 | sprintf(buf, "color %i (%s)\n", i, ee_color_names[i]); |
---|
296 | ee_set_color(EE_WHITE); |
---|
297 | ee_putstr(4, i + 3, buf); |
---|
298 | ee_set_color(i); |
---|
299 | ee_putstr(40, i + 3, "XXXXXXXXXX-XX--X----------"); |
---|
300 | } |
---|
301 | ee_refresh(); |
---|
302 | } |
---|
303 | |
---|
304 | static void demo_lines(void) |
---|
305 | { |
---|
306 | int w = ee_get_width(); |
---|
307 | int h = ee_get_height(); |
---|
308 | int xa, ya, xb, yb; |
---|
309 | |
---|
310 | if(force_clipping) |
---|
311 | { |
---|
312 | xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h); |
---|
313 | xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h); |
---|
314 | } |
---|
315 | else |
---|
316 | { |
---|
317 | xa = ee_rand(0, w - 1); ya = ee_rand(0, h - 1); |
---|
318 | xb = ee_rand(0, w - 1); yb = ee_rand(0, h - 1); |
---|
319 | } |
---|
320 | |
---|
321 | ee_set_color(ee_rand(0, 15)); |
---|
322 | if(thin) |
---|
323 | ee_draw_thin_line(xa, ya, xb, yb); |
---|
324 | else |
---|
325 | ee_draw_line(xa, ya, xb, yb, '#'); |
---|
326 | |
---|
327 | ee_refresh(); |
---|
328 | } |
---|
329 | |
---|
330 | static void demo_boxes(void) |
---|
331 | { |
---|
332 | int w = ee_get_width(); |
---|
333 | int h = ee_get_height(); |
---|
334 | int xa, ya, xb, yb; |
---|
335 | |
---|
336 | if(force_clipping) |
---|
337 | { |
---|
338 | xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h); |
---|
339 | xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h); |
---|
340 | } |
---|
341 | else |
---|
342 | { |
---|
343 | xa = ee_rand(0, w - 1); ya = ee_rand(0, h - 1); |
---|
344 | xb = ee_rand(0, w - 1); yb = ee_rand(0, h - 1); |
---|
345 | } |
---|
346 | |
---|
347 | ee_set_color(ee_rand(0, 15)); |
---|
348 | ee_fill_box(xa, ya, xb, yb, '#'); |
---|
349 | |
---|
350 | if(outline) |
---|
351 | { |
---|
352 | ee_set_color(ee_rand(0, 15)); |
---|
353 | ee_draw_thin_box(xa, ya, xb, yb); |
---|
354 | } |
---|
355 | |
---|
356 | ee_refresh(); |
---|
357 | } |
---|
358 | |
---|
359 | static void demo_ellipses(void) |
---|
360 | { |
---|
361 | int w = ee_get_width(); |
---|
362 | int h = ee_get_height(); |
---|
363 | int x, y, a, b; |
---|
364 | |
---|
365 | if(force_clipping) |
---|
366 | { |
---|
367 | x = ee_rand(- w, 2 * w); y = ee_rand(- h, 2 * h); |
---|
368 | a = ee_rand(0, w); b = ee_rand(0, h); |
---|
369 | } |
---|
370 | else |
---|
371 | { |
---|
372 | do |
---|
373 | { |
---|
374 | x = ee_rand(0, w); y = ee_rand(0, h); |
---|
375 | a = ee_rand(0, w); b = ee_rand(0, h); |
---|
376 | |
---|
377 | } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h); |
---|
378 | } |
---|
379 | |
---|
380 | ee_set_color(ee_rand(0, 15)); |
---|
381 | ee_fill_ellipse(x, y, a, b, '#'); |
---|
382 | |
---|
383 | if(outline) |
---|
384 | { |
---|
385 | ee_set_color(ee_rand(0, 15)); |
---|
386 | ee_draw_thin_ellipse(x, y, a, b); |
---|
387 | } |
---|
388 | |
---|
389 | ee_refresh(); |
---|
390 | } |
---|
391 | |
---|
392 | static void demo_triangles(void) |
---|
393 | { |
---|
394 | int w = ee_get_width(); |
---|
395 | int h = ee_get_height(); |
---|
396 | int xa, ya, xb, yb, xc, yc; |
---|
397 | |
---|
398 | if(force_clipping) |
---|
399 | { |
---|
400 | xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h); |
---|
401 | xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h); |
---|
402 | xc = ee_rand(- w, 2 * w); yc = ee_rand(- h, 2 * h); |
---|
403 | } |
---|
404 | else |
---|
405 | { |
---|
406 | |
---|
407 | xa = ee_rand(0, w - 1); ya = ee_rand(0, h - 1); |
---|
408 | xb = ee_rand(0, w - 1); yb = ee_rand(0, h - 1); |
---|
409 | xc = ee_rand(0, w - 1); yc = ee_rand(0, h - 1); |
---|
410 | } |
---|
411 | |
---|
412 | ee_set_color(ee_rand(0, 15)); |
---|
413 | ee_fill_triangle(xa, ya, xb, yb, xc, yc, '#'); |
---|
414 | |
---|
415 | if(outline) |
---|
416 | { |
---|
417 | ee_set_color(ee_rand(0, 15)); |
---|
418 | ee_draw_thin_triangle(xa, ya, xb, yb, xc, yc); |
---|
419 | } |
---|
420 | |
---|
421 | ee_refresh(); |
---|
422 | } |
---|
423 | |
---|
424 | static void demo_sprites(void) |
---|
425 | { |
---|
426 | ee_draw_sprite(ee_rand(0, ee_get_width() - 1), |
---|
427 | ee_rand(0, ee_get_height() - 1), sprite, 0); |
---|
428 | ee_refresh(); |
---|
429 | } |
---|
430 | |
---|