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

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