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

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