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

Last change on this file since 193 was 193, checked in by Sam Hocevar, 18 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
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 193 2003-11-16 15:20:17Z 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;
50struct caca_sprite *sprite = NULL;
51
52GdkPixbuf *pixbuf;
53char *pixels;
54int bufx, bufy, bufpitch;
55
56int main(int argc, char **argv)
57{
58    void (*demo)(void) = NULL;
59    int quit = 0;
60
61    if(caca_init())
62    {
63        return 1;
64    }
65
66    caca_set_delay(40000);
67
68    /* Initialize data */
69    sprite = caca_load_sprite("caca.txt");
70    if(!sprite)
71        sprite = caca_load_sprite("examples/caca.txt");
72
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);
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);
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
91    /* Main menu */
92    display_menu();
93    caca_refresh();
94
95    /* Go ! */
96    while(!quit)
97    {
98        char key = caca_get_key();
99
100        if(key && demo)
101        {
102            display_menu();
103            caca_refresh();
104            demo = NULL;
105        }
106        else if(key)
107        {
108        handle_key:
109            switch(key)
110            {
111            case 'q':
112            case 'Q':
113                demo = NULL;
114                quit = 1;
115                break;
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;
126            case 'c':
127                demo = demo_color;
128                break;
129            case 'f':
130            case 'F':
131                demo = demo_all;
132                break;
133            case '1':
134                demo = demo_dots;
135                break;
136            case '2':
137                demo = demo_lines;
138                break;
139            case '3':
140                demo = demo_boxes;
141                break;
142            case '4':
143                demo = demo_triangles;
144                break;
145            case '5':
146                demo = demo_ellipses;
147                break;
148            case 's':
149            case 'S':
150                demo = demo_sprites;
151                break;
152            case 'i':
153            case 'I':
154                demo = demo_blit;
155                break;
156            }
157
158            if(demo)
159                caca_clear();
160
161            key = caca_get_key();
162            if(key)
163                goto handle_key;
164
165            caca_refresh();
166        }
167
168        if(demo)
169        {
170            demo();
171
172            caca_set_color(CACA_COLOR_WHITE);
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();
178        }
179    }
180
181    /* Clean up */
182    caca_free_sprite(sprite);
183    caca_end();
184
185    return 0;
186}
187
188static void display_menu(void)
189{
190    int xo = caca_get_width() - 2;
191    int yo = caca_get_height() - 2;
192
193    caca_clear();
194    caca_set_color(CACA_COLOR_WHITE);
195    caca_draw_thin_box(1, 1, xo, yo);
196
197    caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
198    caca_putstr((xo - strlen("==============")) / 2, 4, "==============");
199
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");
210
211    caca_putstr(4, 17, "settings:");
212    caca_printf(4, 18, "'o': outline: %s",
213              outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
214    caca_printf(4, 19, "'b': drawing boundaries: %s",
215              bounds == 0 ? "screen" : "infinite");
216
217    caca_putstr(4, yo - 2, "'q': quit");
218}
219
220static void demo_all(void)
221{
222    static int i = 0;
223
224    int j, xo, yo, xa, ya, xb, yb, xc, yc;
225
226    i++;
227
228    caca_clear();
229
230    /* Draw the sun */
231    caca_set_color(CACA_COLOR_YELLOW);
232    xo = caca_get_width() / 4;
233    yo = caca_get_height() / 4 + 5 * sin(0.03*i);
234
235    for(j = 0; j < 16; j++)
236    {
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);
239        caca_draw_thin_line(xo, yo, xa, ya);
240    }
241
242    j = 15 + sin(0.03*i) * 8;
243    caca_set_color(CACA_COLOR_WHITE);
244    caca_fill_ellipse(xo, yo, j, j / 2, '#');
245    caca_set_color(CACA_COLOR_YELLOW);
246    caca_draw_ellipse(xo, yo, j, j / 2, '#');
247
248    /* Draw the pyramid */
249    xo = caca_get_width() * 5 / 8;
250    yo = 2;
251
252    xa = caca_get_width() / 8 + sin(0.03*i) * 5;
253    ya = caca_get_height() / 2 + cos(0.03*i) * 5;
254
255    xb = caca_get_width() - 10 - cos(0.02*i) * 10;
256    yb = caca_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
257
258    xc = caca_get_width() / 4 - sin(0.02*i) * 5;
259    yc = caca_get_height() * 3 / 4 + cos(0.02*i) * 5;
260
261    caca_set_color(CACA_COLOR_GREEN);
262    caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
263    caca_set_color(CACA_COLOR_YELLOW);
264    caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
265
266    caca_set_color(CACA_COLOR_RED);
267    caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
268    caca_set_color(CACA_COLOR_YELLOW);
269    caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
270
271    caca_set_color(CACA_COLOR_BLUE);
272    caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
273    caca_set_color(CACA_COLOR_YELLOW);
274    caca_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
275
276    /* Draw a background triangle */
277    xa = 2;
278    ya = 2;
279
280    xb = caca_get_width() - 3;
281    yb = caca_get_height() / 2;
282
283    xc = caca_get_width() / 3;
284    yc = caca_get_height() - 3;
285
286    caca_set_color(CACA_COLOR_CYAN);
287    caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
288
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;
291
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);
295
296    /* Draw a sprite on the pyramid */
297    caca_draw_sprite(xo, yo, sprite, 0);
298
299    /* Draw a trail behind the foreground sprite */
300    for(j = i - 60; j < i; j++)
301    {
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),
308                   '#');
309    }
310
311    /* Draw foreground sprite */
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,
314                   sprite, 0);
315}
316
317static void demo_dots(void)
318{
319    int xmax = caca_get_width() - 1;
320    int ymax = caca_get_height() - 1;
321    int i;
322
323    for(i = 1000; i--;)
324    {
325        /* Putpixel */
326        caca_set_color(caca_rand(0, 15));
327        caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax), '#');
328    }
329}
330
331static void demo_color(void)
332{
333    int i;
334    char buf[BUFSIZ];
335
336    caca_clear();
337    for(i = 0; i < 16; i++)
338    {
339        sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i));
340        caca_set_color(CACA_COLOR_WHITE);
341        caca_putstr(4, i + 3, buf);
342        caca_set_color(i);
343        caca_putstr(40, i + 3, "XXXXXXXXXX-XX--X----------");
344    }
345}
346
347static void demo_lines(void)
348{
349    int w = caca_get_width();
350    int h = caca_get_height();
351    int xa, ya, xb, yb;
352
353    if(bounds)
354    {
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);
357    }
358    else
359    {
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);
362    }
363
364    caca_set_color(caca_rand(0, 15));
365    if(outline > 1)
366        caca_draw_thin_line(xa, ya, xb, yb);
367    else
368        caca_draw_line(xa, ya, xb, yb, '#');
369}
370
371static void demo_boxes(void)
372{
373    int w = caca_get_width();
374    int h = caca_get_height();
375    int xa, ya, xb, yb;
376
377    if(bounds)
378    {
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);
381    }
382    else
383    {
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);
386    }
387
388    caca_set_color(caca_rand(0, 15));
389    caca_fill_box(xa, ya, xb, yb, '#');
390
391    caca_set_color(caca_rand(0, 15));
392    if(outline == 2)
393        caca_draw_thin_box(xa, ya, xb, yb);
394    else if(outline == 1)
395        caca_draw_box(xa, ya, xb, yb, '#');
396}
397
398static void demo_ellipses(void)
399{
400    int w = caca_get_width();
401    int h = caca_get_height();
402    int x, y, a, b;
403
404    if(bounds)
405    {
406        x = caca_rand(- w, 2 * w); y = caca_rand(- h, 2 * h);
407        a = caca_rand(0, w); b = caca_rand(0, h);
408    }
409    else
410    {
411        do
412        {
413            x = caca_rand(0, w); y = caca_rand(0, h);
414            a = caca_rand(0, w); b = caca_rand(0, h);
415
416        } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
417    }
418
419    caca_set_color(caca_rand(0, 15));
420    caca_fill_ellipse(x, y, a, b, '#');
421
422    caca_set_color(caca_rand(0, 15));
423    if(outline == 2)
424        caca_draw_thin_ellipse(x, y, a, b);
425    else if(outline == 1)
426        caca_draw_ellipse(x, y, a, b, '#');
427}
428
429static void demo_triangles(void)
430{
431    int w = caca_get_width();
432    int h = caca_get_height();
433    int xa, ya, xb, yb, xc, yc;
434
435    if(bounds)
436    {
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);
440    }
441    else
442    {
443
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);
447    }
448
449    caca_set_color(caca_rand(0, 15));
450    caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
451
452    caca_set_color(caca_rand(0, 15));
453    if(outline == 2)
454        caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
455    else if(outline == 1)
456        caca_draw_triangle(xa, ya, xb, yb, xc, yc, '#');
457}
458
459static void demo_sprites(void)
460{
461    caca_draw_sprite(caca_rand(0, caca_get_width() - 1),
462                   caca_rand(0, caca_get_height() - 1), sprite, 0);
463}
464
465static void demo_blit(void)
466{
467    caca_blit(6, 4, caca_get_width() - 6, caca_get_height() - 4, pixels, bufx, bufy);
468}
469
Note: See TracBrowser for help on using the repository browser.