1 /* shadebobs, Copyright (c) 1999 Shane Smit <blackend@inconnect.com>
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 - "shadebobs.c"
14 * There are two little shading circles (bobs) that zip around the screen.
15 * one of them shades up towards white, and the other shades down toward
17 * This keeps the screen in color balance at a chosen color.
19 * Its kinda like 'The Force'
20 * There is a light side, a dark side, and it keeps the world in balance.
22 * [05/23/99] - Shane Smit: Creation
23 * [05/26/99] - Shane Smit: Port to C/screenhack for use with XScreenSaver
24 * [06/11/99] - Shane Smit: Stopped trying to rape the palette.
25 * [06/20/99] - jwz: cleaned up ximage handling, gave resoources short names,
26 * introduced delay, made it restart after N iterations.
27 * [06/21/99] - Shane Smit: Modified default values slightly, color changes
28 * on cycle, and the extents of the sinus pattern change in
30 * [06/22/99] - Shane Smit: Fixed delay to be fast and use little CPU :).
31 * [09/17/99] - Shane Smit: Made all calculations based on the size of the
32 * window. Thus, it'll look the same at 100x100 as it does at
33 * 1600x1200 ( Only smaller :).
34 * [04/24/00] - Shane Smit: Revamped entire source code:
35 * Shade Bob movement is calculated very differently.
36 * Base color can be any color now.
41 #include "screenhack.h"
45 static const char *shadebobs_defaults [] = {
49 "*degrees: 0", /* default: Automatic degree calculation */
53 "*ncolors: 64", /* changing this doesn't work particularly well */
56 "*ignoreRotation: True",
61 static XrmOptionDescRec shadebobs_options [] = {
62 { "-degrees", ".degrees", XrmoptionSepArg, 0 },
63 { "-color", ".color", XrmoptionSepArg, 0 },
64 { "-ncolors", ".ncolors", XrmoptionSepArg, 0 },
65 { "-count", ".count", XrmoptionSepArg, 0 },
66 { "-delay", ".delay", XrmoptionSepArg, 0 },
67 { "-cycles", ".cycles", XrmoptionSepArg, 0 },
71 /* Ahem. Chocolate is a flavor; not a food. Thank you */
76 signed char *anDeltaMap;
77 double nAngle, nAngleDelta, nAngleInc;
84 unsigned short iDegreeCount;
85 double *anSinTable, *anCosTable;
86 unsigned short iWinWidth, iWinHeight;
87 unsigned short iWinCenterX, iWinCenterY;
89 unsigned char iBobRadius, iBobDiameter;
90 unsigned char iVelocity;
92 unsigned long *aiColorVals;
93 signed short iColorCount;
96 unsigned char nShadeBobCount, iShadeBob;
97 SShadeBob *aShadeBobs;
103 #define RANDOM() ((int) (random() & 0X7FFFFFFFL))
107 static void ResetShadeBob( struct state *st, SShadeBob *pShadeBob )
109 pShadeBob->nPosX = RANDOM() % st->iWinWidth;
110 pShadeBob->nPosY = RANDOM() % st->iWinHeight;
111 pShadeBob->nAngle = RANDOM() % st->iDegreeCount;
112 pShadeBob->nAngleDelta = ( RANDOM() % st->iDegreeCount ) - ( st->iDegreeCount / 2.0F );
113 pShadeBob->nAngleInc = pShadeBob->nAngleDelta / 50.0F;
114 if( pShadeBob->nAngleInc == 0.0F )
115 pShadeBob->nAngleInc = ( pShadeBob->nAngleDelta > 0.0F ) ? 0.0001F : -0.0001F;
119 static void InitShadeBob( struct state *st, SShadeBob *pShadeBob, Bool bDark )
124 if( ( pShadeBob->anDeltaMap = calloc( st->iBobDiameter * st->iBobDiameter, sizeof(char) ) ) == NULL )
126 fprintf( stderr, "%s: Could not allocate Delta Map!\n", progname );
130 for( iHeight=-st->iBobRadius; iHeight<st->iBobRadius; iHeight++ )
131 for( iWidth=-st->iBobRadius; iWidth<st->iBobRadius; iWidth++ )
133 nDelta = 9 - ( ( sqrt( pow( iWidth+0.5, 2 ) + pow( iHeight+0.5, 2 ) ) / st->iBobRadius ) * 8 );
134 if( nDelta < 0 ) nDelta = 0;
135 if( bDark ) nDelta = -nDelta;
136 pShadeBob->anDeltaMap[ ( iWidth + st->iBobRadius ) * st->iBobDiameter + iHeight + st->iBobRadius ] = (char)nDelta;
139 ResetShadeBob( st, pShadeBob );
143 /* A delta is calculated, and the shadebob turns at an increment. When the delta
144 * falls to 0, a new delta and increment are calculated. */
145 static void MoveShadeBob( struct state *st, SShadeBob *pShadeBob )
147 pShadeBob->nAngle += pShadeBob->nAngleInc;
148 pShadeBob->nAngleDelta -= pShadeBob->nAngleInc;
150 if( pShadeBob->nAngle >= st->iDegreeCount ) pShadeBob->nAngle -= st->iDegreeCount;
151 else if( pShadeBob->nAngle < 0 ) pShadeBob->nAngle += st->iDegreeCount;
153 if( ( pShadeBob->nAngleInc>0.0F && pShadeBob->nAngleDelta<pShadeBob->nAngleInc ) ||
154 ( pShadeBob->nAngleInc<=0.0F && pShadeBob->nAngleDelta>pShadeBob->nAngleInc ) )
156 pShadeBob->nAngleDelta = ( RANDOM() % st->iDegreeCount ) - ( st->iDegreeCount / 2.0F );
157 pShadeBob->nAngleInc = pShadeBob->nAngleDelta / 50.0F;
158 if( pShadeBob->nAngleInc == 0.0F )
159 pShadeBob->nAngleInc = ( pShadeBob->nAngleDelta > 0.0F ) ? 0.0001F : -0.0001F;
162 pShadeBob->nPosX = ( st->anSinTable[ (int)pShadeBob->nAngle ] * st->iVelocity ) + pShadeBob->nPosX;
163 pShadeBob->nPosY = ( st->anCosTable[ (int)pShadeBob->nAngle ] * st->iVelocity ) + pShadeBob->nPosY;
165 /* This wraps it around the screen. */
166 if( pShadeBob->nPosX >= st->iWinWidth ) pShadeBob->nPosX -= st->iWinWidth;
167 else if( pShadeBob->nPosX < 0 ) pShadeBob->nPosX += st->iWinWidth;
169 if( pShadeBob->nPosY >= st->iWinHeight ) pShadeBob->nPosY -= st->iWinHeight;
170 else if( pShadeBob->nPosY < 0 ) pShadeBob->nPosY += st->iWinHeight;
174 static void Execute( struct state *st, SShadeBob *pShadeBob )
176 unsigned long iColor;
178 int iPixelX, iPixelY;
179 unsigned int iWidth, iHeight;
181 MoveShadeBob( st, pShadeBob );
183 for( iHeight=0; iHeight<st->iBobDiameter; iHeight++ )
185 iPixelY = pShadeBob->nPosY + iHeight;
186 if( iPixelY >= st->iWinHeight ) iPixelY -= st->iWinHeight;
188 for( iWidth=0; iWidth<st->iBobDiameter; iWidth++ )
190 iPixelX = pShadeBob->nPosX + iWidth;
191 if( iPixelX >= st->iWinWidth ) iPixelX -= st->iWinWidth;
193 iColor = XGetPixel( st->pImage, iPixelX, iPixelY );
195 /* FIXME: Here is a loop I'd love to take out. */
196 for( iColorVal=0; iColorVal<st->iColorCount; iColorVal++ )
197 if( st->aiColorVals[ iColorVal ] == iColor )
200 iColorVal += pShadeBob->anDeltaMap[ iWidth * st->iBobDiameter + iHeight ];
201 if( iColorVal >= st->iColorCount ) iColorVal = st->iColorCount - 1;
202 if( iColorVal < 0 ) iColorVal = 0;
204 XPutPixel( st->pImage, iPixelX, iPixelY, st->aiColorVals[ iColorVal ] );
208 /* FIXME: if it's next to the top or left sides of screen this will break. However, it's not noticable. */
209 XPutImage( st->dpy, st->window, st->gc, st->pImage,
210 pShadeBob->nPosX, pShadeBob->nPosY, pShadeBob->nPosX, pShadeBob->nPosY, st->iBobDiameter, st->iBobDiameter );
214 static void CreateTables( struct state *st, unsigned int nDegrees )
217 unsigned int iDegree;
218 st->anSinTable = calloc( nDegrees, sizeof(double) );
219 st->anCosTable = calloc( nDegrees, sizeof(double) );
221 for( iDegree=0; iDegree<nDegrees; iDegree++ )
223 nRadian = ( (double)(2*iDegree) / (double)nDegrees ) * M_PI;
224 st->anSinTable[ iDegree ] = sin( nRadian );
225 st->anCosTable[ iDegree ] = cos( nRadian );
230 static unsigned long * SetPalette(struct state *st )
232 XWindowAttributes XWinAttribs;
233 XColor Color, *aColors;
237 XGetWindowAttributes( st->dpy, st->window, &XWinAttribs );
239 Color.red = RANDOM() % 0xFFFF;
240 Color.green = RANDOM() % 0xFFFF;
241 Color.blue = RANDOM() % 0xFFFF;
243 if( strcasecmp( st->sColor, "random" ) && !XParseColor( st->dpy, XWinAttribs.colormap, st->sColor, &Color ) )
244 fprintf( stderr, "%s: color %s not found in database. Choosing to random...\n", progname, st->sColor );
247 printf( "%s: Base color (RGB): <%d, %d, %d>\n", progname, Color.red, Color.green, Color.blue );
250 st->iColorCount = get_integer_resource(st->dpy, "ncolors", "Integer" );
251 if( st->iColorCount < 2 ) st->iColorCount = 2;
252 if( st->iColorCount > 255 ) st->iColorCount = 255;
254 aColors = calloc( st->iColorCount, sizeof(XColor) );
255 st->aiColorVals = calloc( st->iColorCount, sizeof(unsigned long) );
257 for( iColor=0; iColor<st->iColorCount; iColor++ )
259 nHalfColors = st->iColorCount / 2.0F;
260 /* Black -> Base Color */
261 if( iColor < (st->iColorCount/2) )
263 aColors[ iColor ].red = ( Color.red / nHalfColors ) * iColor;
264 aColors[ iColor ].green = ( Color.green / nHalfColors ) * iColor;
265 aColors[ iColor ].blue = ( Color.blue / nHalfColors ) * iColor;
267 /* Base Color -> White */
270 aColors[ iColor ].red = ( ( ( 0xFFFF - Color.red ) / nHalfColors ) * ( iColor - nHalfColors ) ) + Color.red;
271 aColors[ iColor ].green = ( ( ( 0xFFFF - Color.green ) / nHalfColors ) * ( iColor - nHalfColors ) ) + Color.green;
272 aColors[ iColor ].blue = ( ( ( 0xFFFF - Color.blue ) / nHalfColors ) * ( iColor - nHalfColors ) ) + Color.blue;
275 if( !XAllocColor( st->dpy, XWinAttribs.colormap, &aColors[ iColor ] ) )
277 /* start all over with less colors */
278 XFreeColors( st->dpy, XWinAttribs.colormap, st->aiColorVals, iColor, 0 );
280 free( st->aiColorVals );
282 aColors = calloc( st->iColorCount, sizeof(XColor) );
283 st->aiColorVals = calloc( st->iColorCount, sizeof(unsigned long) );
287 st->aiColorVals[ iColor ] = aColors[ iColor ].pixel;
292 XSetWindowBackground( st->dpy, st->window, st->aiColorVals[ 0 ] );
294 return st->aiColorVals;
298 static void Initialize( struct state *st )
301 XWindowAttributes XWinAttribs;
302 /*int iBitsPerPixel;*/
304 /* Create the Image for drawing */
305 XGetWindowAttributes( st->dpy, st->window, &XWinAttribs );
308 /* Find the preferred bits-per-pixel. (jwz) */
311 XPixmapFormatValues *pfv = XListPixmapFormats( st->dpy, &pfvc );
312 for( i=0; i<pfvc; i++ )
313 if( pfv[ i ].depth == XWinAttribs.depth )
315 iBitsPerPixel = pfv[ i ].bits_per_pixel;
324 st->gc = XCreateGC( st->dpy, st->window, 0, &gcValues );
326 st->pImage = XCreateImage( st->dpy, XWinAttribs.visual, XWinAttribs.depth, ZPixmap, 0, NULL,
327 XWinAttribs.width, XWinAttribs.height, 8 /*BitmapPad( st->dpy )*/, 0 );
328 st->pImage->data = calloc((st->pImage)->bytes_per_line, (st->pImage)->height);
330 st->iWinWidth = XWinAttribs.width;
331 st->iWinHeight = XWinAttribs.height;
333 /* These are precalculations used in Execute(). */
334 st->iBobDiameter = ( ( st->iWinWidth < st->iWinHeight ) ? st->iWinWidth : st->iWinHeight ) / 25;
335 st->iBobRadius = st->iBobDiameter / 2;
337 printf( "%s: Bob Diameter = %d\n", progname, st->iBobDiameter );
340 st->iWinCenterX = ( XWinAttribs.width / 2 ) - st->iBobRadius;
341 st->iWinCenterY = ( XWinAttribs.height / 2 ) - st->iBobRadius;
343 st->iVelocity = ( ( st->iWinWidth < st->iWinHeight ) ? st->iWinWidth : st->iWinHeight ) / 150;
345 /* Create the Sin and Cosine lookup tables. */
346 st->iDegreeCount = get_integer_resource(st->dpy, "degrees", "Integer" );
347 if( st->iDegreeCount == 0 ) st->iDegreeCount = ( XWinAttribs.width / 6 ) + 400;
348 else if( st->iDegreeCount < 90 ) st->iDegreeCount = 90;
349 else if( st->iDegreeCount > 5400 ) st->iDegreeCount = 5400;
350 CreateTables( st, st->iDegreeCount );
352 printf( "%s: Using a %d degree circle.\n", progname, st->iDegreeCount );
355 /* Get the base color. */
356 st->sColor = get_string_resource(st->dpy, "color", "Color" );
361 shadebobs_init (Display *dpy, Window window)
363 struct state *st = (struct state *) calloc (1, sizeof(*st));
366 time_t nTime = time( NULL );
367 unsigned short iFrame = 0;
373 st->nShadeBobCount = get_integer_resource(st->dpy, "count", "Integer" );
374 if( st->nShadeBobCount > 64 ) st->nShadeBobCount = 64;
375 if( st->nShadeBobCount < 1 ) st->nShadeBobCount = 1;
377 if( ( st->aShadeBobs = calloc( st->nShadeBobCount, sizeof(SShadeBob) ) ) == NULL )
379 fprintf( stderr, "%s: Could not allocate %d ShadeBobs\n", progname, st->nShadeBobCount );
383 printf( "%s: Allocated %d ShadeBobs\n", progname, st->nShadeBobCount );
388 for( st->iShadeBob=0; st->iShadeBob<st->nShadeBobCount; st->iShadeBob++ )
389 InitShadeBob( st, &st->aShadeBobs[ st->iShadeBob ], st->iShadeBob % 2 );
391 st->delay = get_integer_resource(st->dpy, "delay", "Integer" );
392 st->cycles = get_integer_resource(st->dpy, "cycles", "Integer" ) * st->iDegreeCount;
394 st->draw_i = 99999999;
399 shadebobs_draw (Display *dpy, Window window, void *closure)
401 struct state *st = (struct state *) closure;
403 if( st->draw_i++ >= st->cycles )
405 XWindowAttributes XWinAttribs;
406 XGetWindowAttributes( st->dpy, st->window, &XWinAttribs );
410 memset( st->pImage->data, 0, st->pImage->bytes_per_line * st->pImage->height );
413 /* fill the image with the actual value of the black pixel, not 0. */
414 unsigned long black = BlackPixelOfScreen (XWinAttribs.screen);
416 for (y = 0; y < XWinAttribs.height; y++)
417 for (x = 0; x < XWinAttribs.width; x++)
418 XPutPixel (st->pImage, x, y, black);
422 for( st->iShadeBob=0; st->iShadeBob<st->nShadeBobCount; st->iShadeBob++ )
423 ResetShadeBob( st, &st->aShadeBobs[ st->iShadeBob ] );
424 XFreeColors( st->dpy, XWinAttribs.colormap, st->aiColorVals, st->iColorCount, 0 );
425 free( st->aiColorVals );
426 st->aiColorVals = SetPalette( st );
427 XClearWindow( st->dpy, st->window );
430 for( st->iShadeBob=0; st->iShadeBob<st->nShadeBobCount; st->iShadeBob++ )
431 Execute( st, &st->aShadeBobs[ st->iShadeBob ] );
437 shadebobs_reshape (Display *dpy, Window window, void *closure,
438 unsigned int w, unsigned int h)
443 shadebobs_event (Display *dpy, Window window, void *closure, XEvent *event)
449 shadebobs_free (Display *dpy, Window window, void *closure)
451 struct state *st = (struct state *) closure;
452 free( st->anSinTable );
453 free( st->anCosTable );
454 /* free( st->pImage->data ); */
455 XDestroyImage( st->pImage );
456 for( st->iShadeBob=0; st->iShadeBob<st->nShadeBobCount; st->iShadeBob++ )
457 free( st->aShadeBobs[ st->iShadeBob ].anDeltaMap );
458 free( st->aShadeBobs );
459 free( st->aiColorVals );
463 XSCREENSAVER_MODULE ("ShadeBobs", shadebobs)
465 /* End of Module - "shadebobs.c" */