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

Last change on this file since 160 was 160, checked in by Sam Hocevar, 19 years ago
  • libee/ee.h: + Added ee_color_names[] containing our 16 color names.
  • libee/ee.c: + Extended our color set to the full 16 instead of 10. + Precalculate ncurses attributes.
  • libee/graphics.c: + Clip color value in ee_set_color(). + Clip characters in ee_putchar(). + Partially clip characters in ee_putstr(), overflows aren't checked yet.
  • libee/ee_internals.h: + New file to share extern variables within libee.
  • test/demo.c: + Added a simple demo_color() to output all colors. + Replaced four ee_draw_line() with ee_draw_thin_box(). + Replaced x1, y1, x2 etc. with xa, ya, xb etc. because <math.h> already

defines y1.

+ Randomized colours from 0 to 15 instead of 1 to 10.

  • src/Makefiles.am: + Added -lm to the ttyvaders linking flags because of the intro.
  • README BUGS TODO: + Updated.
  • Property svn:keywords set to Id
File size: 10.1 KB
RevLine 
[86]1/*
2 *   demo          demo using libee
3 *   Copyright (c) 2003 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: demo.c 160 2003-11-12 21:18: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
[106]23#include "config.h"
24
[100]25#include <math.h>
26#include <string.h>
[160]27#include <stdio.h>
[100]28
[86]29#include "ee.h"
30
[100]31static void display_menu(void);
32
[124]33static void demo_all(void);
34
[160]35static void demo_color(void);
[100]36static void demo_dots(void);
37static void demo_lines(void);
[152]38static void demo_boxes(void);
[134]39static void demo_ellipses(void);
[108]40static void demo_triangles(void);
[118]41static void demo_sprites(void);
[100]42
[124]43int force_clipping = 0;
[152]44int outline = 0;
45int thin = 0;
[118]46struct ee_sprite *sprite = NULL;
[100]47
[86]48int main(int argc, char **argv)
49{
[100]50    void (*demo)(void) = NULL;
[86]51    int quit = 0;
52
53    if(ee_init())
54    {
55        return 1;
56    }
57
[118]58    /* Initialize data */
59    sprite = ee_load_sprite("data/bar_boss");
60
61    /* Main menu */
[100]62    display_menu();
63
[86]64    /* Go ! */
65    while(!quit)
66    {
67        char key = ee_get_key();
[100]68        if(key && demo)
[86]69        {
[100]70            display_menu();
71            demo = NULL;
[86]72        }
[100]73        else if(key)
[94]74        {
[100]75            switch(key)
76            {
77            case 'q':
78                demo = NULL;
79                quit = 1;
80                break;
[160]81            case 'c':
82                ee_clear();
83                demo = demo_color;
84                break;
[124]85            case '0':
86                ee_clear();
87                demo = demo_all;
88                break;
[100]89            case '1':
90                ee_clear();
91                demo = demo_dots;
92                break;
93            case '2':
94                ee_clear();
95                demo = demo_lines;
[152]96                thin = 0;
[100]97                break;
98            case '3':
99                ee_clear();
[152]100                demo = demo_lines;
101                thin = 1;
[100]102                break;
103            case '4':
104                ee_clear();
[152]105                demo = demo_boxes;
106                outline = 0;
[104]107                break;
108            case '5':
109                ee_clear();
[152]110                demo = demo_boxes;
111                outline = 1;
[106]112                break;
[114]113            case '6':
114                ee_clear();
[152]115                demo = demo_ellipses;
[114]116                break;
[118]117            case '7':
118                ee_clear();
[152]119                demo = demo_triangles;
120                outline = 0;
[134]121                break;
122            case '8':
123                ee_clear();
[152]124                demo = demo_triangles;
125                outline = 1;
126                break;
127            case '9':
128                ee_clear();
[118]129                demo = demo_sprites;
130                break;
[100]131            }
[94]132        }
133
[100]134        if(demo)
135            demo();
[86]136    }
137
138    /* Clean up */
[118]139    ee_free_sprite(sprite);
[86]140    ee_end();
141
142    return 0;
143}
144
[100]145static void display_menu(void)
146{
147    int xo = ee_get_width() - 2;
148    int yo = ee_get_height() - 2;
149
150    ee_clear();
[154]151    ee_set_color(EE_WHITE);
[160]152    ee_draw_thin_box(1, 1, xo, yo);
[100]153
[148]154    ee_putstr((xo - strlen("libee demo")) / 2, 3, "libee demo");
155    ee_putstr((xo - strlen("============")) / 2, 4, "============");
[100]156
[148]157    ee_putstr(4, 6, "0: complete demo");
158    ee_putstr(4, 7, "1: dots demo");
159    ee_putstr(4, 8, "2: lines demo");
160    ee_putstr(4, 9, "3: thin lines demo");
[152]161    ee_putstr(4, 10, "4: boxes demo");
162    ee_putstr(4, 11, "5: outlined boxes demo");
163    ee_putstr(4, 12, "6: ellipses demo");
164    ee_putstr(4, 13, "7: triangles demo");
165    ee_putstr(4, 14, "8: outlined triangles demo");
166    ee_putstr(4, 15, "9: sprites demo");
[100]167
[148]168    ee_putstr(4, yo - 2, "q: quit");
[100]169
170    ee_refresh();
171}
172
[124]173static void demo_all(void)
174{
175    static int i = 0;
176
[160]177    int j, xo, yo, xa, ya, xb, yb, xc, yc;
[124]178
179    i++;
180
181    ee_clear();
182
183    /* Draw the sun */
[154]184    ee_set_color(EE_YELLOW);
[124]185    xo = ee_get_width() / 4;
186    yo = ee_get_height() / 4 + 5 * sin(0.03*i);
187
188    for(j = 0; j < 16; j++)
189    {
[160]190        xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
191        ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
192        ee_draw_thin_line(xo, yo, xa, ya);
[124]193    }
194
[152]195    j = 15 + sin(0.03*i) * 8;
[154]196    ee_set_color(EE_WHITE);
[152]197    ee_fill_ellipse(xo, yo, j, j / 2, '#');
[154]198    ee_set_color(EE_YELLOW);
[152]199    ee_draw_ellipse(xo, yo, j, j / 2, '#');
[124]200
201    /* Draw the pyramid */
202    xo = ee_get_width() * 5 / 8;
203    yo = 2;
204
[160]205    xa = ee_get_width() / 8 + sin(0.03*i) * 5;
206    ya = ee_get_height() / 2 + cos(0.03*i) * 5;
[124]207
[160]208    xb = ee_get_width() - 10 - cos(0.02*i) * 10;
209    yb = ee_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
[124]210
[160]211    xc = ee_get_width() / 4 - sin(0.02*i) * 5;
212    yc = ee_get_height() * 3 / 4 + cos(0.02*i) * 5;
[124]213
[154]214    ee_set_color(EE_GREEN);
[160]215    ee_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
[154]216    ee_set_color(EE_YELLOW);
[160]217    ee_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
[124]218
[154]219    ee_set_color(EE_RED);
[160]220    ee_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
[154]221    ee_set_color(EE_YELLOW);
[160]222    ee_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
[124]223
[154]224    ee_set_color(EE_BLUE);
[160]225    ee_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
[154]226    ee_set_color(EE_YELLOW);
[160]227    ee_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
[124]228
229    /* Draw a background triangle */
[160]230    xa = 2;
231    ya = 2;
[124]232
[160]233    xb = ee_get_width() - 3;
234    yb = ee_get_height() / 2;
[124]235
[160]236    xc = ee_get_width() / 3;
237    yc = ee_get_height() - 3;
[124]238
[154]239    ee_set_color(EE_CYAN);
[160]240    ee_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
[124]241
242    xo = ee_get_width() / 2 + cos(0.027*i) * ee_get_width() / 3;
243    yo = ee_get_height() / 2 - sin(0.027*i) * ee_get_height() / 2;
244
[160]245    ee_draw_thin_line(xa, ya, xo, yo);
246    ee_draw_thin_line(xb, yb, xo, yo);
247    ee_draw_thin_line(xc, yc, xo, yo);
[124]248
[152]249    /* Draw a sprite on the pyramid */
[154]250    ee_draw_sprite(xo, yo, sprite, 0);
[124]251
252    /* Draw a trail behind the foreground sprite */
253    for(j = i - 60; j < i; j++)
254    {
255        int delta = ee_rand(-5, 5);
[160]256        ee_set_color(ee_rand(0, 15));
[148]257        ee_putchar(ee_get_width() / 2
258                    + cos(0.02*j) * (delta + ee_get_width() / 4),
259                   ee_get_height() / 2
260                    + sin(0.02*j) * (delta + ee_get_height() / 3),
261                   '#');
[124]262    }
263
264    /* Draw foreground sprite */
265    ee_draw_sprite(ee_get_width() / 2 + cos(0.02*i) * ee_get_width() / 4,
266                   ee_get_height() / 2 + sin(0.02*i) * ee_get_height() / 3,
[154]267                   sprite, 0);
[124]268
269    ee_refresh();
270}
271
[100]272static void demo_dots(void)
273{
[104]274    int xmax = ee_get_width() - 1;
275    int ymax = ee_get_height() - 1;
[100]276    int i;
277
[104]278    for(i = 1000; i--;)
[100]279    {
280        /* Putpixel */
[160]281        ee_set_color(ee_rand(0, 15));
[148]282        ee_putchar(ee_rand(0, xmax), ee_rand(0, ymax), '#');
[100]283    }
284    ee_refresh();
285}
286
[160]287static void demo_color(void)
288{
289    int i;
290    char buf[BUFSIZ];
291
292    ee_clear();
293    for(i = 0; i < 16; i++)
294    {
295        sprintf(buf, "color %i (%s)\n", i, ee_color_names[i]);
296        ee_set_color(EE_WHITE);
297        ee_putstr(4, i + 3, buf);
298        ee_set_color(i);
299        ee_putstr(40, i + 3, "XXXXXXXXXX-XX--X----------");
300    }
301    ee_refresh();
302}
303
[100]304static void demo_lines(void)
305{
[104]306    int w = ee_get_width();
307    int h = ee_get_height();
[160]308    int xa, ya, xb, yb;
[104]309
[124]310    if(force_clipping)
[100]311    {
[160]312        xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h);
313        xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h);
[100]314    }
315    else
316    {
[160]317        xa = ee_rand(0, w - 1); ya = ee_rand(0, h - 1);
318        xb = ee_rand(0, w - 1); yb = ee_rand(0, h - 1);
[100]319    }
320
[160]321    ee_set_color(ee_rand(0, 15));
[152]322    if(thin)
[160]323        ee_draw_thin_line(xa, ya, xb, yb);
[104]324    else
[160]325        ee_draw_line(xa, ya, xb, yb, '#');
[152]326
[104]327    ee_refresh();
328}
329
[152]330static void demo_boxes(void)
[100]331{
[104]332    int w = ee_get_width();
333    int h = ee_get_height();
[160]334    int xa, ya, xb, yb;
[104]335
[124]336    if(force_clipping)
[100]337    {
[160]338        xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h);
339        xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h);
[100]340    }
341    else
342    {
[160]343        xa = ee_rand(0, w - 1); ya = ee_rand(0, h - 1);
344        xb = ee_rand(0, w - 1); yb = ee_rand(0, h - 1);
[152]345    }
[100]346
[160]347    ee_set_color(ee_rand(0, 15));
348    ee_fill_box(xa, ya, xb, yb, '#');
[100]349
[152]350    if(outline)
351    {
[160]352        ee_set_color(ee_rand(0, 15));
353        ee_draw_thin_box(xa, ya, xb, yb);
[100]354    }
355
356    ee_refresh();
357}
358
[134]359static void demo_ellipses(void)
360{
361    int w = ee_get_width();
362    int h = ee_get_height();
[152]363    int x, y, a, b;
[134]364
365    if(force_clipping)
366    {
[152]367        x = ee_rand(- w, 2 * w); y = ee_rand(- h, 2 * h);
368        a = ee_rand(0, w); b = ee_rand(0, h);
[134]369    }
370    else
371    {
[152]372        do
[134]373        {
[152]374            x = ee_rand(0, w); y = ee_rand(0, h);
375            a = ee_rand(0, w); b = ee_rand(0, h);
[134]376
[152]377        } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
[134]378    }
379
[160]380    ee_set_color(ee_rand(0, 15));
[152]381    ee_fill_ellipse(x, y, a, b, '#');
[134]382
[152]383    if(outline)
[108]384    {
[160]385        ee_set_color(ee_rand(0, 15));
[152]386        ee_draw_thin_ellipse(x, y, a, b);
[108]387    }
[152]388
[108]389    ee_refresh();
390}
391
[152]392static void demo_triangles(void)
[114]393{
394    int w = ee_get_width();
395    int h = ee_get_height();
[160]396    int xa, ya, xb, yb, xc, yc;
[114]397
[124]398    if(force_clipping)
[114]399    {
[160]400        xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h);
401        xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h);
402        xc = ee_rand(- w, 2 * w); yc = ee_rand(- h, 2 * h);
[114]403    }
404    else
405    {
406
[160]407        xa = ee_rand(0, w - 1); ya = ee_rand(0, h - 1);
408        xb = ee_rand(0, w - 1); yb = ee_rand(0, h - 1);
409        xc = ee_rand(0, w - 1); yc = ee_rand(0, h - 1);
[124]410    }
[114]411
[160]412    ee_set_color(ee_rand(0, 15));
413    ee_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
[114]414
[152]415    if(outline)
416    {
[160]417        ee_set_color(ee_rand(0, 15));
418        ee_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
[152]419    }
[124]420
[114]421    ee_refresh();
422}
423
[118]424static void demo_sprites(void)
425{
[124]426    ee_draw_sprite(ee_rand(0, ee_get_width() - 1),
[154]427                   ee_rand(0, ee_get_height() - 1), sprite, 0);
[118]428    ee_refresh();
429}
430
Note: See TracBrowser for help on using the repository browser.