source: libcaca/trunk/src/cacamoir.c @ 896

Last change on this file since 896 was 859, checked in by Sam Hocevar, 15 years ago
  • Removed duplicate uint*_t defines from *_internal.h and included common.h in all .c files that needed it.
  • Property svn:keywords set to Id
File size: 4.2 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 859 2006-04-24 20:35:59Z 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#include "common.h"
16
17#if !defined(__KERNEL__)
18#   include <math.h>
19#   include <string.h>
20#endif
21
22#include "cucul.h"
23#include "caca.h"
24
25/* Virtual buffer size */
26#define XSIZ 256
27#define YSIZ 256
28
29#define DISCSIZ 512
30#define DISCTHICKNESS 96
31
32static unsigned char screen[XSIZ * YSIZ];
33static unsigned char disc[DISCSIZ * DISCSIZ];
34
35static void put_disc(int, int);
36static void draw_disc(int, char);
37static void draw_line(int, int, char);
38
39int main (int argc, char **argv)
40{
41    cucul_canvas_t *cv; caca_display_t *dp;
42    unsigned int red[256], green[256], blue[256], alpha[256];
43    cucul_dither_t *dither;
44    int i, x, y, frame = 0, pause = 0;
45
46    cv = cucul_create_canvas(0, 0);
47    if(!cv)
48        return 1;
49    dp = caca_create_display(cv);
50    if(!dp)
51        return 1;
52
53    caca_set_delay(dp, 20000);
54
55    /* Fill various tables */
56    for(i = 0 ; i < 256; i++)
57        red[i] = green[i] = blue[i] = alpha[i] = 0;
58
59    red[0] = green[0] = blue[0] = 0x777;
60    red[1] = green[1] = blue[1] = 0xfff;
61
62    /* Fill the circle */
63    for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS)
64        draw_disc(i, (i / DISCTHICKNESS) % 2);
65
66    /* Create a libcucul dither */
67    dither = cucul_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
68
69    /* Main loop */
70    for(;;)
71    {
72        caca_event_t ev;
73        if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0))
74        {
75            switch(ev.data.key.ch)
76            {
77                case CACA_KEY_ESCAPE: goto end;
78                case ' ': pause = !pause;
79            }
80        }
81
82        if(pause)
83            goto paused;
84
85        memset(screen, 0, XSIZ * YSIZ);
86
87        /* Set the palette */
88        red[0] = 0.5 * (1 + sin(0.05 * frame)) * 0xfff;
89        green[0] = 0.5 * (1 + cos(0.07 * frame)) * 0xfff;
90        blue[0] = 0.5 * (1 + cos(0.06 * frame)) * 0xfff;
91
92        red[1] = 0.5 * (1 + sin(0.07 * frame + 5.0)) * 0xfff;
93        green[1] = 0.5 * (1 + cos(0.06 * frame + 5.0)) * 0xfff;
94        blue[1] = 0.5 * (1 + cos(0.05 * frame + 5.0)) * 0xfff;
95
96        cucul_set_dither_palette(dither, red, green, blue, alpha);
97
98        /* Draw circles */
99        x = cos(0.07 * frame + 5.0) * 128.0 + (XSIZ / 2);
100        y = sin(0.11 * frame) * 128.0 + (YSIZ / 2);
101        put_disc(x, y);
102
103        x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2);
104        y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2);
105        put_disc(x, y);
106
107        frame++;
108
109paused:
110        cucul_dither_bitmap(cv, 0, 0,
111                            cucul_get_canvas_width(cv),
112                            cucul_get_canvas_height(cv),
113                            dither, screen);
114        cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
115        cucul_putstr(cv, cucul_get_canvas_width(cv) - 30,
116                         cucul_get_canvas_height(cv) - 2,
117                         " -=[ Powered by libcaca ]=- ");
118        caca_refresh_display(dp);
119    }
120
121end:
122    cucul_free_dither(dither);
123    caca_free_display(dp);
124    cucul_free_canvas(cv);
125
126    return 0;
127}
128
129static void put_disc(int x, int y)
130{
131    char *src = ((char*)disc) + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ;
132    int i, j;
133
134    for(j = 0; j < YSIZ; j++)
135        for(i = 0; i < XSIZ; i++)
136    {
137        screen[i + XSIZ * j] ^= src[i + DISCSIZ * j];
138    }
139}
140
141static void draw_disc(int r, char color)
142{
143    int t, dx, dy;
144
145    for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
146    {
147        draw_line(dx / 3,   dy / 3, color);
148        draw_line(dy / 3,   dx / 3, color);
149
150        t += t > 0 ? dx - dy-- : dx;
151    }
152}
153
154static void draw_line(int x, int y, char color)
155{
156    if(x == 0 || y == 0 || y > DISCSIZ / 2)
157        return;
158
159    if(x > DISCSIZ / 2)
160        x = DISCSIZ / 2;
161
162    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y),
163           color, 2 * x - 1);
164    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1),
165           color, 2 * x - 1);
166}
167
Note: See TracBrowser for help on using the repository browser.