source: ttyvaders/trunk/test/demo.c @ 124

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