source: libcaca/trunk/examples/demo.c @ 192

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