http://slackware.bholcomb.com/slackware/slackware-11.0/source/xap/xscreensaver/xscree...
[xscreensaver] / hacks / shadebobs.c
1 /* shadebobs, Copyright (c) 1999 Shane Smit <blackend@inconnect.com>
2  *
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
9  * implied warranty.
10  *
11  * Module - "shadebobs.c"
12  *
13  * Description:
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
16  *  black.
17  *  This keeps the screen in color balance at a chosen color.
18  *
19  *  Its kinda like 'The Force'
20  *   There is a light side, a dark side, and it keeps the world in balance.
21  *
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
29  *                real-time.
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.
37  */
38
39
40 #include <math.h>
41 #include "screenhack.h"
42
43 /* #define VERBOSE */
44
45 static const char *shadebobs_defaults [] = {
46   ".background: black",
47   ".foreground: white",
48   "*degrees:  0",       /* default: Automatic degree calculation */
49   "*color:    random",
50   "*count:    4",
51   "*cycles:   10",
52   "*ncolors:  64",    /* changing this doesn't work particularly well */
53   "*delay:    5000",
54   0
55 };
56
57 static XrmOptionDescRec shadebobs_options [] = {
58   { "-degrees", ".degrees", XrmoptionSepArg, 0 },
59   { "-color",   ".color",   XrmoptionSepArg, 0 },
60   { "-ncolors", ".ncolors", XrmoptionSepArg, 0 },
61   { "-count",   ".count",   XrmoptionSepArg, 0 },
62   { "-delay",   ".delay",   XrmoptionSepArg, 0 },
63   { "-cycles",  ".cycles",  XrmoptionSepArg, 0 },
64   { 0, 0, 0, 0 }
65 };
66
67 /* Ahem. Chocolate is a flavor; not a food. Thank you */
68
69
70 typedef struct
71 {
72         signed char *anDeltaMap;
73         double nAngle, nAngleDelta, nAngleInc;
74         double nPosX, nPosY;
75 } SShadeBob;
76
77 struct state {
78   Display *dpy;
79   Window window;
80   unsigned short iDegreeCount;
81   double *anSinTable, *anCosTable;
82   unsigned short iWinWidth, iWinHeight;
83   unsigned short iWinCenterX, iWinCenterY;
84   char *sColor;
85   unsigned char iBobRadius, iBobDiameter; 
86   unsigned char iVelocity;
87
88   unsigned long *aiColorVals;
89   signed short iColorCount;
90   int cycles;
91   XImage *pImage;
92   unsigned char nShadeBobCount, iShadeBob;
93   SShadeBob *aShadeBobs;
94   GC gc;
95   int delay;
96   int draw_i;
97 };
98
99 #define RANDOM() ((int) (random() & 0X7FFFFFFFL))
100
101
102
103 static void ResetShadeBob( struct state *st, SShadeBob *pShadeBob )
104 {
105         pShadeBob->nPosX = RANDOM() % st->iWinWidth;
106         pShadeBob->nPosY = RANDOM() % st->iWinHeight;
107         pShadeBob->nAngle = RANDOM() % st->iDegreeCount;
108         pShadeBob->nAngleDelta = ( RANDOM() % st->iDegreeCount ) - ( st->iDegreeCount / 2.0F );
109         pShadeBob->nAngleInc = pShadeBob->nAngleDelta / 50.0F; 
110         if( pShadeBob->nAngleInc == 0.0F )      
111                 pShadeBob->nAngleInc = ( pShadeBob->nAngleDelta > 0.0F ) ? 0.0001F : -0.0001F;
112 }
113
114
115 static void InitShadeBob( struct state *st, SShadeBob *pShadeBob, Bool bDark )
116 {
117         double nDelta;
118         int iWidth, iHeight;
119
120         if( ( pShadeBob->anDeltaMap = calloc( st->iBobDiameter * st->iBobDiameter, sizeof(char) ) ) == NULL )
121         {
122                 fprintf( stderr, "%s: Could not allocate Delta Map!\n", progname );
123                 return;
124         }
125
126         for( iHeight=-st->iBobRadius; iHeight<st->iBobRadius; iHeight++ )
127                 for( iWidth=-st->iBobRadius; iWidth<st->iBobRadius; iWidth++ )
128                 {
129                         nDelta = 9 - ( ( sqrt( pow( iWidth+0.5, 2 ) + pow( iHeight+0.5, 2 ) ) / st->iBobRadius ) * 8 );
130                         if( nDelta < 0 )  nDelta = 0;
131                         if( bDark ) nDelta = -nDelta;
132                         pShadeBob->anDeltaMap[ ( iWidth + st->iBobRadius ) * st->iBobDiameter + iHeight + st->iBobRadius ] = (char)nDelta;
133                 }
134   
135         ResetShadeBob( st, pShadeBob );
136 }
137
138
139 /* A delta is calculated, and the shadebob turns at an increment.  When the delta
140  * falls to 0, a new delta and increment are calculated. */
141 static void MoveShadeBob( struct state *st, SShadeBob *pShadeBob )
142 {
143         pShadeBob->nAngle          += pShadeBob->nAngleInc;
144         pShadeBob->nAngleDelta -= pShadeBob->nAngleInc;
145
146         if( pShadeBob->nAngle >= st->iDegreeCount )     pShadeBob->nAngle -= st->iDegreeCount;
147         else if( pShadeBob->nAngle < 0 )                pShadeBob->nAngle += st->iDegreeCount;
148         
149         if( ( pShadeBob->nAngleInc>0.0F  && pShadeBob->nAngleDelta<pShadeBob->nAngleInc ) ||
150             ( pShadeBob->nAngleInc<=0.0F && pShadeBob->nAngleDelta>pShadeBob->nAngleInc ) )
151         {
152                 pShadeBob->nAngleDelta = ( RANDOM() % st->iDegreeCount ) - ( st->iDegreeCount / 2.0F );
153                 pShadeBob->nAngleInc = pShadeBob->nAngleDelta / 50.0F;
154                 if( pShadeBob->nAngleInc == 0.0F )
155                         pShadeBob->nAngleInc = ( pShadeBob->nAngleDelta > 0.0F ) ? 0.0001F : -0.0001F;
156         }
157         
158         pShadeBob->nPosX = ( st->anSinTable[ (int)pShadeBob->nAngle ] * st->iVelocity ) + pShadeBob->nPosX;
159         pShadeBob->nPosY = ( st->anCosTable[ (int)pShadeBob->nAngle ] * st->iVelocity ) + pShadeBob->nPosY;
160
161         /* This wraps it around the screen. */
162         if( pShadeBob->nPosX >= st->iWinWidth ) pShadeBob->nPosX -= st->iWinWidth;
163         else if( pShadeBob->nPosX < 0 )         pShadeBob->nPosX += st->iWinWidth;
164         
165         if( pShadeBob->nPosY >= st->iWinHeight )        pShadeBob->nPosY -= st->iWinHeight;
166         else if( pShadeBob->nPosY < 0 )                 pShadeBob->nPosY += st->iWinHeight;
167 }
168
169
170 static void Execute( struct state *st, SShadeBob *pShadeBob )
171 {
172         unsigned long iColor;
173         short iColorVal;
174         int iPixelX, iPixelY;
175         unsigned int iWidth, iHeight;
176
177         MoveShadeBob( st, pShadeBob );
178         
179         for( iHeight=0; iHeight<st->iBobDiameter; iHeight++ )
180         {
181                 iPixelY = pShadeBob->nPosY + iHeight;
182                 if( iPixelY >= st->iWinHeight ) iPixelY -= st->iWinHeight;
183
184                 for( iWidth=0; iWidth<st->iBobDiameter; iWidth++ )
185                 {
186                         iPixelX = pShadeBob->nPosX + iWidth;
187                         if( iPixelX >= st->iWinWidth )  iPixelX -= st->iWinWidth;
188
189                         iColor = XGetPixel( st->pImage, iPixelX, iPixelY );
190
191                         /*  FIXME: Here is a loop I'd love to take out. */
192                         for( iColorVal=0; iColorVal<st->iColorCount; iColorVal++ )
193                                 if( st->aiColorVals[ iColorVal ] == iColor )
194                                         break;
195
196                         iColorVal += pShadeBob->anDeltaMap[ iWidth * st->iBobDiameter + iHeight ];
197                         if( iColorVal >= st->iColorCount ) iColorVal = st->iColorCount - 1;
198                         if( iColorVal < 0 )                        iColorVal = 0;
199
200                         XPutPixel( st->pImage, iPixelX, iPixelY, st->aiColorVals[ iColorVal ] );
201                 }
202         }
203
204         /* FIXME: if it's next to the top or left sides of screen this will break. However, it's not noticable. */
205         XPutImage( st->dpy, st->window, st->gc, st->pImage,
206              pShadeBob->nPosX, pShadeBob->nPosY, pShadeBob->nPosX, pShadeBob->nPosY, st->iBobDiameter, st->iBobDiameter );
207 }
208
209
210 static void CreateTables( struct state *st, unsigned int nDegrees )
211 {
212         double nRadian;
213         unsigned int iDegree;
214         st->anSinTable = calloc( nDegrees, sizeof(double) );
215         st->anCosTable = calloc( nDegrees, sizeof(double) );
216
217         for( iDegree=0; iDegree<nDegrees; iDegree++ )
218         {
219                 nRadian = ( (double)(2*iDegree) / (double)nDegrees ) * M_PI;
220                 st->anSinTable[ iDegree ] = sin( nRadian );
221                 st->anCosTable[ iDegree ] = cos( nRadian );
222         }
223 }
224
225
226 static unsigned long * SetPalette(struct state *st )
227 {
228         XWindowAttributes XWinAttribs;
229         XColor Color, *aColors;
230         signed short iColor;
231         float nHalfColors;
232         
233         XGetWindowAttributes( st->dpy, st->window, &XWinAttribs );
234         
235         Color.red =   RANDOM() % 0xFFFF;
236         Color.green = RANDOM() % 0xFFFF;
237         Color.blue =  RANDOM() % 0xFFFF;
238
239         if( strcasecmp( st->sColor, "random" ) && !XParseColor( st->dpy, XWinAttribs.colormap, st->sColor, &Color ) )
240                 fprintf( stderr, "%s: color %s not found in database. Choosing to random...\n", progname, st->sColor );
241
242 #ifdef VERBOSE
243         printf( "%s: Base color (RGB): <%d, %d, %d>\n", progname, Color.red, Color.green, Color.blue );
244 #endif  /*  VERBOSE */
245
246         st->iColorCount = get_integer_resource(st->dpy,  "ncolors", "Integer" );
247         if( st->iColorCount <   2 )     st->iColorCount = 2;
248         if( st->iColorCount > 255 )     st->iColorCount = 255;
249
250         aColors    = calloc( st->iColorCount, sizeof(XColor) );
251         st->aiColorVals = calloc( st->iColorCount, sizeof(unsigned long) );
252         
253         for( iColor=0; iColor<st->iColorCount; iColor++ )
254         {
255                 nHalfColors = st->iColorCount / 2.0F;
256                 /* Black -> Base Color */
257                 if( iColor < (st->iColorCount/2) )
258                 {
259                         aColors[ iColor ].red   = ( Color.red   / nHalfColors ) * iColor;
260                         aColors[ iColor ].green = ( Color.green / nHalfColors ) * iColor;
261                         aColors[ iColor ].blue  = ( Color.blue  / nHalfColors ) * iColor;
262                 }
263                 /* Base Color -> White */
264                 else
265                 {
266                         aColors[ iColor ].red   = ( ( ( 0xFFFF - Color.red )   / nHalfColors ) * ( iColor - nHalfColors ) ) + Color.red;
267                         aColors[ iColor ].green = ( ( ( 0xFFFF - Color.green ) / nHalfColors ) * ( iColor - nHalfColors ) ) + Color.green;
268                         aColors[ iColor ].blue  = ( ( ( 0xFFFF - Color.blue )  / nHalfColors ) * ( iColor - nHalfColors ) ) + Color.blue;
269                 }
270
271                 if( !XAllocColor( st->dpy, XWinAttribs.colormap, &aColors[ iColor ] ) )
272                 {
273                         /* start all over with less colors */   
274                         XFreeColors( st->dpy, XWinAttribs.colormap, st->aiColorVals, iColor, 0 );
275                         free( aColors );
276                         free( st->aiColorVals );
277                         st->iColorCount--;
278                         aColors     = calloc( st->iColorCount, sizeof(XColor) );
279                         st->aiColorVals = calloc( st->iColorCount, sizeof(unsigned long) );
280                         iColor = -1;
281                 }
282                 else
283                         st->aiColorVals[ iColor ] = aColors[ iColor ].pixel;
284         }
285
286         free( aColors );
287
288         XSetWindowBackground( st->dpy, st->window, st->aiColorVals[ 0 ] );
289
290         return st->aiColorVals;
291 }
292
293
294 static void Initialize( struct state *st )
295 {
296         XGCValues gcValues;
297         XWindowAttributes XWinAttribs;
298         int iBitsPerPixel;
299
300         /* Create the Image for drawing */
301         XGetWindowAttributes( st->dpy, st->window, &XWinAttribs );
302
303         /* Find the preferred bits-per-pixel. (jwz) */
304         {
305                 int i, pfvc = 0;
306                 XPixmapFormatValues *pfv = XListPixmapFormats( st->dpy, &pfvc );
307                 for( i=0; i<pfvc; i++ )
308                         if( pfv[ i ].depth == XWinAttribs.depth )
309                         {
310                                 iBitsPerPixel = pfv[ i ].bits_per_pixel;
311                                 break;
312                         }
313                 if( pfv )
314                         XFree (pfv);
315         }
316
317         /*  Create the GC. */
318         st->gc = XCreateGC( st->dpy, st->window, 0, &gcValues );
319
320         st->pImage = XCreateImage( st->dpy, XWinAttribs.visual, XWinAttribs.depth, ZPixmap, 0, NULL,
321                                                           XWinAttribs.width, XWinAttribs.height, 8 /*BitmapPad( st->dpy )*/, 0 );
322         st->pImage->data = calloc((st->pImage)->bytes_per_line, (st->pImage)->height);
323
324         st->iWinWidth = XWinAttribs.width;
325         st->iWinHeight = XWinAttribs.height;
326
327         /*  These are precalculations used in Execute(). */
328         st->iBobDiameter = ( ( st->iWinWidth < st->iWinHeight ) ? st->iWinWidth : st->iWinHeight ) / 25;
329         st->iBobRadius = st->iBobDiameter / 2;
330 #ifdef VERBOSE
331         printf( "%s: Bob Diameter = %d\n", progname, st->iBobDiameter );
332 #endif
333
334         st->iWinCenterX = ( XWinAttribs.width / 2 ) - st->iBobRadius;
335         st->iWinCenterY = ( XWinAttribs.height / 2 ) - st->iBobRadius;
336
337         st->iVelocity = ( ( st->iWinWidth < st->iWinHeight ) ? st->iWinWidth : st->iWinHeight ) / 150;
338         
339         /*  Create the Sin and Cosine lookup tables. */
340         st->iDegreeCount = get_integer_resource(st->dpy,  "degrees", "Integer" );
341         if(      st->iDegreeCount == 0   ) st->iDegreeCount = ( XWinAttribs.width / 6 ) + 400;
342         else if( st->iDegreeCount < 90   ) st->iDegreeCount = 90;
343         else if( st->iDegreeCount > 5400 ) st->iDegreeCount = 5400;
344         CreateTables( st, st->iDegreeCount );
345 #ifdef VERBOSE
346         printf( "%s: Using a %d degree circle.\n", progname );
347 #endif /* VERBOSE */
348   
349         /*  Get the base color. */
350         st->sColor = get_string_resource(st->dpy,  "color", "Color" );
351 }
352
353
354 static void *
355 shadebobs_init (Display *dpy, Window window)
356 {
357   struct state *st = (struct state *) calloc (1, sizeof(*st));
358
359 #ifdef VERBOSE
360         time_t nTime = time( NULL );
361         unsigned short iFrame = 0;
362 #endif  /*  VERBOSE */
363
364   st->dpy = dpy;
365   st->window = window;
366
367         st->nShadeBobCount = get_integer_resource(st->dpy,  "count", "Integer" );
368         if( st->nShadeBobCount > 64 ) st->nShadeBobCount = 64;
369         if( st->nShadeBobCount <  1 ) st->nShadeBobCount = 1;
370
371         if( ( st->aShadeBobs = calloc( st->nShadeBobCount, sizeof(SShadeBob) ) ) == NULL )
372         {
373                 fprintf( stderr, "%s: Could not allocate %d ShadeBobs\n", progname, st->nShadeBobCount );
374                 abort();
375         }
376 #ifdef VERBOSE 
377         printf( "%s: Allocated %d ShadeBobs\n", progname, st->nShadeBobCount );
378 #endif  /*  VERBOSE */
379
380         Initialize( st );
381
382         for( st->iShadeBob=0; st->iShadeBob<st->nShadeBobCount; st->iShadeBob++ )
383                 InitShadeBob( st, &st->aShadeBobs[ st->iShadeBob ], st->iShadeBob % 2 );
384
385         st->delay = get_integer_resource(st->dpy,  "delay", "Integer" );
386         st->cycles = get_integer_resource(st->dpy,  "cycles", "Integer" ) * st->iDegreeCount;
387
388         st->draw_i = 99999999;
389         return st;
390 }
391
392 static unsigned long
393 shadebobs_draw (Display *dpy, Window window, void *closure)
394 {
395   struct state *st = (struct state *) closure;
396
397   if( st->draw_i++ >= st->cycles )
398     {
399       XWindowAttributes XWinAttribs;
400       XGetWindowAttributes( st->dpy, st->window, &XWinAttribs );
401
402       st->draw_i = 0;
403 #if 0
404       memset( st->pImage->data, 0, st->pImage->bytes_per_line * st->pImage->height );
405 #else
406       {
407         /* fill the image with the actual value of the black pixel, not 0. */
408         unsigned long black = BlackPixelOfScreen (XWinAttribs.screen);
409         int x, y;
410         for (y = 0; y < XWinAttribs.height; y++)
411           for (x = 0; x < XWinAttribs.width; x++)
412             XPutPixel (st->pImage, x, y, black);
413       }
414 #endif
415
416       for( st->iShadeBob=0; st->iShadeBob<st->nShadeBobCount; st->iShadeBob++ )
417         ResetShadeBob( st, &st->aShadeBobs[ st->iShadeBob ] );
418       XFreeColors( st->dpy, XWinAttribs.colormap, st->aiColorVals, st->iColorCount, 0 );
419       free( st->aiColorVals );
420       st->aiColorVals = SetPalette( st );
421       XClearWindow( st->dpy, st->window );
422     }
423
424   for( st->iShadeBob=0; st->iShadeBob<st->nShadeBobCount; st->iShadeBob++ )
425     Execute( st, &st->aShadeBobs[ st->iShadeBob ] );
426
427 #ifdef VERBOSE
428   iFrame++;
429   if( nTime - time( NULL ) )
430     {
431       printf( "%s: %d FPS\n", progname, iFrame );
432       nTime = time( NULL );
433       iFrame = 0;
434     }
435 #endif  /*  VERBOSE */
436
437   return st->delay;
438 }
439
440 static void
441 shadebobs_reshape (Display *dpy, Window window, void *closure, 
442                  unsigned int w, unsigned int h)
443 {
444 }
445
446 static Bool
447 shadebobs_event (Display *dpy, Window window, void *closure, XEvent *event)
448 {
449   return False;
450 }
451
452 static void
453 shadebobs_free (Display *dpy, Window window, void *closure)
454 {
455   struct state *st = (struct state *) closure;
456         free( st->anSinTable );
457         free( st->anCosTable );
458         /* free( st->pImage->data ); */
459         XDestroyImage( st->pImage );
460         for( st->iShadeBob=0; st->iShadeBob<st->nShadeBobCount; st->iShadeBob++ )
461                 free( st->aShadeBobs[ st->iShadeBob ].anDeltaMap );
462         free( st->aShadeBobs );
463         free( st->aiColorVals );
464 }
465
466
467 XSCREENSAVER_MODULE ("ShadeBobs", shadebobs)
468
469 /* End of Module - "shadebobs.c" */
470
471 /* vim: ts=4
472  */