http://www.jwz.org/xscreensaver/xscreensaver-5.09.tar.gz
[xscreensaver] / hacks / goop.c
1 /* xscreensaver, Copyright (c) 1997-2008 Jamie Zawinski <jwz@jwz.org>
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
12 #include <math.h>
13 #include "screenhack.h"
14 #include "spline.h"
15 #include "alpha.h"
16
17
18 /* This is pretty compute-intensive, probably due to the large number of
19    polygon fills.  I tried introducing a scaling factor to make the spline
20    code emit fewer line segments, but that made the edges very rough.
21    However, tuning *maxVelocity, *elasticity and *delay can result in much
22    smoother looking animation.  I tuned these for a 1280x1024 Indy display,
23    but I don't know whether these values will be reasonable for a slower
24    machine...
25
26    The more planes the better -- SGIs have a 12-bit pseudocolor display
27    (4096 colormap cells) which is mostly useless, except for this program,
28    where it means you can have 11 or 12 mutually-transparent objects instead
29    of only 7 or 8.  But, if you are using the 12-bit visual, you should crank
30    down the velocity and elasticity, or server slowness will cause the
31    animation to look jerky (yes, it's sad but true, SGI's X server is
32    perceptibly slower when using plane masks on a 12-bit visual than on an
33    8-bit visual.)  Using -max-velocity 0.5 -elasticity 0.9 seems to work ok
34    on my Indy R5k with visual 0x27 and the bottom-of-the-line 24-bit graphics
35    board.
36
37    It might look better if each blob had an outline, which was a *slightly*
38    darker color than the center, to give them a bit more definition -- but
39    that would mean using two planes per blob.  (Or maybe allocating the
40    outline colors outside of the plane-space?  Then the outlines wouldn't be
41    transparent, but maybe that wouldn't be so noticeable?)
42
43    Oh, for an alpha channel... maybe I should rewrite this in GL.  Then the
44    blobs could have thickness, and curved edges with specular reflections...
45  */
46
47 #define SCALE       10000  /* fixed-point math, for sub-pixel motion */
48 #define DEF_COUNT   12     /* When planes and count are 0, how many blobs. */
49
50 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
51 #define RANDSIGN() ((random() & 1) ? 1 : -1)
52
53 struct blob {
54   long x, y;            /* position of midpoint */
55   long dx, dy;          /* velocity and direction */
56   double torque;        /* rotational speed */
57   double th;            /* angle of rotation */
58   long elasticity;      /* how fast they deform */
59   long max_velocity;    /* speed limit */
60   long min_r, max_r;    /* radius range */
61   int npoints;          /* control points */
62   long *r;              /* radii */
63   spline *spline;
64 };
65
66 struct layer {
67   int nblobs;
68   struct blob **blobs;
69   Pixmap pixmap;
70   unsigned long pixel;
71   GC gc;
72 };
73
74 enum goop_mode {
75   transparent,
76   opaque,
77   xor,
78   outline
79 };
80
81 struct goop {
82   enum goop_mode mode;
83   int width, height;
84   int nlayers;
85   struct layer **layers;
86   unsigned long background;
87   Pixmap pixmap;
88   GC pixmap_gc;
89   GC window_gc;
90   Bool additive_p;
91   Bool cmap_p;
92   int delay;
93 };
94
95
96 static struct blob *
97 make_blob (Display *dpy, int maxx, int maxy, int size)
98 {
99   struct blob *b = (struct blob *) calloc(1, sizeof(*b));
100   int i;
101   int mid;
102
103   maxx *= SCALE;
104   maxy *= SCALE;
105   size *= SCALE;
106
107   b->max_r = size/2;
108   b->min_r = size/10;
109
110   if (b->min_r < (5*SCALE)) b->min_r = (5*SCALE);
111   mid = ((b->min_r + b->max_r) / 2);
112
113   b->torque       = get_float_resource (dpy, "torque", "Torque");
114   b->elasticity   = SCALE * get_float_resource (dpy, "elasticity", "Elasticity");
115   b->max_velocity = SCALE * get_float_resource (dpy, "maxVelocity", "MaxVelocity");
116
117   b->x = RAND(maxx);
118   b->y = RAND(maxy);
119
120   b->dx = RAND(b->max_velocity) * RANDSIGN();
121   b->dy = RAND(b->max_velocity) * RANDSIGN();
122   b->th = frand(M_PI+M_PI) * RANDSIGN();
123   b->npoints = (random() % 5) + 5;
124
125   b->spline = make_spline (b->npoints);
126   b->r = (long *) malloc (sizeof(*b->r) * b->npoints);
127   for (i = 0; i < b->npoints; i++)
128     b->r[i] = (long) ((random() % mid) + (mid/2)) * RANDSIGN();
129   return b;
130 }
131
132 static void 
133 throb_blob (struct blob *b)
134 {
135   int i;
136   double frac = ((M_PI+M_PI) / b->npoints);
137
138   for (i = 0; i < b->npoints; i++)
139     {
140       long r = b->r[i];
141       long ra = (r > 0 ? r : -r);
142       double th = (b->th > 0 ? b->th : -b->th);
143       long x, y;
144
145       /* place control points evenly around perimiter, shifted by theta */
146       x = b->x + ra * cos (i * frac + th);
147       y = b->y + ra * sin (i * frac + th);
148
149       b->spline->control_x[i] = x / SCALE;
150       b->spline->control_y[i] = y / SCALE;
151
152       /* alter the radius by a random amount, in the direction in which
153          it had been going (the sign of the radius indicates direction.) */
154       ra += (RAND(b->elasticity) * (r > 0 ? 1 : -1));
155       r = ra * (r >= 0 ? 1 : -1);
156
157       /* If we've reached the end (too long or too short) reverse direction. */
158       if ((ra > b->max_r && r >= 0) ||
159           (ra < b->min_r && r < 0))
160         r = -r;
161       /* And reverse direction in mid-course once every 50 times. */
162       else if (! (random() % 50))
163         r = -r;
164
165       b->r[i] = r;
166     }
167 }
168
169 static void
170 move_blob (struct blob *b, int maxx, int maxy)
171 {
172   maxx *= SCALE;
173   maxy *= SCALE;
174
175   b->x += b->dx;
176   b->y += b->dy;
177
178   /* If we've reached the edge of the box, reverse direction. */
179   if ((b->x > maxx && b->dx >= 0) ||
180       (b->x < 0    && b->dx < 0))
181     {
182       b->dx = -b->dx;
183     }
184   if ((b->y > maxy && b->dy >= 0) ||
185       (b->y < 0    && b->dy < 0))
186     {
187       b->dy = -b->dy;
188     }
189
190   /* Alter velocity randomly. */
191   if (! (random() % 10))
192     {
193       b->dx += (RAND(b->max_velocity/2) * RANDSIGN());
194       b->dy += (RAND(b->max_velocity/2) * RANDSIGN());
195
196       /* Throttle velocity */
197       if (b->dx > b->max_velocity || b->dx < -b->max_velocity)
198         b->dx /= 2;
199       if (b->dy > b->max_velocity || b->dy < -b->max_velocity)
200         b->dy /= 2;
201     }
202
203   {
204     double th = b->th;
205     double d = (b->torque == 0 ? 0 : frand(b->torque));
206     if (th < 0)
207       th = -(th + d);
208     else
209       th += d;
210
211     if (th > (M_PI+M_PI))
212       th -= (M_PI+M_PI);
213     else if (th < 0)
214       th += (M_PI+M_PI);
215
216     b->th = (b->th > 0 ? th : -th);
217   }
218
219   /* Alter direction of rotation randomly. */
220   if (! (random() % 100))
221     b->th *= -1;
222 }
223
224 static void
225 draw_blob (Display *dpy, Drawable drawable, GC gc, struct blob *b,
226            Bool fill_p)
227 {
228   compute_closed_spline (b->spline);
229 #ifdef DEBUG
230   {
231     int i;
232     for (i = 0; i < b->npoints; i++)
233       XDrawLine (dpy, drawable, gc, b->x/SCALE, b->y/SCALE,
234                  b->spline->control_x[i], b->spline->control_y[i]);
235   }
236 #else
237   if (fill_p)
238     XFillPolygon (dpy, drawable, gc, b->spline->points, b->spline->n_points,
239                   Nonconvex, CoordModeOrigin);
240   else
241 #endif
242     XDrawLines (dpy, drawable, gc, b->spline->points, b->spline->n_points,
243                 CoordModeOrigin);
244 }
245
246
247 static struct layer *
248 make_layer (Display *dpy, Window window, int width, int height, int nblobs)
249 {
250   int i;
251   struct layer *layer = (struct layer *) calloc(1, sizeof(*layer));
252   int blob_min, blob_max;
253   XGCValues gcv;
254   layer->nblobs = nblobs;
255
256   layer->blobs = (struct blob **) malloc(sizeof(*layer->blobs)*layer->nblobs);
257
258   blob_max = (width < height ? width : height) / 2;
259   blob_min = (blob_max * 2) / 3;
260   for (i = 0; i < layer->nblobs; i++){
261     int j = blob_max - blob_min;
262     layer->blobs[i] = make_blob (dpy, width, height,
263                                  (j ? random() % j : 0) + blob_min);
264   }
265
266   layer->pixmap = XCreatePixmap (dpy, window, width, height, 1);
267   layer->gc = XCreateGC (dpy, layer->pixmap, 0, &gcv);
268
269 # ifdef HAVE_COCOA
270   jwxyz_XSetAlphaAllowed (dpy, layer->gc, True);
271 # endif /* HAVE_COCOA */
272
273   return layer;
274 }
275
276
277 #ifndef HAVE_COCOA
278 static void
279 draw_layer_plane (Display *dpy, struct layer *layer, int width, int height)
280 {
281   int i;
282   for (i = 0; i < layer->nblobs; i++)
283     {
284       throb_blob (layer->blobs[i]);
285       move_blob (layer->blobs[i], width, height);
286       draw_blob (dpy, layer->pixmap, layer->gc, layer->blobs[i], True);
287     }
288 }
289 #endif /* !HAVE_COCOA */
290
291
292 static void
293 draw_layer_blobs (Display *dpy, Drawable drawable, GC gc,
294                   struct layer *layer, int width, int height,
295                   Bool fill_p)
296 {
297   int i;
298   for (i = 0; i < layer->nblobs; i++)
299     {
300       draw_blob (dpy, drawable, gc, layer->blobs[i], fill_p);
301       throb_blob (layer->blobs[i]);
302       move_blob (layer->blobs[i], width, height);
303     }
304 }
305
306
307 static struct goop *
308 make_goop (Screen *screen, Visual *visual, Window window, Colormap cmap,
309            int width, int height, long depth)
310 {
311   Display *dpy = DisplayOfScreen (screen);
312   int i;
313   struct goop *goop = (struct goop *) calloc(1, sizeof(*goop));
314   XGCValues gcv;
315   int nblobs = get_integer_resource (dpy, "count", "Count");
316
317   unsigned long *plane_masks = 0;
318 # ifndef HAVE_COCOA
319   unsigned long base_pixel = 0;
320 # endif
321   char *s;
322
323   s = get_string_resource (dpy, "mode", "Mode");
324   goop->mode = transparent;
325   if (!s || !*s || !strcasecmp (s, "transparent"))
326     ;
327   else if (!strcasecmp (s, "opaque"))
328     goop->mode = opaque;
329   else if (!strcasecmp (s, "xor"))
330     goop->mode = xor;
331   else
332     fprintf (stderr, "%s: bogus mode: \"%s\"\n", progname, s);
333
334   goop->delay = get_integer_resource (dpy, "delay", "Integer");
335
336   goop->width = width;
337   goop->height = height;
338
339   goop->nlayers = get_integer_resource (dpy, "planes", "Planes");
340   if (goop->nlayers <= 0)
341     goop->nlayers = (random() % (depth-2)) + 2;
342   goop->layers = (struct layer **) malloc(sizeof(*goop->layers)*goop->nlayers);
343
344   goop->additive_p = get_boolean_resource (dpy, "additive", "Additive");
345   goop->cmap_p = has_writable_cells (screen, visual);
346
347   if (mono_p && goop->mode == transparent)
348     goop->mode = opaque;
349
350 # ifndef HAVE_COCOA
351   /* Try to allocate some color planes before committing to nlayers.
352    */
353   if (goop->mode == transparent)
354     {
355       int nplanes = goop->nlayers;
356       allocate_alpha_colors (screen, visual, cmap,
357                              &nplanes, goop->additive_p, &plane_masks,
358                              &base_pixel);
359       if (nplanes > 1)
360         goop->nlayers = nplanes;
361       else
362         {
363           fprintf (stderr,
364          "%s: couldn't allocate any color planes; turning transparency off.\n",
365                    progname);
366           goop->mode = opaque;
367         }
368     }
369 # endif /* !HAVE_COCOA */
370
371   {
372     int lblobs[32];
373     int total = DEF_COUNT;
374     memset (lblobs, 0, sizeof(lblobs));
375     if (nblobs <= 0)
376       while (total)
377         for (i = 0; total && i < goop->nlayers; i++)
378           lblobs[i]++, total--;
379     for (i = 0; i < goop->nlayers; i++)
380       goop->layers[i] = make_layer (dpy, window, width, height, 
381                                     (nblobs > 0 ? nblobs : lblobs[i]));
382   }
383
384 # ifndef HAVE_COCOA
385   if (goop->mode == transparent && plane_masks)
386     {
387       for (i = 0; i < goop->nlayers; i++)
388         goop->layers[i]->pixel = base_pixel | plane_masks[i];
389       goop->background = base_pixel;
390     }
391 # endif /* !HAVE_COCOA */
392
393   if (plane_masks)
394     free (plane_masks);
395
396 # ifndef HAVE_COCOA
397   if (goop->mode != transparent)
398 # endif /* !HAVE_COCOA */
399     {
400       XColor color;
401       color.flags = DoRed|DoGreen|DoBlue;
402
403       goop->background =
404         get_pixel_resource (dpy,cmap, "background", "Background");
405
406       for (i = 0; i < goop->nlayers; i++)
407         {
408           int H = random() % 360;                          /* range 0-360    */
409           double S = ((double) (random()%70) + 30)/100.0;  /* range 30%-100% */
410           double V = ((double) (random()%34) + 66)/100.0;  /* range 66%-100% */
411           hsv_to_rgb (H, S, V, &color.red, &color.green, &color.blue);
412           if (XAllocColor (dpy, cmap, &color))
413             goop->layers[i]->pixel = color.pixel;
414           else
415             goop->layers[i]->pixel =
416               WhitePixelOfScreen(DefaultScreenOfDisplay(dpy));
417 # ifdef HAVE_COCOA
418           if (goop->mode == transparent)
419             {
420               /* give a non-opaque alpha to the color */
421               unsigned long pixel = goop->layers[i]->pixel;
422               unsigned long amask = BlackPixelOfScreen (0);
423               unsigned long a = (0xBBBBBBBB & amask);
424               pixel = (pixel & (~amask)) | a;
425               goop->layers[i]->pixel = pixel;
426             }
427 # endif /* HAVE_COCOA */
428         }
429     }
430
431   goop->pixmap = XCreatePixmap (dpy, window, width, height,
432                                 (goop->mode == xor ? 1L : depth));
433
434   gcv.background = goop->background;
435   gcv.foreground = get_pixel_resource (dpy, cmap, "foreground", "Foreground");
436   gcv.line_width = get_integer_resource (dpy, "thickness","Thickness");
437   goop->pixmap_gc = XCreateGC (dpy, goop->pixmap, GCLineWidth, &gcv);
438   goop->window_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
439
440 # ifdef HAVE_COCOA
441   jwxyz_XSetAlphaAllowed (dpy, goop->pixmap_gc, True);
442 # endif /* HAVE_COCOA */
443   
444   return goop;
445 }
446
447 static void *
448 goop_init (Display *dpy, Window window)
449 {
450   XWindowAttributes xgwa;
451   XGetWindowAttributes (dpy, window, &xgwa);
452   return make_goop (xgwa.screen, xgwa.visual, window, xgwa.colormap,
453                     xgwa.width, xgwa.height, xgwa.depth);
454 }
455
456 static unsigned long
457 goop_draw (Display *dpy, Window window, void *closure)
458 {
459   struct goop *goop = (struct goop *) closure;
460   int i;
461
462   switch (goop->mode)
463     {
464 # ifndef HAVE_COCOA
465     case transparent:
466
467       for (i = 0; i < goop->nlayers; i++)
468         draw_layer_plane (dpy, goop->layers[i], goop->width, goop->height);
469
470       XSetForeground (dpy, goop->pixmap_gc, goop->background);
471       XSetFunction (dpy, goop->pixmap_gc, GXcopy);
472       XSetPlaneMask (dpy, goop->pixmap_gc, AllPlanes);
473       XFillRectangle (dpy, goop->pixmap, goop->pixmap_gc, 0, 0,
474                       goop->width, goop->height);
475
476       XSetForeground (dpy, goop->pixmap_gc, ~0L);
477
478       if (!goop->cmap_p && !goop->additive_p)
479         {
480           int j;
481           for (i = 0; i < goop->nlayers; i++)
482             for (j = 0; j < goop->layers[i]->nblobs; j++)
483               draw_blob (dpy, goop->pixmap, goop->pixmap_gc,
484                          goop->layers[i]->blobs[j], True);
485           XSetFunction (dpy, goop->pixmap_gc, GXclear);
486         }
487
488       for (i = 0; i < goop->nlayers; i++)
489         {
490           XSetPlaneMask (dpy, goop->pixmap_gc, goop->layers[i]->pixel);
491           draw_layer_blobs (dpy, goop->pixmap, goop->pixmap_gc,
492                             goop->layers[i], goop->width, goop->height,
493                             True);
494         }
495       XCopyArea (dpy, goop->pixmap, window, goop->window_gc, 0, 0,
496                  goop->width, goop->height, 0, 0);
497       break;
498 #endif /* !HAVE_COCOA */
499
500     case xor:
501       XSetFunction (dpy, goop->pixmap_gc, GXcopy);
502       XSetForeground (dpy, goop->pixmap_gc, 0);
503       XFillRectangle (dpy, goop->pixmap, goop->pixmap_gc, 0, 0,
504                       goop->width, goop->height);
505       XSetFunction (dpy, goop->pixmap_gc, GXxor);
506       XSetForeground (dpy, goop->pixmap_gc, 1);
507       for (i = 0; i < goop->nlayers; i++)
508         draw_layer_blobs (dpy, goop->pixmap, goop->pixmap_gc,
509                           goop->layers[i], goop->width, goop->height,
510                           (goop->mode != outline));
511       XCopyPlane (dpy, goop->pixmap, window, goop->window_gc, 0, 0,
512                   goop->width, goop->height, 0, 0, 1L);
513       break;
514
515 # ifdef HAVE_COCOA
516     case transparent:
517 # endif
518     case opaque:
519     case outline:
520       XSetForeground (dpy, goop->pixmap_gc, goop->background);
521       XFillRectangle (dpy, goop->pixmap, goop->pixmap_gc, 0, 0,
522                       goop->width, goop->height);
523       for (i = 0; i < goop->nlayers; i++)
524         {
525           XSetForeground (dpy, goop->pixmap_gc, goop->layers[i]->pixel);
526           draw_layer_blobs (dpy, goop->pixmap, goop->pixmap_gc,
527                             goop->layers[i], goop->width, goop->height,
528                             (goop->mode != outline));
529         }
530       XCopyArea (dpy, goop->pixmap, window, goop->window_gc, 0, 0,
531                  goop->width, goop->height, 0, 0);
532       break;
533
534     default:
535       abort ();
536       break;
537     }
538   return goop->delay;
539 }
540
541 static void
542 goop_reshape (Display *dpy, Window window, void *closure, 
543                  unsigned int w, unsigned int h)
544 {
545   /* #### write me */
546 }
547
548 static Bool
549 goop_event (Display *dpy, Window window, void *closure, XEvent *event)
550 {
551   return False;
552 }
553
554 static void
555 goop_free (Display *dpy, Window window, void *closure)
556 {
557 }
558
559
560 \f
561
562 static const char *goop_defaults [] = {
563   ".background:         black",
564   ".foreground:         yellow",
565   "*delay:              12000",
566   "*additive:           true",
567   "*mode:               transparent",
568   "*count:              1",
569   "*planes:             12",
570   "*thickness:          5",
571   "*torque:             0.0075",
572   "*elasticity:         0.9",
573   "*maxVelocity:        0.5",
574   0
575 };
576
577 static XrmOptionDescRec goop_options [] = {
578   { "-delay",           ".delay",       XrmoptionSepArg, 0 },
579   { "-count",           ".count",       XrmoptionSepArg, 0 },
580   { "-planes",          ".planes",      XrmoptionSepArg, 0 },
581   { "-mode",            ".mode",        XrmoptionSepArg, 0 },
582   { "-xor",             ".mode",        XrmoptionNoArg, "xor" },
583   { "-transparent",     ".mode",        XrmoptionNoArg, "transparent" },
584   { "-opaque",          ".mode",        XrmoptionNoArg, "opaque" },
585   { "-additive",        ".additive",    XrmoptionNoArg, "True" },
586   { "-subtractive",     ".additive",    XrmoptionNoArg, "false" },
587   { "-thickness",       ".thickness",   XrmoptionSepArg, 0 },
588   { "-torque",          ".torque",      XrmoptionSepArg, 0 },
589   { "-elasticity",      ".elasticity",  XrmoptionSepArg, 0 },
590   { "-max-velocity",    ".maxVelocity", XrmoptionSepArg, 0 },
591   { 0, 0, 0, 0 }
592 };
593
594 XSCREENSAVER_MODULE ("Goop", goop)