1 /* MetaBalls, Copyright (c) 2002-2003 W.P. van Paassen <peter@paassen.tmfweb.nl>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
11 * Module - "metaballs.c"
13 * [01/24/03] - W.P. van Paassen: Additional features
14 * [12/29/02] - W.P. van Paassen: Port to X for use with XScreenSaver, the shadebob hack by Shane Smit was used as a template
15 * [12/26/02] - W.P. van Paassen: Creation for the Demo Effects Collection (http://demo-effects.sourceforge.net)
19 #include "screenhack.h"
33 unsigned short iWinWidth, iWinHeight;
36 unsigned int nBlobCount;
39 unsigned char dradius;
40 unsigned short sradius;
46 signed short iColorCount;
47 unsigned long *aiColorVals;
55 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
57 static void init_blob(struct state *st, BLOB *blob)
59 blob->xpos = st->iWinWidth/4 + BELLRAND(st->iWinWidth/2) - st->radius;
60 blob->ypos = st->iWinHeight/4 + BELLRAND(st->iWinHeight/2) - st->radius;
63 static void Execute( struct state *st )
67 /* clear st->blub array */
68 for (i = 0; i < st->iWinHeight; ++i)
69 memset(st->blub[i], 0, st->iWinWidth * sizeof(unsigned char));
72 for (i = 0; i < st->nBlobCount; i++)
74 st->blobs[i].xpos += -st->delta + (int)((st->delta + .5f) * frand(2.0));
75 st->blobs[i].ypos += -st->delta + (int)((st->delta + .5f) * frand(2.0));
78 /* draw st->blobs to st->blub array */
79 for (k = 0; k < st->nBlobCount; ++k)
81 if (st->blobs[k].ypos > -st->dradius && st->blobs[k].xpos > -st->dradius && st->blobs[k].ypos < st->iWinHeight && st->blobs[k].xpos < st->iWinWidth)
83 for (i = 0; i < st->dradius; ++i)
85 if (st->blobs[k].ypos + i >= 0 && st->blobs[k].ypos + i < st->iWinHeight)
87 for (j = 0; j < st->dradius; ++j)
89 if (st->blobs[k].xpos + j >= 0 && st->blobs[k].xpos + j < st->iWinWidth)
91 if (st->blub[st->blobs[k].ypos + i][st->blobs[k].xpos + j] < st->iColorCount-1)
93 if (st->blub[st->blobs[k].ypos + i][st->blobs[k].xpos + j] + st->blob[i][j] > st->iColorCount-1)
94 st->blub[st->blobs[k].ypos + i][st->blobs[k].xpos + j] = st->iColorCount-1;
96 st->blub[st->blobs[k].ypos + i][st->blobs[k].xpos + j] += st->blob[i][j];
104 init_blob(st, st->blobs + k);
107 memset( st->pImage->data, 0, st->pImage->bytes_per_line * st->pImage->height);
109 /* draw st->blub array to screen */
110 for (i = 0; i < st->iWinHeight; ++i)
112 for (j = 0; j < st->iWinWidth; ++j)
114 if (st->aiColorVals[st->blub[i][j]] > 0)
115 XPutPixel( st->pImage, j, i, st->aiColorVals[st->blub[i][j]] );
119 XPutImage( st->dpy, st->window, st->gc, st->pImage,
120 0, 0, 0, 0, st->iWinWidth, st->iWinHeight );
123 static unsigned long * SetPalette(struct state *st )
125 XWindowAttributes XWinAttribs;
126 XColor Color, *aColors;
130 XGetWindowAttributes( st->dpy, st->window, &XWinAttribs );
132 Color.red = random() % 0xFFFF;
133 Color.green = random() % 0xFFFF;
134 Color.blue = random() % 0xFFFF;
136 if( strcasecmp( st->sColor, "random" ) && !XParseColor( st->dpy, XWinAttribs.colormap, st->sColor, &Color ) )
137 fprintf( stderr, "%s: color %s not found in database. Choosing to random...\n", progname, st->sColor );
140 printf( "%s: Base color (RGB): <%d, %d, %d>\n", progname, Color.red, Color.green, Color.blue );
143 st->iColorCount = get_integer_resource(st->dpy, "ncolors", "Integer" );
144 if( st->iColorCount < 2 ) st->iColorCount = 2;
145 if( st->iColorCount > 255 ) st->iColorCount = 255;
147 aColors = calloc( st->iColorCount, sizeof(XColor) );
148 st->aiColorVals = calloc( st->iColorCount, sizeof(unsigned long) );
150 for( iColor=0; iColor < st->iColorCount; iColor++ )
152 nHalfColors = st->iColorCount / 2.0F;
153 /* Black -> Base Color */
154 if( iColor < (st->iColorCount/2) )
156 aColors[ iColor ].red = ( Color.red / nHalfColors ) * iColor;
157 aColors[ iColor ].green = ( Color.green / nHalfColors ) * iColor;
158 aColors[ iColor ].blue = ( Color.blue / nHalfColors ) * iColor;
160 /* Base Color -> White */
163 aColors[ iColor ].red = ( ( ( 0xFFFF - Color.red ) / nHalfColors ) * ( iColor - nHalfColors ) ) + Color.red;
164 aColors[ iColor ].green = ( ( ( 0xFFFF - Color.green ) / nHalfColors ) * ( iColor - nHalfColors ) ) + Color.green;
165 aColors[ iColor ].blue = ( ( ( 0xFFFF - Color.blue ) / nHalfColors ) * ( iColor - nHalfColors ) ) + Color.blue;
168 if( !XAllocColor( st->dpy, XWinAttribs.colormap, &aColors[ iColor ] ) )
170 /* start all over with less colors */
171 XFreeColors( st->dpy, XWinAttribs.colormap, st->aiColorVals, iColor, 0 );
173 free( st->aiColorVals );
176 if (st->iColorCount < 6)
178 fprintf (stderr, "%s: insufficient colors!\n",
183 aColors = calloc( st->iColorCount, sizeof(XColor) );
184 st->aiColorVals = calloc( st->iColorCount, sizeof(unsigned long) );
188 st->aiColorVals[ iColor ] = aColors[ iColor ].pixel;
193 XSetWindowBackground( st->dpy, st->window, st->aiColorVals[ 0 ] );
195 return st->aiColorVals;
199 static void Initialize( struct state *st )
202 XWindowAttributes XWinAttribs;
203 int /*iBitsPerPixel,*/ i, j;
204 unsigned int distance_squared;
207 /* Create the Image for drawing */
208 XGetWindowAttributes( st->dpy, st->window, &XWinAttribs );
210 /* Find the preferred bits-per-pixel. (jwz) */
214 XPixmapFormatValues *pfv = XListPixmapFormats( st->dpy, &pfvc );
215 for( i=0; i<pfvc; i++ )
216 if( pfv[ i ].depth == XWinAttribs.depth )
218 iBitsPerPixel = pfv[ i ].bits_per_pixel;
227 st->gc = XCreateGC( st->dpy, st->window, 0, &gcValues );
229 st->pImage = XCreateImage( st->dpy, XWinAttribs.visual, XWinAttribs.depth, ZPixmap, 0, NULL,
230 XWinAttribs.width, XWinAttribs.height, BitmapPad( st->dpy ), 0 );
231 (st->pImage)->data = calloc((st->pImage)->bytes_per_line, (st->pImage)->height);
233 st->iWinWidth = XWinAttribs.width;
234 st->iWinHeight = XWinAttribs.height;
236 /* Get the base color. */
237 st->sColor = get_string_resource(st->dpy, "color", "Color" );
239 /* Get the st->delta. */
240 st->delta = get_integer_resource(st->dpy, "delta", "Integer" );
243 else if (st->delta > 20)
246 /* Get the st->radius. */
247 st->radius = get_integer_resource(st->dpy, "radius", "Integer" );
250 if (st->radius > 100)
253 st->radius = (st->radius / 100.0) * (st->iWinHeight >> 3);
254 if (st->radius >= 128) /* should use UCHAR_MAX? */
255 st->radius = 127; /* st->dradius should fit in u_char */
257 if (st->iWinWidth < 100 || st->iWinHeight < 100) /* tiny window */
261 st->dradius = st->radius * 2;
262 st->sradius = st->radius * st->radius;
264 /* create st->blob */
265 st->blob = malloc ( st->dradius * sizeof(unsigned char*));
266 for (i = 0; i < st->dradius; ++i)
267 st->blob[i] = malloc( st->dradius * sizeof(unsigned char));
269 /* create st->blub array */
270 st->blub = malloc( st->iWinHeight * sizeof(unsigned char*));
271 for (i = 0; i < st->iWinHeight; ++i)
272 st->blub[i] = malloc( st->iWinWidth * sizeof(unsigned char));
274 /* create st->blob */
275 for (i = -st->radius; i < st->radius; ++i)
277 for (j = -st->radius; j < st->radius; ++j)
279 distance_squared = i * i + j * j;
280 if (distance_squared <= st->sradius)
282 /* compute density */
283 fraction = (float)distance_squared / (float)st->sradius;
284 st->blob[i + st->radius][j + st->radius] = pow((1.0 - (fraction * fraction)),4.0) * 255.0;
288 st->blob[i + st->radius][j + st->radius] = 0;
293 for (i = 0; i < st->nBlobCount; i++)
295 init_blob(st, st->blobs + i);
300 metaballs_init (Display *dpy, Window window)
302 struct state *st = (struct state *) calloc (1, sizeof(*st));
304 time_t nTime = time( NULL );
305 unsigned short iFrame = 0;
311 st->nBlobCount = get_integer_resource(st->dpy, "count", "Integer" );
312 if( st->nBlobCount > 255 ) st->nBlobCount = 255;
313 if( st->nBlobCount < 2 ) st->nBlobCount = 2;
315 if( ( st->blobs = calloc( st->nBlobCount, sizeof(BLOB) ) ) == NULL )
317 fprintf( stderr, "%s: Could not allocate %d Blobs\n", progname, st->nBlobCount );
321 printf( "%s: Allocated %d Blobs\n", progname, st->nBlobCount );
326 st->delay = get_integer_resource(st->dpy, "delay", "Integer" );
327 st->cycles = get_integer_resource(st->dpy, "cycles", "Integer" );
336 metaballs_draw (Display *dpy, Window window, void *closure)
338 struct state *st = (struct state *) closure;
340 if( st->draw_i < 0 || st->draw_i++ >= st->cycles )
343 XWindowAttributes XWinAttribs;
344 XGetWindowAttributes( st->dpy, st->window, &XWinAttribs );
346 memset( st->pImage->data, 0, st->pImage->bytes_per_line * st->pImage->height );
347 XFreeColors( st->dpy, XWinAttribs.colormap, st->aiColorVals, st->iColorCount, 0 );
348 free( st->aiColorVals );
349 st->aiColorVals = SetPalette( st );
350 XClearWindow( st->dpy, st->window );
351 for (i = 0; i < st->nBlobCount; i++)
353 init_blob(st, st->blobs + i);
362 if( nTime - time( NULL ) )
364 printf( "%s: %d FPS\n", progname, iFrame );
365 nTime = time( NULL );
375 metaballs_reshape (Display *dpy, Window window, void *closure,
376 unsigned int w, unsigned int h)
381 metaballs_event (Display *dpy, Window window, void *closure, XEvent *event)
387 metaballs_free (Display *dpy, Window window, void *closure)
390 struct state *st = (struct state *) closure;
391 free( st->pImage->data );
392 XDestroyImage( st->pImage );
393 free( st->aiColorVals );
395 for (i = 0; i < st->iWinHeight; ++i)
398 for (i = 0; i < st->dradius; ++i)
405 static const char *metaballs_defaults [] = {
406 ".background: black",
407 ".foreground: white",
416 "*ignoreRotation: True",
421 static XrmOptionDescRec metaballs_options [] = {
422 { "-color", ".color", XrmoptionSepArg, 0 },
423 { "-ncolors", ".ncolors", XrmoptionSepArg, 0 },
424 { "-count", ".count", XrmoptionSepArg, 0 },
425 { "-delay", ".delay", XrmoptionSepArg, 0 },
426 { "-cycles", ".cycles", XrmoptionSepArg, 0 },
427 { "-radius", ".radius", XrmoptionSepArg, 0 },
428 { "-delta", ".delta", XrmoptionSepArg, 0 },
433 XSCREENSAVER_MODULE ("MetaBalls", metaballs)
435 /* End of Module - "metaballs.c" */