1 /* xscreensaver, Copyright (c) 1997 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...
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;
98 make_blob (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 ("torque", "Torque");
115 b->elasticity = SCALE * get_float_resource ("elasticity", "Elasticity");
116 b->max_velocity = SCALE * get_float_resource ("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 layer->blobs[i] = make_blob (width, height,
263 (random() % (blob_max-blob_min)) + blob_min);
265 layer->pixmap = XCreatePixmap (dpy, window, width, height, 1);
266 layer->gc = XCreateGC (dpy, layer->pixmap, 0, &gcv);
272 draw_layer_plane (Display *dpy, struct layer *layer, int width, int height)
275 for (i = 0; i < layer->nblobs; i++)
277 throb_blob (layer->blobs[i]);
278 move_blob (layer->blobs[i], width, height);
279 draw_blob (dpy, layer->pixmap, layer->gc, layer->blobs[i], True);
285 draw_layer_blobs (Display *dpy, Drawable drawable, GC gc,
286 struct layer *layer, int width, int height,
290 for (i = 0; i < layer->nblobs; i++)
292 draw_blob (dpy, drawable, gc, layer->blobs[i], fill_p);
293 throb_blob (layer->blobs[i]);
294 move_blob (layer->blobs[i], width, height);
300 make_goop (Screen *screen, Visual *visual, Window window, Colormap cmap,
301 int width, int height, long depth)
303 Display *dpy = DisplayOfScreen (screen);
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);
327 goop->additive_p = get_boolean_resource ("additive", "Additive");
328 goop->cmap_p = has_writable_cells (screen, visual);
330 if (mono_p && goop->mode == transparent)
333 /* Try to allocate some color planes before committing to nlayers.
335 if (goop->mode == transparent)
337 int nplanes = goop->nlayers;
338 allocate_alpha_colors (screen, visual, cmap,
339 &nplanes, goop->additive_p, &plane_masks,
342 goop->nlayers = nplanes;
346 "%s: couldn't allocate any color planes; turning transparency off.\n",
354 int total = DEF_COUNT;
355 memset (lblobs, 0, sizeof(lblobs));
358 for (i = 0; total && i < goop->nlayers; i++)
359 lblobs[i]++, total--;
360 for (i = 0; i < goop->nlayers; i++)
361 goop->layers[i] = make_layer (dpy, window, width, height,
362 (nblobs > 0 ? nblobs : lblobs[i]));
365 if (goop->mode == transparent && plane_masks)
367 for (i = 0; i < goop->nlayers; i++)
368 goop->layers[i]->pixel = base_pixel | plane_masks[i];
369 goop->background = base_pixel;
374 if (goop->mode != transparent)
377 color.flags = DoRed|DoGreen|DoBlue;
380 get_pixel_resource ("background", "Background", dpy,cmap);
382 for (i = 0; i < goop->nlayers; i++)
384 int H = random() % 360; /* range 0-360 */
385 double S = ((double) (random()%70) + 30)/100.0; /* range 30%-100% */
386 double V = ((double) (random()%34) + 66)/100.0; /* range 66%-100% */
387 hsv_to_rgb (H, S, V, &color.red, &color.green, &color.blue);
388 if (XAllocColor (dpy, cmap, &color))
389 goop->layers[i]->pixel = color.pixel;
391 goop->layers[i]->pixel =
392 WhitePixelOfScreen(DefaultScreenOfDisplay(dpy));
396 goop->pixmap = XCreatePixmap (dpy, window, width, height,
397 (goop->mode == xor ? 1L : depth));
399 gcv.background = goop->background;
400 gcv.foreground = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
401 gcv.line_width = get_integer_resource ("thickness","Thickness");
402 goop->pixmap_gc = XCreateGC (dpy, goop->pixmap, GCLineWidth, &gcv);
403 goop->window_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
409 init_goop (Display *dpy, Window window)
411 XWindowAttributes xgwa;
412 XGetWindowAttributes (dpy, window, &xgwa);
414 return make_goop (xgwa.screen, xgwa.visual, window, xgwa.colormap,
415 xgwa.width, xgwa.height, xgwa.depth);
419 run_goop (Display *dpy, Window window, struct goop *goop)
427 for (i = 0; i < goop->nlayers; i++)
428 draw_layer_plane (dpy, goop->layers[i], goop->width, goop->height);
430 XSetForeground (dpy, goop->pixmap_gc, goop->background);
431 XSetFunction (dpy, goop->pixmap_gc, GXcopy);
432 XSetPlaneMask (dpy, goop->pixmap_gc, AllPlanes);
433 XFillRectangle (dpy, goop->pixmap, goop->pixmap_gc, 0, 0,
434 goop->width, goop->height);
436 XSetForeground (dpy, goop->pixmap_gc, ~0L);
438 if (!goop->cmap_p && !goop->additive_p)
440 for (i = 0; i < goop->nlayers; i++)
441 for (j = 0; j < goop->layers[i]->nblobs; j++)
442 draw_blob (dpy, goop->pixmap, goop->pixmap_gc,
443 goop->layers[i]->blobs[j], True);
444 XSetFunction (dpy, goop->pixmap_gc, GXclear);
447 for (i = 0; i < goop->nlayers; i++)
449 XSetPlaneMask (dpy, goop->pixmap_gc, goop->layers[i]->pixel);
450 draw_layer_blobs (dpy, goop->pixmap, goop->pixmap_gc,
451 goop->layers[i], goop->width, goop->height,
454 XCopyArea (dpy, goop->pixmap, window, goop->window_gc, 0, 0,
455 goop->width, goop->height, 0, 0);
459 XSetFunction (dpy, goop->pixmap_gc, GXcopy);
460 XSetForeground (dpy, goop->pixmap_gc, 0);
461 XFillRectangle (dpy, goop->pixmap, goop->pixmap_gc, 0, 0,
462 goop->width, goop->height);
463 XSetFunction (dpy, goop->pixmap_gc, GXxor);
464 XSetForeground (dpy, goop->pixmap_gc, 1);
465 for (i = 0; i < goop->nlayers; i++)
466 draw_layer_blobs (dpy, goop->pixmap, goop->pixmap_gc,
467 goop->layers[i], goop->width, goop->height,
468 (goop->mode != outline));
469 XCopyPlane (dpy, goop->pixmap, window, goop->window_gc, 0, 0,
470 goop->width, goop->height, 0, 0, 1L);
475 XSetForeground (dpy, goop->pixmap_gc, goop->background);
476 XFillRectangle (dpy, goop->pixmap, goop->pixmap_gc, 0, 0,
477 goop->width, goop->height);
478 for (i = 0; i < goop->nlayers; i++)
480 XSetForeground (dpy, goop->pixmap_gc, goop->layers[i]->pixel);
481 draw_layer_blobs (dpy, goop->pixmap, goop->pixmap_gc,
482 goop->layers[i], goop->width, goop->height,
483 (goop->mode != outline));
485 XCopyArea (dpy, goop->pixmap, window, goop->window_gc, 0, 0,
486 goop->width, goop->height, 0, 0);
496 char *progclass = "Goop";
498 char *defaults [] = {
499 ".background: black",
500 ".foreground: white",
502 "*transparent: true",
514 XrmOptionDescRec options [] = {
515 { "-delay", ".delay", XrmoptionSepArg, 0 },
516 { "-count", ".count", XrmoptionSepArg, 0 },
517 { "-planes", ".planes", XrmoptionSepArg, 0 },
518 { "-transparent", ".transparent", XrmoptionNoArg, "True" },
519 { "-non-transparent", ".transparent", XrmoptionNoArg, "False" },
520 { "-additive", ".additive", XrmoptionNoArg, "True" },
521 { "-subtractive", ".additive", XrmoptionNoArg, "false" },
522 { "-xor", ".xor", XrmoptionNoArg, "true" },
523 { "-no-xor", ".xor", XrmoptionNoArg, "false" },
524 { "-thickness", ".thickness", XrmoptionSepArg, 0 },
525 { "-torque", ".torque", XrmoptionSepArg, 0 },
526 { "-elasticity", ".elasticity", XrmoptionSepArg, 0 },
527 { "-max-velocity", ".maxVelocity", XrmoptionSepArg, 0 },
532 screenhack (Display *dpy, Window window)
534 struct goop *g = init_goop (dpy, window);
535 int delay = get_integer_resource ("delay", "Integer");
538 run_goop (dpy, window, g);
540 screenhack_handle_events (dpy);
541 if (delay) usleep (delay);