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

Last change on this file since 184 was 184, checked in by Sam Hocevar, 17 years ago
  • Very ugly ee_blit function. Will be polished in a while.
  • Property svn:keywords set to Id
File size: 11.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 184 2003-11-16 00:09:09Z 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#include <stdio.h>
28
29#include <gdk/gdk.h>
30#include <gdk/gdkpixbuf.h>
31
32#include "ee.h"
33
34static void display_menu(void);
35
36static void demo_all(void);
37
38static void demo_color(void);
39static void demo_dots(void);
40static void demo_lines(void);
41static void demo_boxes(void);
42static void demo_ellipses(void);
43static void demo_triangles(void);
44static void demo_sprites(void);
45static void demo_blit(void);
46
47int bounds = 0;
48int outline = 0;
49struct ee_sprite *sprite = NULL;
50
51GdkPixbuf *pixbuf;
52char *pixels;
53int bufx, bufy, bufpitch;
54
55int main(int argc, char **argv)
56{
57    void (*demo)(void) = NULL;
58    int quit = 0;
59
60    if(ee_init())
61    {
62        return 1;
63    }
64
65    ee_set_delay(40000);
66
67    /* Initialize data */
68    sprite = ee_load_sprite("data/barboss.txt");
69
70gdk_init (&argc, &argv);
71    //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/gally4.jpeg", NULL);
72    //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/badge1.jpeg", NULL);
73    //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/union.png", NULL);
74    pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/pikachu.jpeg", NULL);
75if(!pixbuf) return -2;
76    pixels = gdk_pixbuf_get_pixels(pixbuf);
77    bufx = gdk_pixbuf_get_width(pixbuf);
78    bufy = gdk_pixbuf_get_height(pixbuf);
79    bufpitch = gdk_pixbuf_get_rowstride(pixbuf);
80fprintf(stderr, "bits: %i\n", gdk_pixbuf_get_bits_per_sample(pixbuf));
81fprintf(stderr, "w %i, h %i, stride %i\n", bufx, bufy, bufpitch);
82
83    /* Main menu */
84    display_menu();
85    ee_refresh();
86
87    /* Go ! */
88    while(!quit)
89    {
90        char key = ee_get_key();
91
92        if(key && demo)
93        {
94            display_menu();
95            ee_refresh();
96            demo = NULL;
97        }
98        else if(key)
99        {
100        handle_key:
101            switch(key)
102            {
103            case 'q':
104            case 'Q':
105                demo = NULL;
106                quit = 1;
107                break;
108            case 'o':
109            case 'O':
110                outline = (outline + 1) % 3;
111                display_menu();
112                break;
113            case 'b':
114            case 'B':
115                bounds = (bounds + 1) % 2;
116                display_menu();
117                break;
118            case 'c':
119                demo = demo_color;
120                break;
121            case 'f':
122            case 'F':
123                demo = demo_all;
124                break;
125            case '1':
126                demo = demo_dots;
127                break;
128            case '2':
129                demo = demo_lines;
130                break;
131            case '3':
132                demo = demo_boxes;
133                break;
134            case '4':
135                demo = demo_triangles;
136                break;
137            case '5':
138                demo = demo_ellipses;
139                break;
140            case 's':
141            case 'S':
142                demo = demo_sprites;
143                break;
144            case 'i':
145            case 'I':
146                demo = demo_blit;
147                break;
148            }
149
150            if(demo)
151                ee_clear();
152
153            key = ee_get_key();
154            if(key)
155                goto handle_key;
156
157            ee_refresh();
158        }
159
160        if(demo)
161        {
162            demo();
163
164            ee_set_color(EE_WHITE);
165            ee_draw_thin_box(1, 1, ee_get_width() - 2, ee_get_height() - 2);
166            ee_printf(4, 1, "[%i.%i fps]----",
167                            1000000 / ee_get_rendertime(),
168                            (10000000 / ee_get_rendertime()) % 10);
169            ee_refresh();
170        }
171    }
172
173    /* Clean up */
174    ee_free_sprite(sprite);
175    ee_end();
176
177    return 0;
178}
179
180static void display_menu(void)
181{
182    int xo = ee_get_width() - 2;
183    int yo = ee_get_height() - 2;
184
185    ee_clear();
186    ee_set_color(EE_WHITE);
187    ee_draw_thin_box(1, 1, xo, yo);
188
189    ee_putstr((xo - strlen("libee demo")) / 2, 3, "libee demo");
190    ee_putstr((xo - strlen("============")) / 2, 4, "============");
191
192    ee_putstr(4, 6, "demos:");
193    ee_putstr(4, 7, "'f': full");
194    ee_putstr(4, 8, "'1': dots");
195    ee_putstr(4, 9, "'2': lines");
196    ee_putstr(4, 10, "'3': boxes");
197    ee_putstr(4, 11, "'4': triangles");
198    ee_putstr(4, 12, "'5': ellipses");
199    ee_putstr(4, 13, "'s': sprites");
200    ee_putstr(4, 14, "'c': color");
201    ee_putstr(4, 15, "'i': image blit");
202
203    ee_putstr(4, 17, "settings:");
204    ee_printf(4, 18, "'o': outline: %s",
205              outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
206    ee_printf(4, 19, "'b': drawing boundaries: %s",
207              bounds == 0 ? "screen" : "infinite");
208
209    ee_putstr(4, yo - 2, "'q': quit");
210}
211
212static void demo_all(void)
213{
214    static int i = 0;
215
216    int j, xo, yo, xa, ya, xb, yb, xc, yc;
217
218    i++;
219
220    ee_clear();
221
222    /* Draw the sun */
223    ee_set_color(EE_YELLOW);
224    xo = ee_get_width() / 4;
225    yo = ee_get_height() / 4 + 5 * sin(0.03*i);
226
227    for(j = 0; j < 16; j++)
228    {
229        xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
230        ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
231        ee_draw_thin_line(xo, yo, xa, ya);
232    }
233
234    j = 15 + sin(0.03*i) * 8;
235    ee_set_color(EE_WHITE);
236    ee_fill_ellipse(xo, yo, j, j / 2, '#');
237    ee_set_color(EE_YELLOW);
238    ee_draw_ellipse(xo, yo, j, j / 2, '#');
239
240    /* Draw the pyramid */
241    xo = ee_get_width() * 5 / 8;
242    yo = 2;
243
244    xa = ee_get_width() / 8 + sin(0.03*i) * 5;
245    ya = ee_get_height() / 2 + cos(0.03*i) * 5;
246
247    xb = ee_get_width() - 10 - cos(0.02*i) * 10;
248    yb = ee_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
249
250    xc = ee_get_width() / 4 - sin(0.02*i) * 5;
251    yc = ee_get_height() * 3 / 4 + cos(0.02*i) * 5;
252
253    ee_set_color(EE_GREEN);
254    ee_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
255    ee_set_color(EE_YELLOW);
256    ee_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
257
258    ee_set_color(EE_RED);
259    ee_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
260    ee_set_color(EE_YELLOW);
261    ee_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
262
263    ee_set_color(EE_BLUE);
264    ee_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
265    ee_set_color(EE_YELLOW);
266    ee_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
267
268    /* Draw a background triangle */
269    xa = 2;
270    ya = 2;
271
272    xb = ee_get_width() - 3;
273    yb = ee_get_height() / 2;
274
275    xc = ee_get_width() / 3;
276    yc = ee_get_height() - 3;
277
278    ee_set_color(EE_CYAN);
279    ee_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
280
281    xo = ee_get_width() / 2 + cos(0.027*i) * ee_get_width() / 3;
282    yo = ee_get_height() / 2 - sin(0.027*i) * ee_get_height() / 2;
283
284    ee_draw_thin_line(xa, ya, xo, yo);
285    ee_draw_thin_line(xb, yb, xo, yo);
286    ee_draw_thin_line(xc, yc, xo, yo);
287
288    /* Draw a sprite on the pyramid */
289    ee_draw_sprite(xo, yo, sprite, 0);
290
291    /* Draw a trail behind the foreground sprite */
292    for(j = i - 60; j < i; j++)
293    {
294        int delta = ee_rand(-5, 5);
295        ee_set_color(ee_rand(0, 15));
296        ee_putchar(ee_get_width() / 2
297                    + cos(0.02*j) * (delta + ee_get_width() / 4),
298                   ee_get_height() / 2
299                    + sin(0.02*j) * (delta + ee_get_height() / 3),
300                   '#');
301    }
302
303    /* Draw foreground sprite */
304    ee_draw_sprite(ee_get_width() / 2 + cos(0.02*i) * ee_get_width() / 4,
305                   ee_get_height() / 2 + sin(0.02*i) * ee_get_height() / 3,
306                   sprite, 0);
307}
308
309static void demo_dots(void)
310{
311    int xmax = ee_get_width() - 1;
312    int ymax = ee_get_height() - 1;
313    int i;
314
315    for(i = 1000; i--;)
316    {
317        /* Putpixel */
318        ee_set_color(ee_rand(0, 15));
319        ee_putchar(ee_rand(0, xmax), ee_rand(0, ymax), '#');
320    }
321}
322
323static void demo_color(void)
324{
325    int i;
326    char buf[BUFSIZ];
327
328    ee_clear();
329    for(i = 0; i < 16; i++)
330    {
331        sprintf(buf, "'%c': %i (%s)", 'a' + i, i, ee_get_color_name(i));
332        ee_set_color(EE_WHITE);
333        ee_putstr(4, i + 3, buf);
334        ee_set_color(i);
335        ee_putstr(40, i + 3, "XXXXXXXXXX-XX--X----------");
336    }
337}
338
339static void demo_lines(void)
340{
341    int w = ee_get_width();
342    int h = ee_get_height();
343    int xa, ya, xb, yb;
344
345    if(bounds)
346    {
347        xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h);
348        xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h);
349    }
350    else
351    {
352        xa = ee_rand(0, w - 1); ya = ee_rand(0, h - 1);
353        xb = ee_rand(0, w - 1); yb = ee_rand(0, h - 1);
354    }
355
356    ee_set_color(ee_rand(0, 15));
357    if(outline > 1)
358        ee_draw_thin_line(xa, ya, xb, yb);
359    else
360        ee_draw_line(xa, ya, xb, yb, '#');
361}
362
363static void demo_boxes(void)
364{
365    int w = ee_get_width();
366    int h = ee_get_height();
367    int xa, ya, xb, yb;
368
369    if(bounds)
370    {
371        xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h);
372        xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h);
373    }
374    else
375    {
376        xa = ee_rand(0, w - 1); ya = ee_rand(0, h - 1);
377        xb = ee_rand(0, w - 1); yb = ee_rand(0, h - 1);
378    }
379
380    ee_set_color(ee_rand(0, 15));
381    ee_fill_box(xa, ya, xb, yb, '#');
382
383    ee_set_color(ee_rand(0, 15));
384    if(outline == 2)
385        ee_draw_thin_box(xa, ya, xb, yb);
386    else if(outline == 1)
387        ee_draw_box(xa, ya, xb, yb, '#');
388}
389
390static void demo_ellipses(void)
391{
392    int w = ee_get_width();
393    int h = ee_get_height();
394    int x, y, a, b;
395
396    if(bounds)
397    {
398        x = ee_rand(- w, 2 * w); y = ee_rand(- h, 2 * h);
399        a = ee_rand(0, w); b = ee_rand(0, h);
400    }
401    else
402    {
403        do
404        {
405            x = ee_rand(0, w); y = ee_rand(0, h);
406            a = ee_rand(0, w); b = ee_rand(0, h);
407
408        } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
409    }
410
411    ee_set_color(ee_rand(0, 15));
412    ee_fill_ellipse(x, y, a, b, '#');
413
414    ee_set_color(ee_rand(0, 15));
415    if(outline == 2)
416        ee_draw_thin_ellipse(x, y, a, b);
417    else if(outline == 1)
418        ee_draw_ellipse(x, y, a, b, '#');
419}
420
421static void demo_triangles(void)
422{
423    int w = ee_get_width();
424    int h = ee_get_height();
425    int xa, ya, xb, yb, xc, yc;
426
427    if(bounds)
428    {
429        xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h);
430        xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h);
431        xc = ee_rand(- w, 2 * w); yc = ee_rand(- h, 2 * h);
432    }
433    else
434    {
435
436        xa = ee_rand(0, w - 1); ya = ee_rand(0, h - 1);
437        xb = ee_rand(0, w - 1); yb = ee_rand(0, h - 1);
438        xc = ee_rand(0, w - 1); yc = ee_rand(0, h - 1);
439    }
440
441    ee_set_color(ee_rand(0, 15));
442    ee_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
443
444    ee_set_color(ee_rand(0, 15));
445    if(outline == 2)
446        ee_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
447    else if(outline == 1)
448        ee_draw_triangle(xa, ya, xb, yb, xc, yc, '#');
449}
450
451static void demo_sprites(void)
452{
453    ee_draw_sprite(ee_rand(0, ee_get_width() - 1),
454                   ee_rand(0, ee_get_height() - 1), sprite, 0);
455}
456
457static void demo_blit(void)
458{
459ee_set_color(EE_LIGHTGRAY);
460    ee_blit(6, 4, ee_get_width() - 6, ee_get_height() - 4, pixels, bufx, bufy);
461}
462
Note: See TracBrowser for help on using the repository browser.