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

Last change on this file since 522 was 522, checked in by Sam Hocevar, 17 years ago
  • Changed the licensing to WTFPL, as per all copyright holders' permission.
  • Property svn:keywords set to Id
File size: 3.6 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 522 2006-03-04 00:35:45Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14#include "config.h"
15
16#include <math.h>
17#include <string.h>
18
19#include "caca.h"
20
21/* Virtual buffer size */
22#define XSIZ 256
23#define YSIZ 256
24
25#define DISCSIZ 512
26#define DISCTHICKNESS 64
27
28static unsigned char screen[XSIZ * YSIZ];
29static unsigned char disc[DISCSIZ * DISCSIZ];
30
31static void put_disc(int, int);
32static void draw_disc(int, char);
33static void draw_line(int, int, char);
34
35int main (int argc, char **argv)
36{
37    unsigned int red[256], green[256], blue[256], alpha[256];
38    struct caca_bitmap *bitmap;
39    int i, x, y, frame = 0, pause = 0;
40
41    if(caca_init() < 0)
42        return 1;
43
44    caca_set_delay(20000);
45
46    /* Fill various tables */
47    for(i = 0 ; i < 256; i++)
48        red[i] = green[i] = blue[i] = alpha[i] = 0;
49
50    red[0] = green[0] = blue[0] = 0x777;
51    red[1] = green[1] = blue[1] = 0xfff;
52
53    /* Fill the circle */
54    for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS)
55        draw_disc(i, (i / DISCTHICKNESS) % 2);
56
57    /* Create a libcaca bitmap */
58    bitmap = caca_create_bitmap(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
59
60    /* Main loop */
61    for(;;)
62    {
63        switch(caca_get_event(CACA_EVENT_KEY_PRESS))
64        {
65            case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
66            case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
67        }
68
69        if(pause)
70            goto paused;
71
72        memset(screen, 0, XSIZ * YSIZ);
73
74        /* Set the palette */
75        red[0] = 0.5 * (1 + sin(0.05 * frame)) * 0xfff;
76        green[0] = 0.5 * (1 + cos(0.07 * frame)) * 0xfff;
77        blue[0] = 0.5 * (1 + cos(0.06 * frame)) * 0xfff;
78
79        red[1] = 0.5 * (1 + sin(0.07 * frame + 5.0)) * 0xfff;
80        green[1] = 0.5 * (1 + cos(0.06 * frame + 5.0)) * 0xfff;
81        blue[1] = 0.5 * (1 + cos(0.05 * frame + 5.0)) * 0xfff;
82
83        caca_set_bitmap_palette(bitmap, red, green, blue, alpha);
84
85        /* Draw circles */
86        x = cos(0.07 * frame + 5.0) * 128.0 + (XSIZ / 2);
87        y = sin(0.11 * frame) * 128.0 + (YSIZ / 2);
88        put_disc(x, y);
89
90        x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2);
91        y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2);
92        put_disc(x, y);
93
94        frame++;
95
96paused:
97        caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
98                         bitmap, screen);
99        caca_refresh();
100    }
101
102end:
103    caca_free_bitmap(bitmap);
104    caca_end();
105
106    return 0;
107}
108
109static void put_disc(int x, int y)
110{
111    char *src = ((char*)disc) + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ;
112    int i, j;
113
114    for(j = 0; j < YSIZ; j++)
115        for(i = 0; i < XSIZ; i++)
116    {
117        screen[i + XSIZ * j] ^= src[i + DISCSIZ * j];
118    }
119}
120
121static void draw_disc(int r, char color)
122{
123    int t, dx, dy;
124
125    for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
126    {
127        draw_line(dx / 3,   dy / 3, color);
128        draw_line(dy / 3,   dx / 3, color);
129
130        t += t > 0 ? dx - dy-- : dx;
131    }
132}
133
134static void draw_line(int x, int y, char color)
135{
136    if(x == 0 || y == 0 || y > DISCSIZ / 2)
137        return;
138
139    if(x > DISCSIZ / 2)
140        x = DISCSIZ / 2;
141
142    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y),
143           color, 2 * x - 1);
144    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1),
145           color, 2 * x - 1);
146}
147
Note: See TracBrowser for help on using the repository browser.