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

Last change on this file since 537 was 527, checked in by Sam Hocevar, 14 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: 4.2 KB
Line 
1/*
2 *  cacaplas      plasma effect for libcaca
3 *  Copyright (c) 2004 Sam Hocevar <sam@zoy.org>
4 *                1998 Michele Bini <mibin@tin.it>
5 *                All Rights Reserved
6 *
7 *  $Id: cacaplas.c 527 2006-03-06 08:32:40Z sam $
8 *
9 *  This program is free software; you can redistribute it and/or
10 *  modify it under the terms of the Do What The Fuck You Want To
11 *  Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15#include "config.h"
16
17#include <math.h>
18
19#ifndef M_PI
20#   define M_PI 3.14159265358979323846
21#endif
22
23#include "cucul.h"
24#include "caca.h"
25
26/* Virtual buffer size */
27#define XSIZ 256
28#define YSIZ 256
29
30#define TABLEX (XSIZ * 2)
31#define TABLEY (YSIZ * 2)
32
33static unsigned char screen[XSIZ * YSIZ];
34static unsigned char table[TABLEX * TABLEY];
35
36static void do_plasma(unsigned char *,
37                      double, double, double, double, double, double);
38
39int main (int argc, char **argv)
40{
41    cucul_t *qq; caca_t *kk;
42    unsigned int red[256], green[256], blue[256], alpha[256];
43    double r[3], R[6];
44    struct cucul_bitmap *bitmap;
45    int i, x, y, frame = 0, pause = 0;
46
47    qq = cucul_init();
48    if(!qq)
49        return 1;
50    kk = caca_attach(qq);
51    if(!kk)
52        return 1;
53
54    caca_set_delay(kk, 20000);
55
56    /* Fill various tables */
57    for(i = 0 ; i < 256; i++)
58        red[i] = green[i] = blue[i] = alpha[i] = 0;
59
60    for(i = 0; i < 3; i++)
61        r[i] = (double)(cucul_rand(1, 1000)) / 60000 * M_PI;
62
63    for(i = 0; i < 6; i++)
64        R[i] = (double)(cucul_rand(1, 1000)) / 10000;
65
66    for(y = 0 ; y < TABLEY ; y++)
67        for(x = 0 ; x < TABLEX ; x++)
68    {
69        double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2))
70                              + (y - (TABLEX / 2)) * (y - (TABLEX / 2))))
71                      * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY)));
72
73        table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6;
74    }
75
76    /* Create a libcucul bitmap */
77    bitmap = cucul_create_bitmap(qq, 8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
78
79    /* Main loop */
80    for(;;)
81    {
82        switch(caca_get_event(kk, CACA_EVENT_KEY_PRESS))
83        {
84            case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
85            case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
86        }
87
88        if(pause)
89            goto paused;
90
91        for(i = 0 ; i < 256; i++)
92        {
93            double z = ((double)i) / 256 * 6 * M_PI;
94
95            red[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff;
96            green[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
97            blue[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff;
98        }
99
100        /* Set the palette */
101        cucul_set_bitmap_palette(qq, bitmap, red, green, blue, alpha);
102
103        do_plasma(screen,
104                  (1.0 + sin(((double)frame) * R[0])) / 2,
105                  (1.0 + sin(((double)frame) * R[1])) / 2,
106                  (1.0 + sin(((double)frame) * R[2])) / 2,
107                  (1.0 + sin(((double)frame) * R[3])) / 2,
108                  (1.0 + sin(((double)frame) * R[4])) / 2,
109                  (1.0 + sin(((double)frame) * R[5])) / 2);
110        frame++;
111
112paused:
113        cucul_draw_bitmap(qq, 0, 0,
114                          cucul_get_width(qq) - 1, cucul_get_height(qq) - 1,
115                          bitmap, screen);
116        caca_display(kk);
117    }
118
119end:
120    cucul_free_bitmap(qq, bitmap);
121    caca_detach(kk);
122    cucul_end(qq);
123
124    return 0;
125}
126
127static void do_plasma(unsigned char *pixels, double x_1, double y_1,
128                      double x_2, double y_2, double x_3, double y_3)
129{
130    unsigned int X1 = x_1 * (TABLEX / 2),
131                 Y1 = y_1 * (TABLEY / 2),
132                 X2 = x_2 * (TABLEX / 2),
133                 Y2 = y_2 * (TABLEY / 2),
134                 X3 = x_3 * (TABLEX / 2),
135                 Y3 = y_3 * (TABLEY / 2);
136    unsigned int y;
137    unsigned char * t1 = table + X1 + Y1 * TABLEX,
138                  * t2 = table + X2 + Y2 * TABLEX,
139                  * t3 = table + X3 + Y3 * TABLEX;
140
141    for(y = 0; y < YSIZ; y++)
142    {
143        unsigned int x;
144        unsigned char * tmp = pixels + y * YSIZ;
145        unsigned int ty = y * TABLEX, tmax = ty + XSIZ;
146        for(x = 0; ty < tmax; ty++, tmp++)
147            tmp[0] = t1[ty] + t2[ty] + t3[ty];
148    }
149}
150
Note: See TracBrowser for help on using the repository browser.