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

Last change on this file since 100 was 100, checked in by Sam Hocevar, 16 years ago
  • test/demo.c: + Putpixel demo. + Draw line demo. + Draw circle demo. + Animated radar demo.
  • Property svn:keywords set to Id
File size: 5.1 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 100 2003-11-09 17:45:12Z 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 <math.h>
24#include <string.h>
25
26#include "ee.h"
27
28static void display_menu(void);
29
30static void demo_dots(void);
31static void demo_lines(void);
32static void demo_circles(void);
33static void demo_radar(void);
34
35int clipping = 0;
36
37int main(int argc, char **argv)
38{
39    void (*demo)(void) = NULL;
40    int quit = 0;
41
42    if(ee_init())
43    {
44        return 1;
45    }
46
47    display_menu();
48
49    /* Go ! */
50    while(!quit)
51    {
52        char key = ee_get_key();
53        if(key && demo)
54        {
55            display_menu();
56            demo = NULL;
57        }
58        else if(key)
59        {
60            switch(key)
61            {
62            case 'q':
63                demo = NULL;
64                quit = 1;
65                break;
66            case '1':
67                ee_clear();
68                demo = demo_dots;
69                break;
70            case '2':
71                ee_clear();
72                demo = demo_lines;
73                break;
74            case '3':
75                ee_clear();
76                demo = demo_circles;
77                break;
78            case '4':
79                ee_clear();
80                demo = demo_radar;
81                break;
82            }
83        }
84
85        if(demo)
86            demo();
87    }
88
89    /* Clean up */
90    ee_end();
91
92    return 0;
93}
94
95static void display_menu(void)
96{
97    int xo = ee_get_width() - 2;
98    int yo = ee_get_height() - 2;
99
100    ee_clear();
101    ee_color(EE_WHITE);
102    ee_draw_line(xo, yo, 1, yo, '.');
103    ee_draw_line(1, yo, 1, 1, ':');
104    ee_draw_line(xo, 1, xo, yo, ':');
105    ee_draw_line(1, 1, xo, 1, '.');
106
107    ee_goto((xo - strlen("libee demo")) / 2, 3);
108    ee_putstr("libee demo");
109    ee_goto((xo - strlen("============")) / 2, 4);
110    ee_putstr("============");
111
112    ee_goto(4, 6);
113    ee_putstr("1: dots demo");
114    ee_goto(4, 7);
115    ee_putstr("2: lines demo");
116    ee_goto(4, 8);
117    ee_putstr("3: circles demo");
118    ee_goto(4, 9);
119    ee_putstr("4: radar demo");
120
121    ee_goto(4, yo - 2);
122    ee_putstr("q: quit");
123
124    ee_refresh();
125}
126
127static void demo_dots(void)
128{
129    int i;
130
131    for(i=1000; i--;)
132    {
133        /* Putpixel */
134        ee_color(ee_rand(1, 10));
135        ee_goto(ee_rand(0, ee_get_width() - 1),
136                ee_rand(0, ee_get_height() - 1));
137        ee_putchar('#');
138    }
139    ee_refresh();
140}
141
142static void demo_lines(void)
143{
144    /* Draw lines */
145    ee_color(ee_rand(1, 10));
146    if(clipping)
147    {
148        ee_draw_line(ee_rand(- ee_get_width(), 2 * ee_get_width()),
149                     ee_rand(- ee_get_height(), 2 * ee_get_height()),
150                     ee_rand(- ee_get_width(), 2 * ee_get_width()),
151                     ee_rand(- ee_get_height(), 2 * ee_get_height()),
152                     '#');
153    }
154    else
155    {
156        ee_draw_line(ee_rand(0, ee_get_width()),
157                     ee_rand(0, ee_get_height()),
158                     ee_rand(0, ee_get_width()),
159                     ee_rand(0, ee_get_height()),
160                     '*');
161    }
162    ee_refresh();
163}
164
165static void demo_circles(void)
166{
167    /* Draw circles */
168    if(clipping)
169    {
170        ee_color(ee_rand(1, 10));
171        ee_draw_circle(ee_rand(- ee_get_width(), 2 * ee_get_width()),
172                       ee_rand(- ee_get_height(), 2 * ee_get_height()),
173                       ee_rand(0, ee_get_width() + ee_get_height()) / 2,
174                       '#');
175    }
176    else
177    {
178        int x = ee_rand(0, ee_get_width());
179        int y = ee_rand(0, ee_get_height());
180        int r = ee_rand(0, (ee_get_width() + ee_get_height()) / 2);
181
182        if(x - r < 0 || x + r >= ee_get_width() ||
183           y - r < 0 || y + r >= ee_get_height())
184        {
185            demo_circles();
186            return;
187        }
188
189        ee_color(ee_rand(1, 10));
190        ee_draw_circle(x, y, r, '*');
191    }
192
193    ee_refresh();
194}
195
196static void demo_radar(void)
197{
198    static int i = 0;
199
200    int xo = ee_get_width() / 2;
201    int yo = ee_get_height() / 2;
202    int l = ee_get_height() + ee_get_width();
203
204    i++;
205
206    ee_color(EE_BLUE);
207    ee_draw_line(xo,yo,xo+(sin(0.03*(i-30))*l*2),yo+(cos(0.03*(i-30))*l),'.');
208    ee_color(EE_CYAN);
209    ee_draw_line(xo,yo,xo+(sin(0.03*(i-2))*l*2),yo+(cos(0.03*(i-2))*l),':');
210    ee_color(EE_WHITE);
211    ee_draw_line(xo,yo,xo+(sin(0.03*(i-1))*l*2),yo+(cos(0.03*(i-1))*l),':');
212    ee_color(EE_WHITE);
213    ee_draw_line(xo,yo,xo+(sin(0.03*i)*l*2),yo+(cos(0.03*i)*l),'#');
214
215    ee_refresh();
216}
217
Note: See TracBrowser for help on using the repository browser.