1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* drift --- drifting recursive fractal cosmic flames */
5 static const char sccsid[] = "@(#)drift.c 5.00 2000/11/01 xlockmore";
9 * Copyright (c) 1991 by Patrick J. Naughton.
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation.
17 * This file is provided AS IS with no warranties of any kind. The author
18 * shall have no liability with respect to the infringement of copyrights,
19 * trade secrets or any patents by this file or any part thereof. In no
20 * event will the author be liable for any lost revenue or profits or
21 * other special, indirect and consequential damages.
24 * 01-Nov-2000: Allocation checks
25 * 10-May-1997: Jamie Zawinski <jwz@jwz.org> compatible with xscreensaver
26 * 01-Jan-1997: Moved new flame to drift. Compile time options now run time.
27 * 01-Jun-1995: Updated by Scott Draves.
28 * 27-Jun-1991: vary number of functions used.
29 * 24-Jun-1991: fixed portability problem with integer mod (%).
30 * 06-Jun-1991: Written, received from Scott Draves <spot@cs.cmu.edu>
35 # define DEFAULTS "*delay: 10000 \n" \
38 "*fpsSolid: true \n" \
40 # define SMOOTH_COLORS
41 # define reshape_drift 0
42 # define drift_handle_event 0
43 # include "xlockmore.h" /* in xscreensaver distribution */
45 #else /* STANDALONE */
46 # define ENTRYPOINT /**/
47 # include "xlock.h" /* in xlockmore distribution */
48 #endif /* STANDALONE */
52 #define DEF_GROW "False" /* Grow fractals instead of animating one at a time,
53 would then be like flame */
54 #define DEF_LISS "False" /* if this is defined then instead of a point
55 bouncing around in a high dimensional sphere, we
56 use lissojous figures. Only makes sense if
62 static XrmOptionDescRec opts[] =
64 {"-grow", ".drift.grow", XrmoptionNoArg, "on"},
65 {"+grow", ".drift.grow", XrmoptionNoArg, "off"},
66 {"-liss", ".drift.trail", XrmoptionNoArg, "on"},
67 {"+liss", ".drift.trail", XrmoptionNoArg, "off"}
69 static argtype vars[] =
71 {&grow, "grow", "Grow", DEF_GROW, t_Bool},
72 {&liss, "trail", "Trail", DEF_LISS, t_Bool}
74 static OptionStruct desc[] =
76 {"-/+grow", "turn on/off growing fractals, else they are animated"},
77 {"-/+liss", "turn on/off using lissojous figures to get points"}
80 ENTRYPOINT ModeSpecOpt drift_opts =
81 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
84 ModStruct drift_description =
85 {"drift", "init_drift", "draw_drift", "release_drift",
86 "refresh_drift", "init_drift", (char *) NULL, &drift_opts,
87 10000, 30, 1, 1, 64, 1.0, "",
88 "Shows cosmic drifting flame fractals", 0, NULL};
92 #define MAXBATCH1 200 /* mono */
93 #define MAXBATCH2 20 /* color */
94 #define FUSE 10 /* discard this many initial iterations */
99 /* shape of current flame */
101 double f[2][3][MAXLEV]; /* a bunch of non-homogeneous xforms */
102 int variation[10]; /* for each xform */
105 double df[2][3][MAXLEV];
107 /* high-level control */
108 int mode; /* 0->slow/single 1->fast/many */
109 int nfractals; /* draw this many fractals */
111 int fractal_len; /* pts/fractal */
113 int rainbow; /* more than one color per fractal
114 1-> computed by adding dimension to fractal */
116 int width, height; /* of window */
119 /* draw info about current flame */
120 int fuse; /* iterate this many before drawing */
121 int total_points; /* draw this many pts before fractal ends */
122 int npoints; /* how many we've computed but not drawn */
123 XPoint pts[MAXBATCH1]; /* here they are */
124 unsigned long pixcol;
125 /* when drawing in color, we have a buffer per color */
134 long saved_random_bits;
139 eraser_state *eraser;
143 static driftstruct *drifts = (driftstruct *) NULL;
146 halfrandom(driftstruct * dp, int mv)
155 dp->lasthalf = (short) (r >> 16);
162 frandom(driftstruct * dp, int n)
167 dp->saved_random_bits = LRAND();
172 result = (int) (dp->saved_random_bits & 1);
173 dp->saved_random_bits >>= 1;
178 result = (int) (dp->saved_random_bits & 3);
179 dp->saved_random_bits >>= 2;
182 return frandom(dp, 3);
186 result = (int) (dp->saved_random_bits & 3);
187 dp->saved_random_bits >>= 2;
192 result = (int) (dp->saved_random_bits & 7);
193 dp->saved_random_bits >>= 3;
196 return frandom(dp, 5);
199 (void) fprintf(stderr, "bad arg to frandom\n");
204 #define DISTRIB_A (halfrandom(dp, 7000) + 9000)
205 #define DISTRIB_B ((frandom(dp, 3) + 1) * (frandom(dp, 3) + 1) * 120000)
206 #define LEN(x) (sizeof(x)/sizeof((x)[0]))
209 initmode(ModeInfo * mi, int mode)
211 driftstruct *dp = &drifts[MI_SCREEN(mi)];
213 #define VARIATION_LEN 14
217 dp->major_variation = halfrandom(dp, VARIATION_LEN);
218 /* 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6, 6 */
219 dp->major_variation = ((dp->major_variation >= VARIATION_LEN >> 1) &&
220 (dp->major_variation < VARIATION_LEN - 1)) ?
221 (dp->major_variation + 1) >> 1 : dp->major_variation >> 1;
226 if (!dp->color || halfrandom(dp, 8)) {
227 dp->nfractals = halfrandom(dp, 30) + 5;
228 dp->fractal_len = DISTRIB_A;
230 dp->nfractals = halfrandom(dp, 5) + 5;
231 dp->fractal_len = DISTRIB_B;
234 dp->rainbow = dp->color;
236 dp->fractal_len = DISTRIB_B;
240 dp->rainbow = dp->color;
241 dp->fractal_len = 2000000;
243 dp->fractal_len = (dp->fractal_len * MI_COUNT(mi)) / 20;
251 pick_df_coefs(ModeInfo * mi)
253 driftstruct *dp = &drifts[MI_SCREEN(mi)];
257 for (i = 0; i < dp->nxforms; i++) {
260 for (j = 0; j < 2; j++)
261 for (k = 0; k < 3; k++) {
262 dp->df[j][k][i] = ((double) halfrandom(dp, 1000) / 500.0 - 1.0);
263 r += dp->df[j][k][i] * dp->df[j][k][i];
265 r = (3 + halfrandom(dp, 5)) * 0.01 / sqrt(r);
266 for (j = 0; j < 2; j++)
267 for (k = 0; k < 3; k++)
268 dp->df[j][k][i] *= r;
273 free_drift(driftstruct *dp)
275 if (dp->ncpoints != NULL) {
276 (void) free((void *) dp->ncpoints);
277 dp->ncpoints = (int *) NULL;
279 if (dp->cpts != NULL) {
280 (void) free((void *) dp->cpts);
281 dp->cpts = (XPoint *) NULL;
286 initfractal(ModeInfo * mi)
288 driftstruct *dp = &drifts[MI_SCREEN(mi)];
294 dp->total_points = 0;
297 if ((dp->ncpoints = (int *) malloc(sizeof (int) * MI_NCOLORS(mi))) ==
304 if ((dp->cpts = (XPoint *) malloc(MAXBATCH2 * sizeof (XPoint) *
305 MI_NCOLORS(mi))) == NULL) {
312 for (i = 0; i < MI_NPIXELS(mi); i++)
316 dp->nxforms = halfrandom(dp, XFORM_LEN);
317 /* 2, 2, 2, 3, 3, 3, 4, 4, 5 */
318 dp->nxforms = (dp->nxforms >= XFORM_LEN - 1) + dp->nxforms / 3 + 2;
320 dp->c = dp->x = dp->y = 0.0;
321 if (dp->liss && !halfrandom(dp, 10)) {
326 for (i = 0; i < dp->nxforms; i++) {
327 if (NMAJORVARS == dp->major_variation)
328 dp->variation[i] = halfrandom(dp, NMAJORVARS);
330 dp->variation[i] = dp->major_variation;
331 for (j = 0; j < 2; j++)
332 for (k = 0; k < 3; k++) {
334 dp->f[j][k][i] = sin(dp->liss_time * dp->df[j][k][i]);
336 dp->f[j][k][i] = ((double) halfrandom(dp, 1000) / 500.0 - 1.0);
340 dp->pixcol = MI_PIXEL(mi, halfrandom(dp, MI_NPIXELS(mi)));
342 dp->pixcol = MI_WHITE_PIXEL(mi);
348 init_drift(ModeInfo * mi)
352 if (drifts == NULL) {
353 if ((drifts = (driftstruct *) calloc(MI_NUM_SCREENS(mi),
354 sizeof (driftstruct))) == NULL)
357 dp = &drifts[MI_SCREEN(mi)];
359 dp->width = MI_WIDTH(mi);
360 dp->height = MI_HEIGHT(mi);
361 dp->color = MI_NPIXELS(mi) > 2;
363 if (MI_IS_FULLRANDOM(mi)) {
368 dp->liss = (Bool) (LRAND() & 1);
382 iter(driftstruct * dp)
384 int i = frandom(dp, dp->nxforms);
389 nc = (dp->c + 1.0) / 2.0;
393 nx = dp->f[0][0][i] * dp->x + dp->f[0][1][i] * dp->y + dp->f[0][2][i];
394 ny = dp->f[1][0][i] * dp->x + dp->f[1][1][i] * dp->y + dp->f[1][2][i];
397 switch (dp->variation[i]) {
406 double r2 = nx * nx + ny * ny + 1e-6;
423 double r = (nx * nx + ny * ny); /* times k here is fun */
428 if (nx > 1e4 || nx < -1e4 || ny > 1e4 || ny < -1e4)
431 ny = c2 * t + c1 * ny;
432 nx = c1 * nx - c2 * ny;
440 /* Avoid atan2: DOMAIN error message */
441 if (nx == 0.0 && ny == 0.0)
444 r = atan2(nx, ny); /* times k here is fun */
449 nx = c1 * nx - c2 * ny;
450 ny = c2 * t + c1 * ny;
458 /* Avoid atan2: DOMAIN error message */
459 if (nx == 0.0 && ny == 0.0)
462 t = atan2(nx, ny) / M_PI;
464 if (nx > 1e4 || nx < -1e4 || ny > 1e4 || ny < -1e4)
467 ny = sqrt(nx * nx + ny * ny) - 1.0;
474 /* here are some others */
489 double u = nx, v = ny;
491 double emv = exp(-v);
493 nx = (ev + emv) * sin(u) / 2.0;
494 ny = (ev - emv) * cos(u) / 2.0;
511 double r = 0.5 + sqrt(nx * nx + ny * ny + 1e-6);
517 nx = atan(nx) / M_PI_2
518 ny = atan(ny) / M_PI_2
522 /* how to check nan too? some machines don't have finite().
523 don't need to check ny, it'll propogate */
524 if (nx > 1e4 || nx < -1e4) {
525 nx = halfrandom(dp, 1000) / 500.0 - 1.0;
526 ny = halfrandom(dp, 1000) / 500.0 - 1.0;
536 draw(ModeInfo * mi, driftstruct * dp, Drawable d)
538 Display *display = MI_DISPLAY(mi);
542 int fixed_x, fixed_y, npix, c, n;
548 if (!(x > -1.0 && x < 1.0 && y > -1.0 && y < 1.0))
551 fixed_x = (int) ((dp->width / 2) * (x + 1.0));
552 fixed_y = (int) ((dp->height / 2) * (y + 1.0));
556 dp->pts[dp->npoints].x = fixed_x;
557 dp->pts[dp->npoints].y = fixed_y;
559 if (dp->npoints == MAXBATCH1) {
560 XSetForeground(display, gc, dp->pixcol);
561 XDrawPoints(display, d, gc, dp->pts, dp->npoints, CoordModeOrigin);
566 npix = MI_NPIXELS(mi);
567 c = (int) (dp->c * npix);
574 dp->cpts[c * MAXBATCH2 + n].x = fixed_x;
575 dp->cpts[c * MAXBATCH2 + n].y = fixed_y;
576 if (++dp->ncpoints[c] == MAXBATCH2) {
577 XSetForeground(display, gc, MI_PIXEL(mi, c));
578 XDrawPoints(display, d, gc, &(dp->cpts[c * MAXBATCH2]),
579 dp->ncpoints[c], CoordModeOrigin);
586 draw_flush(ModeInfo * mi, driftstruct * dp, Drawable d)
588 Display *display = MI_DISPLAY(mi);
592 int npix = MI_NPIXELS(mi);
595 for (i = 0; i < npix; i++) {
596 if (dp->ncpoints[i]) {
597 XSetForeground(display, gc, MI_PIXEL(mi, i));
598 XDrawPoints(display, d, gc, &(dp->cpts[i * MAXBATCH2]),
599 dp->ncpoints[i], CoordModeOrigin);
605 XSetForeground(display, gc, dp->pixcol);
606 XDrawPoints(display, d, gc, dp->pts,
607 dp->npoints, CoordModeOrigin);
614 draw_drift(ModeInfo * mi)
616 Window window = MI_WINDOW(mi);
621 dp = &drifts[MI_SCREEN(mi)];
622 if (dp->ncpoints == NULL)
625 if (dp->erase_countdown) {
626 if (!--dp->erase_countdown) {
627 dp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), dp->eraser);
632 dp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), dp->eraser);
636 MI_IS_DRAWN(mi) = True;
640 draw(mi, dp, window);
641 if (dp->total_points++ > dp->fractal_len) {
642 draw_flush(mi, dp, window);
643 if (0 == --dp->nfractals) {
645 dp->erase_countdown = 4 * 1000000 / MI_PAUSE(mi);
646 #endif /* STANDALONE */
647 initmode(mi, frandom(dp, 2));
656 draw_flush(mi, dp, window);
659 for (i = 0; i < dp->nxforms; i++)
660 for (j = 0; j < 2; j++)
661 for (k = 0; k < 3; k++) {
663 dp->f[j][k][i] = sin(dp->liss_time * dp->df[j][k][i]);
665 double t = dp->f[j][k][i] += dp->df[j][k][i];
667 if (t < -1.0 || 1.0 < t)
668 dp->df[j][k][i] *= -1.0;
675 release_drift(ModeInfo * mi)
677 if (drifts != NULL) {
680 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
681 free_drift(&drifts[screen]);
682 (void) free((void *) drifts);
683 drifts = (driftstruct *) NULL;
688 refresh_drift(ModeInfo * mi)
693 XSCREENSAVER_MODULE ("Drift", drift)
695 #endif /* MODE_drift */