source: libcaca/trunk/examples/demo0.c @ 3404

Last change on this file since 3404 was 3404, checked in by Sam Hocevar, 14 years ago

Remove occurrences of DATADIR, it conflicts with MinGW's <objidl.h>.
Fixes ticket #51.

  • Property svn:keywords set to Id
File size: 15.7 KB
Line 
1/*
2 *  demo          demo for libcaca version 0.9
3 *  Copyright (c) 2003 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: demo0.c 3404 2009-02-26 10:41:32Z 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#if !defined(__KERNEL__)
27#   include <math.h>
28#   include <string.h>
29#   include <stdio.h>
30#endif
31
32#include "caca0.h"
33
34static void display_menu(void);
35
36static void demo_all(void);
37
38static void demo_color(void);
39static void demo_dots(void);
40static void demo_lines(void);
41static void demo_boxes(void);
42static void demo_ellipses(void);
43static void demo_triangles(void);
44#if 0
45static void demo_sprites(void);
46#endif
47static void demo_render(void);
48
49int bounds = 0;
50int outline = 0;
51int dithering = 0;
52struct caca_sprite *sprite = NULL;
53
54int main(int argc, char **argv)
55{
56    void (*demo)(void) = NULL;
57    int quit = 0;
58
59    if(caca_init())
60        return 1;
61
62    caca_set_delay(40000);
63
64    /* Initialize data */
65#if 0
66    sprite = caca_load_sprite("caca.txt");
67    if(!sprite)
68        sprite = caca_load_sprite("examples/caca.txt");
69#endif
70
71    /* Main menu */
72    display_menu();
73
74    /* Go ! */
75    while(!quit)
76    {
77        int menu = 0, mouse = 0, xmouse = 0, ymouse = 0;
78        int event;
79
80        while((event = caca_get_event(CACA_EVENT_ANY)))
81        {
82            if(demo && (event & CACA_EVENT_KEY_PRESS))
83            {
84                menu = 1;
85                demo = NULL;
86            }
87            else if(event & CACA_EVENT_KEY_PRESS)
88            {
89                switch(event & 0xffff)
90                {
91                case 'q':
92                case 'Q':
93                case CACA_KEY_ESCAPE:
94                    demo = NULL;
95                    quit = 1;
96                    break;
97                case 'o':
98                case 'O':
99                    outline = (outline + 1) % 3;
100                    display_menu();
101                    break;
102                case 'b':
103                case 'B':
104                    bounds = (bounds + 1) % 2;
105                    display_menu();
106                    break;
107                case 'd':
108                case 'D':
109                    dithering = (dithering + 1) % 5;
110                    caca_set_dithering(CACA_DITHERING_NONE + dithering);
111                    display_menu();
112                    break;
113                case 'c':
114                    demo = demo_color;
115                    break;
116                case 'f':
117                case 'F':
118                    demo = demo_all;
119                    break;
120                case '1':
121                    demo = demo_dots;
122                    break;
123                case '2':
124                    demo = demo_lines;
125                    break;
126                case '3':
127                    demo = demo_boxes;
128                    break;
129                case '4':
130                    demo = demo_triangles;
131                    break;
132                case '5':
133                    demo = demo_ellipses;
134                    break;
135#if 0
136                case 's':
137                case 'S':
138                    if(sprite)
139                        demo = demo_sprites;
140                    break;
141#endif
142                case 'r':
143                case 'R':
144                    demo = demo_render;
145                    break;
146                }
147
148                if(demo)
149                    caca_clear();
150            }
151            else if(event & CACA_EVENT_MOUSE_MOTION)
152            {
153                mouse = 1;
154                xmouse = (event & 0xfff000) >> 12;
155                ymouse = event & 0xfff;
156            }
157        }
158
159        if(menu || (mouse && !demo))
160        {
161            display_menu();
162            if(mouse && !demo)
163            {
164                caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
165                caca_putstr(xmouse, ymouse, "|\\");
166            }
167            caca_refresh();
168            mouse = menu = 0;
169        }
170
171        if(demo)
172        {
173            demo();
174
175            caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
176            caca_draw_thin_box(1, 1, caca_get_width() - 2, caca_get_height() - 2);
177            caca_printf(4, 1, "[%i.%i fps]----",
178                            1000000 / caca_get_rendertime(),
179                            (10000000 / caca_get_rendertime()) % 10);
180            caca_refresh();
181        }
182    }
183
184    /* Clean up */
185#if 0
186    caca_free_sprite(sprite);
187#endif
188    caca_end();
189
190    return 0;
191}
192
193static void display_menu(void)
194{
195    int xo = caca_get_width() - 2;
196    int yo = caca_get_height() - 2;
197
198    caca_clear();
199    caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
200    caca_draw_thin_box(1, 1, xo, yo);
201
202    caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
203    caca_putstr((xo - strlen("==============")) / 2, 4, "==============");
204
205    caca_putstr(4, 6, "demos:");
206    caca_putstr(4, 7, "'f': full");
207    caca_putstr(4, 8, "'1': dots");
208    caca_putstr(4, 9, "'2': lines");
209    caca_putstr(4, 10, "'3': boxes");
210    caca_putstr(4, 11, "'4': triangles");
211    caca_putstr(4, 12, "'5': ellipses");
212    caca_putstr(4, 13, "'c': colour");
213    caca_putstr(4, 14, "'r': render");
214    if(sprite)
215        caca_putstr(4, 15, "'s': sprites");
216
217    caca_putstr(4, 16, "settings:");
218    caca_printf(4, 17, "'o': outline: %s",
219              outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
220    caca_printf(4, 18, "'b': drawing boundaries: %s",
221              bounds == 0 ? "screen" : "infinite");
222    caca_printf(4, 19, "'d': dithering (%s)",
223                caca_get_dithering_name(CACA_DITHERING_NONE + dithering));
224
225    caca_putstr(4, yo - 2, "'q': quit");
226    caca_refresh();
227}
228
229static void demo_all(void)
230{
231    static int i = 0;
232
233    int j, xo, yo, xa, ya, xb, yb, xc, yc;
234
235    i++;
236
237    caca_clear();
238
239    /* Draw the sun */
240    caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
241    xo = caca_get_width() / 4;
242    yo = caca_get_height() / 4 + 5 * sin(0.03*i);
243
244    for(j = 0; j < 16; j++)
245    {
246        xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
247        ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
248        caca_draw_thin_line(xo, yo, xa, ya);
249    }
250
251    j = 15 + sin(0.03*i) * 8;
252    caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK);
253    caca_fill_ellipse(xo, yo, j, j / 2, '#');
254    caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
255    caca_draw_ellipse(xo, yo, j, j / 2, '#');
256
257    /* Draw the pyramid */
258    xo = caca_get_width() * 5 / 8;
259    yo = 2;
260
261    xa = caca_get_width() / 8 + sin(0.03*i) * 5;
262    ya = caca_get_height() / 2 + cos(0.03*i) * 5;
263
264    xb = caca_get_width() - 10 - cos(0.02*i) * 10;
265    yb = caca_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
266
267    xc = caca_get_width() / 4 - sin(0.02*i) * 5;
268    yc = caca_get_height() * 3 / 4 + cos(0.02*i) * 5;
269
270    caca_set_color(CACA_COLOR_GREEN, CACA_COLOR_BLACK);
271    caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
272    caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
273    caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
274
275    caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
276    caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
277    caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
278    caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
279
280    caca_set_color(CACA_COLOR_BLUE, CACA_COLOR_BLACK);
281    caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
282    caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
283    caca_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
284
285    /* Draw a background triangle */
286    xa = 2;
287    ya = 2;
288
289    xb = caca_get_width() - 3;
290    yb = caca_get_height() / 2;
291
292    xc = caca_get_width() / 3;
293    yc = caca_get_height() - 3;
294
295    caca_set_color(CACA_COLOR_CYAN, CACA_COLOR_BLACK);
296    caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
297
298    xo = caca_get_width() / 2 + cos(0.027*i) * caca_get_width() / 3;
299    yo = caca_get_height() / 2 - sin(0.027*i) * caca_get_height() / 2;
300
301    caca_draw_thin_line(xa, ya, xo, yo);
302    caca_draw_thin_line(xb, yb, xo, yo);
303    caca_draw_thin_line(xc, yc, xo, yo);
304
305    /* Draw a sprite on the pyramid */
306#if 0
307    caca_draw_sprite(xo, yo, sprite, 0);
308#endif
309
310    /* Draw a trail behind the foreground sprite */
311    for(j = i - 60; j < i; j++)
312    {
313        int delta = caca_rand(-5, 5);
314        caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
315        caca_putchar(caca_get_width() / 2
316                    + cos(0.02*j) * (delta + caca_get_width() / 4),
317                   caca_get_height() / 2
318                    + sin(0.02*j) * (delta + caca_get_height() / 3),
319                   '#');
320    }
321
322    /* Draw foreground sprite */
323#if 0
324    caca_draw_sprite(caca_get_width() / 2 + cos(0.02*i) * caca_get_width() / 4,
325                   caca_get_height() / 2 + sin(0.02*i) * caca_get_height() / 3,
326                   sprite, 0);
327#endif
328}
329
330static void demo_dots(void)
331{
332    int xmax = caca_get_width() - 1;
333    int ymax = caca_get_height() - 1;
334    int i;
335    static char chars[10] =
336    {
337        '+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W'
338    };
339
340    for(i = 1000; i--;)
341    {
342        /* Putpixel */
343        caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
344        caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax),
345                     chars[caca_rand(0, 9)]);
346    }
347}
348
349static void demo_color(void)
350{
351    int i, j;
352    char buf[BUFSIZ];
353
354    caca_clear();
355    for(i = 0; i < 16; i++)
356    {
357        sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i));
358        caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
359        caca_putstr(4, i + (i >= 8 ? 4 : 3), buf);
360        for(j = 0; j < 16; j++)
361        {
362            caca_set_color(i, j);
363            caca_putstr((j >= 8 ? 41 : 40) + j * 2, i + (i >= 8 ? 4 : 3), "# ");
364        }
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), CACA_COLOR_BLACK);
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), caca_rand(0, 15));
410    caca_fill_box(xa, ya, xb, yb, '#');
411
412    caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
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), caca_rand(0, 15));
441    caca_fill_ellipse(x, y, a, b, '#');
442
443    caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
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), caca_rand(0, 15));
471    caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
472
473    caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
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
480#if 0
481static void demo_sprites(void)
482{
483    caca_draw_sprite(caca_rand(0, caca_get_width() - 1),
484                   caca_rand(0, caca_get_height() - 1), sprite, 0);
485}
486#endif
487
488#if 0
489static void demo_render(void)
490{
491    struct caca_bitmap *bitmap;
492    //short buffer[256*256];
493    //short *dest = buffer;
494    int buffer[256*256];
495    int *dest = buffer;
496    int x, y, z;
497    static int i = 0;
498
499    i = (i + 1) % 512;
500    z = i < 256 ? i : 511 - i;
501
502    for(x = 0; x < 256; x++)
503        for(y = 0; y < 256; y++)
504    {
505        //*dest++ = ((x >> 3) << 11) | ((y >> 2) << 5) | ((z >> 3));
506        *dest++ = (x << 16) | (y << 8) | (z);
507    }
508
509    //bitmap = caca_create_bitmap(16, 256, 256, 2 * 256, 0xf800, 0x07e0, 0x001f, 0x0000);
510    bitmap = caca_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
511    caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
512                     bitmap, buffer);
513    caca_free_bitmap(bitmap);
514}
515#endif
516
517static void draw_circle(int *buffer, int xo, int yo, int r, int mask, int val);
518
519static void demo_render(void)
520{
521    struct caca_bitmap *bitmap;
522    int buffer[256*256];
523    int *dest;
524    int x, y, z, xo, yo;
525    static int i = 0;
526
527    i++;
528
529    dest = buffer;
530    for(x = 0; x < 256; x++)
531        for(y = 0; y < 256; y++)
532    {
533        *dest++ = 0xff000000;
534    }
535
536    /* red */
537    xo = 128 + 48 * sin(0.02 * i);
538    yo = 128 + 48 * cos(0.03 * i);
539    for(z = 0; z < 240; z++)
540        draw_circle(buffer, xo, yo, z, 0x00ff0000, 200 << 16);
541
542    /* green */
543    xo = 128 + 48 * sin(2 + 0.06 * i);
544    yo = 128 + 48 * cos(2 + 0.05 * i);
545    for(z = 0; z < 240; z++)
546        draw_circle(buffer, xo, yo, z, 0x0000ff00, 200 << 8);
547
548    /* blue */
549    xo = 128 + 48 * sin(1 + 0.04 * i);
550    yo = 128 + 48 * cos(1 + 0.03 * i);
551    for(z = 0; z < 240; z++)
552        draw_circle(buffer, xo, yo, z, 0x000000ff, 200);
553
554    bitmap = caca_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
555    caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
556                     bitmap, (char *)buffer);
557    caca_free_bitmap(bitmap);
558}
559
560static void draw_circle(int *buffer, int x, int y, int r, int mask, int val)
561{
562    int t, dx, dy;
563
564#define POINT(X,Y) \
565    buffer[(X) + 256 * (Y)] = 0xff000000 | (buffer[(X) + 256 * (Y)] & ~mask) | val;
566
567    for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
568    {
569        POINT(x - dx / 3, y - dy / 3);
570        POINT(x + dx / 3, y - dy / 3);
571        POINT(x - dx / 3, y + dy / 3);
572        POINT(x + dx / 3, y + dy / 3);
573
574        POINT(x - dy / 3, y - dx / 3);
575        POINT(x + dy / 3, y - dx / 3);
576        POINT(x - dy / 3, y + dx / 3);
577        POINT(x + dy / 3, y + dx / 3);
578
579        t += t > 0 ? dx - dy-- : dx;
580    }
581}
582
Note: See TracBrowser for help on using the repository browser.