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

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