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

Last change on this file since 193 was 193, checked in by Sam Hocevar, 19 years ago
  • src/blit.c: + Added caca_set_dithering() (unused yet). + Added random dithering.
  • src/caca.h: + Renamed legacy EE_* enums to CACA_*.
  • examples/demo.c examples/caca.txt: + Added a default sprite for libcaca.
  • Property svn:keywords set to Id
File size: 12.5 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 193 2003-11-16 15:20:17Z 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 */
[193]69    sprite = caca_load_sprite("caca.txt");
70    if(!sprite)
71        sprite = caca_load_sprite("examples/caca.txt");
[117]72
[183]73gdk_init (&argc, &argv);
74    //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/gally4.jpeg", NULL);
75    //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/badge1.jpeg", NULL);
76    //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/union.png", NULL);
[193]77    //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/pikachu.jpeg", NULL);
78    //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/gradient.png", NULL);
79    //pixbuf = gdk_pixbuf_new_from_file("/home/sam/artwork/aboire.png", NULL);
80    //pixbuf = gdk_pixbuf_new_from_file("/home/sam/web/sam.zoy.org/artwork/goret.png", NULL);
81    //pixbuf = gdk_pixbuf_new_from_file("/home/sam/lilkim02.jpg", NULL);
82    pixbuf = gdk_pixbuf_new_from_file("/home/sam/etw.bmp", NULL);
[183]83if(!pixbuf) return -2;
84    pixels = gdk_pixbuf_get_pixels(pixbuf);
85    bufx = gdk_pixbuf_get_width(pixbuf);
86    bufy = gdk_pixbuf_get_height(pixbuf);
87    bufpitch = gdk_pixbuf_get_rowstride(pixbuf);
88fprintf(stderr, "bits: %i\n", gdk_pixbuf_get_bits_per_sample(pixbuf));
89fprintf(stderr, "w %i, h %i, stride %i\n", bufx, bufy, bufpitch);
90
[117]91    /* Main menu */
[99]92    display_menu();
[185]93    caca_refresh();
[99]94
[85]95    /* Go ! */
96    while(!quit)
97    {
[185]98        char key = caca_get_key();
[181]99
[99]100        if(key && demo)
[85]101        {
[99]102            display_menu();
[185]103            caca_refresh();
[99]104            demo = NULL;
[85]105        }
[99]106        else if(key)
[93]107        {
[181]108        handle_key:
[99]109            switch(key)
110            {
111            case 'q':
[181]112            case 'Q':
[99]113                demo = NULL;
114                quit = 1;
115                break;
[181]116            case 'o':
117            case 'O':
118                outline = (outline + 1) % 3;
119                display_menu();
120                break;
121            case 'b':
122            case 'B':
123                bounds = (bounds + 1) % 2;
124                display_menu();
125                break;
[159]126            case 'c':
127                demo = demo_color;
128                break;
[181]129            case 'f':
130            case 'F':
[123]131                demo = demo_all;
132                break;
[99]133            case '1':
134                demo = demo_dots;
135                break;
136            case '2':
137                demo = demo_lines;
138                break;
139            case '3':
[181]140                demo = demo_boxes;
[99]141                break;
142            case '4':
[181]143                demo = demo_triangles;
[103]144                break;
145            case '5':
[151]146                demo = demo_ellipses;
[113]147                break;
[181]148            case 's':
149            case 'S':
[117]150                demo = demo_sprites;
151                break;
[183]152            case 'i':
153            case 'I':
154                demo = demo_blit;
155                break;
[99]156            }
[181]157
158            if(demo)
[185]159                caca_clear();
[181]160
[185]161            key = caca_get_key();
[181]162            if(key)
163                goto handle_key;
164
[185]165            caca_refresh();
[93]166        }
167
[99]168        if(demo)
[181]169        {
[99]170            demo();
[181]171
[193]172            caca_set_color(CACA_COLOR_WHITE);
[185]173            caca_draw_thin_box(1, 1, caca_get_width() - 2, caca_get_height() - 2);
174            caca_printf(4, 1, "[%i.%i fps]----",
175                            1000000 / caca_get_rendertime(),
176                            (10000000 / caca_get_rendertime()) % 10);
177            caca_refresh();
[181]178        }
[85]179    }
180
181    /* Clean up */
[185]182    caca_free_sprite(sprite);
183    caca_end();
[85]184
185    return 0;
186}
187
[99]188static void display_menu(void)
189{
[185]190    int xo = caca_get_width() - 2;
191    int yo = caca_get_height() - 2;
[99]192
[185]193    caca_clear();
[193]194    caca_set_color(CACA_COLOR_WHITE);
[185]195    caca_draw_thin_box(1, 1, xo, yo);
[99]196
[185]197    caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
198    caca_putstr((xo - strlen("==============")) / 2, 4, "==============");
[99]199
[185]200    caca_putstr(4, 6, "demos:");
201    caca_putstr(4, 7, "'f': full");
202    caca_putstr(4, 8, "'1': dots");
203    caca_putstr(4, 9, "'2': lines");
204    caca_putstr(4, 10, "'3': boxes");
205    caca_putstr(4, 11, "'4': triangles");
206    caca_putstr(4, 12, "'5': ellipses");
207    caca_putstr(4, 13, "'s': sprites");
208    caca_putstr(4, 14, "'c': color");
209    caca_putstr(4, 15, "'i': image blit");
[99]210
[185]211    caca_putstr(4, 17, "settings:");
212    caca_printf(4, 18, "'o': outline: %s",
[181]213              outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
[185]214    caca_printf(4, 19, "'b': drawing boundaries: %s",
[181]215              bounds == 0 ? "screen" : "infinite");
[99]216
[185]217    caca_putstr(4, yo - 2, "'q': quit");
[99]218}
219
[123]220static void demo_all(void)
221{
222    static int i = 0;
223
[159]224    int j, xo, yo, xa, ya, xb, yb, xc, yc;
[123]225
226    i++;
227
[185]228    caca_clear();
[123]229
230    /* Draw the sun */
[193]231    caca_set_color(CACA_COLOR_YELLOW);
[185]232    xo = caca_get_width() / 4;
233    yo = caca_get_height() / 4 + 5 * sin(0.03*i);
[123]234
235    for(j = 0; j < 16; j++)
236    {
[159]237        xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
238        ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
[185]239        caca_draw_thin_line(xo, yo, xa, ya);
[123]240    }
241
[151]242    j = 15 + sin(0.03*i) * 8;
[193]243    caca_set_color(CACA_COLOR_WHITE);
[185]244    caca_fill_ellipse(xo, yo, j, j / 2, '#');
[193]245    caca_set_color(CACA_COLOR_YELLOW);
[185]246    caca_draw_ellipse(xo, yo, j, j / 2, '#');
[123]247
248    /* Draw the pyramid */
[185]249    xo = caca_get_width() * 5 / 8;
[123]250    yo = 2;
251
[185]252    xa = caca_get_width() / 8 + sin(0.03*i) * 5;
253    ya = caca_get_height() / 2 + cos(0.03*i) * 5;
[123]254
[185]255    xb = caca_get_width() - 10 - cos(0.02*i) * 10;
256    yb = caca_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
[123]257
[185]258    xc = caca_get_width() / 4 - sin(0.02*i) * 5;
259    yc = caca_get_height() * 3 / 4 + cos(0.02*i) * 5;
[123]260
[193]261    caca_set_color(CACA_COLOR_GREEN);
[185]262    caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
[193]263    caca_set_color(CACA_COLOR_YELLOW);
[185]264    caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
[123]265
[193]266    caca_set_color(CACA_COLOR_RED);
[185]267    caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
[193]268    caca_set_color(CACA_COLOR_YELLOW);
[185]269    caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
[123]270
[193]271    caca_set_color(CACA_COLOR_BLUE);
[185]272    caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
[193]273    caca_set_color(CACA_COLOR_YELLOW);
[185]274    caca_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
[123]275
276    /* Draw a background triangle */
[159]277    xa = 2;
278    ya = 2;
[123]279
[185]280    xb = caca_get_width() - 3;
281    yb = caca_get_height() / 2;
[123]282
[185]283    xc = caca_get_width() / 3;
284    yc = caca_get_height() - 3;
[123]285
[193]286    caca_set_color(CACA_COLOR_CYAN);
[185]287    caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
[123]288
[185]289    xo = caca_get_width() / 2 + cos(0.027*i) * caca_get_width() / 3;
290    yo = caca_get_height() / 2 - sin(0.027*i) * caca_get_height() / 2;
[123]291
[185]292    caca_draw_thin_line(xa, ya, xo, yo);
293    caca_draw_thin_line(xb, yb, xo, yo);
294    caca_draw_thin_line(xc, yc, xo, yo);
[123]295
[151]296    /* Draw a sprite on the pyramid */
[185]297    caca_draw_sprite(xo, yo, sprite, 0);
[123]298
299    /* Draw a trail behind the foreground sprite */
300    for(j = i - 60; j < i; j++)
301    {
[185]302        int delta = caca_rand(-5, 5);
303        caca_set_color(caca_rand(0, 15));
304        caca_putchar(caca_get_width() / 2
305                    + cos(0.02*j) * (delta + caca_get_width() / 4),
306                   caca_get_height() / 2
307                    + sin(0.02*j) * (delta + caca_get_height() / 3),
[147]308                   '#');
[123]309    }
310
311    /* Draw foreground sprite */
[185]312    caca_draw_sprite(caca_get_width() / 2 + cos(0.02*i) * caca_get_width() / 4,
313                   caca_get_height() / 2 + sin(0.02*i) * caca_get_height() / 3,
[153]314                   sprite, 0);
[123]315}
316
[99]317static void demo_dots(void)
318{
[185]319    int xmax = caca_get_width() - 1;
320    int ymax = caca_get_height() - 1;
[99]321    int i;
322
[103]323    for(i = 1000; i--;)
[99]324    {
325        /* Putpixel */
[185]326        caca_set_color(caca_rand(0, 15));
327        caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax), '#');
[99]328    }
329}
330
[159]331static void demo_color(void)
332{
333    int i;
334    char buf[BUFSIZ];
335
[185]336    caca_clear();
[159]337    for(i = 0; i < 16; i++)
338    {
[185]339        sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i));
[193]340        caca_set_color(CACA_COLOR_WHITE);
[185]341        caca_putstr(4, i + 3, buf);
342        caca_set_color(i);
343        caca_putstr(40, i + 3, "XXXXXXXXXX-XX--X----------");
[159]344    }
345}
346
[99]347static void demo_lines(void)
348{
[185]349    int w = caca_get_width();
350    int h = caca_get_height();
[159]351    int xa, ya, xb, yb;
[103]352
[181]353    if(bounds)
[99]354    {
[185]355        xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
356        xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
[99]357    }
358    else
359    {
[185]360        xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
361        xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
[99]362    }
363
[185]364    caca_set_color(caca_rand(0, 15));
[181]365    if(outline > 1)
[185]366        caca_draw_thin_line(xa, ya, xb, yb);
[103]367    else
[185]368        caca_draw_line(xa, ya, xb, yb, '#');
[103]369}
370
[151]371static void demo_boxes(void)
[99]372{
[185]373    int w = caca_get_width();
374    int h = caca_get_height();
[159]375    int xa, ya, xb, yb;
[103]376
[181]377    if(bounds)
[99]378    {
[185]379        xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
380        xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
[99]381    }
382    else
383    {
[185]384        xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
385        xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
[151]386    }
[99]387
[185]388    caca_set_color(caca_rand(0, 15));
389    caca_fill_box(xa, ya, xb, yb, '#');
[99]390
[185]391    caca_set_color(caca_rand(0, 15));
[181]392    if(outline == 2)
[185]393        caca_draw_thin_box(xa, ya, xb, yb);
[181]394    else if(outline == 1)
[185]395        caca_draw_box(xa, ya, xb, yb, '#');
[99]396}
397
[133]398static void demo_ellipses(void)
399{
[185]400    int w = caca_get_width();
401    int h = caca_get_height();
[151]402    int x, y, a, b;
[133]403
[181]404    if(bounds)
[133]405    {
[185]406        x = caca_rand(- w, 2 * w); y = caca_rand(- h, 2 * h);
407        a = caca_rand(0, w); b = caca_rand(0, h);
[133]408    }
409    else
410    {
[151]411        do
[133]412        {
[185]413            x = caca_rand(0, w); y = caca_rand(0, h);
414            a = caca_rand(0, w); b = caca_rand(0, h);
[133]415
[151]416        } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
[133]417    }
418
[185]419    caca_set_color(caca_rand(0, 15));
420    caca_fill_ellipse(x, y, a, b, '#');
[133]421
[185]422    caca_set_color(caca_rand(0, 15));
[181]423    if(outline == 2)
[185]424        caca_draw_thin_ellipse(x, y, a, b);
[181]425    else if(outline == 1)
[185]426        caca_draw_ellipse(x, y, a, b, '#');
[107]427}
428
[151]429static void demo_triangles(void)
[113]430{
[185]431    int w = caca_get_width();
432    int h = caca_get_height();
[159]433    int xa, ya, xb, yb, xc, yc;
[113]434
[181]435    if(bounds)
[113]436    {
[185]437        xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
438        xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
439        xc = caca_rand(- w, 2 * w); yc = caca_rand(- h, 2 * h);
[113]440    }
441    else
442    {
443
[185]444        xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
445        xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
446        xc = caca_rand(0, w - 1); yc = caca_rand(0, h - 1);
[123]447    }
[113]448
[185]449    caca_set_color(caca_rand(0, 15));
450    caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
[113]451
[185]452    caca_set_color(caca_rand(0, 15));
[181]453    if(outline == 2)
[185]454        caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
[181]455    else if(outline == 1)
[185]456        caca_draw_triangle(xa, ya, xb, yb, xc, yc, '#');
[113]457}
458
[117]459static void demo_sprites(void)
460{
[185]461    caca_draw_sprite(caca_rand(0, caca_get_width() - 1),
462                   caca_rand(0, caca_get_height() - 1), sprite, 0);
[117]463}
464
[183]465static void demo_blit(void)
466{
[185]467    caca_blit(6, 4, caca_get_width() - 6, caca_get_height() - 4, pixels, bufx, bufy);
[183]468}
469
Note: See TracBrowser for help on using the repository browser.