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

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