1 /* xscreensaver, Copyright (c) 1997-2008 Jamie Zawinski <jwz@jwz.org>
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
13 #include "screenhack.h"
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
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
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?)
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...
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. */
51 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
52 #define RANDSIGN() ((random() & 1) ? 1 : -1)
55 long x, y; /* position of midpoint */
56 long dx, dy; /* velocity and direction */
57 double torque; /* rotational speed */
58 double th; /* angle of rotation */
59 long elasticity; /* how fast they deform */
60 long max_velocity; /* speed limit */
61 long min_r, max_r; /* radius range */
62 int npoints; /* control points */
86 struct layer **layers;
87 unsigned long background;
98 make_blob (Display *dpy, int maxx, int maxy, int size)
100 struct blob *b = (struct blob *) calloc(1, sizeof(*b));
111 if (b->min_r < (5*SCALE)) b->min_r = (5*SCALE);
112 mid = ((b->min_r + b->max_r) / 2);
114 b->torque = get_float_resource (dpy, "torque", "Torque");
115 b->elasticity = SCALE * get_float_resource (dpy, "elasticity", "Elasticity");
116 b->max_velocity = SCALE * get_float_resource (dpy, "maxVelocity", "MaxVelocity");
121 b->dx = RAND(b->max_velocity) * RANDSIGN();
122 b->dy = RAND(b->max_velocity) * RANDSIGN();
123 b->th = frand(M_PI+M_PI) * RANDSIGN();
124 b->npoints = (random() % 5) + 5;
126 b->spline = make_spline (b->npoints);
127 b->r = (long *) malloc (sizeof(*b->r) * b->npoints);
128 for (i = 0; i < b->npoints; i++)
129 b->r[i] = ((random() % mid) + (mid/2)) * RANDSIGN();
134 throb_blob (struct blob *b)
137 double frac = ((M_PI+M_PI) / b->npoints);
139 for (i = 0; i < b->npoints; i++)
142 long ra = (r > 0 ? r : -r);
143 double th = (b->th > 0 ? b->th : -b->th);
146 /* place control points evenly around perimiter, shifted by theta */
147 x = b->x + ra * cos (i * frac + th);
148 y = b->y + ra * sin (i * frac + th);
150 b->spline->control_x[i] = x / SCALE;
151 b->spline->control_y[i] = y / SCALE;
153 /* alter the radius by a random amount, in the direction in which
154 it had been going (the sign of the radius indicates direction.) */
155 ra += (RAND(b->elasticity) * (r > 0 ? 1 : -1));
156 r = ra * (r >= 0 ? 1 : -1);
158 /* If we've reached the end (too long or too short) reverse direction. */
159 if ((ra > b->max_r && r >= 0) ||
160 (ra < b->min_r && r < 0))
162 /* And reverse direction in mid-course once every 50 times. */
163 else if (! (random() % 50))
171 move_blob (struct blob *b, int maxx, int maxy)
179 /* If we've reached the edge of the box, reverse direction. */
180 if ((b->x > maxx && b->dx >= 0) ||
181 (b->x < 0 && b->dx < 0))
185 if ((b->y > maxy && b->dy >= 0) ||
186 (b->y < 0 && b->dy < 0))
191 /* Alter velocity randomly. */
192 if (! (random() % 10))
194 b->dx += (RAND(b->max_velocity/2) * RANDSIGN());
195 b->dy += (RAND(b->max_velocity/2) * RANDSIGN());
197 /* Throttle velocity */
198 if (b->dx > b->max_velocity || b->dx < -b->max_velocity)
200 if (b->dy > b->max_velocity || b->dy < -b->max_velocity)
206 double d = (b->torque == 0 ? 0 : frand(b->torque));
212 if (th > (M_PI+M_PI))
217 b->th = (b->th > 0 ? th : -th);
220 /* Alter direction of rotation randomly. */
221 if (! (random() % 100))
226 draw_blob (Display *dpy, Drawable drawable, GC gc, struct blob *b,
229 compute_closed_spline (b->spline);
233 for (i = 0; i < b->npoints; i++)
234 XDrawLine (dpy, drawable, gc, b->x/SCALE, b->y/SCALE,
235 b->spline->control_x[i], b->spline->control_y[i]);
239 XFillPolygon (dpy, drawable, gc, b->spline->points, b->spline->n_points,
240 Nonconvex, CoordModeOrigin);
243 XDrawLines (dpy, drawable, gc, b->spline->points, b->spline->n_points,
248 static struct layer *
249 make_layer (Display *dpy, Window window, int width, int height, int nblobs)
252 struct layer *layer = (struct layer *) calloc(1, sizeof(*layer));
253 int blob_min, blob_max;
255 layer->nblobs = nblobs;
257 layer->blobs = (struct blob **) malloc(sizeof(*layer->blobs)*layer->nblobs);
259 blob_max = (width < height ? width : height) / 2;
260 blob_min = (blob_max * 2) / 3;
261 for (i = 0; i < layer->nblobs; i++){
262 int j = blob_max - blob_min;
263 layer->blobs[i] = make_blob (dpy, width, height,
264 (j ? random() % j : 0) + blob_min);
267 layer->pixmap = XCreatePixmap (dpy, window, width, height, 1);
268 layer->gc = XCreateGC (dpy, layer->pixmap, 0, &gcv);
271 jwxyz_XSetAlphaAllowed (dpy, layer->gc, True);
272 # endif /* HAVE_COCOA */
280 draw_layer_plane (Display *dpy, struct layer *layer, int width, int height)
283 for (i = 0; i < layer->nblobs; i++)
285 throb_blob (layer->blobs[i]);
286 move_blob (layer->blobs[i], width, height);
287 draw_blob (dpy, layer->pixmap, layer->gc, layer->blobs[i], True);
290 #endif /* !HAVE_COCOA */
294 draw_layer_blobs (Display *dpy, Drawable drawable, GC gc,
295 struct layer *layer, int width, int height,
299 for (i = 0; i < layer->nblobs; i++)
301 draw_blob (dpy, drawable, gc, layer->blobs[i], fill_p);
302 throb_blob (layer->blobs[i]);
303 move_blob (layer->blobs[i], width, height);
309 make_goop (Screen *screen, Visual *visual, Window window, Colormap cmap,
310 int width, int height, long depth)
312 Display *dpy = DisplayOfScreen (screen);
314 struct goop *goop = (struct goop *) calloc(1, sizeof(*goop));
316 int nblobs = get_integer_resource (dpy, "count", "Count");
318 unsigned long *plane_masks = 0;
320 unsigned long base_pixel = 0;
324 s = get_string_resource (dpy, "mode", "Mode");
325 goop->mode = transparent;
326 if (!s || !*s || !strcasecmp (s, "transparent"))
328 else if (!strcasecmp (s, "opaque"))
330 else if (!strcasecmp (s, "xor"))
333 fprintf (stderr, "%s: bogus mode: \"%s\"\n", progname, s);
335 goop->delay = get_integer_resource (dpy, "delay", "Integer");
338 goop->height = height;
340 goop->nlayers = get_integer_resource (dpy, "planes", "Planes");
341 if (goop->nlayers <= 0)
342 goop->nlayers = (random() % (depth-2)) + 2;
343 goop->layers = (struct layer **) malloc(sizeof(*goop->layers)*goop->nlayers);
345 goop->additive_p = get_boolean_resource (dpy, "additive", "Additive");
346 goop->cmap_p = has_writable_cells (screen, visual);
348 if (mono_p && goop->mode == transparent)
352 /* Try to allocate some color planes before committing to nlayers.
354 if (goop->mode == transparent)
356 int nplanes = goop->nlayers;
357 allocate_alpha_colors (screen, visual, cmap,
358 &nplanes, goop->additive_p, &plane_masks,
361 goop->nlayers = nplanes;
365 "%s: couldn't allocate any color planes; turning transparency off.\n",
370 # endif /* !HAVE_COCOA */
374 int total = DEF_COUNT;
375 memset (lblobs, 0, sizeof(lblobs));
378 for (i = 0; total && i < goop->nlayers; i++)
379 lblobs[i]++, total--;
380 for (i = 0; i < goop->nlayers; i++)
381 goop->layers[i] = make_layer (dpy, window, width, height,
382 (nblobs > 0 ? nblobs : lblobs[i]));
386 if (goop->mode == transparent && plane_masks)
388 for (i = 0; i < goop->nlayers; i++)
389 goop->layers[i]->pixel = base_pixel | plane_masks[i];
390 goop->background = base_pixel;
392 # endif /* !HAVE_COCOA */
398 if (goop->mode != transparent)
399 # endif /* !HAVE_COCOA */
402 color.flags = DoRed|DoGreen|DoBlue;
405 get_pixel_resource (dpy,cmap, "background", "Background");
407 for (i = 0; i < goop->nlayers; i++)
409 int H = random() % 360; /* range 0-360 */
410 double S = ((double) (random()%70) + 30)/100.0; /* range 30%-100% */
411 double V = ((double) (random()%34) + 66)/100.0; /* range 66%-100% */
412 hsv_to_rgb (H, S, V, &color.red, &color.green, &color.blue);
413 if (XAllocColor (dpy, cmap, &color))
414 goop->layers[i]->pixel = color.pixel;
416 goop->layers[i]->pixel =
417 WhitePixelOfScreen(DefaultScreenOfDisplay(dpy));
419 if (goop->mode == transparent)
421 /* give a non-opaque alpha to the color */
422 unsigned long pixel = goop->layers[i]->pixel;
423 unsigned long amask = BlackPixelOfScreen (0);
424 unsigned long a = (0xBBBBBBBB & amask);
425 pixel = (pixel & (~amask)) | a;
426 goop->layers[i]->pixel = pixel;
428 # endif /* HAVE_COCOA */
432 goop->pixmap = XCreatePixmap (dpy, window, width, height,
433 (goop->mode == xor ? 1L : depth));
435 gcv.background = goop->background;
436 gcv.foreground = get_pixel_resource (dpy, cmap, "foreground", "Foreground");
437 gcv.line_width = get_integer_resource (dpy, "thickness","Thickness");
438 goop->pixmap_gc = XCreateGC (dpy, goop->pixmap, GCLineWidth, &gcv);
439 goop->window_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
442 jwxyz_XSetAlphaAllowed (dpy, goop->pixmap_gc, True);
443 # endif /* HAVE_COCOA */
449 goop_init (Display *dpy, Window window)
451 XWindowAttributes xgwa;
452 XGetWindowAttributes (dpy, window, &xgwa);
453 return make_goop (xgwa.screen, xgwa.visual, window, xgwa.colormap,
454 xgwa.width, xgwa.height, xgwa.depth);
458 goop_draw (Display *dpy, Window window, void *closure)
460 struct goop *goop = (struct goop *) closure;
468 for (i = 0; i < goop->nlayers; i++)
469 draw_layer_plane (dpy, goop->layers[i], goop->width, goop->height);
471 XSetForeground (dpy, goop->pixmap_gc, goop->background);
472 XSetFunction (dpy, goop->pixmap_gc, GXcopy);
473 XSetPlaneMask (dpy, goop->pixmap_gc, AllPlanes);
474 XFillRectangle (dpy, goop->pixmap, goop->pixmap_gc, 0, 0,
475 goop->width, goop->height);
477 XSetForeground (dpy, goop->pixmap_gc, ~0L);
479 if (!goop->cmap_p && !goop->additive_p)
482 for (i = 0; i < goop->nlayers; i++)
483 for (j = 0; j < goop->layers[i]->nblobs; j++)
484 draw_blob (dpy, goop->pixmap, goop->pixmap_gc,
485 goop->layers[i]->blobs[j], True);
486 XSetFunction (dpy, goop->pixmap_gc, GXclear);
489 for (i = 0; i < goop->nlayers; i++)
491 XSetPlaneMask (dpy, goop->pixmap_gc, goop->layers[i]->pixel);
492 draw_layer_blobs (dpy, goop->pixmap, goop->pixmap_gc,
493 goop->layers[i], goop->width, goop->height,
496 XCopyArea (dpy, goop->pixmap, window, goop->window_gc, 0, 0,
497 goop->width, goop->height, 0, 0);
499 #endif /* !HAVE_COCOA */
502 XSetFunction (dpy, goop->pixmap_gc, GXcopy);
503 XSetForeground (dpy, goop->pixmap_gc, 0);
504 XFillRectangle (dpy, goop->pixmap, goop->pixmap_gc, 0, 0,
505 goop->width, goop->height);
506 XSetFunction (dpy, goop->pixmap_gc, GXxor);
507 XSetForeground (dpy, goop->pixmap_gc, 1);
508 for (i = 0; i < goop->nlayers; i++)
509 draw_layer_blobs (dpy, goop->pixmap, goop->pixmap_gc,
510 goop->layers[i], goop->width, goop->height,
511 (goop->mode != outline));
512 XCopyPlane (dpy, goop->pixmap, window, goop->window_gc, 0, 0,
513 goop->width, goop->height, 0, 0, 1L);
521 XSetForeground (dpy, goop->pixmap_gc, goop->background);
522 XFillRectangle (dpy, goop->pixmap, goop->pixmap_gc, 0, 0,
523 goop->width, goop->height);
524 for (i = 0; i < goop->nlayers; i++)
526 XSetForeground (dpy, goop->pixmap_gc, goop->layers[i]->pixel);
527 draw_layer_blobs (dpy, goop->pixmap, goop->pixmap_gc,
528 goop->layers[i], goop->width, goop->height,
529 (goop->mode != outline));
531 XCopyArea (dpy, goop->pixmap, window, goop->window_gc, 0, 0,
532 goop->width, goop->height, 0, 0);
543 goop_reshape (Display *dpy, Window window, void *closure,
544 unsigned int w, unsigned int h)
550 goop_event (Display *dpy, Window window, void *closure, XEvent *event)
556 goop_free (Display *dpy, Window window, void *closure)
563 static const char *goop_defaults [] = {
564 ".background: black",
565 ".foreground: yellow",
568 "*mode: transparent",
578 static XrmOptionDescRec goop_options [] = {
579 { "-delay", ".delay", XrmoptionSepArg, 0 },
580 { "-count", ".count", XrmoptionSepArg, 0 },
581 { "-planes", ".planes", XrmoptionSepArg, 0 },
582 { "-mode", ".mode", XrmoptionSepArg, 0 },
583 { "-xor", ".mode", XrmoptionNoArg, "xor" },
584 { "-transparent", ".mode", XrmoptionNoArg, "transparent" },
585 { "-opaque", ".mode", XrmoptionNoArg, "opaque" },
586 { "-additive", ".additive", XrmoptionNoArg, "True" },
587 { "-subtractive", ".additive", XrmoptionNoArg, "false" },
588 { "-thickness", ".thickness", XrmoptionSepArg, 0 },
589 { "-torque", ".torque", XrmoptionSepArg, 0 },
590 { "-elasticity", ".elasticity", XrmoptionSepArg, 0 },
591 { "-max-velocity", ".maxVelocity", XrmoptionSepArg, 0 },
595 XSCREENSAVER_MODULE ("Goop", goop)