From http://www.jwz.org/xscreensaver/xscreensaver-5.22.tar.gz
[xscreensaver] / hacks / interaggregate.c
1 /*
2  *  InterAggregate (dagraz@gmail.com)
3  *  Based on code from complexification.net Intersection Aggregate
4  *  http://www.complexification.net/gallery/machines/interAggregate/index.php
5  *
6  *  Intersection Aggregate code:
7  *  j.tarbell   May, 2004
8  *  Albuquerque, New Mexico
9  *  complexification.net
10  *
11  *  Also based on substrate, a port of j.tarbell's Substrate Art done
12  *  by dragorn@kismetwireless.net
13  *
14  *  
15  *  Interesting command line options, all of which serve to
16  *  concentrate the painting in some way:
17  *  
18  *  -percent-orbits 100 -base-orbits 50 -base-on-center -growth-delay 0
19  *
20  *  Paint should be concentrated in the center of the canvas, orbiting
21  *  about it.  -percent-orbits 100 implies -base-on-center, so that's
22  *  not really needed.
23  *
24  *
25  *  -percent-orbits 99 -base-orbits 50 -growth-delay 0
26  *
27  *  Like the above example, but the 'center' will rove about the screen.
28  *
29  *  -percent-orbits 98 -base-orbits 50 -growth-delay 0
30  *
31  *  Like the above example, but there will be two roving centers.
32  *
33  *
34  *  TODO:
35  *  -fix alpha blending / byte ordering
36  *
37  *  CHANGES
38  *
39  *
40  * Directly based the hacks of: 
41  * 
42  * xscreensaver, Copyright (c) 1997, 1998, 2002 Jamie Zawinski <jwz@jwz.org>
43  *
44  * Permission to use, copy, modify, distribute, and sell this software and its
45  * documentation for any purpose is hereby granted without fee, provided that
46  * the above copyright notice appear in all copies and that both that
47  * copyright notice and this permission notice appear in supporting
48  * documentation.  No representations are made about the suitability of this
49  * software for any purpose.  It is provided "as is" without express or 
50  * implied warranty.
51  */
52
53 #include <math.h>
54 #include "screenhack.h"
55
56
57 /* this program goes faster if some functions are inline.  The following is
58  * borrowed from ifs.c */
59 #if !defined( __GNUC__ ) && !defined(__cplusplus) && !defined(c_plusplus)
60 #undef inline
61 #define inline                  /* */
62 #endif
63
64 #ifndef MIN
65 #define MIN(x,y) ((x < y) ? x : y)
66 #endif
67
68 #ifndef MAX
69 #define MAX(x,y) ((x < y) ? y : x)
70 #endif
71
72 static const char *interaggregate_defaults[] = 
73 {
74     ".background: white",
75     ".foreground: black",
76     "*fpsSolid: true",
77     "*maxCycles: 100000",
78 #ifdef TIME_ME
79     "*growthDelay: 0",
80 #else
81     "*growthDelay: 18000", 
82 #endif
83     "*numCircles: 100",
84     "*percentOrbits: 0",
85     "*baseOrbits: 75",
86     "*baseOnCenter: False",
87     "*drawCenters: False",
88 #ifdef USE_IPHONE
89     "*ignoreRotation: True",
90 #endif
91     0
92 };
93
94 static XrmOptionDescRec interaggregate_options[] = 
95 {
96     {"-background", ".background", XrmoptionSepArg, 0},
97     {"-foreground", ".foreground", XrmoptionSepArg, 0},
98     {"-max-cycles", ".maxCycles", XrmoptionSepArg, 0},
99     {"-growth-delay", ".growthDelay", XrmoptionSepArg, 0},
100     {"-num-circles", ".numCircles", XrmoptionSepArg, 0},
101     {"-percent-orbits", ".percentOrbits", XrmoptionSepArg, 0},
102     {"-base-orbits", ".baseOrbits", XrmoptionSepArg, 0},
103     {"-base-on-center", ".baseOnCenter", XrmoptionNoArg, "true"}, 
104     {"-draw-centers", ".drawCenters", XrmoptionNoArg, "true"}, 
105     {0, 0, 0, 0}
106 };
107
108 /* Raw colormap extracted from pollockEFF.gif */
109 #if 0
110 char *rgb_colormap[] = 
111 {
112     "#FFFFFF", /* white */
113     "#000000", /* black */
114     "#000000", /* more black */
115     /* "#736451",  */
116     "#4e3e2e", /* olive */
117     /* "#666666", */
118     "#694d35",  /* camel */
119     "#b9a88c",  /* tan */
120     0
121 };
122 #endif
123
124 static const char *rgb_colormap[] = 
125 {
126     "#FFFFFF", /* white */
127     "#000000", /* more black */
128     "#000000", /* more black */
129     "#4e3e2e", /* olive */
130     "#694d35",  /* camel */
131     "#b0a085",  /* tan */
132     "#e6d3ae",
133     0
134 };
135
136 /* black white brown olive grey camel */
137
138 typedef enum { LINEAR, ORBIT } PathType;
139
140 typedef struct 
141 {
142
143     unsigned long color;
144     double gain;
145     double p;
146
147 } SandPainter;
148
149 typedef struct _circle
150 {
151     double radius;
152
153     double x;
154     double y;
155
156     PathType path_type;
157
158     /* for a linear path */
159     double dx;
160     double dy;
161
162     /* for orbital path */
163     double theta;
164     double r; 
165     double dtheta;
166     
167     struct _circle* center;
168
169     int num_painters;
170     SandPainter* painters;
171
172 } Circle;
173
174
175 struct field 
176 {
177     int height;
178     int width;
179
180     int num_circles;
181     Circle* circles;
182
183     int percent_orbits;
184     int base_orbits;
185     Bool base_on_center;
186
187     /* used for orbits circling the center of the screen */
188     Circle center_of_universe;
189
190     /* Raw map of pixels we need to keep for alpha blending */
191     unsigned long int *off_img;
192    
193     /* color parms */
194     int numcolors;
195     unsigned long *parsedcolors;
196     unsigned long fgcolor;
197     unsigned long bgcolor;
198     int visdepth;
199
200     unsigned int cycles;
201
202     double max_gain;
203
204     /* for debugging */
205     Bool draw_centers;
206
207     /* for profiling whatnot */ 
208     int possible_intersections;
209     int intersection_count;
210 };
211
212
213 static struct field *
214 init_field(void)
215 {
216     struct field *f = (struct field*) malloc(sizeof(struct field));
217     if ( f == NULL )
218     {
219         fprintf(stderr, "%s: Failed to allocate field!\n", progname);
220         exit(1);
221     }
222
223     f->height = 0;
224     f->width = 0;
225     f->num_circles = 0;
226     f->circles = NULL;
227     f->percent_orbits = 0;
228     f->base_orbits = 0;
229     f->base_on_center = False;
230     f->off_img = NULL;
231     f->numcolors = 0;
232     f->parsedcolors = NULL;
233     f->fgcolor = 0;
234     f->bgcolor = 0;
235     f->visdepth = 0;
236
237     f->cycles = 0;
238
239     f->max_gain = 0.22;
240
241     f->draw_centers = False;
242
243     f->possible_intersections = 0;
244     f->intersection_count = 0;
245
246     return f;
247 }
248
249 /* Quick references to pixels in the offscreen map and in the crack grid */
250 #define ref_pixel(f, x, y)   ((f)->off_img[(y) * (f)->width + (x)])
251
252 #define in_bounds(f, x, y) ((x >= 0) && (x < f->width) && (y >= 0) && (y < f->height))
253
254 /* Consider rewriting with XQueryColor, or ImageByteOrder */
255
256 static inline void point2rgb(int depth, unsigned long c, int *r, int *g, int *b) 
257 {
258     switch(depth) 
259     {
260     case 32:
261     case 24:
262 #ifdef HAVE_COCOA
263         /* This program idiotically does not go through a color map, so
264            we have to hardcode in knowledge of how jwxyz.a packs pixels!
265            Fix it to go through st->colors[st->ncolors] instead!
266          */
267         *r = (c & 0x00ff0000) >> 16; 
268         *g = (c & 0x0000ffff) >>  8;
269         *b = (c & 0x000000ff);
270 #else
271         *b = c & 0xff; 
272         *g = (c & 0xff00) >> 8; 
273         *r = (c & 0xff0000) >> 16; 
274 #endif
275         break;
276     case 16:
277         *b = (c & 0x1f) << 3; 
278         *g = ((c >> 5) & 0x3f) << 2;
279         *r = ((c >> 11) & 0x1f) << 3; 
280         break;
281     case 15:
282         *b = (c & 0x1f) << 3;
283         *g = ((c >> 5) & 0x1f) << 3;
284         *r = ((c >> 10) & 0x1f) << 3;
285         break;
286     }
287 }
288
289 static inline unsigned long rgb2point(int depth, int r, int g, int b) 
290 {
291     unsigned long ret = 0;
292
293     switch(depth) 
294     {
295     case 32:
296     case 24:
297 #ifdef HAVE_COCOA
298         /* This program idiotically does not go through a color map, so
299            we have to hardcode in knowledge of how jwxyz.a packs pixels!
300            Fix it to go through st->colors[st->ncolors] instead!
301          */
302         ret = 0xFF000000 | (r << 16) | (g << 8) | b;
303 #else
304         ret |= (r << 16) | (g << 8) | b;
305 #endif
306         break;
307     case 16:
308         ret = ((r>>3) << 11) | ((g>>2)<<5) | (b>>3);
309         break;
310     case 15:
311         ret = ((r>>3) << 10) | ((g>>3)<<5) | (b>>3);
312         break;
313     }
314
315     return ret;
316 }
317
318 /* alpha blended point drawing -- this is Not Right and will likely fail on 
319  * non-intel platforms as it is now, needs fixing */
320 static inline unsigned long trans_point(int x1, int y1, unsigned long myc, double a, 
321                                  struct field *f) 
322 {
323     if (a >= 1.0) 
324     {
325         ref_pixel(f, x1, y1) = myc;
326         return myc;
327     } 
328     else 
329     {
330         int or=0, og=0, ob=0;
331         int r=0, g=0, b=0;
332         int nr, ng, nb;
333         unsigned long c;
334
335         c = ref_pixel(f, x1, y1);
336
337         point2rgb(f->visdepth, c, &or, &og, &ob);
338         point2rgb(f->visdepth, myc, &r, &g, &b);
339
340         nr = or + (r - or) * a;
341         ng = og + (g - og) * a;
342         nb = ob + (b - ob) * a;
343
344         c = rgb2point(f->visdepth, nr, ng, nb);
345
346         ref_pixel(f, x1, y1) = c;
347
348         return c;
349     }
350 }
351
352 static inline void drawPoint(int x, int y, unsigned long color, double intensity,
353                       Display *dpy, Window window, GC fgc, struct field *f)
354                
355 {
356     unsigned long c;
357
358     while ( x >= f->width ) x -= f->width;
359     while ( x < 0 ) x += f->width;
360         
361     while ( y >= f->height ) y -= f->height;
362     while ( y < 0 ) y += f->height;
363
364     /* if ( in_bounds(f, x, y) ) ... */
365
366     c = trans_point(x, y, color, intensity, f);
367
368     XSetForeground(dpy, fgc, c);
369     XDrawPoint(dpy, window, fgc, x, y);
370 }
371
372 static inline void paint(SandPainter* painter, double ax, double ay, double bx, double by,
373                   Display *dpy, Window window, GC fgc, 
374                   struct field *f)
375 {
376     /* the sand painter */
377
378     double inc, sandp;
379     int i;
380
381     /* XXX try adding tpoint here, like orig */
382
383     /* jitter the painter's values */
384     painter->gain += frand(0.05) - 0.025;
385     
386     if ( painter->gain > f->max_gain )
387         painter->gain = -f->max_gain;
388     else if ( painter->gain < -f->max_gain )
389         painter->gain = f->max_gain;
390
391     painter->p += frand(0.1) - 0.05;
392     
393     if ( 0 < painter->p )
394         painter->p = 0;
395     else if ( painter->p > 1.0 )
396         painter->p = 1.0;
397
398     /* replace 0.1 with 1 / f->grains */
399     inc = painter->gain * 0.1;
400     sandp = 0;
401
402     for(i = 0; i <= 10; ++i)
403     {
404         int drawx, drawy;
405         double sp, sm;
406         double intensity = 0.1 - 0.009 * i;
407
408         sp = sin(painter->p + sandp);
409         drawx = ax + (bx - ax) * sp;
410         drawy = ay + (by - ay) * sp;
411
412         drawPoint(drawx, drawy, painter->color,
413                   intensity,
414                   dpy, window, fgc, f);
415
416         sm = sin(painter->p - sandp);
417         drawx = ax + (bx - ax) * sm;
418         drawy = ay + (by - ay) * sm;
419
420         drawPoint(drawx, drawy, painter->color,
421                   intensity,
422                   dpy, window, fgc, f);
423
424         sandp += inc;
425     }
426 }
427
428 static void build_colors(struct field *f, Display *dpy, XWindowAttributes *xgwa) 
429 {
430
431     XColor tmpcolor;
432     int i;
433     /* Count the colors in our map and assign them in a horrifically inefficient 
434      * manner but it only happens once */
435
436     for( f->numcolors = 0; 
437          rgb_colormap[f->numcolors] != NULL; 
438          ++f->numcolors )
439     {
440         ;
441     }
442
443     f->parsedcolors = (unsigned long *) calloc(f->numcolors,
444                                                sizeof(unsigned long));
445     if ( f->parsedcolors == NULL )
446     {
447         fprintf(stderr, "%s: Failed to allocate parsedcolors\n",
448                 progname);
449         exit(1);
450     }
451         
452     for(i = 0; i < f->numcolors; ++i)
453     {
454         if (!XParseColor(dpy, xgwa->colormap, 
455                          rgb_colormap[i], &tmpcolor)) 
456         {
457             fprintf(stderr, "%s: couldn't parse color %s\n", progname,
458                     rgb_colormap[i]);
459             exit(1);
460         }
461
462         if (!XAllocColor(dpy, xgwa->colormap, &tmpcolor)) 
463         {
464             fprintf(stderr, "%s: couldn't allocate color %s\n", progname,
465                     rgb_colormap[i]);
466             exit(1);
467         }
468
469         f->parsedcolors[i] = tmpcolor.pixel;
470
471     }
472 }
473
474 /* used when the window is resized */
475 static void build_img(struct field *f)
476 {
477     if (f->off_img) {
478         free(f->off_img);
479         f->off_img = NULL;
480     }
481
482     f->off_img = (unsigned long *) calloc(f->width * f->height,
483                                           sizeof(unsigned long));
484                                            
485
486     if ( f->off_img == NULL )
487     {
488         fprintf(stderr, "%s: Failed to allocate off_img\n",
489                 progname);
490         exit(1);
491     }
492
493     memset(f->off_img, f->bgcolor, 
494            sizeof(unsigned long) * f->width * f->height);
495 }
496
497 static void free_circles(struct field *f) 
498 {
499     int i;
500
501     if ( f->circles != NULL )
502     {
503         for(i = 0; i < f->num_circles; ++i)
504         {
505             free (f->circles[i].painters);
506         }
507
508         free (f->circles);
509         f->circles = NULL;
510     }
511 }
512
513 static void build_field(Display *dpy, Window window, XWindowAttributes xgwa, GC fgc, 
514                  struct field *f) 
515 {
516     int i;
517     int num_orbits;
518     int base_orbits;
519     int orbit_start;
520     build_img(f);
521
522     f->center_of_universe.x = f->width / 2.0;
523     f->center_of_universe.y = f->height / 2.0;
524     f->center_of_universe.r = MAX(f->width, f->height) / 2.0;
525
526     num_orbits = (f->percent_orbits * f->num_circles) / 100;
527     orbit_start = f->num_circles - num_orbits;
528     base_orbits = orbit_start + (num_orbits * f->base_orbits) / 100;
529
530     free_circles(f);
531
532     f->circles = (Circle*) calloc(f->num_circles, sizeof(Circle));
533     if ( f->circles == NULL )
534     {
535         fprintf(stderr, "%s: Failed to allocate off_img\n",
536                 progname);
537         exit(1);
538     }
539
540     for(i = 0; i < f->num_circles; ++i)
541     {
542         int j;
543         Circle *circle = f->circles + i;
544
545         /* make this a pref */
546
547         if ( i >= orbit_start )
548           circle->path_type = ORBIT;
549         else
550           circle->path_type = LINEAR;
551
552
553         if ( circle->path_type == LINEAR )
554         {
555             circle->x = frand(f->width);
556             circle->y = frand(f->height);
557
558             circle->dx = frand(0.5) - 0.25; 
559             circle->dy = frand(0.5) - 0.25;
560             /* circle->dy = 0; */
561             /* circle->r  = f->height * (0.05 + frand(0.1)); */
562             circle->radius = 5 + frand(55);
563
564             /* in case we want orbits based on lines */
565             circle->r = MIN(f->width, f->height) / 2.0;
566             circle->center = NULL;
567         }
568         else /* == ORBIT */
569         {
570             if (i < base_orbits )
571             {
572                 if ( f->base_on_center )
573                     circle->center = &f->center_of_universe; 
574                 else
575                 {
576                     circle->center = f->circles + 
577                         ((int)frand(orbit_start - 0.1));
578                 }
579
580                 circle->r = 1 + frand(MIN(f->width, f->height) / 2.0);
581
582                 /* circle->radius = 5 + frand(55); */
583             }
584             else
585             {
586                 /* give a preference for the earlier circles */
587
588                 double p = frand(0.9);
589
590                 circle->center = f->circles + (int) (p*i);
591
592                 circle->r = 1 + 0.5 * circle->center->r + 0.5 * frand(circle->center->r); 
593                 /* circle->r = 1 + frand(circle->center->r / 2); */
594
595
596                 /* circle->radius = MAX(5, frand(circle->r)); */
597                 /* circle->radius = 5 + frand(55); */
598             }
599
600             circle->radius = 5 + frand(MIN(55, circle->r)); 
601             circle->dtheta = (frand(0.5) - 0.25) / circle->r;
602             circle->theta = frand(2 * M_PI);
603
604             circle->x = circle->r * cos(circle->theta) + circle->center->x;
605             circle->y = circle->r * sin(circle->theta) + circle->center->y;
606
607         }
608
609         /* make this a command line option */
610         circle->num_painters = 3;
611         circle->painters = (SandPainter*) calloc(circle->num_painters, 
612                                                  sizeof(SandPainter));
613         if ( circle->painters == NULL )
614         {
615             fprintf(stderr, "%s: failed to allocate painters", progname);
616             exit(1);
617         }
618
619         for(j = 0; j < circle->num_painters; ++j)
620         {
621             SandPainter *painter = circle->painters + j;
622
623             painter->gain = frand(0.09) + 0.01;
624             painter->p = frand(1.0);
625             painter->color = 
626                 f->parsedcolors[(int)(frand(0.999) * f->numcolors)];
627         }
628     }
629 }
630
631 static void moveCircles(struct field *f)
632 {
633     int i;
634
635     for(i = 0; i < f->num_circles; ++i)
636     {
637         Circle *circle = f->circles + i;
638
639         if ( circle->path_type == LINEAR )
640         {
641             circle->x += circle->dx;
642             circle->y += circle->dy;
643
644 #if 0
645             if ( circle->x < -circle->radius )
646                 circle->x = f->width + circle->radius;
647             else if ( circle->x >= f->width + circle->radius )
648                 circle->x = -circle->radius;
649
650             if ( circle->y < -circle->radius )
651                 circle->y = f->height + circle->radius;
652             else if ( circle->y >= f->height + circle->radius )
653                 circle->y = -circle->radius;
654 #else
655             if ( circle->x < 0 ) circle->x += f->width;
656             else if ( circle->x >= f->width ) circle->x -= f->width;
657
658             if ( circle->y < 0 ) circle->y += f->height;
659             else if ( circle->y >= f->height ) circle->y -= f->height;
660 #endif
661         }
662         else /* (circle->path_type == ORBIT) */
663         {
664             circle->theta += circle->dtheta;
665
666             if ( circle->theta < 0 ) circle->theta += 2 * M_PI;
667             else if ( circle->theta > 2 * M_PI ) circle->theta -= 2 * M_PI;
668
669             circle->x = circle->r * cos(circle->theta) + circle->center->x;
670             circle->y = circle->r * sin(circle->theta) + circle->center->y;
671
672 #if 0
673             if ( circle->x < -circle->radius )
674                 circle->x += f->width + 2 * circle->radius;
675             else if ( circle->x >= f->width + circle->radius )
676                 circle->x -= f->width + 2 * circle->radius;
677
678             if ( circle->y < -circle->radius )
679                 circle->y += f->height + 2 * circle->radius;
680             else if ( circle->y >= f->height + circle->radius )
681                 circle->y -= f->height + 2 * circle->radius;
682 #else
683             if ( circle->x < 0 ) circle->x += f->width;
684             else if ( circle->x >= f->width ) circle->x -= f->width;
685
686             if ( circle->y < 0 ) circle->y += f->height;
687             else if ( circle->y >= f->height ) circle->y -= f->height;
688 #endif
689         }
690     }
691 }
692
693 static void drawIntersections(Display *dpy, Window window, GC fgc, struct field *f)
694 {
695     int i,j;
696
697     /* One might be tempted to think 'hey, this is a crude algorithm
698      * that is going to check each of the n (n-1) / 2 possible
699      * intersections!  Why not try bsp trees, quad trees, etc, etc,
700      * etc'
701      *
702      * In practice the time spent drawing the intersection of two
703      * circles dwarfs the time takes to check for intersection.
704      * Profiling on a 640x480 screen with 100 circles shows possible
705      * speed gains to be only a couple of percent.
706      * 
707      * But hey, if you're bored, go have fun.  Let me know how it
708      * turns out.
709      */
710
711
712     for(i = 0; i < f->num_circles; ++i)
713     {
714         Circle *c1 = f->circles + i;
715
716         if ( !f->draw_centers )
717         {
718             /* the default branch */
719
720             for(j = i + 1; j < f->num_circles; ++j)
721             {
722                 double d, dsqr, dx, dy;
723                 Circle *c2 = f->circles + j;
724
725 #ifdef TIME_ME
726                 ++f->possible_intersections;
727 #endif
728                 dx = c2->x - c1->x;
729                 dy = c2->y - c1->y;
730
731                 dsqr = dx * dx + dy * dy;
732                 d = sqrt(dsqr);
733
734                 if ( (fabs(dx) < (c1->radius + c2->radius)) &&
735                      (fabs(dy) < (c1->radius + c2->radius)) &&
736                      ( d < (c1->radius + c2->radius) ) &&
737                      ( d > fabs(c1->radius - c2->radius) ) )
738                 {
739                     double d1, d2, r1sqr; 
740                     double bx, by;
741                     double midpx, midpy;
742                     double int1x, int1y;
743                     double int2x, int2y;
744                     int s;
745
746                     /* woo-hoo.  the circles are neither outside nor
747                      * inside each other.  they intersect.  
748                      *
749                      * Now, compute the coordinates of the points of
750                      * intersection 
751                      */
752
753 #ifdef TIME_ME
754                     ++f->intersection_count;
755 #endif
756
757                     /* unit vector in direction of c1 to c2 */
758                     bx = dx / d;
759                     by = dy / d;
760
761                     r1sqr = c1->radius * c1->radius;
762
763                     /* distance from c1's center midpoint of intersection
764                      * points */
765
766                     d1 = 0.5 * (r1sqr - c2->radius * c2->radius + dsqr) / d;
767                 
768                     midpx = c1->x + d1 * bx;
769                     midpy = c1->y + d1 * by;
770
771                     /* distance from midpoint to points of intersection */
772
773                     d2 = sqrt(r1sqr - d1 * d1);
774
775                     int1x = midpx + d2 * by;
776                     int1y = midpy - d2 * bx;
777
778                     int2x = midpx - d2 * by;
779                     int2y = midpy + d2 * bx;
780
781                     for(s = 0; s < c1->num_painters; ++s)
782                     {
783                         paint(c1->painters + s, int1x, int1y, int2x, int2y, 
784                               dpy, window, fgc, f);
785                     }
786                 }
787             }
788         }
789         else /* f->draw_centers */
790         {
791             XDrawPoint(dpy, window, fgc, c1->x, c1->y);
792         }
793     }
794 }
795
796 struct state {
797   Display *dpy;
798   Window window;
799
800   unsigned int max_cycles;
801   int growth_delay;
802   GC fgc;
803   XGCValues gcv;
804   XWindowAttributes xgwa;
805
806   struct field *f;
807 };
808
809
810 static void *
811 interaggregate_init (Display *dpy, Window window)
812 {
813     struct state *st = (struct state *) calloc (1, sizeof(*st));
814
815 #ifdef TIME_ME
816     int frames;
817     struct timeval tm1, tm2;
818     double tdiff;
819 #endif
820
821     st->dpy = dpy;
822     st->window = window;
823     st->f = init_field();
824     st->growth_delay = (get_integer_resource(st->dpy, "growthDelay", "Integer"));
825     st->max_cycles = (get_integer_resource(st->dpy, "maxCycles", "Integer"));
826     st->f->num_circles = (get_integer_resource(st->dpy, "numCircles", "Integer"));
827     st->f->percent_orbits = (get_integer_resource(st->dpy, "percentOrbits", "Integer"));
828     st->f->base_orbits = (get_integer_resource(st->dpy, "baseOrbits", "Integer"));
829     st->f->base_on_center = (get_boolean_resource(st->dpy, "baseOnCenter", "Boolean"));
830     st->f->draw_centers = (get_boolean_resource(st->dpy, "drawCenters", "Boolean"));
831
832     if (st->f->num_circles <= 1) 
833     {
834         fprintf(stderr, "%s: Minimum number of circles is 2\n", 
835                 progname);
836         exit (1);
837     }
838
839     if ( (st->f->percent_orbits < 0) || (st->f->percent_orbits > 100) )
840     {
841         fprintf(stderr, "%s: percent-oribts must be between 0 and 100\n", 
842                 progname);
843         exit (1);
844     }
845
846     if ( (st->f->base_orbits < 0) || (st->f->base_orbits > 100) )
847     {
848         fprintf(stderr, "%s: base-oribts must be between 0 and 100\n", 
849                 progname);
850         exit (1);
851     }
852
853     if ( st->f->percent_orbits == 100 )
854         st->f->base_on_center = True;
855
856     XGetWindowAttributes(st->dpy, st->window, &st->xgwa);
857
858     build_colors(st->f, st->dpy, &st->xgwa);
859
860     st->gcv.foreground = get_pixel_resource(st->dpy, st->xgwa.colormap,
861                                         "foreground", "Foreground");
862     st->gcv.background = get_pixel_resource(st->dpy, st->xgwa.colormap,
863                                         "background", "Background");
864
865     st->fgc = XCreateGC(st->dpy, st->window, GCForeground, &st->gcv);
866
867     st->f->height = st->xgwa.height;
868     st->f->width = st->xgwa.width;
869     st->f->visdepth = st->xgwa.depth;
870     st->f->fgcolor = st->gcv.foreground;
871     st->f->bgcolor = st->gcv.background;
872
873     /* Initialize stuff */
874     build_field(st->dpy, st->window, st->xgwa, st->fgc, st->f);
875
876 #ifdef TIME_ME
877     gettimeofday(&tm1, NULL);
878     frames = 0;
879 #endif
880
881     return st;
882 }
883
884
885 static unsigned long
886 interaggregate_draw (Display *dpy, Window window, void *closure)
887 {
888   struct state *st = (struct state *) closure;
889
890   if ((st->f->cycles % 10) == 0) 
891     {
892       /* Restart if the window size changes */
893       XGetWindowAttributes(st->dpy, st->window, &st->xgwa);
894
895       if (st->f->height != st->xgwa.height || st->f->width != st->xgwa.width) 
896         {
897           st->f->height = st->xgwa.height;
898           st->f->width = st->xgwa.width;
899           st->f->visdepth = st->xgwa.depth;
900
901           build_field(st->dpy, st->window, st->xgwa, st->fgc, st->f);
902           XSetForeground(st->dpy, st->fgc, st->gcv.background);
903           XFillRectangle(st->dpy, st->window, st->fgc, 0, 0, st->xgwa.width, st->xgwa.height);
904           XSetForeground(st->dpy, st->fgc, st->gcv.foreground);
905         }
906     }
907
908   moveCircles(st->f);
909   drawIntersections(st->dpy, st->window, st->fgc, st->f);
910
911   st->f->cycles++;
912
913
914   if (st->f->cycles >= st->max_cycles && st->max_cycles != 0)
915     {
916       build_field(st->dpy, st->window, st->xgwa, st->fgc, st->f);
917       XSetForeground(st->dpy, st->fgc, st->gcv.background);
918       XFillRectangle(st->dpy, st->window, st->fgc, 0, 0, st->xgwa.width, st->xgwa.height);
919       XSetForeground(st->dpy, st->fgc, st->gcv.foreground);
920     }
921
922 #ifdef TIME_ME
923   frames++;
924   gettimeofday(&tm2, NULL);
925
926   tdiff = (tm2.tv_sec - tm1.tv_sec) 
927     + (tm2.tv_usec - tm1.tv_usec) * 0.00001;
928
929   if ( tdiff > 1 )
930     {
931       fprintf(stderr, "fps: %d %f %f\n", 
932               frames, tdiff, frames / tdiff );
933
934       fprintf(stderr, "intersections: %d %d %f\n", 
935               f->intersection_count, f->possible_intersections, 
936               ((double)f->intersection_count) / 
937               f->possible_intersections);
938
939       fprintf(stderr, "fpi: %f\n", 
940               ((double)frames) / f->intersection_count );
941
942       frames = 0;
943       tm1.tv_sec = tm2.tv_sec;
944       tm1.tv_usec = tm2.tv_usec;
945
946       f->intersection_count = f->possible_intersections = 0;
947     }
948 #endif
949
950   return st->growth_delay;
951 }
952
953
954 static void
955 interaggregate_reshape (Display *dpy, Window window, void *closure, 
956                  unsigned int w, unsigned int h)
957 {
958 }
959
960 static Bool
961 interaggregate_event (Display *dpy, Window window, void *closure, XEvent *event)
962 {
963   return False;
964 }
965
966 static void
967 interaggregate_free (Display *dpy, Window window, void *closure)
968 {
969   struct state *st = (struct state *) closure;
970   free (st);
971 }
972
973
974 XSCREENSAVER_MODULE ("Interaggregate", interaggregate)