source: libcaca/trunk/examples/cacamoir.c @ 377

Last change on this file since 377 was 377, checked in by Sam Hocevar, 18 years ago
  • examples/: + Use Space to pause demos, Esc to quit.
  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
1/*
2 *  cacamoir      moiré circles effect for libcaca
3 *  Copyright (c) 2004 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: cacamoir.c 377 2004-11-05 14:44:41Z 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
29#include "caca.h"
30
31/* Virtual buffer size */
32#define XSIZ 256
33#define YSIZ 256
34
35#define DISCSIZ 512
36#define DISCTHICKNESS 64
37
38static unsigned char screen[XSIZ * YSIZ];
39static unsigned char disc[DISCSIZ * DISCSIZ];
40
41static void put_disc(int, int);
42static void draw_disc(int, char);
43static void draw_line(int, int, char);
44
45int main (int argc, char **argv)
46{
47    int red[256], green[256], blue[256], alpha[256];
48    struct caca_bitmap *bitmap;
49    int i, x, y, frame = 0, pause = 0;
50
51    if(caca_init() < 0)
52        return 1;
53
54    caca_set_delay(20000);
55
56    /* Fill various tables */
57    for(i = 0 ; i < 256; i++)
58        red[i] = green[i] = blue[i] = alpha[i] = 0;
59
60    red[0] = green[0] = blue[0] = 0x777;
61    red[1] = green[1] = blue[1] = 0xfff;
62
63    /* Fill the circle */
64    for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS)
65        draw_disc(i, (i / DISCTHICKNESS) % 2);
66
67    /* Create a libcaca bitmap */
68    bitmap = caca_create_bitmap(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
69
70    /* Main loop */
71    for(;;)
72    {
73        switch(caca_get_event(CACA_EVENT_KEY_PRESS))
74        {
75            case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
76            case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
77        }
78
79        if(pause)
80            goto paused;
81
82        memset(screen, 0, XSIZ * YSIZ);
83
84        /* Set the palette */
85        red[0] = 0.5 * (1 + sin(0.05 * frame)) * 0xfff;
86        green[0] = 0.5 * (1 + cos(0.07 * frame)) * 0xfff;
87        blue[0] = 0.5 * (1 + cos(0.06 * frame)) * 0xfff;
88
89        red[1] = 0.5 * (1 + sin(0.07 * frame + 5.0)) * 0xfff;
90        green[1] = 0.5 * (1 + cos(0.06 * frame + 5.0)) * 0xfff;
91        blue[1] = 0.5 * (1 + cos(0.05 * frame + 5.0)) * 0xfff;
92
93        caca_set_bitmap_palette(bitmap, red, green, blue, alpha);
94
95        /* Draw circles */
96        x = cos(0.07 * frame + 5.0) * 128.0 + (XSIZ / 2);
97        y = sin(0.11 * frame) * 128.0 + (YSIZ / 2);
98        put_disc(x, y);
99
100        x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2);
101        y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2);
102        put_disc(x, y);
103
104        frame++;
105
106paused:
107        caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
108                         bitmap, screen);
109        caca_refresh();
110    }
111
112end:
113    caca_free_bitmap(bitmap);
114    caca_end();
115
116    return 0;
117}
118
119static void put_disc(int x, int y)
120{
121    char *src = disc + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ;
122    int i, j;
123
124    for(j = 0; j < YSIZ; j++)
125        for(i = 0; i < XSIZ; i++)
126    {
127        screen[i + XSIZ * j] ^= src[i + DISCSIZ * j];
128    }
129}
130
131static void draw_disc(int r, char color)
132{
133    int t, dx, dy;
134
135    for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
136    {
137        draw_line(dx / 3,   dy / 3, color);
138        draw_line(dy / 3,   dx / 3, color);
139
140        t += t > 0 ? dx - dy-- : dx;
141    }
142}
143
144static void draw_line(int x, int y, char color)
145{
146    if(x == 0 || y == 0 || y > DISCSIZ / 2)
147        return;
148
149    if(x > DISCSIZ / 2)
150        x = DISCSIZ / 2;
151
152    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y),
153           color, 2 * x - 1);
154    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1),
155           color, 2 * x - 1);
156}
157
Note: See TracBrowser for help on using the repository browser.