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

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