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

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