1 /* -*- Mode: C; tab-width: 4 -*-
2 * swirl --- swirly color-cycling patterns.
5 static const char sccsid[] = "@(#)swirl.c 4.00 97/01/01 xlockmore";
8 /* Copyright (c) 1994 M.Dobie <mrd@ecs.soton.ac.uk>
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation for any purpose and without fee is hereby granted,
12 * provided that the above copyright notice appear in all copies and that
13 * both that copyright notice and this permission notice appear in
14 * supporting documentation.
16 * This file is provided AS IS with no warranties of any kind. The author
17 * shall have no liability with respect to the infringement of copyrights,
18 * trade secrets or any patents by this file or any part thereof. In no
19 * event will the author be liable for any lost revenue or profits or
20 * other special, indirect and consequential damages.
22 * 13-May-97: jwz@jwz.org: turned into a standalone program.
23 * 21-Apr-95: improved startup time for TrueColour displays
24 * (limited to 16bpp to save memory) S.Early <sde1000@cam.ac.uk>
25 * 09-Jan-95: fixed colour maps (more colourful) and the image now spirals
26 * outwards from the centre with a fixed number of points drawn
27 * every iteration. Thanks to M.Dobie <mrd@ecs.soton.ac.uk>.
28 * 1994: written. Copyright (c) 1994 M.Dobie <mrd@ecs.soton.ac.uk>
29 * based on original code by R.Taylor
33 # define DEFAULTS "*count: 5 \n" \
37 "*fpsSolid: true \n" \
39 # define SMOOTH_COLORS
40 # define WRITABLE_COLORS
41 # define swirl_handle_event 0
42 # include "xlockmore.h" /* from the xscreensaver distribution */
43 # ifdef HAVE_XSHM_EXTENSION
45 # endif /* HAVE_XSHM_EXTENSION */
46 #else /* !STANDALONE */
47 # include "xlock.h" /* from the xlockmore distribution */
48 # undef HAVE_XSHM_EXTENSION
49 #endif /* !STANDALONE */
51 ENTRYPOINT ModeSpecOpt swirl_opts = {
52 0, NULL, 0, NULL, NULL };
56 /****************************************************************/
58 #define MASS 4 /* maximum mass of a knot */
59 #define MIN_RES 5 /* minimim resolution (>= MIN_RES) */
60 #define MAX_RES 1 /* maximum resolution (>0) */
61 #define TWO_PLANE_PCNT 30 /* probability for two plane mode (0-100) */
62 #define RESTART 2500 /* number of cycles before restart */
63 #define BATCH_DRAW 100 /* points to draw per iteration */
78 int x, y; /* position */
80 KNOT_T t; /* type in the first (or only) plane */
81 KNOT_T T; /* type in second plane if there is one */
82 int M; /* mass in second plane if there is one */
85 /* a colour specification */
86 typedef struct Colour {
87 unsigned short r, g, b;
90 /* drawing direction */
92 DRAW_RIGHT, DRAW_DOWN, DRAW_LEFT, DRAW_UP
95 /****************************************************************/
97 /* data associated with a swirl window */
98 typedef struct swirl_data {
99 /* window paramaters */
100 Window win; /* the window */
101 int width, height; /* window size */
102 int depth; /* depth */
103 int rdepth; /* real depth (for XImage) */
104 Visual *visual; /* visual */
106 /* swirl drawing parameters */
107 int n_knots; /* number of knots */
108 KNOT_P knots; /* knot details */
109 KNOT_T knot_type; /* general type of knots */
110 int resolution; /* drawing resolution, 1..5 */
111 int max_resolution; /* maximum resolution, MAX_RES */
112 int r; /* pixel step */
113 Bool two_plane; /* two plane mode? */
114 Bool first_plane; /* doing first plane? */
115 int start_again; /* when to restart */
117 /* spiral drawing parameters */
118 int x, y; /* current point */
119 DIR_T direction; /* current direction */
120 int dir_todo, dir_done; /* how many points in current direction? */
121 int batch_todo, batch_done; /* how many points in this batch */
122 Bool started, drawing; /* are we drawing? */
125 unsigned char *image; /* image data */
129 int colours; /* how many colours possible */
130 int dcolours; /* how many colours for shading */
132 Bool fixed_colourmap; /* fixed colourmap? */
133 #endif /* !STANDALONE */
134 Bool monochrome; /* monochrome? */
135 Colormap cmap; /* colour map for the window */
136 XColor *rgb_values; /* colour definitions array */
138 int current_map; /* current colour map, 0..dcolours-1 */
139 unsigned long fg, bg, white, black; /* black and white pixel values */
140 int shift; /* colourmap shift */
141 int dshift; /* colourmap shift while drawing */
142 XColor fgcol, bgcol; /* foreground and background colour specs */
143 #endif /* !STANDALONE */
147 #define SWIRLCOLOURS 13
151 static COLOUR basic_colours[SWIRLCOLOURS];
152 #endif /* !STANDALONE */
154 /* an array of swirls for each screen */
155 static SWIRL_P swirls = NULL;
160 Return a random integer between 0 and n inclusive
162 - n is the maximum number
164 Returns a random integer */
167 random_no(unsigned int n)
169 return ((int) ((n + 1) * (double) LRAND() / MAXRAND));
172 /****************************************************************/
177 Initialise all the swirl data
179 - swirl is the swirl data */
182 initialise_swirl(ModeInfo * mi, SWIRL_P swirl)
185 Display *display = MI_DISPLAY(mi);
186 #endif /* !STANDALONE */
188 swirl->width = 0; /* width and height of window */
192 swirl->visual = NULL;
193 swirl->resolution = MIN_RES + 1; /* current resolution */
194 swirl->max_resolution = MAX_RES; /* maximum resolution */
195 swirl->n_knots = 0; /* number of knots */
196 swirl->knot_type = ALL; /* general type of knots */
197 swirl->two_plane = False; /* two plane mode? */
198 swirl->first_plane = False; /* doing first plane? */
199 swirl->start_again = -1; /* restart counter */
201 /* drawing parameters */
204 swirl->started = False;
205 swirl->drawing = False;
208 swirl->image = NULL; /* image data */
209 swirl->ximage = NULL;
212 swirl->colours = 0; /* how many colours possible */
213 swirl->dcolours = 0; /* how many colours for shading */
214 swirl->cmap = (Colormap) NULL;
215 swirl->rgb_values = NULL; /* colour definitions array */
217 swirl->current_map = 0; /* current colour map, 0..dcolours-1 */
219 /* set up fg fb colour specs */
220 swirl->white = MI_WIN_WHITE_PIXEL(mi);
221 swirl->black = MI_WIN_BLACK_PIXEL(mi);
222 #endif /* !STANDALONE */
226 swirl->fg = MI_FG_COLOR(mi);
227 swirl->bg = MI_BG_COLOR(mi);
228 swirl->fgcol.pixel = swirl->fg;
229 swirl->bgcol.pixel = swirl->bg;
230 XQueryColor(display, MI_COLORMAP(mi), &(swirl->fgcol));
231 XQueryColor(display, MI_COLORMAP(mi), &(swirl->bgcol));
232 #endif /* !STANDALONE */
235 /****************************************************************/
240 * Initialise the image for drawing to
242 * - swirl is the swirl data
245 initialise_image(ModeInfo * mi, SWIRL_P swirl)
247 Display *dpy = MI_DISPLAY(mi);
249 if (swirl->ximage != NULL)
250 XDestroyImage(swirl->ximage);
253 #ifdef HAVE_XSHM_EXTENSION
256 swirl->ximage = create_xshm_image(dpy, swirl->visual, swirl->rdepth,
257 ZPixmap, 0, &mi->shm_info,
258 swirl->width, swirl->height);
262 #endif /* HAVE_XSHM_EXTENSION */
266 swirl->ximage = XCreateImage(dpy, swirl->visual, swirl->rdepth, ZPixmap,
267 0, 0, swirl->width, swirl->height,
269 swirl->image = (unsigned char *)
270 calloc(swirl->height, swirl->ximage->bytes_per_line);
271 swirl->ximage->data = (char *) swirl->image;
275 /****************************************************************/
281 * Initialise the list of colours from which the colourmaps are derived
283 * - colours is the array to initialise
284 * - saturation is the saturation value to use 0->grey,
285 * 1.0->full saturation
288 initialise_colours(COLOUR * colours, float saturate)
292 /* start off fully saturated, medium and bright colours */
293 colours[0].r = 0xA000;
294 colours[0].g = 0x0000;
295 colours[0].b = 0x0000;
296 colours[1].r = 0xD000;
297 colours[1].g = 0x0000;
298 colours[1].b = 0x0000;
299 colours[2].r = 0x0000;
300 colours[2].g = 0x6000;
301 colours[2].b = 0x0000;
302 colours[3].r = 0x0000;
303 colours[3].g = 0x9000;
304 colours[3].b = 0x0000;
305 colours[4].r = 0x0000;
306 colours[4].g = 0x0000;
307 colours[4].b = 0xC000;
308 colours[5].r = 0x0000;
309 colours[5].g = 0x0000;
310 colours[5].b = 0xF000;
311 colours[6].r = 0xA000;
312 colours[6].g = 0x6000;
313 colours[6].b = 0x0000;
314 colours[7].r = 0xD000;
315 colours[7].g = 0x9000;
316 colours[7].b = 0x0000;
317 colours[8].r = 0xA000;
318 colours[8].g = 0x0000;
319 colours[8].b = 0xC000;
320 colours[9].r = 0xD000;
321 colours[9].g = 0x0000;
322 colours[9].b = 0xF000;
323 colours[10].r = 0x0000;
324 colours[10].g = 0x6000;
325 colours[10].b = 0xC000;
326 colours[11].r = 0x0000;
327 colours[11].g = 0x9000;
328 colours[11].b = 0xF000;
329 colours[12].r = 0xA000;
330 colours[12].g = 0xA000;
331 colours[12].b = 0xA000;
333 /* add white for low saturation */
334 for (i = 0; i < SWIRLCOLOURS - 1; i++) {
335 unsigned short max_rg, max;
337 /* what is the max intensity for this colour? */
338 max_rg = (colours[i].r > colours[i].g) ? colours[i].r : colours[i].g;
339 max = (max_rg > colours[i].b) ? max_rg : colours[i].b;
341 /* bring elements up to max as saturation approaches 0.0 */
342 colours[i].r += (unsigned short) ((float) (1.0 - saturate) *
343 ((float) max - colours[i].r));
344 colours[i].g += (unsigned short) ((float) (1.0 - saturate) *
345 ((float) max - colours[i].g));
346 colours[i].b += (unsigned short) ((float) (1.0 - saturate) *
347 ((float) max - colours[i].b));
350 #endif /* !STANDALONE */
352 /****************************************************************/
356 * set_black_and_white
358 * Set the entries for foreground & background pixels and
359 * WhitePixel & BlackPixel in an array of colour specifications.
361 * - swirl is the swirl data
362 * - values is the array of specifications
365 set_black_and_white(SWIRL_P swirl, XColor * values)
367 unsigned long white, black;
369 /* where is black and white? */
370 white = swirl->white;
371 black = swirl->black;
373 /* set black and white up */
374 values[white].flags = DoRed | DoGreen | DoBlue;
375 values[white].pixel = white;
376 values[white].red = 0xFFFF;
377 values[white].green = 0xFFFF;
378 values[white].blue = 0xFFFF;
379 values[black].flags = DoRed | DoGreen | DoBlue;
380 values[black].pixel = black;
381 values[black].red = 0;
382 values[black].green = 0;
383 values[black].blue = 0;
385 /* copy the colour specs from the original entries */
386 values[swirl->fg] = swirl->fgcol;
387 values[swirl->bg] = swirl->bgcol;
390 /****************************************************************/
395 * Set an entry in an array of XColor specifications. The given entry will be
396 * set to the given colour. If the entry corresponds to the foreground,
397 * background, WhitePixel, or BlackPixel it is ignored and the given colour
398 * is is put in the next entry.
400 * Therefore, the given colour may be placed up to four places after the
401 * specified entry in the array, if foreground, background, white, or black
404 * - swirl is the swirl data
405 * - value points to a pointer to the array entry. It gets updated to
406 * point to the next free entry.
407 * - pixel points to the current pixel number. It gets updated.
408 * - c points to the colour to add
411 set_colour(SWIRL_P swirl, XColor ** value, unsigned long *pixel, COLOUR_P c)
414 unsigned long fg, bg, white, black;
416 /* where are foreground, background, white, and black? */
419 white = swirl->white;
420 black = swirl->black;
422 /* haven't set it yet */
425 /* try and set the colour */
427 (**value).flags = DoRed | DoGreen | DoBlue;
428 (**value).pixel = *pixel;
430 /* white, black, fg, bg, or a colour? */
431 if ((*pixel != fg) && (*pixel != bg) &&
432 (*pixel != white) && (*pixel != black)) {
433 (**value).red = c->r;
434 (**value).green = c->g;
435 (**value).blue = c->b;
437 /* now we've done it */
446 /****************************************************************/
451 * Get an entry from an array of XColor specifications. The next colour from
452 * the array will be returned. Foreground, background, WhitePixel, or
453 * BlackPixel will be ignored.
455 * - swirl is the swirl data
456 * - value points the array entry. It is updated to point to the entry
457 * following the one returned.
458 * - c is set to the colour found
461 get_colour(SWIRL_P swirl, XColor ** value, COLOUR_P c)
464 unsigned long fg, bg, white, black;
466 /* where is white and black? */
469 white = swirl->white;
470 black = swirl->black;
472 /* haven't set it yet */
475 /* try and set the colour */
477 /* black, white or a colour? */
478 if (((*value)->pixel != fg) && ((*value)->pixel != bg) &&
479 ((*value)->pixel != white) && ((*value)->pixel != black)) {
480 c->r = (*value)->red;
481 c->g = (*value)->green;
482 c->b = (*value)->blue;
484 /* now we've done it */
491 #endif /* !STANDALONE */
493 /****************************************************************/
499 * Generate n colours between c1 and c2. n XColors at *value are set up with
500 * ascending pixel values.
502 * If the pixel range includes BlackPixel or WhitePixel they are set to black
503 * and white respectively but otherwise ignored. Therefore, up to n+2 colours
504 * may actually be set by this function.
506 * - swirl is the swirl data
507 * - values points a pointer to an array of XColors to update
508 * - pixel points to the pixel number to start at
509 * - k n is the number of colours to generate
510 * - c1, c2 are the colours to interpolate between
513 interpolate(SWIRL_P swirl, XColor ** values, unsigned long *pixel, int n, COLOUR_P c1, COLOUR_P c2)
522 for (i = 0; i < n / 2 && (int) *pixel < swirl->colours; i++) {
523 /* work out the colour */
524 r = c1->r + 2 * i * ((int) c2->r) / n;
525 c.r = (r > (int) maxv) ? maxv : r;
526 g = c1->g + 2 * i * ((int) c2->g) / n;
527 c.g = (g > (int) maxv) ? maxv : g;
528 b = c1->b + 2 * i * ((int) c2->b) / n;
529 c.b = (b > (int) maxv) ? maxv : b;
532 set_colour(swirl, values, pixel, &c);
534 for (i = n / 2; i >= 0 && (int) *pixel < swirl->colours; i--) {
535 r = c2->r + 2 * i * ((int) c1->r) / n;
536 c.r = (r > (int) maxv) ? maxv : r;
537 g = c2->g + 2 * i * ((int) c1->g) / n;
538 c.g = (g > (int) maxv) ? maxv : g;
539 b = c2->b + 2 * i * ((int) c1->b) / n;
540 c.b = (b > (int) maxv) ? maxv : b;
543 set_colour(swirl, values, pixel, &c);
547 /****************************************************************/
552 * Generate a `random' closed loop colourmap that occupies the whole colour
555 * - swirl is the swirl data
556 * - values is the array of colour definitions to set up
559 basic_map(SWIRL_P swirl, XColor * values)
563 unsigned short r1, g1, b1, r2, g2, b2, r3, g3, b3;
568 /* start at the beginning of the colour map */
572 /* choose 3 different basic colours at random */
573 for (i = 0; i < 3;) {
577 /* choose colour i */
578 c[i] = basic_colours[random_no(SWIRLCOLOURS - 1)];
580 /* assume different */
583 /* different from the rest? */
584 for (j = 0; j < i; j++)
585 if ((c[i].r == c[j].r) &&
586 (c[i].g == c[j].g) &&
590 /* ready for the next colour? */
595 /* extract components into variables */
606 /* work out the lengths of each side of the triangle */
607 L1 = (int) sqrt((((double) r1 - r2) * ((double) r1 - r2) +
608 ((double) g1 - g2) * ((double) g1 - g2) +
609 ((double) b1 - b2) * ((double) b1 - b2)));
611 L2 = (int) sqrt((((double) r3 - r2) * ((double) r3 - r2) +
612 ((double) g3 - g2) * ((double) g3 - g2) +
613 ((double) b3 - b2) * ((double) b3 - b2)));
615 L3 = (int) sqrt((((double) r1 - r3) * ((double) r1 - r3) +
616 ((double) g1 - g3) * ((double) g1 - g3) +
617 ((double) b1 - b3) * ((double) b1 - b3)));
621 /* allocate colours in proportion to the lengths of the sides */
622 interpolate(swirl, &value, &pixel,
623 (int) ((double) swirl->dcolours * ((double) L1 / (double) L)) + 1, c, c + 1);
624 interpolate(swirl, &value, &pixel,
625 (int) ((double) swirl->dcolours * ((double) L2 / (double) L)) + 1, c + 1, c + 2);
626 interpolate(swirl, &value, &pixel,
627 (int) ((double) swirl->dcolours * ((double) L3 / (double) L)) + 1, c + 2, c);
629 /* fill up any remaining slots (due to rounding) */
630 while ((int) pixel < swirl->colours) {
631 /* repeat the last colour */
632 set_colour(swirl, &value, &pixel, c);
635 /* ensure black and white are correct */
636 if (!swirl->fixed_colourmap)
637 set_black_and_white(swirl, values);
640 /****************************************************************/
645 * Generate pre-rotated versions of the colour specifications
647 * - swirl is the swirl data
648 * - values is an array of colour specifications
651 pre_rotate(SWIRL_P swirl, XColor * values)
658 /* how many colours to display? */
659 dcolours = swirl->dcolours;
661 /* start at the first map */
663 dest = values + swirl->colours;
665 /* generate dcolours-1 rotated maps */
666 for (i = 0; i < dcolours - 1; i++) {
669 /* start at the first pixel */
672 /* remember the first one and skip it */
673 get_colour(swirl, &src, &first);
675 /* put a rotated version of src at dest */
676 for (j = 0; j < dcolours - 1; j++) {
679 /* get the source colour */
680 get_colour(swirl, &src, &c);
683 set_colour(swirl, &dest, &pixel, &c);
686 /* put the first one at the end */
687 set_colour(swirl, &dest, &pixel, &first);
689 /* NB: src and dest should now be ready for the next table */
691 /* ensure black and white are properly set */
692 set_black_and_white(swirl, src);
696 /****************************************************************/
701 * Create a read/write colourmap to use
703 * - swirl is the swirl data
707 create_colourmap(ModeInfo * mi, SWIRL_P swirl)
709 Display *display = MI_DISPLAY(mi);
714 unsigned long redmask, greenmask, bluemask;
716 swirl->fixed_colourmap = !setupColormap(mi, &(swirl->colours),
717 &truecolor, &redmask, &greenmask, &bluemask);
718 preserve = preserveColors(swirl->fg, swirl->bg, swirl->white, swirl->black);
720 /* how many colours should we animate? */
721 swirl->dcolours = (swirl->colours > preserve + 1) ?
722 swirl->colours - preserve : swirl->colours;
724 if (MI_NPIXELS(mi) < 2)
727 /* how fast to shift the colourmap? */
728 swirl->shift = (swirl->colours > 64) ? swirl->colours / 64 : 1;
729 swirl->dshift = (swirl->shift > 1) ? swirl->shift * 2 : 1;
731 /* how may colour map rotations are there? */
732 n_rotations = (swirl->fixed_colourmap) ? 1 : swirl->dcolours;
734 /* allocate space for colour definitions (if not already there) */
735 if (swirl->rgb_values == NULL) {
736 swirl->rgb_values = (XColor *) calloc((swirl->colours + 3) * n_rotations,
739 /* create a colour map */
740 if (!swirl->fixed_colourmap)
742 XCreateColormap(display, swirl->win, swirl->visual, AllocAll);
744 /* select a set of colours for the colour map */
745 basic_map(swirl, swirl->rgb_values);
747 /* are we rotating them? */
748 if (!swirl->fixed_colourmap) {
749 /* generate rotations of the colour maps */
750 pre_rotate(swirl, swirl->rgb_values);
752 /* store the colours in the colour map */
753 XStoreColors(display, swirl->cmap, swirl->rgb_values, swirl->colours);
760 for (i = 0; (int) t > 0; i++, t >>= 1);
763 for (i = 0; (int) t > 0; i++, t >>= 1);
766 for (i = 0; (int) t > 0; i++, t >>= 1);
768 for (i = 0; i < swirl->colours; i++)
769 swirl->rgb_values[i].pixel =
770 ((rsh > 0 ? (swirl->rgb_values[i].red) >> rsh :
771 (swirl->rgb_values[i].red) << (-rsh)) & redmask) |
772 ((gsh > 0 ? (swirl->rgb_values[i].green) >> gsh :
773 (swirl->rgb_values[i].green) << (-gsh)) & greenmask) |
774 ((bsh > 0 ? (swirl->rgb_values[i].blue) >> bsh :
775 (swirl->rgb_values[i].blue) << (-bsh)) & bluemask);
777 /* lookup the colours in the fixed colour map */
778 for (i = 0; i < swirl->colours; i++)
779 (void) XAllocColor(display, MI_COLORMAP(mi),
780 &(swirl->rgb_values[i]));
785 /****************************************************************/
790 * Install a new set of colours into the colour map
792 * - dpy is the display
793 * - swirl is the swirl data
794 * - shift is the amount to rotate the colour map by
797 install_map(Display * dpy, SWIRL_P swirl, int shift)
799 if (!swirl->fixed_colourmap) {
800 /* shift the colour map */
801 swirl->current_map = (swirl->current_map + shift) %
805 XStoreColors(dpy, swirl->cmap,
807 swirl->current_map * swirl->colours,
811 #endif /* !STANDALONE */
813 /****************************************************************/
818 * Initialise the array of knot
820 * swirl is the swirl data
823 create_knots(SWIRL_P swirl)
826 Bool orbit, wheel, picasso, ray, hook;
829 /* create array for knots */
831 (void) free((void *) swirl->knots);
832 swirl->knots = (KNOT_P) calloc(swirl->n_knots, sizeof (KNOT));
835 orbit = wheel = picasso = ray = hook = False;
837 /* what types do we have? */
838 if ((int) swirl->knot_type & (int) ALL) {
839 orbit = wheel = ray = hook = True;
841 if ((int) swirl->knot_type & (int) ORBIT)
843 if ((int) swirl->knot_type & (int) WHEEL)
845 if ((int) swirl->knot_type & (int) PICASSO)
847 if ((int) swirl->knot_type & (int) RAY)
849 if ((int) swirl->knot_type & (int) HOOK)
853 /* initialise each knot */
855 for (k = 0; k < swirl->n_knots; k++) {
857 knot->x = random_no((unsigned int) swirl->width);
858 knot->y = random_no((unsigned int) swirl->height);
861 knot->m = random_no(MASS) + 1;
863 /* can be negative */
864 if (random_no(100) > 50)
869 while (knot->t == NONE) {
870 /* choose a random one from the types available */
871 switch (random_no(4)) {
895 /* if two planes, do same for second plane */
896 if (swirl->two_plane) {
898 while (knot->T == NONE || knot->T == knot->t) {
899 /* choose a different type */
900 switch (random_no(4)) {
929 /****************************************************************/
934 * Work out the pixel value at i, j. Ensure it does not clash with BlackPixel
937 * - swirl is the swirl data
938 * - i, j is the point to calculate
940 * Returns the value of the point
943 do_point(SWIRL_P swirl, int i, int j)
945 int tT, k, value, add;
946 double dx, dy, theta, dist;
947 int dcolours, qcolours;
951 /* how many colours? */
952 dcolours = swirl->dcolours;
953 qcolours = dcolours / 4;
955 /* colour step round a circle */
956 rads = (double) dcolours / (2.0 * M_PI);
961 /* go through all the knots */
963 for (k = 0; k < swirl->n_knots; k++) {
967 /* in two_plane mode get the appropriate knot type */
968 if (swirl->two_plane)
969 tT = (int) ((swirl->first_plane) ? knot->t : knot->T);
973 /* distance from knot */
974 dist = sqrt(dx * dx + dy * dy);
976 /* nothing to add at first */
979 /* work out the contribution (if close enough) */
983 add = (int) (dcolours / (1.0 + 0.01 * abs(knot->m) * dist));
986 /* Avoid atan2: DOMAIN error message */
987 if (dy == 0.0 && dx == 0.0)
990 theta = (atan2(dy, dx) + M_PI) / M_PI;
992 add = (int) (dcolours * theta +
993 sin(0.1 * knot->m * dist) *
994 qcolours * exp(-0.01 * dist));
996 add = (int) (dcolours * (theta - 1.0) +
997 sin(0.1 * knot->m * dist) *
998 qcolours * exp(-0.01 * dist));
1001 add = (int) (dcolours *
1002 fabs(cos(0.002 * knot->m * dist)));
1005 /* Avoid atan2: DOMAIN error message */
1006 if (dy == 0.0 && dx == 0.0)
1009 add = (int) (dcolours * fabs(sin(2.0 * atan2(dy, dx))));
1013 /* Avoid atan2: DOMAIN error message */
1014 if (dy == 0.0 && dx == 0.0)
1015 add = (int) (0.05 * (abs(knot->m) - 1) * dist);
1017 add = (int) (rads * atan2(dy, dx) +
1018 0.05 * (abs(knot->m) - 1) * dist);
1021 /* for a +ve mass add on the contribution else take it off */
1032 swirl->first_plane = (!swirl->first_plane);
1034 /* make sure we handle -ve values properly */
1036 value = (value % dcolours) + 2;
1038 value = dcolours - (abs(value) % (dcolours - 1));
1041 /* if fg and bg are 1 and 0 we should be OK, but just in case */
1042 while ((dcolours > 2) &&
1043 (((value % swirl->colours) == (int) swirl->fg) ||
1044 ((value % swirl->colours) == (int) swirl->bg) ||
1045 ((value % swirl->colours) == (int) swirl->white) ||
1046 ((value % swirl->colours) == (int) swirl->black))) {
1049 #endif /* !STANDALONE */
1051 /* definitely make sure it is in range */
1052 value = value % swirl->colours;
1054 /* lookup the pixel value if necessary */
1056 if (swirl->fixed_colourmap && swirl->dcolours > 2)
1058 value = swirl->rgb_values[value].pixel;
1061 return ((unsigned long) value);
1064 /****************************************************************/
1069 * Draw a square block of points with the same value.
1071 * - ximage is the XImage to draw on.
1072 * - x, y is the top left corner
1073 * - s is the length of each side
1077 draw_block(XImage * ximage, int x, int y, int s, unsigned long v)
1081 for (a = 0; a < s; a++)
1082 for (b = 0; b < s; b++) {
1083 XPutPixel(ximage, x + b, y + a, v);
1087 /****************************************************************/
1090 * draw_point Draw the current point in a swirl pattern onto the XImage
1092 * - swirl is the swirl
1093 * - win is the window to update
1096 draw_point(ModeInfo * mi, SWIRL_P swirl)
1101 /* get current point coordinates and resolution */
1106 /* check we are within the window */
1107 if ((x < 0) || (x > swirl->width - r) || (y < 0) || (y > swirl->height - r))
1110 /* what style are we drawing? */
1111 if (swirl->two_plane) {
1114 /* halve the block size */
1117 /* interleave blocks at half r */
1118 draw_block(swirl->ximage, x, y, r2, do_point(swirl, x, y));
1119 draw_block(swirl->ximage, x + r2, y, r2, do_point(swirl, x + r2, y));
1120 draw_block(swirl->ximage, x + r2, y + r2, r2, do_point(swirl,
1122 draw_block(swirl->ximage, x, y + r2, r2, do_point(swirl, x, y + r2));
1124 draw_block(swirl->ximage, x, y, r, do_point(swirl, x, y));
1126 /* update the screen */
1128 #ifdef HAVE_XSHM_EXTENSION
1130 XShmPutImage(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi), swirl->ximage,
1131 x, y, x, y, r, r, False);
1133 #endif /* !HAVE_XSHM_EXTENSION */
1134 /* PURIFY 4.0.1 on SunOS4 and on Solaris 2 reports a 256 byte memory
1135 leak on the next line. */
1136 XPutImage(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi), swirl->ximage,
1140 /****************************************************************/
1143 * next_point Move to the next point in the spiral pattern
1144 * - swirl is the swirl
1145 * - win is the window to update
1148 next_point(SWIRL_P swirl)
1150 /* more to do in this direction? */
1151 if (swirl->dir_done < swirl->dir_todo) {
1152 /* move in the current direction */
1153 switch (swirl->direction) {
1155 swirl->x += swirl->r;
1158 swirl->y += swirl->r;
1161 swirl->x -= swirl->r;
1164 swirl->y -= swirl->r;
1168 /* done another point */
1171 /* none drawn yet */
1172 swirl->dir_done = 0;
1174 /* change direction - check and record if off screen */
1175 switch (swirl->direction) {
1177 swirl->direction = DRAW_DOWN;
1178 if (swirl->x > swirl->width - swirl->r) {
1179 /* skip these points */
1180 swirl->dir_done = swirl->dir_todo;
1181 swirl->y += (swirl->dir_todo * swirl->r);
1183 /* check for finish */
1184 if (swirl->off_screen)
1185 swirl->drawing = False;
1186 swirl->off_screen = True;
1188 swirl->off_screen = False;
1191 swirl->direction = DRAW_LEFT;
1193 if (swirl->y > swirl->height - swirl->r) {
1194 /* skip these points */
1195 swirl->dir_done = swirl->dir_todo;
1196 swirl->x -= (swirl->dir_todo * swirl->r);
1198 /* check for finish */
1199 if (swirl->off_screen)
1200 swirl->drawing = False;
1201 swirl->off_screen = True;
1203 swirl->off_screen = False;
1206 swirl->direction = DRAW_UP;
1208 /* skip these points */
1209 swirl->dir_done = swirl->dir_todo;
1210 swirl->y -= (swirl->dir_todo * swirl->r);
1212 /* check for finish */
1213 if (swirl->off_screen)
1214 swirl->drawing = False;
1215 swirl->off_screen = True;
1217 swirl->off_screen = False;
1220 swirl->direction = DRAW_RIGHT;
1223 /* skip these points */
1224 swirl->dir_done = swirl->dir_todo;
1225 swirl->x += (swirl->dir_todo * swirl->r);
1227 /* check for finish */
1228 if (swirl->off_screen)
1229 swirl->drawing = False;
1230 swirl->off_screen = True;
1232 swirl->off_screen = False;
1238 /****************************************************************/
1243 * Initialise things for swirling
1245 * - win is the window to draw in
1248 init_swirl(ModeInfo * mi)
1250 Display *display = MI_DISPLAY(mi);
1251 Window window = MI_WINDOW(mi);
1254 /* does the swirls array exist? */
1255 if (swirls == NULL) {
1256 /* allocate an array, one entry for each screen */
1257 swirls = (SWIRL_P) calloc(MI_NUM_SCREENS(mi), sizeof (SWIRL));
1259 /* get a pointer to this swirl */
1260 swirl = &(swirls[MI_SCREEN(mi)]);
1261 initialise_swirl(mi, swirl);
1263 /* get window parameters */
1264 swirl->win = window;
1265 swirl->width = MI_WIN_WIDTH(mi);
1266 swirl->height = MI_WIN_HEIGHT(mi);
1267 swirl->depth = MI_WIN_DEPTH(mi);
1268 swirl->rdepth = swirl->depth;
1269 swirl->visual = MI_VISUAL(mi);
1271 if (swirl->depth > 16)
1274 /* initialise image for speeding up drawing */
1275 initialise_image(mi, swirl);
1277 /* clear the window (before setting the colourmap) */
1278 XClearWindow(display, MI_WINDOW(mi));
1282 swirl->rgb_values = mi->colors;
1283 swirl->colours = mi->npixels;
1284 swirl->dcolours = swirl->colours;
1285 /* swirl->fixed_colourmap = !mi->writable_p;*/
1287 #else /* !STANDALONE */
1289 /* initialise the colours from which the colourmap is derived */
1290 initialise_colours(basic_colours, MI_SATURATION(mi));
1292 /* set up the colour map */
1293 create_colourmap(mi, swirl);
1295 /* attach the colour map to the window (if we have one) */
1296 if (!swirl->fixed_colourmap) {
1298 setColormap(display, window, swirl->cmap, MI_WIN_IS_INWINDOW(mi));
1300 XSetWindowColormap(display, window, swirl->cmap);
1301 (void) XSetWMColormapWindows(display, window, &window, 1);
1302 XInstallColormap(display, swirl->cmap);
1305 #endif /* STANDALONE */
1307 /* resolution starts off chunky */
1308 swirl->resolution = MIN_RES + 1;
1310 /* calculate the pixel step for this resulution */
1311 swirl->r = (1 << (swirl->resolution - 1));
1313 /* how many knots? */
1314 swirl->n_knots = random_no((unsigned int) MI_BATCHCOUNT(mi) / 2) +
1315 MI_BATCHCOUNT(mi) + 1;
1317 /* what type of knots? */
1318 swirl->knot_type = ALL; /* for now */
1320 /* use two_plane mode occaisionally */
1321 if (random_no(100) <= TWO_PLANE_PCNT) {
1322 swirl->two_plane = swirl->first_plane = True;
1323 swirl->max_resolution = 2;
1325 swirl->two_plane = False;
1327 /* fix the knot values */
1328 create_knots(swirl);
1331 swirl->started = True;
1332 swirl->drawing = False;
1335 /****************************************************************/
1340 * Draw one iteration of swirling
1342 * - win is the window to draw in
1345 draw_swirl(ModeInfo * mi)
1347 SWIRL_P swirl = &(swirls[MI_SCREEN(mi)]);
1350 if (swirl->started) {
1351 /* in the middle of drawing? */
1352 if (swirl->drawing) {
1355 rotate_colors(MI_DISPLAY(mi), MI_COLORMAP(mi),
1356 swirl->rgb_values, swirl->colours, 1);
1357 #else /* !STANDALONE */
1358 /* rotate the colours */
1359 install_map(MI_DISPLAY(mi), swirl, swirl->dshift);
1360 #endif /* !STANDALONE */
1362 /* draw a batch of points */
1363 swirl->batch_todo = BATCH_DRAW;
1364 while ((swirl->batch_todo > 0) && swirl->drawing) {
1366 draw_point(mi, swirl);
1368 /* move to the next point */
1372 swirl->batch_todo--;
1377 rotate_colors(MI_DISPLAY(mi), MI_COLORMAP(mi),
1378 swirl->rgb_values, swirl->colours, 1);
1379 #else /* !STANDALONE */
1380 /* rotate the colours */
1381 install_map(MI_DISPLAY(mi), swirl, swirl->shift);
1382 #endif /* !STANDALONE */
1384 /* time for a higher resolution? */
1385 if (swirl->resolution > swirl->max_resolution) {
1386 /* move to higher resolution */
1387 swirl->resolution--;
1389 /* calculate the pixel step for this resulution */
1390 swirl->r = (1 << (swirl->resolution - 1));
1392 /* start drawing again */
1393 swirl->drawing = True;
1395 /* start in the middle of the screen */
1396 swirl->x = (swirl->width - swirl->r) / 2;
1397 swirl->y = (swirl->height - swirl->r) / 2;
1399 /* initialise spiral drawing parameters */
1400 swirl->direction = DRAW_RIGHT;
1401 swirl->dir_todo = 1;
1402 swirl->dir_done = 0;
1404 /* all done, decide when to restart */
1405 if (swirl->start_again == -1) {
1406 /* start the counter */
1407 swirl->start_again = RESTART;
1408 } else if (swirl->start_again == 0) {
1409 /* reset the counter */
1410 swirl->start_again = -1;
1413 /* Pick a new colormap! */
1414 XClearWindow (MI_DISPLAY(mi), MI_WINDOW(mi));
1415 free_colors (MI_DISPLAY(mi), MI_COLORMAP(mi),
1416 mi->colors, mi->npixels);
1417 make_smooth_colormap (MI_DISPLAY(mi),
1420 mi->colors, &mi->npixels, True,
1421 &mi->writable_p, True);
1422 swirl->colours = mi->npixels;
1423 #endif /* STANDALONE */
1428 /* decrement the counter */
1429 swirl->start_again--;
1436 reshape_swirl(ModeInfo * mi, int width, int height)
1438 XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
1442 /****************************************************************/
1445 release_swirl (ModeInfo * mi)
1447 /* does the swirls array exist? */
1448 if (swirls != NULL) {
1452 for (i = 0; i < MI_NUM_SCREENS(mi); i++) {
1453 SWIRL_P swirl = &(swirls[i]);
1456 if (swirl->cmap != (Colormap) NULL)
1457 XFreeColormap(MI_DISPLAY(mi), swirl->cmap);
1458 #endif /* STANDALONE */
1460 if (swirl->rgb_values != NULL)
1461 XFree((void *) swirl->rgb_values);
1462 #endif /* !STANDALONE */
1463 if (swirl->ximage != NULL)
1464 XDestroyImage(swirl->ximage);
1466 (void) free((void *) swirl->knots);
1468 /* deallocate an array, one entry for each screen */
1469 (void) free((void *) swirls);
1474 /****************************************************************/
1477 refresh_swirl (ModeInfo * mi)
1479 SWIRL_P swirl = &(swirls[MI_SCREEN(mi)]);
1481 if (swirl->started) {
1483 swirl->resolution = swirl->resolution + 1;
1484 swirl->drawing = False;
1488 XSCREENSAVER_MODULE ("Swirl", swirl)