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

Last change on this file since 734 was 734, checked in by Sam Hocevar, 15 years ago
  • Renamed *bitmap to *dither. Ben ouais connard, je fais ce que je veux.
  • Property svn:keywords set to Id
File size: 4.1 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 734 2006-04-10 20:02:38Z 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#if !defined(__KERNEL__)
17#   include <math.h>
18#   include <string.h>
19#endif
20
21#include "cucul.h"
22#include "caca.h"
23
24/* Virtual buffer size */
25#define XSIZ 256
26#define YSIZ 256
27
28#define DISCSIZ 512
29#define DISCTHICKNESS 96
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{
40    cucul_t *qq; caca_t *kk;
41    unsigned int red[256], green[256], blue[256], alpha[256];
42    struct cucul_dither *dither;
43    int i, x, y, frame = 0, pause = 0;
44
45    qq = cucul_create(0, 0);
46    if(!qq)
47        return 1;
48    kk = caca_attach(qq);
49    if(!kk)
50        return 1;
51
52    caca_set_delay(kk, 20000);
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
65    /* Create a libcucul dither */
66    dither = cucul_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
67
68    /* Main loop */
69    for(;;)
70    {
71        struct caca_event ev;
72        if(caca_get_event(kk, CACA_EVENT_KEY_PRESS, &ev, 0))
73        {
74            switch(ev.data.key.c)
75            {
76                case CACA_KEY_ESCAPE: goto end;
77                case ' ': pause = !pause;
78            }
79        }
80
81        if(pause)
82            goto paused;
83
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
95        cucul_set_dither_palette(dither, red, green, blue, alpha);
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
106        frame++;
107
108paused:
109        cucul_dither_bitmap(qq, 0, 0,
110                            cucul_get_width(qq) - 1, cucul_get_height(qq) - 1,
111                            dither, screen);
112        cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
113        cucul_putstr(qq, cucul_get_width(qq) - 30, cucul_get_height(qq) - 2,
114                     " -=[ Powered by libcaca ]=- ");
115        caca_display(kk);
116    }
117
118end:
119    cucul_free_dither(dither);
120    caca_detach(kk);
121    cucul_free(qq);
122
123    return 0;
124}
125
126static void put_disc(int x, int y)
127{
128    char *src = ((char*)disc) + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ;
129    int i, j;
130
131    for(j = 0; j < YSIZ; j++)
132        for(i = 0; i < XSIZ; i++)
133    {
134        screen[i + XSIZ * j] ^= src[i + DISCSIZ * j];
135    }
136}
137
138static void draw_disc(int r, char color)
139{
140    int t, dx, dy;
141
142    for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
143    {
144        draw_line(dx / 3,   dy / 3, color);
145        draw_line(dy / 3,   dx / 3, color);
146
147        t += t > 0 ? dx - dy-- : dx;
148    }
149}
150
151static void draw_line(int x, int y, char color)
152{
153    if(x == 0 || y == 0 || y > DISCSIZ / 2)
154        return;
155
156    if(x > DISCSIZ / 2)
157        x = DISCSIZ / 2;
158
159    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y),
160           color, 2 * x - 1);
161    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1),
162           color, 2 * x - 1);
163}
164
Note: See TracBrowser for help on using the repository browser.