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

Last change on this file since 537 was 527, checked in by Sam Hocevar, 15 years ago
  • All output plugins should now work again.
  • Renamed caca_refresh() into caca_display() which makes more sense.
  • Optimised gl_bgpal by directly storing floats instead of doing the conversion at runtime.
  • Handle resizing in cucul_set_size() and try to keep as much information as possible from the previous canvas.
  • Moved most global variables into cucul_t or caca_t contexts.
  • Moved time.c into libcaca.
  • Property svn:keywords set to Id
File size: 3.8 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 527 2006-03-06 08:32:40Z 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 "cucul.h"
20#include "caca.h"
21
22/* Virtual buffer size */
23#define XSIZ 256
24#define YSIZ 256
25
26#define DISCSIZ 512
27#define DISCTHICKNESS 64
28
29static unsigned char screen[XSIZ * YSIZ];
30static unsigned char disc[DISCSIZ * DISCSIZ];
31
32static void put_disc(int, int);
33static void draw_disc(int, char);
34static void draw_line(int, int, char);
35
36int main (int argc, char **argv)
37{
38    cucul_t *qq; caca_t *kk;
39    unsigned int red[256], green[256], blue[256], alpha[256];
40    struct cucul_bitmap *bitmap;
41    int i, x, y, frame = 0, pause = 0;
42
43    qq = cucul_init();
44    if(!qq)
45        return 1;
46    kk = caca_attach(qq);
47    if(!kk)
48        return 1;
49
50    caca_set_delay(kk, 20000);
51
52    /* Fill various tables */
53    for(i = 0 ; i < 256; i++)
54        red[i] = green[i] = blue[i] = alpha[i] = 0;
55
56    red[0] = green[0] = blue[0] = 0x777;
57    red[1] = green[1] = blue[1] = 0xfff;
58
59    /* Fill the circle */
60    for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS)
61        draw_disc(i, (i / DISCTHICKNESS) % 2);
62
63    /* Create a libcucul bitmap */
64    bitmap = cucul_create_bitmap(qq, 8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
65
66    /* Main loop */
67    for(;;)
68    {
69        switch(caca_get_event(kk, CACA_EVENT_KEY_PRESS))
70        {
71            case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
72            case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
73        }
74
75        if(pause)
76            goto paused;
77
78        memset(screen, 0, XSIZ * YSIZ);
79
80        /* Set the palette */
81        red[0] = 0.5 * (1 + sin(0.05 * frame)) * 0xfff;
82        green[0] = 0.5 * (1 + cos(0.07 * frame)) * 0xfff;
83        blue[0] = 0.5 * (1 + cos(0.06 * frame)) * 0xfff;
84
85        red[1] = 0.5 * (1 + sin(0.07 * frame + 5.0)) * 0xfff;
86        green[1] = 0.5 * (1 + cos(0.06 * frame + 5.0)) * 0xfff;
87        blue[1] = 0.5 * (1 + cos(0.05 * frame + 5.0)) * 0xfff;
88
89        cucul_set_bitmap_palette(qq, bitmap, red, green, blue, alpha);
90
91        /* Draw circles */
92        x = cos(0.07 * frame + 5.0) * 128.0 + (XSIZ / 2);
93        y = sin(0.11 * frame) * 128.0 + (YSIZ / 2);
94        put_disc(x, y);
95
96        x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2);
97        y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2);
98        put_disc(x, y);
99
100        frame++;
101
102paused:
103        cucul_draw_bitmap(qq, 0, 0,
104                          cucul_get_width(qq) - 1, cucul_get_height(qq) - 1,
105                          bitmap, screen);
106        caca_display(kk);
107    }
108
109end:
110    cucul_free_bitmap(qq, bitmap);
111    caca_detach(kk);
112    cucul_end(qq);
113
114    return 0;
115}
116
117static void put_disc(int x, int y)
118{
119    char *src = ((char*)disc) + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ;
120    int i, j;
121
122    for(j = 0; j < YSIZ; j++)
123        for(i = 0; i < XSIZ; i++)
124    {
125        screen[i + XSIZ * j] ^= src[i + DISCSIZ * j];
126    }
127}
128
129static void draw_disc(int r, char color)
130{
131    int t, dx, dy;
132
133    for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
134    {
135        draw_line(dx / 3,   dy / 3, color);
136        draw_line(dy / 3,   dx / 3, color);
137
138        t += t > 0 ? dx - dy-- : dx;
139    }
140}
141
142static void draw_line(int x, int y, char color)
143{
144    if(x == 0 || y == 0 || y > DISCSIZ / 2)
145        return;
146
147    if(x > DISCSIZ / 2)
148        x = DISCSIZ / 2;
149
150    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y),
151           color, 2 * x - 1);
152    memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1),
153           color, 2 * x - 1);
154}
155
Note: See TracBrowser for help on using the repository browser.