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

Last change on this file since 818 was 816, checked in by Sam Hocevar, 14 years ago
  • Changed the cucul_dither_bitmap behaviour so that the 4 coordinates are now x/y/width/height instead of x1/y1/x2/y2. Avoids unnecessary +1/-1 computations.
  • Property svn:keywords set to Id
File size: 4.2 KB
RevLine 
[339]1/*
[340]2 *  cacamoir      moiré circles effect for libcaca
[339]3 *  Copyright (c) 2004 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: cacamoir.c 816 2006-04-18 16:50:56Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
[522]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.
[339]12 */
13
14#include "config.h"
15
[581]16#if !defined(__KERNEL__)
17#   include <math.h>
18#   include <string.h>
19#endif
[339]20
[524]21#include "cucul.h"
[339]22#include "caca.h"
23
24/* Virtual buffer size */
25#define XSIZ 256
26#define YSIZ 256
27
28#define DISCSIZ 512
[614]29#define DISCTHICKNESS 96
[339]30
31static unsigned char screen[XSIZ * YSIZ];
32static unsigned char disc[DISCSIZ * DISCSIZ];
33
34static void put_disc(int, int);
35static void draw_disc(int, char);
36static void draw_line(int, int, char);
37
38int main (int argc, char **argv)
39{
[811]40    cucul_canvas_t *cv; caca_display_t *dp;
[514]41    unsigned int red[256], green[256], blue[256], alpha[256];
[777]42    cucul_dither_t *dither;
[377]43    int i, x, y, frame = 0, pause = 0;
[339]44
[813]45    cv = cucul_create_canvas(0, 0);
[811]46    if(!cv)
[339]47        return 1;
[811]48    dp = caca_attach(cv);
49    if(!dp)
[524]50        return 1;
[339]51
[811]52    caca_set_delay(dp, 20000);
[339]53
54    /* Fill various tables */
55    for(i = 0 ; i < 256; i++)
56        red[i] = green[i] = blue[i] = alpha[i] = 0;
57
58    red[0] = green[0] = blue[0] = 0x777;
59    red[1] = green[1] = blue[1] = 0xfff;
60
61    /* Fill the circle */
62    for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS)
63        draw_disc(i, (i / DISCTHICKNESS) % 2);
64
[734]65    /* Create a libcucul dither */
66    dither = cucul_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
[339]67
68    /* Main loop */
[377]69    for(;;)
[339]70    {
[777]71        caca_event_t ev;
[811]72        if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0))
[377]73        {
[810]74            switch(ev.data.key.ch)
[681]75            {
76                case CACA_KEY_ESCAPE: goto end;
77                case ' ': pause = !pause;
78            }
[377]79        }
80
81        if(pause)
82            goto paused;
83
[339]84        memset(screen, 0, XSIZ * YSIZ);
85
86        /* Set the palette */
87        red[0] = 0.5 * (1 + sin(0.05 * frame)) * 0xfff;
88        green[0] = 0.5 * (1 + cos(0.07 * frame)) * 0xfff;
89        blue[0] = 0.5 * (1 + cos(0.06 * frame)) * 0xfff;
90
91        red[1] = 0.5 * (1 + sin(0.07 * frame + 5.0)) * 0xfff;
92        green[1] = 0.5 * (1 + cos(0.06 * frame + 5.0)) * 0xfff;
93        blue[1] = 0.5 * (1 + cos(0.05 * frame + 5.0)) * 0xfff;
94
[734]95        cucul_set_dither_palette(dither, red, green, blue, alpha);
[339]96
97        /* Draw circles */
98        x = cos(0.07 * frame + 5.0) * 128.0 + (XSIZ / 2);
99        y = sin(0.11 * frame) * 128.0 + (YSIZ / 2);
100        put_disc(x, y);
101
102        x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2);
103        y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2);
104        put_disc(x, y);
105
[377]106        frame++;
107
108paused:
[811]109        cucul_dither_bitmap(cv, 0, 0,
[816]110                            cucul_get_canvas_width(cv),
111                            cucul_get_canvas_height(cv),
[734]112                            dither, screen);
[811]113        cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
[813]114        cucul_putstr(cv, cucul_get_canvas_width(cv) - 30,
115                         cucul_get_canvas_height(cv) - 2,
116                         " -=[ Powered by libcaca ]=- ");
[811]117        caca_display(dp);
[339]118    }
119
[377]120end:
[734]121    cucul_free_dither(dither);
[811]122    caca_detach(dp);
[813]123    cucul_free_canvas(cv);
[339]124
125    return 0;
126}
127
128static void put_disc(int x, int y)
129{
[514]130    char *src = ((char*)disc) + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ;
[339]131    int i, j;
132
133    for(j = 0; j < YSIZ; j++)
134        for(i = 0; i < XSIZ; i++)
135    {
136        screen[i + XSIZ * j] ^= src[i + DISCSIZ * j];
137    }
138}
139
140static void draw_disc(int r, char color)
141{
142    int t, dx, dy;
143
144    for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
145    {
146        draw_line(dx / 3,   dy / 3, color);
147        draw_line(dy / 3,   dx / 3, color);
148
149        t += t > 0 ? dx - dy-- : dx;
150    }
151}
152
153static void draw_line(int x, int y, char color)
154{
155    if(x == 0 || y == 0 || y > DISCSIZ / 2)
156        return;
157
158    if(x > DISCSIZ / 2)
159        x = DISCSIZ / 2;
160
161    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y),
162           color, 2 * x - 1);
163    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1),
164           color, 2 * x - 1);
165}
166
Note: See TracBrowser for help on using the repository browser.