Changeset 321


Ignore:
Timestamp:
Jan 7, 2004, 1:42:22 PM (18 years ago)
Author:
Sam Hocevar
Message:
  • examples/cacaball.c: + 24 metaballs instead of 5, but smaller. + Zoom the bitmap to make more use of the screen area.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/examples/cacaball.c

    r320 r321  
    3535#define YSIZ 256
    3636
    37 #define METASIZE 128
     37#define METASIZE 100
     38#define METABALLS 24
     39
     40/* Colour index where to crop balls */
     41#define CROPBALL 180
    3842
    3943static void create_ball(void);
     
    4549int main(int argc, char **argv)
    4650{
     51    int r[256], g[256], b[256], a[256];
     52    float d[METABALLS], di[METABALLS], dj[METABALLS], dk[METABALLS];
     53    unsigned int x[METABALLS], y[METABALLS];
    4754    struct caca_bitmap *caca_bitmap;
    48     float i = 0, j = 0, k = 0;
     55    float i = 10.0, j = 17.0, k = 11.0;
    4956    int p;
    50     unsigned int x[5], y[5];
    51     int r[256], g[256], b[256], a[256];
    5257
    5358    if(caca_init())
    5459        return 1;
    5560
    56     caca_set_delay(0);
     61    caca_set_delay(10000);
    5762
    5863    /* Make the palette eatable by libcaca */
    5964    for(p = 0; p < 256; p++)
    6065    {
    61         r[p] = p < 0xc0 ? 0 : (p - 0xc0) * 0x40;
    62         g[p] = p < 0x40 ? 0 : p < 0xc0 ? (p - 0x40) * 0x20 : 0xfff;
     66        r[p] = p < 0x40 ? 0 : p < 0xc0 ? (p - 0x40) * 0x20 : 0xfff;
     67        g[p] = p < 0xc0 ? 0 : (p - 0xc0) * 0x40;
    6368        b[p] = p < 0x40 ? p * 0x40 : 0xfff;
    6469        a[p] = 0x0;
     
    6671
    6772    /* Crop the palette */
    68     for(p = 0; p < 150; p++)
     73    for(p = 0; p < CROPBALL; p++)
    6974        r[p] = g[p] = b[p] = a[p] = 0x0;
    7075
    71     /* Create the bitmap */
    72     caca_bitmap = caca_create_bitmap(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
     76    /* Create a libcaca bitmap smaller than our pixel buffer, so that we
     77     * display only the interesting part of it */
     78    caca_bitmap = caca_create_bitmap(8, XSIZ - METASIZE, YSIZ - METASIZE,
     79                                     XSIZ, 0, 0, 0, 0);
    7380
    7481    /* Set the palette */
     
    7885    create_ball();
    7986
     87    for(p = 0; p < METABALLS; p++)
     88    {
     89        d[p] = caca_rand(0, 100);
     90        di[p] = (float)caca_rand(500, 4000) / 6000.0;
     91        dj[p] = (float)caca_rand(500, 4000) / 6000.0;
     92        dk[p] = (float)caca_rand(500, 4000) / 6000.0;
     93    }
     94
    8095    /* Go ! */
    8196    while(!caca_get_event(CACA_EVENT_KEY_PRESS))
    8297    {
    8398        /* Silly paths for our balls */
    84         x[0] = (1 + sin(i + k)) * (XSIZ-METASIZE) / 2;
    85         y[0] = (1 + cos(1 + j)) * (YSIZ-METASIZE) / 2;
    86         x[1] = (1 + cos(2 + j * 2 + k)) * (XSIZ-METASIZE) / 2;
    87         y[1] = (1 + cos(3 + i / 2 + j)) * (YSIZ-METASIZE) / 2;
    88         x[2] = (1 + cos(4 + k * 2)) * (XSIZ-METASIZE) / 2;
    89         y[2] = (1 + cos(i + j / 2)) * (YSIZ-METASIZE) / 2;
    90         x[3] = (1 + sin(6 + j * 2)) * (XSIZ-METASIZE) / 2;
    91         y[3] = (1 + cos(7 + i / 2)) * (YSIZ-METASIZE) / 2;
    92         x[4] = (1 + cos(i - k / 2)) * (XSIZ-METASIZE) / 2;
    93         y[4] = (1 + sin(i + k / 2)) * (YSIZ-METASIZE) / 2;
     99        for(p = 0; p < METABALLS; p++)
     100        {
     101            float u = di[p] * i + dj[p] * j + dk[p] * sin(di[p] * k);
     102            float v = d[p] + di[p] * j + dj[p] * k + dk[p] * sin(dk[p] * i);
     103            u = sin(i + u * 2.1) * (1.0 + sin(u));
     104            v = sin(j + v * 1.9) * (1.0 + sin(v));
     105            x[p] = (XSIZ - METASIZE) / 2 + u * (XSIZ - METASIZE) / 4;
     106            y[p] = (YSIZ - METASIZE) / 2 + v * (YSIZ - METASIZE) / 4;
     107        }
    94108
    95109        i += 0.011;
    96         j += 0.021;
    97         k += 0.029;
     110        j += 0.017;
     111        k += 0.019;
    98112
    99113        memset(pixels, 0, XSIZ * YSIZ);
     
    101115        /* Here is all the trick. Maybe if you're that
    102116         * clever you'll understand. */
    103         for(p = 0; p < 5; p++)
     117        for(p = 0; p < METABALLS; p++)
    104118            draw_ball(x[p], y[p]);
    105119
    106120        /* Draw our virtual buffer to screen, letting libcaca resize it */
    107         caca_draw_bitmap(-10, -10, caca_get_width() + 9, caca_get_height() + 9,
    108                          caca_bitmap, pixels);
     121        caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
     122                         caca_bitmap, pixels + (METASIZE / 2) * (1 + XSIZ));
    109123        caca_refresh();
    110124    }
Note: See TracChangeset for help on using the changeset viewer.