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

Last change on this file since 148 was 148, checked in by Sam Hocevar, 17 years ago
  • libee/graphics.c: + Moved ee_putstr() and ee_putchar() in here.
  • libee/ee.h: + Got rid of ee_goto(). + Moved <slang.h> or <curses.h> into libee.
  • Replaced ee_goto()/ee_putstr() pairs with ee_putstr().
  • Ditto for ee_putchar().
  • Property svn:keywords set to Id
File size: 12.2 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 148 2003-11-10 23:38: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
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_ellipses(void);
39static void demo_triangles(void);
40static void demo_outlined_triangles(void);
41static void demo_sprites(void);
42
43int force_clipping = 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                break;
91            case '3':
92                ee_clear();
93                demo = demo_thin_lines;
94                break;
95            case '4':
96                ee_clear();
97                demo = demo_circles;
98                break;
99            case '5':
100                ee_clear();
101                demo = demo_ellipses;
102                break;
103            case '6':
104                ee_clear();
105                demo = demo_triangles;
106                break;
107            case '7':
108                ee_clear();
109                demo = demo_outlined_triangles;
110                break;
111            case '8':
112                ee_clear();
113                demo = demo_sprites;
114                break;
115            }
116        }
117
118        if(demo)
119            demo();
120    }
121
122    /* Clean up */
123    ee_free_sprite(sprite);
124    ee_end();
125
126    return 0;
127}
128
129static void display_menu(void)
130{
131    int xo = ee_get_width() - 2;
132    int yo = ee_get_height() - 2;
133
134    ee_clear();
135    ee_color(EE_WHITE);
136    ee_draw_line(xo, yo, 1, yo, '.');
137    ee_draw_line(1, yo, 1, 1, ':');
138    ee_draw_line(xo, 1, xo, yo, ':');
139    ee_draw_line(1, 1, xo, 1, '.');
140
141    ee_putstr((xo - strlen("libee demo")) / 2, 3, "libee demo");
142    ee_putstr((xo - strlen("============")) / 2, 4, "============");
143
144    ee_putstr(4, 6, "0: complete demo");
145    ee_putstr(4, 7, "1: dots demo");
146    ee_putstr(4, 8, "2: lines demo");
147    ee_putstr(4, 9, "3: thin lines demo");
148    ee_putstr(4, 10, "4: circles demo");
149    ee_putstr(4, 11, "5: ellipses demo");
150    ee_putstr(4, 12, "6: triangles demo");
151    ee_putstr(4, 13, "7: outlined triangles demo");
152    ee_putstr(4, 14, "8: sprites demo");
153
154    ee_putstr(4, yo - 2, "q: quit");
155
156    ee_refresh();
157}
158
159static void demo_all(void)
160{
161    static int i = 0;
162
163    int j, xo, yo, x1, y1, x2, y2, x3, y3;
164
165    i++;
166
167    ee_clear();
168
169    /* Draw the sun */
170    ee_color(EE_YELLOW);
171    xo = ee_get_width() / 4;
172    yo = ee_get_height() / 4 + 5 * sin(0.03*i);
173
174    for(j = 0; j < 16; j++)
175    {
176        x1 = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
177        y1 = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
178        ee_draw_thin_line(xo, yo, x1, y1);
179    }
180
181    ee_color(EE_WHITE);
182    for(j = 15 + sin(0.03*i) * 8; j--;)
183    {
184        ee_draw_circle(xo, yo, j, '#');
185    }
186
187    ee_color(EE_YELLOW);
188    ee_draw_circle(xo, yo, 15 + sin(0.03*i) * 8, '#');
189
190    /* Draw the pyramid */
191    xo = ee_get_width() * 5 / 8;
192    yo = 2;
193
194    x1 = ee_get_width() / 8 + sin(0.03*i) * 5;
195    y1 = ee_get_height() / 2 + cos(0.03*i) * 5;
196
197    x2 = ee_get_width() - 10 - cos(0.02*i) * 10;
198    y2 = ee_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
199
200    x3 = ee_get_width() / 4 - sin(0.02*i) * 5;
201    y3 = ee_get_height() * 3 / 4 + cos(0.02*i) * 5;
202
203    ee_color(EE_GREEN);
204    ee_fill_triangle(xo, yo, x2, y2, x1, y1, '%');
205    ee_color(EE_YELLOW);
206    ee_draw_thin_line(xo, yo, x2, y2);
207    ee_draw_thin_line(x2, y2, x1, y1);
208    ee_draw_thin_line(x1, y1, xo, yo);
209
210    ee_color(EE_RED);
211    ee_fill_triangle(x1, y1, x2, y2, x3, y3, '#');
212    ee_color(EE_YELLOW);
213    ee_draw_thin_line(x1, y1, x2, y2);
214    ee_draw_thin_line(x2, y2, x3, y3);
215    ee_draw_thin_line(x3, y3, x1, y1);
216
217    ee_color(EE_BLUE);
218    ee_fill_triangle(xo, yo, x2, y2, x3, y3, '%');
219    ee_color(EE_YELLOW);
220    ee_draw_thin_line(xo, yo, x2, y2);
221    ee_draw_thin_line(x2, y2, x3, y3);
222    ee_draw_thin_line(x3, y3, xo, yo);
223
224    /* Draw a background triangle */
225    x1 = 2;
226    y1 = 2;
227
228    x2 = ee_get_width() - 3;
229    y2 = ee_get_height() / 2;
230
231    x3 = ee_get_width() / 3;
232    y3 = ee_get_height() - 3;
233
234    ee_color(EE_BLUE);
235//    ee_fill_triangle(x1, y1, x2, y2, x3, y3, '.');
236    ee_color(EE_CYAN);
237    ee_draw_thin_line(x1, y1, x3, y3);
238    ee_draw_thin_line(x2, y2, x1, y1);
239    ee_draw_thin_line(x3, y3, x2, y2);
240
241    xo = ee_get_width() / 2 + cos(0.027*i) * ee_get_width() / 3;
242    yo = ee_get_height() / 2 - sin(0.027*i) * ee_get_height() / 2;
243
244    ee_draw_thin_line(x1, y1, xo, yo);
245    ee_draw_thin_line(x2, y2, xo, yo);
246    ee_draw_thin_line(x3, y3, xo, yo);
247
248    /* Draw a sprite behind the pyramid */
249    ee_draw_sprite(xo, yo, sprite);
250
251    /* Draw a trail behind the foreground sprite */
252    for(j = i - 60; j < i; j++)
253    {
254        int delta = ee_rand(-5, 5);
255        ee_color(ee_rand(1, 10));
256        ee_putchar(ee_get_width() / 2
257                    + cos(0.02*j) * (delta + ee_get_width() / 4),
258                   ee_get_height() / 2
259                    + sin(0.02*j) * (delta + ee_get_height() / 3),
260                   '#');
261    }
262
263    /* Draw foreground sprite */
264    ee_draw_sprite(ee_get_width() / 2 + cos(0.02*i) * ee_get_width() / 4,
265                   ee_get_height() / 2 + sin(0.02*i) * ee_get_height() / 3,
266                   sprite);
267
268    ee_refresh();
269}
270
271static void demo_dots(void)
272{
273    int xmax = ee_get_width() - 1;
274    int ymax = ee_get_height() - 1;
275    int i;
276
277    for(i = 1000; i--;)
278    {
279        /* Putpixel */
280        ee_color(ee_rand(1, 10));
281        ee_putchar(ee_rand(0, xmax), ee_rand(0, ymax), '#');
282    }
283    ee_refresh();
284}
285
286static void demo_lines(void)
287{
288    int w = ee_get_width();
289    int h = ee_get_height();
290
291    /* Draw lines */
292    ee_color(ee_rand(1, 10));
293    if(force_clipping)
294    {
295        ee_draw_line(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
296                     ee_rand(- w, 2 * w), ee_rand(- h, 2 * h), '#');
297    }
298    else
299    {
300        ee_draw_line(ee_rand(0, w - 1), ee_rand(0, h - 1),
301                     ee_rand(0, w - 1), ee_rand(0, h - 1), '#');
302    }
303    ee_refresh();
304}
305
306static void demo_thin_lines(void)
307{
308    int w = ee_get_width();
309    int h = ee_get_height();
310
311    /* Draw lines */
312    ee_color(ee_rand(1, 10));
313    if(force_clipping)
314    {
315        ee_draw_thin_line(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
316                          ee_rand(- w, 2 * w), ee_rand(- h, 2 * h));
317    }
318    else
319    {
320        ee_draw_thin_line(ee_rand(0, w), ee_rand(0, h),
321                          ee_rand(0, w), ee_rand(0, h));
322    }
323    ee_refresh();
324}
325
326static void demo_circles(void)
327{
328    int w = ee_get_width();
329    int h = ee_get_height();
330
331    /* Draw circles */
332    if(force_clipping)
333    {
334        ee_color(ee_rand(1, 10));
335        ee_draw_circle(ee_rand(- w, 2 * w),
336                       ee_rand(- h, 2 * h),
337                       ee_rand(0, (w + h) / 2),
338                       '#');
339    }
340    else
341    {
342        int x = ee_rand(0, w);
343        int y = ee_rand(0, h);
344        int r = ee_rand(0, (w + h) / 2);
345
346        if(x - r < 0 || x + r >= w || y - r < 0 || y + r >= h)
347        {
348            demo_circles();
349            return;
350        }
351
352        ee_color(ee_rand(1, 10));
353        ee_draw_circle(x, y, r, '#');
354    }
355
356    ee_refresh();
357}
358
359static void demo_ellipses(void)
360{
361    int w = ee_get_width();
362    int h = ee_get_height();
363
364    /* Draw circles */
365    if(force_clipping)
366    {
367        ee_color(ee_rand(1, 10));
368        ee_draw_ellipse(ee_rand(- w, 2 * w),
369                        ee_rand(- h, 2 * h),
370                        ee_rand(0, w),
371                        ee_rand(0, h),
372                        '#');
373    }
374    else
375    {
376        int x = ee_rand(0, w);
377        int y = ee_rand(0, h);
378        int a = ee_rand(0, w);
379        int b = ee_rand(0, h);
380
381        if(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h)
382        {
383            demo_ellipses();
384            return;
385        }
386
387        ee_color(ee_rand(1, 10));
388        ee_draw_ellipse(x, y, a, b, '#');
389    }
390
391    ee_refresh();
392}
393
394static void demo_triangles(void)
395{
396    int w = ee_get_width();
397    int h = ee_get_height();
398
399    /* Draw lines */
400    ee_color(ee_rand(1, 10));
401    if(force_clipping)
402    {
403        ee_fill_triangle(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
404                         ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
405                         ee_rand(- w, 2 * w), ee_rand(- h, 2 * h), '#');
406    }
407    else
408    {
409        ee_fill_triangle(ee_rand(0, w - 1), ee_rand(0, h - 1),
410                         ee_rand(0, w - 1), ee_rand(0, h - 1),
411                         ee_rand(0, w - 1), ee_rand(0, h - 1), '#');
412    }
413    ee_refresh();
414}
415
416static void demo_outlined_triangles(void)
417{
418    int w = ee_get_width();
419    int h = ee_get_height();
420    int x1, y1, x2, y2, x3, y3;
421
422    /* Draw lines */
423    ee_color(ee_rand(1, 10));
424    if(force_clipping)
425    {
426        x1 = ee_rand(- w, 2 * w);
427        y1 = ee_rand(- h, 2 * h);
428        x2 = ee_rand(- w, 2 * w);
429        y2 = ee_rand(- h, 2 * h);
430        x3 = ee_rand(- w, 2 * w);
431        y3 = ee_rand(- h, 2 * h);
432    }
433    else
434    {
435
436        x1 = ee_rand(0, w - 1);
437        y1 = ee_rand(0, h - 1);
438        x2 = ee_rand(0, w - 1);
439        y2 = ee_rand(0, h - 1);
440        x3 = ee_rand(0, w - 1);
441        y3 = ee_rand(0, h - 1);
442    }
443
444    ee_fill_triangle(x1, y1, x2, y2, x3, y3, '#');
445
446    ee_color(ee_rand(1, 10));
447    ee_draw_thin_line(x1, y1, x2, y2);
448    ee_draw_thin_line(x2, y2, x3, y3);
449    ee_draw_thin_line(x3, y3, x1, y1);
450
451    ee_refresh();
452}
453
454static void demo_sprites(void)
455{
456    ee_draw_sprite(ee_rand(0, ee_get_width() - 1),
457                   ee_rand(0, ee_get_height() - 1), sprite);
458    ee_refresh();
459}
460
461static void demo_pyramid(void)
462{
463    static int i = 0;
464
465    int xo, yo, x1, y1, x2, y2, x3, y3;
466
467    i++;
468
469    xo = ee_get_width() * 5 / 8;
470    yo = 2;
471
472    x1 = ee_get_width() / 8 + sin(0.03*i) * 5;
473    y1 = ee_get_height() / 2 + cos(0.03*i) * 5;
474
475    x2 = ee_get_width() - 10 - cos(0.02*i) * 10;
476    y2 = ee_get_height() - 5 + sin(0.02*i) * 5;
477
478    x3 = ee_get_width() / 4 - sin(0.02*i) * 5;
479    y3 = ee_get_height() + cos(0.02*i) * 5;
480
481    ee_clear();
482
483    ee_color(EE_GREEN);
484    ee_fill_triangle(xo, yo, x2, y2, x1, y1, '%');
485    ee_color(EE_YELLOW);
486    ee_draw_thin_line(xo, yo, x2, y2);
487    ee_draw_thin_line(x2, y2, x1, y1);
488    ee_draw_thin_line(x1, y1, xo, yo);
489
490    ee_color(EE_RED);
491    ee_fill_triangle(x1, y1, x2, y2, x3, y3, '#');
492    ee_color(EE_YELLOW);
493    ee_draw_thin_line(x1, y1, x2, y2);
494    ee_draw_thin_line(x2, y2, x3, y3);
495    ee_draw_thin_line(x3, y3, x1, y1);
496
497    ee_color(EE_BLUE);
498    ee_fill_triangle(xo, yo, x2, y2, x3, y3, '%');
499    ee_color(EE_YELLOW);
500    ee_draw_thin_line(xo, yo, x2, y2);
501    ee_draw_thin_line(x2, y2, x3, y3);
502    ee_draw_thin_line(x3, y3, xo, yo);
503
504    ee_refresh();
505}
506
Note: See TracBrowser for help on using the repository browser.