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

Last change on this file since 108 was 108, checked in by Sam Hocevar, 16 years ago
  • test/demo.c: + Disabled the pyramid demo and wrote a simple triangles demo.
  • Property svn:keywords set to Id
File size: 6.6 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 108 2003-11-09 23:01:29Z 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);
37
38int clipping = 0;
39
40int main(int argc, char **argv)
41{
42    void (*demo)(void) = NULL;
43    int quit = 0;
44
45    if(ee_init())
46    {
47        return 1;
48    }
49
50    display_menu();
51
52    /* Go ! */
53    while(!quit)
54    {
55        char key = ee_get_key();
56        if(key && demo)
57        {
58            display_menu();
59            demo = NULL;
60        }
61        else if(key)
62        {
63            switch(key)
64            {
65            case 'q':
66                demo = NULL;
67                quit = 1;
68                break;
69            case '1':
70                ee_clear();
71                demo = demo_dots;
72                break;
73            case '2':
74                ee_clear();
75                demo = demo_lines;
76                break;
77            case '3':
78                ee_clear();
79                demo = demo_thin_lines;
80                break;
81            case '4':
82                ee_clear();
83                demo = demo_circles;
84                break;
85            case '5':
86                ee_clear();
87                demo = demo_triangles;
88                break;
89            }
90        }
91
92        if(demo)
93            demo();
94    }
95
96    /* Clean up */
97    ee_end();
98
99    return 0;
100}
101
102static void display_menu(void)
103{
104    int xo = ee_get_width() - 2;
105    int yo = ee_get_height() - 2;
106
107    ee_clear();
108    ee_color(EE_WHITE);
109    ee_draw_line(xo, yo, 1, yo, '.');
110    ee_draw_line(1, yo, 1, 1, ':');
111    ee_draw_line(xo, 1, xo, yo, ':');
112    ee_draw_line(1, 1, xo, 1, '.');
113
114    ee_goto((xo - strlen("libee demo")) / 2, 3);
115    ee_putstr("libee demo");
116    ee_goto((xo - strlen("============")) / 2, 4);
117    ee_putstr("============");
118
119    ee_goto(4, 6);
120    ee_putstr("1: dots demo");
121    ee_goto(4, 7);
122    ee_putstr("2: lines demo");
123    ee_goto(4, 8);
124    ee_putstr("3: thin lines demo");
125    ee_goto(4, 9);
126    ee_putstr("4: circles demo");
127    ee_goto(4, 10);
128    ee_putstr("5: triangles demo");
129
130    ee_goto(4, yo - 2);
131    ee_putstr("q: quit");
132
133    ee_refresh();
134}
135
136static void demo_dots(void)
137{
138    int xmax = ee_get_width() - 1;
139    int ymax = ee_get_height() - 1;
140    int i;
141
142    for(i = 1000; i--;)
143    {
144        /* Putpixel */
145        ee_color(ee_rand(1, 10));
146        ee_goto(ee_rand(0, xmax), ee_rand(0, ymax));
147        ee_putchar('#');
148    }
149    ee_refresh();
150}
151
152static void demo_lines(void)
153{
154    int w = ee_get_width();
155    int h = ee_get_height();
156
157    /* Draw lines */
158    ee_color(ee_rand(1, 10));
159    if(clipping)
160    {
161        ee_draw_line(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
162                     ee_rand(- w, 2 * w), ee_rand(- h, 2 * h), '#');
163    }
164    else
165    {
166        ee_draw_line(ee_rand(0, w - 1), ee_rand(0, h - 1),
167                     ee_rand(0, w - 1), ee_rand(0, h - 1), '#');
168    }
169    ee_refresh();
170}
171
172static void demo_thin_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_thin_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_thin_line(ee_rand(0, w), ee_rand(0, h),
187                          ee_rand(0, w), ee_rand(0, h));
188    }
189    ee_refresh();
190}
191
192static void demo_circles(void)
193{
194    int w = ee_get_width();
195    int h = ee_get_height();
196
197    /* Draw circles */
198    if(clipping)
199    {
200        ee_color(ee_rand(1, 10));
201        ee_draw_circle(ee_rand(- w, 2 * w),
202                       ee_rand(- h, 2 * h),
203                       ee_rand(0, (w + h) / 2),
204                       '#');
205    }
206    else
207    {
208        int x = ee_rand(0, w);
209        int y = ee_rand(0, h);
210        int r = ee_rand(0, (w + h) / 2);
211
212        if(x - r < 0 || x + r >= w || y - r < 0 || y + r >= h)
213        {
214            demo_circles();
215            return;
216        }
217
218        ee_color(ee_rand(1, 10));
219        ee_draw_circle(x, y, r, '#');
220    }
221
222    ee_refresh();
223}
224
225static void demo_triangles(void)
226{
227    int w = ee_get_width();
228    int h = ee_get_height();
229
230    /* Draw lines */
231    ee_color(ee_rand(1, 10));
232    if(clipping)
233    {
234        ee_fill_triangle(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
235                         ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
236                         ee_rand(- w, 2 * w), ee_rand(- h, 2 * h), '#');
237    }
238    else
239    {
240        ee_fill_triangle(ee_rand(0, w - 1), ee_rand(0, h - 1),
241                         ee_rand(0, w - 1), ee_rand(0, h - 1),
242                         ee_rand(0, w - 1), ee_rand(0, h - 1), '#');
243    }
244    ee_refresh();
245}
246
247static void demo_pyramid(void)
248{
249    static int i = 0;
250
251    int xo, yo, x1, y1, x2, y2, x3, y3;
252
253    i++;
254
255    xo = ee_get_width() * 5 / 8;
256    yo = 2;
257
258    x1 = ee_get_width() / 8 + sin(0.03*i) * 5;
259    y1 = ee_get_height() / 2 + cos(0.03*i) * 5;
260
261    x2 = ee_get_width() - 10 - cos(0.02*i) * 10;
262    y2 = ee_get_height() - 5 + sin(0.02*i) * 5;
263
264    x3 = ee_get_width() / 4 - sin(0.02*i) * 5;
265    y3 = ee_get_height() + cos(0.02*i) * 5;
266
267    ee_clear();
268
269    ee_color(EE_GREEN);
270    ee_fill_triangle(xo, yo, x2, y2, x1, y1, '%');
271    ee_color(EE_YELLOW);
272    ee_draw_thin_line(xo, yo, x2, y2);
273    ee_draw_thin_line(x2, y2, x1, y1);
274    ee_draw_thin_line(x1, y1, xo, yo);
275
276    ee_color(EE_RED);
277    ee_fill_triangle(x1, y1, x2, y2, x3, y3, '#');
278    ee_color(EE_YELLOW);
279    ee_draw_thin_line(x1, y1, x2, y2);
280    ee_draw_thin_line(x2, y2, x3, y3);
281    ee_draw_thin_line(x3, y3, x1, y1);
282
283    ee_color(EE_BLUE);
284    ee_fill_triangle(xo, yo, x2, y2, x3, y3, '%');
285    ee_color(EE_YELLOW);
286    ee_draw_thin_line(xo, yo, x2, y2);
287    ee_draw_thin_line(x2, y2, x3, y3);
288    ee_draw_thin_line(x3, y3, xo, yo);
289
290    ee_refresh();
291}
292
Note: See TracBrowser for help on using the repository browser.