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) */
213 XPixmapFormatValues *pfv = XListPixmapFormats( st->dpy, &pfvc );
214 for( i=0; i<pfvc; i++ )
215 if( pfv[ i ].depth == XWinAttribs.depth )
217 iBitsPerPixel = pfv[ i ].bits_per_pixel;
225 st->gc = XCreateGC( st->dpy, st->window, 0, &gcValues );
227 st->pImage = XCreateImage( st->dpy, XWinAttribs.visual, XWinAttribs.depth, ZPixmap, 0, NULL,
228 XWinAttribs.width, XWinAttribs.height, BitmapPad( st->dpy ), 0 );
229 (st->pImage)->data = calloc((st->pImage)->bytes_per_line, (st->pImage)->height);
231 st->iWinWidth = XWinAttribs.width;
232 st->iWinHeight = XWinAttribs.height;
234 /* Get the base color. */
235 st->sColor = get_string_resource(st->dpy, "color", "Color" );
237 /* Get the st->delta. */
238 st->delta = get_integer_resource(st->dpy, "delta", "Integer" );
241 else if (st->delta > 20)
244 /* Get the st->radius. */
245 st->radius = get_integer_resource(st->dpy, "radius", "Integer" );
248 if (st->radius > 100)
251 st->radius = (st->radius / 100.0) * (st->iWinHeight >> 3);
252 if (st->radius >= 128) /* should use UCHAR_MAX? */
253 st->radius = 127; /* st->dradius should fit in u_char */
255 st->dradius = st->radius * 2;
256 st->sradius = st->radius * st->radius;
258 /* create st->blob */
259 st->blob = malloc ( st->dradius * sizeof(unsigned char*));
260 for (i = 0; i < st->dradius; ++i)
261 st->blob[i] = malloc( st->dradius * sizeof(unsigned char));
263 /* create st->blub array */
264 st->blub = malloc( st->iWinHeight * sizeof(unsigned char*));
265 for (i = 0; i < st->iWinHeight; ++i)
266 st->blub[i] = malloc( st->iWinWidth * sizeof(unsigned char));
268 /* create st->blob */
269 for (i = -st->radius; i < st->radius; ++i)
271 for (j = -st->radius; j < st->radius; ++j)
273 distance_squared = i * i + j * j;
274 if (distance_squared <= st->sradius)
276 /* compute density */
277 fraction = (float)distance_squared / (float)st->sradius;
278 st->blob[i + st->radius][j + st->radius] = pow((1.0 - (fraction * fraction)),4.0) * 255.0;
282 st->blob[i + st->radius][j + st->radius] = 0;
287 for (i = 0; i < st->nBlobCount; i++)
289 init_blob(st, st->blobs + i);
294 metaballs_init (Display *dpy, Window window)
296 struct state *st = (struct state *) calloc (1, sizeof(*st));
298 time_t nTime = time( NULL );
299 unsigned short iFrame = 0;
305 st->nBlobCount = get_integer_resource(st->dpy, "count", "Integer" );
306 if( st->nBlobCount > 255 ) st->nBlobCount = 255;
307 if( st->nBlobCount < 2 ) st->nBlobCount = 2;
309 if( ( st->blobs = calloc( st->nBlobCount, sizeof(BLOB) ) ) == NULL )
311 fprintf( stderr, "%s: Could not allocate %d Blobs\n", progname, st->nBlobCount );
315 printf( "%s: Allocated %d Blobs\n", progname, st->nBlobCount );
320 st->delay = get_integer_resource(st->dpy, "delay", "Integer" );
321 st->cycles = get_integer_resource(st->dpy, "cycles", "Integer" );
330 metaballs_draw (Display *dpy, Window window, void *closure)
332 struct state *st = (struct state *) closure;
334 if( st->draw_i < 0 || st->draw_i++ >= st->cycles )
337 XWindowAttributes XWinAttribs;
338 XGetWindowAttributes( st->dpy, st->window, &XWinAttribs );
340 memset( st->pImage->data, 0, st->pImage->bytes_per_line * st->pImage->height );
341 XFreeColors( st->dpy, XWinAttribs.colormap, st->aiColorVals, st->iColorCount, 0 );
342 free( st->aiColorVals );
343 st->aiColorVals = SetPalette( st );
344 XClearWindow( st->dpy, st->window );
345 for (i = 0; i < st->nBlobCount; i++)
347 init_blob(st, st->blobs + i);
356 if( nTime - time( NULL ) )
358 printf( "%s: %d FPS\n", progname, iFrame );
359 nTime = time( NULL );
369 metaballs_reshape (Display *dpy, Window window, void *closure,
370 unsigned int w, unsigned int h)
375 metaballs_event (Display *dpy, Window window, void *closure, XEvent *event)
381 metaballs_free (Display *dpy, Window window, void *closure)
384 struct state *st = (struct state *) closure;
385 free( st->pImage->data );
386 XDestroyImage( st->pImage );
387 free( st->aiColorVals );
389 for (i = 0; i < st->iWinHeight; ++i)
392 for (i = 0; i < st->dradius; ++i)
399 static const char *metaballs_defaults [] = {
400 ".background: black",
401 ".foreground: white",
412 static XrmOptionDescRec metaballs_options [] = {
413 { "-color", ".color", XrmoptionSepArg, 0 },
414 { "-ncolors", ".ncolors", XrmoptionSepArg, 0 },
415 { "-count", ".count", XrmoptionSepArg, 0 },
416 { "-delay", ".delay", XrmoptionSepArg, 0 },
417 { "-cycles", ".cycles", XrmoptionSepArg, 0 },
418 { "-radius", ".radius", XrmoptionSepArg, 0 },
419 { "-delta", ".delta", XrmoptionSepArg, 0 },
424 XSCREENSAVER_MODULE ("MetaBalls", metaballs)
426 /* End of Module - "metaballs.c" */