source: libcaca/trunk/examples/cacacirc.c @ 339

Last change on this file since 339 was 339, checked in by Sam Hocevar, 18 years ago
  • examples/cacaballs.c: + Bigger metaballs, less metaballs. + Rotating palette.
  • examples/cacaplas.c: + Slightly decreased speed.
  • examples/cacacirc.c: + New effect.
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1/*
2 *  cacacirc      circle effect for libcaca
3 *  Copyright (c) 2004 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: cacacirc.c 339 2004-01-12 14:25:46Z 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;
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(frame = 0; !caca_get_event(CACA_EVENT_KEY_PRESS); frame++)
72    {
73        memset(screen, 0, XSIZ * YSIZ);
74
75        /* Set the palette */
76        red[0] = 0.5 * (1 + sin(0.05 * frame)) * 0xfff;
77        green[0] = 0.5 * (1 + cos(0.07 * frame)) * 0xfff;
78        blue[0] = 0.5 * (1 + cos(0.06 * frame)) * 0xfff;
79
80        red[1] = 0.5 * (1 + sin(0.07 * frame + 5.0)) * 0xfff;
81        green[1] = 0.5 * (1 + cos(0.06 * frame + 5.0)) * 0xfff;
82        blue[1] = 0.5 * (1 + cos(0.05 * frame + 5.0)) * 0xfff;
83
84        caca_set_bitmap_palette(bitmap, red, green, blue, alpha);
85
86        /* Draw circles */
87        x = cos(0.07 * frame + 5.0) * 128.0 + (XSIZ / 2);
88        y = sin(0.11 * frame) * 128.0 + (YSIZ / 2);
89        put_disc(x, y);
90
91        x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2);
92        y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2);
93        put_disc(x, y);
94
95        caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
96                         bitmap, screen);
97        caca_refresh();
98    }
99
100    caca_free_bitmap(bitmap);
101    caca_end();
102
103    return 0;
104}
105
106static void put_disc(int x, int y)
107{
108    char *src = disc + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ;
109    int i, j;
110
111    for(j = 0; j < YSIZ; j++)
112        for(i = 0; i < XSIZ; i++)
113    {
114        screen[i + XSIZ * j] ^= src[i + DISCSIZ * j];
115    }
116}
117
118static void draw_disc(int r, char color)
119{
120    int t, dx, dy;
121
122    for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
123    {
124        draw_line(dx / 3,   dy / 3, color);
125        draw_line(dy / 3,   dx / 3, color);
126
127        t += t > 0 ? dx - dy-- : dx;
128    }
129}
130
131static void draw_line(int x, int y, char color)
132{
133    if(x == 0 || y == 0 || y > DISCSIZ / 2)
134        return;
135
136    if(x > DISCSIZ / 2)
137        x = DISCSIZ / 2;
138
139    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y),
140           color, 2 * x - 1);
141    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1),
142           color, 2 * x - 1);
143}
144
Note: See TracBrowser for help on using the repository browser.