1 /* xscreensaver, Copyright (c) 1997 Jamie Zawinski <jwz@netscape.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
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...
48 #define SCALE 10000 /* fixed-point math, for sub-pixel motion */
49 #define DEF_COUNT 12 /* When planes and count are 0, how many blobs. */
52 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
53 #define RANDSIGN() ((random() & 1) ? 1 : -1)
56 long x, y; /* position of midpoint */
57 long dx, dy; /* velocity and direction */
58 double torque; /* rotational speed */
59 double th; /* angle of rotation */
60 long elasticity; /* how fast they deform */
61 long max_velocity; /* speed limit */
62 long min_r, max_r; /* radius range */
63 int npoints; /* control points */
87 struct layer **layers;
88 unsigned long background;
96 make_blob (int maxx, int maxy, int size)
98 struct blob *b = (struct blob *) calloc(1, sizeof(*b));
109 if (b->min_r < (5*SCALE)) b->min_r = (5*SCALE);
110 mid = ((b->min_r + b->max_r) / 2);
112 b->torque = get_float_resource ("torque", "Torque");
113 b->elasticity = SCALE * get_float_resource ("elasticity", "Elasticity");
114 b->max_velocity = SCALE * get_float_resource ("maxVelocity", "MaxVelocity");
119 b->dx = RAND(b->max_velocity) * RANDSIGN();
120 b->dy = RAND(b->max_velocity) * RANDSIGN();
121 b->th = frand(M_PI+M_PI) * RANDSIGN();
122 b->npoints = (random() % 5) + 5;
124 b->spline = make_spline (b->npoints);
125 b->r = (long *) malloc (sizeof(*b->r) * b->npoints);
126 for (i = 0; i < b->npoints; i++)
127 b->r[i] = ((random() % mid) + (mid/2)) * RANDSIGN();
132 throb_blob (struct blob *b)
135 double frac = ((M_PI+M_PI) / b->npoints);
137 for (i = 0; i < b->npoints; i++)
140 long ra = (r > 0 ? r : -r);
141 double th = (b->th > 0 ? b->th : -b->th);
144 /* place control points evenly around perimiter, shifted by theta */
145 x = b->x + ra * cos (i * frac + th);
146 y = b->y + ra * sin (i * frac + th);
148 b->spline->control_x[i] = x / SCALE;
149 b->spline->control_y[i] = y / SCALE;
151 /* alter the radius by a random amount, in the direction in which
152 it had been going (the sign of the radius indicates direction.) */
153 ra += (RAND(b->elasticity) * (r > 0 ? 1 : -1));
154 r = ra * (r >= 0 ? 1 : -1);
156 /* If we've reached the end (too long or too short) reverse direction. */
157 if ((ra > b->max_r && r >= 0) ||
158 (ra < b->min_r && r < 0))
160 /* And reverse direction in mid-course once every 50 times. */
161 else if (! (random() % 50))
169 move_blob (struct blob *b, int maxx, int maxy)
177 /* If we've reached the edge of the box, reverse direction. */
178 if ((b->x > maxx && b->dx >= 0) ||
179 (b->x < 0 && b->dx < 0))
183 if ((b->y > maxy && b->dy >= 0) ||
184 (b->y < 0 && b->dy < 0))
189 /* Alter velocity randomly. */
190 if (! (random() % 10))
192 b->dx += (RAND(b->max_velocity/2) * RANDSIGN());
193 b->dy += (RAND(b->max_velocity/2) * RANDSIGN());
195 /* Throttle velocity */
196 if (b->dx > b->max_velocity || b->dx < -b->max_velocity)
198 if (b->dy > b->max_velocity || b->dy < -b->max_velocity)
204 double d = (b->torque == 0 ? 0 : frand(b->torque));
210 if (th > (M_PI+M_PI))
215 b->th = (b->th > 0 ? th : -th);
218 /* Alter direction of rotation randomly. */
219 if (! (random() % 100))
224 draw_blob (Display *dpy, Drawable drawable, GC gc, struct blob *b,
227 compute_closed_spline (b->spline);
231 for (i = 0; i < b->npoints; i++)
232 XDrawLine (dpy, drawable, gc, b->x/SCALE, b->y/SCALE,
233 b->spline->control_x[i], b->spline->control_y[i]);
237 XFillPolygon (dpy, drawable, gc, b->spline->points, b->spline->n_points,
238 Nonconvex, CoordModeOrigin);
241 XDrawLines (dpy, drawable, gc, b->spline->points, b->spline->n_points,
246 static struct layer *
247 make_layer (Display *dpy, Window window, int width, int height, int nblobs)
250 struct layer *layer = (struct layer *) calloc(1, sizeof(*layer));
251 int blob_min, blob_max;
253 layer->nblobs = nblobs;
255 layer->blobs = (struct blob **) malloc(sizeof(*layer->blobs)*layer->nblobs);
257 blob_max = (width < height ? width : height) / 2;
258 blob_min = (blob_max * 2) / 3;
259 for (i = 0; i < layer->nblobs; i++)
260 layer->blobs[i] = make_blob (width, height,
261 (random() % (blob_max-blob_min)) + blob_min);
263 layer->pixmap = XCreatePixmap (dpy, window, width, height, 1);
264 layer->gc = XCreateGC (dpy, layer->pixmap, 0, &gcv);
270 draw_layer_plane (Display *dpy, struct layer *layer, int width, int height)
273 XSetForeground (dpy, layer->gc, 1L);
274 XFillRectangle (dpy, layer->pixmap, layer->gc, 0, 0, width, height);
275 XSetForeground (dpy, layer->gc, 0L);
276 for (i = 0; i < layer->nblobs; i++)
278 throb_blob (layer->blobs[i]);
279 move_blob (layer->blobs[i], width, height);
280 draw_blob (dpy, layer->pixmap, layer->gc, layer->blobs[i], True);
286 draw_layer_blobs (Display *dpy, Drawable drawable, GC gc,
287 struct layer *layer, int width, int height,
291 for (i = 0; i < layer->nblobs; i++)
293 throb_blob (layer->blobs[i]);
294 move_blob (layer->blobs[i], width, height);
295 draw_blob (dpy, drawable, gc, layer->blobs[i], fill_p);
301 make_goop (Display *dpy, Window window, Colormap cmap,
302 int width, int height, long depth)
305 struct goop *goop = (struct goop *) calloc(1, sizeof(*goop));
307 int nblobs = get_integer_resource ("count", "Count");
309 unsigned long *plane_masks = 0;
310 unsigned long base_pixel = 0;
312 goop->mode = (get_boolean_resource("xor", "Xor")
314 : (get_boolean_resource("transparent", "Transparent")
319 goop->height = height;
322 goop->nlayers = get_integer_resource ("planes", "Planes");
323 if (goop->nlayers <= 0)
324 goop->nlayers = (random() % (depth-2)) + 2;
325 goop->layers = (struct layer **) malloc(sizeof(*goop->layers)*goop->nlayers);
328 if (mono_p && goop->mode == transparent)
331 /* Try to allocate some color planes before committing to nlayers.
333 if (goop->mode == transparent)
335 Bool additive_p = get_boolean_resource ("additive", "Additive");
336 int nplanes = goop->nlayers;
337 allocate_alpha_colors (dpy, cmap, &nplanes, additive_p, &plane_masks,
340 goop->nlayers = nplanes;
344 "%s: couldn't allocate any color planes; turning transparency off.\n",
352 int total = DEF_COUNT;
353 memset (lblobs, 0, sizeof(lblobs));
356 for (i = 0; total && i < goop->nlayers; i++)
357 lblobs[i]++, total--;
358 for (i = 0; i < goop->nlayers; i++)
359 goop->layers[i] = make_layer (dpy, window, width, height,
360 (nblobs > 0 ? nblobs : lblobs[i]));
363 if (goop->mode == transparent && plane_masks)
365 for (i = 0; i < goop->nlayers; i++)
366 goop->layers[i]->pixel = base_pixel | plane_masks[i];
367 goop->background = base_pixel;
372 if (goop->mode != transparent)
375 color.flags = DoRed|DoGreen|DoBlue;
378 get_pixel_resource ("background", "Background", dpy,cmap);
380 for (i = 0; i < goop->nlayers; i++)
382 int H = random() % 360; /* range 0-360 */
383 double S = ((double) (random()%70) + 30)/100.0; /* range 30%-100% */
384 double V = ((double) (random()%34) + 66)/100.0; /* range 66%-100% */
385 hsv_to_rgb (H, S, V, &color.red, &color.green, &color.blue);
386 if (XAllocColor (dpy, cmap, &color))
387 goop->layers[i]->pixel = color.pixel;
389 goop->layers[i]->pixel =
390 WhitePixelOfScreen(DefaultScreenOfDisplay(dpy));
394 goop->pixmap = XCreatePixmap (dpy, window, width, height,
395 (goop->mode == xor ? 1L : depth));
397 gcv.background = goop->background;
398 gcv.foreground = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
399 gcv.line_width = get_integer_resource ("thickness","Thickness");
400 goop->pixmap_gc = XCreateGC (dpy, goop->pixmap, GCLineWidth, &gcv);
401 goop->window_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
407 init_goop (Display *dpy, Window window)
409 XWindowAttributes xgwa;
410 XGetWindowAttributes (dpy, window, &xgwa);
412 return make_goop (dpy, window, xgwa.colormap,
413 xgwa.width, xgwa.height, xgwa.depth);
417 run_goop (Display *dpy, Window window, struct goop *goop)
425 for (i = 0; i < goop->nlayers; i++)
426 draw_layer_plane (dpy, goop->layers[i], goop->width, goop->height);
428 XSetForeground (dpy, goop->pixmap_gc, goop->background);
429 XSetPlaneMask (dpy, goop->pixmap_gc, AllPlanes);
430 XFillRectangle (dpy, goop->pixmap, goop->pixmap_gc, 0, 0,
431 goop->width, goop->height);
432 XSetForeground (dpy, goop->pixmap_gc, ~0L);
433 for (i = 0; i < goop->nlayers; i++)
435 XSetPlaneMask (dpy, goop->pixmap_gc, goop->layers[i]->pixel);
437 XSetForeground (dpy, goop->pixmap_gc, ~0L);
438 XFillRectangle (dpy, goop->pixmap, goop->pixmap_gc, 0, 0,
439 goop->width, goop->height);
440 XSetForeground (dpy, goop->pixmap_gc, 0L);
442 draw_layer_blobs (dpy, goop->pixmap, goop->pixmap_gc,
443 goop->layers[i], goop->width, goop->height,
446 XCopyArea (dpy, goop->pixmap, window, goop->window_gc, 0, 0,
447 goop->width, goop->height, 0, 0);
451 XSetFunction (dpy, goop->pixmap_gc, GXcopy);
452 XSetForeground (dpy, goop->pixmap_gc, 0);
453 XFillRectangle (dpy, goop->pixmap, goop->pixmap_gc, 0, 0,
454 goop->width, goop->height);
455 XSetFunction (dpy, goop->pixmap_gc, GXxor);
456 XSetForeground (dpy, goop->pixmap_gc, 1);
457 for (i = 0; i < goop->nlayers; i++)
458 draw_layer_blobs (dpy, goop->pixmap, goop->pixmap_gc,
459 goop->layers[i], goop->width, goop->height,
460 (goop->mode != outline));
461 XCopyPlane (dpy, goop->pixmap, window, goop->window_gc, 0, 0,
462 goop->width, goop->height, 0, 0, 1L);
467 XSetForeground (dpy, goop->pixmap_gc, goop->background);
468 XFillRectangle (dpy, goop->pixmap, goop->pixmap_gc, 0, 0,
469 goop->width, goop->height);
470 for (i = 0; i < goop->nlayers; i++)
472 XSetForeground (dpy, goop->pixmap_gc, goop->layers[i]->pixel);
473 draw_layer_blobs (dpy, goop->pixmap, goop->pixmap_gc,
474 goop->layers[i], goop->width, goop->height,
475 (goop->mode != outline));
477 XCopyArea (dpy, goop->pixmap, window, goop->window_gc, 0, 0,
478 goop->width, goop->height, 0, 0);
488 char *progclass = "Goop";
490 char *defaults [] = {
491 "Goop.background: black", /* to placate SGI */
492 "Goop.foreground: white",
494 "*transparent: true",
506 XrmOptionDescRec options [] = {
507 { "-delay", ".delay", XrmoptionSepArg, 0 },
508 { "-count", ".count", XrmoptionSepArg, 0 },
509 { "-planes", ".planes", XrmoptionSepArg, 0 },
510 { "-transparent", ".transparent", XrmoptionNoArg, "True" },
511 { "-non-transparent", ".transparent", XrmoptionNoArg, "False" },
512 { "-additive", ".additive", XrmoptionNoArg, "True" },
513 { "-subtractive", ".additive", XrmoptionNoArg, "false" },
514 { "-xor", ".xor", XrmoptionNoArg, "true" },
515 { "-no-xor", ".xor", XrmoptionNoArg, "false" },
516 { "-thickness", ".thickness", XrmoptionSepArg, 0 },
517 { "-torque", ".torque", XrmoptionSepArg, 0 },
518 { "-elasticity", ".elasticity", XrmoptionSepArg, 0 },
519 { "-max-velocity", ".maxVelocity", XrmoptionSepArg, 0 },
524 screenhack (Display *dpy, Window window)
526 struct goop *g = init_goop (dpy, window);
527 int delay = get_integer_resource ("delay", "Integer");
530 run_goop (dpy, window, g);
532 if (delay) usleep (delay);