source: libcaca/branches/0.5/examples/demo.c @ 1203

Last change on this file since 1203 was 268, checked in by Sam Hocevar, 19 years ago
  • src/io.c: + Added caca_wait_event(), a blocking caca_get_event().
  • src/ examples/: + More documentation.
  • doc/doxygen.cfg.in: + doc/doxygen.cfg is now generated by configure, so that we can use

@top_srcdir@ and we no longer need to hardcode PROJECT_NUMBER.

+ Create manpages.

  • Property svn:keywords set to Id
File size: 15.2 KB
Line 
1/*
2 *  demo          demo for libcaca
3 *  Copyright (c) 2003 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: demo.c 268 2003-12-23 13:27:40Z 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 "caca.h"
31
32static void display_menu(void);
33
34static void demo_all(void);
35
36static void demo_color(void);
37static void demo_dots(void);
38static void demo_lines(void);
39static void demo_boxes(void);
40static void demo_ellipses(void);
41static void demo_triangles(void);
42static void demo_sprites(void);
43static void demo_render(void);
44
45int bounds = 0;
46int outline = 0;
47int dithering = 0;
48struct caca_sprite *sprite = NULL;
49
50int main(int argc, char **argv)
51{
52    void (*demo)(void) = NULL;
53    int quit = 0;
54
55    if(caca_init())
56        return 1;
57
58    caca_set_delay(40000);
59
60    /* Initialize data */
61    sprite = caca_load_sprite(DATADIR "/caca.txt");
62    if(!sprite)
63        sprite = caca_load_sprite("caca.txt");
64    if(!sprite)
65        sprite = caca_load_sprite("examples/caca.txt");
66
67    /* Main menu */
68    display_menu();
69    caca_refresh();
70
71    /* Go ! */
72    while(!quit)
73    {
74        int event = caca_get_event();
75
76        if(event && demo)
77        {
78            display_menu();
79            caca_refresh();
80            demo = NULL;
81        }
82        else if(event & CACA_EVENT_KEY_PRESS)
83        {
84        handle_key:
85            switch(event & 0xffff)
86            {
87            case 'q':
88            case 'Q':
89                demo = NULL;
90                quit = 1;
91                break;
92            case 'o':
93            case 'O':
94                outline = (outline + 1) % 3;
95                display_menu();
96                break;
97            case 'b':
98            case 'B':
99                bounds = (bounds + 1) % 2;
100                display_menu();
101                break;
102            case 'd':
103            case 'D':
104                dithering = (dithering + 1) % 5;
105                caca_set_dithering(dithering);
106                display_menu();
107                break;
108            case 'c':
109                demo = demo_color;
110                break;
111            case 'f':
112            case 'F':
113                demo = demo_all;
114                break;
115            case '1':
116                demo = demo_dots;
117                break;
118            case '2':
119                demo = demo_lines;
120                break;
121            case '3':
122                demo = demo_boxes;
123                break;
124            case '4':
125                demo = demo_triangles;
126                break;
127            case '5':
128                demo = demo_ellipses;
129                break;
130            case 's':
131            case 'S':
132                if(sprite)
133                    demo = demo_sprites;
134                break;
135            case 'r':
136            case 'R':
137                demo = demo_render;
138                break;
139            }
140
141        handle_event:
142            event = caca_get_event();
143            if(event & CACA_EVENT_KEY_PRESS)
144                goto handle_key;
145            else if(event)
146                goto handle_event;
147
148            caca_refresh();
149
150            if(demo)
151                caca_clear();
152        }
153        else if(event & CACA_EVENT_MOUSE_CLICK)
154        {
155            display_menu();
156            caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
157            caca_putstr((event & 0xff00) >> 8, event & 0xff, "|\\");
158            caca_refresh();
159        }
160
161        if(demo)
162        {
163            demo();
164
165            caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
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();
171        }
172    }
173
174    /* Clean up */
175    caca_free_sprite(sprite);
176    caca_end();
177
178    return 0;
179}
180
181static void display_menu(void)
182{
183    int xo = caca_get_width() - 2;
184    int yo = caca_get_height() - 2;
185
186    caca_clear();
187    caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
188    caca_draw_thin_box(1, 1, xo, yo);
189
190    caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
191    caca_putstr((xo - strlen("==============")) / 2, 4, "==============");
192
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, "'c': colour");
201    caca_putstr(4, 14, "'r': render");
202    if(sprite)
203        caca_putstr(4, 15, "'s': sprites");
204
205    caca_putstr(4, 16, "settings:");
206    caca_printf(4, 17, "'o': outline: %s",
207              outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
208    caca_printf(4, 18, "'b': drawing boundaries: %s",
209              bounds == 0 ? "screen" : "infinite");
210    caca_printf(4, 19, "'d': dithering (%s)",
211                caca_get_dithering_name(dithering));
212
213    caca_putstr(4, yo - 2, "'q': quit");
214}
215
216static void demo_all(void)
217{
218    static int i = 0;
219
220    int j, xo, yo, xa, ya, xb, yb, xc, yc;
221
222    i++;
223
224    caca_clear();
225
226    /* Draw the sun */
227    caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
228    xo = caca_get_width() / 4;
229    yo = caca_get_height() / 4 + 5 * sin(0.03*i);
230
231    for(j = 0; j < 16; j++)
232    {
233        xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
234        ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
235        caca_draw_thin_line(xo, yo, xa, ya);
236    }
237
238    j = 15 + sin(0.03*i) * 8;
239    caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK);
240    caca_fill_ellipse(xo, yo, j, j / 2, '#');
241    caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
242    caca_draw_ellipse(xo, yo, j, j / 2, '#');
243
244    /* Draw the pyramid */
245    xo = caca_get_width() * 5 / 8;
246    yo = 2;
247
248    xa = caca_get_width() / 8 + sin(0.03*i) * 5;
249    ya = caca_get_height() / 2 + cos(0.03*i) * 5;
250
251    xb = caca_get_width() - 10 - cos(0.02*i) * 10;
252    yb = caca_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
253
254    xc = caca_get_width() / 4 - sin(0.02*i) * 5;
255    yc = caca_get_height() * 3 / 4 + cos(0.02*i) * 5;
256
257    caca_set_color(CACA_COLOR_GREEN, CACA_COLOR_BLACK);
258    caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
259    caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
260    caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
261
262    caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
263    caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
264    caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
265    caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
266
267    caca_set_color(CACA_COLOR_BLUE, CACA_COLOR_BLACK);
268    caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
269    caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
270    caca_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
271
272    /* Draw a background triangle */
273    xa = 2;
274    ya = 2;
275
276    xb = caca_get_width() - 3;
277    yb = caca_get_height() / 2;
278
279    xc = caca_get_width() / 3;
280    yc = caca_get_height() - 3;
281
282    caca_set_color(CACA_COLOR_CYAN, CACA_COLOR_BLACK);
283    caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
284
285    xo = caca_get_width() / 2 + cos(0.027*i) * caca_get_width() / 3;
286    yo = caca_get_height() / 2 - sin(0.027*i) * caca_get_height() / 2;
287
288    caca_draw_thin_line(xa, ya, xo, yo);
289    caca_draw_thin_line(xb, yb, xo, yo);
290    caca_draw_thin_line(xc, yc, xo, yo);
291
292    /* Draw a sprite on the pyramid */
293    caca_draw_sprite(xo, yo, sprite, 0);
294
295    /* Draw a trail behind the foreground sprite */
296    for(j = i - 60; j < i; j++)
297    {
298        int delta = caca_rand(-5, 5);
299        caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
300        caca_putchar(caca_get_width() / 2
301                    + cos(0.02*j) * (delta + caca_get_width() / 4),
302                   caca_get_height() / 2
303                    + sin(0.02*j) * (delta + caca_get_height() / 3),
304                   '#');
305    }
306
307    /* Draw foreground sprite */
308    caca_draw_sprite(caca_get_width() / 2 + cos(0.02*i) * caca_get_width() / 4,
309                   caca_get_height() / 2 + sin(0.02*i) * caca_get_height() / 3,
310                   sprite, 0);
311}
312
313static void demo_dots(void)
314{
315    int xmax = caca_get_width() - 1;
316    int ymax = caca_get_height() - 1;
317    int i;
318    static char chars[10] =
319    {
320        '+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W'
321    };
322
323    for(i = 1000; i--;)
324    {
325        /* Putpixel */
326        caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
327        caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax),
328                     chars[caca_rand(0, 9)]);
329    }
330}
331
332static void demo_color(void)
333{
334    int i, j;
335    char buf[BUFSIZ];
336
337    caca_clear();
338    for(i = 0; i < 16; i++)
339    {
340        sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i));
341        caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
342        caca_putstr(4, i + (i >= 8 ? 4 : 3), buf);
343        for(j = 0; j < 16; j++)
344        {
345            caca_set_color(i, j);
346            caca_putstr((j >= 8 ? 41 : 40) + j * 2, i + (i >= 8 ? 4 : 3), "# ");
347        }
348    }
349}
350
351static void demo_lines(void)
352{
353    int w = caca_get_width();
354    int h = caca_get_height();
355    int xa, ya, xb, yb;
356
357    if(bounds)
358    {
359        xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
360        xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
361    }
362    else
363    {
364        xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
365        xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
366    }
367
368    caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
369    if(outline > 1)
370        caca_draw_thin_line(xa, ya, xb, yb);
371    else
372        caca_draw_line(xa, ya, xb, yb, '#');
373}
374
375static void demo_boxes(void)
376{
377    int w = caca_get_width();
378    int h = caca_get_height();
379    int xa, ya, xb, yb;
380
381    if(bounds)
382    {
383        xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
384        xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
385    }
386    else
387    {
388        xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
389        xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
390    }
391
392    caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
393    caca_fill_box(xa, ya, xb, yb, '#');
394
395    caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
396    if(outline == 2)
397        caca_draw_thin_box(xa, ya, xb, yb);
398    else if(outline == 1)
399        caca_draw_box(xa, ya, xb, yb, '#');
400}
401
402static void demo_ellipses(void)
403{
404    int w = caca_get_width();
405    int h = caca_get_height();
406    int x, y, a, b;
407
408    if(bounds)
409    {
410        x = caca_rand(- w, 2 * w); y = caca_rand(- h, 2 * h);
411        a = caca_rand(0, w); b = caca_rand(0, h);
412    }
413    else
414    {
415        do
416        {
417            x = caca_rand(0, w); y = caca_rand(0, h);
418            a = caca_rand(0, w); b = caca_rand(0, h);
419
420        } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
421    }
422
423    caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
424    caca_fill_ellipse(x, y, a, b, '#');
425
426    caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
427    if(outline == 2)
428        caca_draw_thin_ellipse(x, y, a, b);
429    else if(outline == 1)
430        caca_draw_ellipse(x, y, a, b, '#');
431}
432
433static void demo_triangles(void)
434{
435    int w = caca_get_width();
436    int h = caca_get_height();
437    int xa, ya, xb, yb, xc, yc;
438
439    if(bounds)
440    {
441        xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
442        xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
443        xc = caca_rand(- w, 2 * w); yc = caca_rand(- h, 2 * h);
444    }
445    else
446    {
447
448        xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
449        xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
450        xc = caca_rand(0, w - 1); yc = caca_rand(0, h - 1);
451    }
452
453    caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
454    caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
455
456    caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
457    if(outline == 2)
458        caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
459    else if(outline == 1)
460        caca_draw_triangle(xa, ya, xb, yb, xc, yc, '#');
461}
462
463static void demo_sprites(void)
464{
465    caca_draw_sprite(caca_rand(0, caca_get_width() - 1),
466                   caca_rand(0, caca_get_height() - 1), sprite, 0);
467}
468
469#if 0
470static void demo_render(void)
471{
472    struct caca_bitmap *bitmap;
473    //short buffer[256*256];
474    //short *dest = buffer;
475    int buffer[256*256];
476    int *dest = buffer;
477    int x, y, z;
478    static int i = 0;
479
480    i = (i + 1) % 512;
481    z = i < 256 ? i : 511 - i;
482
483    for(x = 0; x < 256; x++)
484        for(y = 0; y < 256; y++)
485    {
486        //*dest++ = ((x >> 3) << 11) | ((y >> 2) << 5) | ((z >> 3));
487        *dest++ = (x << 16) | (y << 8) | (z);
488    }
489
490    //bitmap = caca_create_bitmap(16, 256, 256, 2 * 256, 0xf800, 0x07e0, 0x001f, 0x0000);
491    bitmap = caca_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
492    caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
493                     bitmap, buffer);
494    caca_free_bitmap(bitmap);
495}
496#endif
497
498static void draw_circle(int *buffer, int xo, int yo, int r, int mask, int val);
499
500static void demo_render(void)
501{
502    struct caca_bitmap *bitmap;
503    int buffer[256*256];
504    int *dest;
505    int x, y, z, xo, yo;
506    static int i = 0;
507
508    i++;
509
510    dest = buffer;
511    for(x = 0; x < 256; x++)
512        for(y = 0; y < 256; y++)
513    {
514        *dest++ = 0xff000000;
515    }
516
517    /* red */
518    xo = 128 + 48 * sin(0.02 * i);
519    yo = 128 + 48 * cos(0.03 * i);
520    for(z = 0; z < 240; z++)
521        draw_circle(buffer, xo, yo, z, 0x00ff0000, 200 << 16);
522
523    /* green */
524    xo = 128 + 48 * sin(2 + 0.06 * i);
525    yo = 128 + 48 * cos(2 + 0.05 * i);
526    for(z = 0; z < 240; z++)
527        draw_circle(buffer, xo, yo, z, 0x0000ff00, 200 << 8);
528
529    /* blue */
530    xo = 128 + 48 * sin(1 + 0.04 * i);
531    yo = 128 + 48 * cos(1 + 0.03 * i);
532    for(z = 0; z < 240; z++)
533        draw_circle(buffer, xo, yo, z, 0x000000ff, 200);
534
535    bitmap = caca_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
536    caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
537                     bitmap, (char *)buffer);
538    caca_free_bitmap(bitmap);
539}
540
541static void draw_circle(int *buffer, int x, int y, int r, int mask, int val)
542{
543    int t, dx, dy;
544
545#define POINT(X,Y) \
546    buffer[(X) + 256 * (Y)] = 0xff000000 | (buffer[(X) + 256 * (Y)] & ~mask) | val;
547
548    for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
549    {
550        POINT(x - dx / 3, y - dy / 3);
551        POINT(x + dx / 3, y - dy / 3);
552        POINT(x - dx / 3, y + dy / 3);
553        POINT(x + dx / 3, y + dy / 3);
554
555        POINT(x - dy / 3, y - dx / 3);
556        POINT(x + dy / 3, y - dx / 3);
557        POINT(x - dy / 3, y + dx / 3);
558        POINT(x + dy / 3, y + dx / 3);
559
560        t += t > 0 ? dx - dy-- : dx;
561    }
562}
563
Note: See TracBrowser for help on using the repository browser.