1 /* xscreensaver, Copyright (c) 1993, 1995, 1996
2 * Jamie Zawinski <jwz@netscape.com>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
13 /* This file was ported from xlock for use in xscreensaver (and standalone)
14 * by jwz on 18-Oct-93. (And again, 11-May-97.) Original copyright reads:
16 * static char sccsid[] = "@(#)flame.c 1.4 91/09/27 XLOCK";
18 * flame.c - recursive fractal cosmic flames.
20 * Copyright (c) 1991 by Patrick J. Naughton.
22 * Permission to use, copy, modify, and distribute this software and its
23 * documentation for any purpose and without fee is hereby granted,
24 * provided that the above copyright notice appear in all copies and that
25 * both that copyright notice and this permission notice appear in
26 * supporting documentation.
28 * This file is provided AS IS with no warranties of any kind. The author
29 * shall have no liability with respect to the infringement of copyrights,
30 * trade secrets or any patents by this file or any part thereof. In no
31 * event will the author be liable for any lost revenue or profits or
32 * other special, indirect and consequential damages.
34 * Comments and additions should be sent to the author:
36 * naughton@eng.sun.com
40 * Sun Laboritories, Inc.
42 * Mountain View, CA 94043
45 * 01-Jun-95: This should look more like the original with some updates by
47 * 27-Jun-91: vary number of functions used.
48 * 24-Jun-91: fixed portability problem with integer mod (%).
49 * 06-Jun-91: Written. (received from Scott Draves, spot@cs.cmu.edu).
53 #include "screenhack.h"
55 #define POINT_BUFFER_SIZE 10
59 static double f[2][3][MAXLEV]; /* three non-homogeneous transforms */
61 static int max_levels;
62 static int max_points;
67 static int num_points;
68 static int total_points;
71 static XColor *colors;
72 static XPoint points [POINT_BUFFER_SIZE];
75 static int delay, delay2;
76 static int width, height;
81 static short lasthalf = 0;
98 init_flame (Display *dpy, Window window)
101 XWindowAttributes xgwa;
103 XGetWindowAttributes (dpy, window, &xgwa);
105 height = xgwa.height;
106 cmap = xgwa.colormap;
108 max_points = get_integer_resource ("iterations", "Integer");
109 if (max_points <= 0) max_points = 100;
111 max_levels = max_points;
113 max_total = get_integer_resource ("points", "Integer");
114 if (max_total <= 0) max_total = 10000;
116 delay = get_integer_resource ("delay", "Integer");
117 if (delay < 0) delay = 0;
118 delay2 = get_integer_resource ("delay2", "Integer");
119 if (delay2 < 0) delay2 = 0;
121 variation = random() % MAXKINDS;
127 ncolors = get_integer_resource ("colors", "Integer");
128 if (ncolors <= 0) ncolors = 128;
129 colors = (XColor *) malloc ((ncolors+1) * sizeof (*colors));
130 make_smooth_colormap (dpy, xgwa.visual, xgwa.colormap, colors, &ncolors,
133 mono_p = True, ncolors = 0;
136 gcv.foreground = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
137 gcv.background = get_pixel_resource ("background", "Background", dpy, cmap);
141 pixcol = halfrandom (ncolors);
142 gcv.foreground = (colors [pixcol].pixel);
145 gc = XCreateGC (dpy, window, GCForeground | GCBackground, &gcv);
149 recurse (double x, double y, int l, Display *dpy, Window win)
157 if (total_points > max_total) /* how long each fractal runs */
160 if (x > -1.0 && x < 1.0 && y > -1.0 && y < 1.0)
162 xp = points[num_points].x = (int) ((width / 2) * (x + 1.0));
163 yp = points[num_points].y = (int) ((height / 2) * (y + 1.0));
165 if (num_points >= POINT_BUFFER_SIZE)
167 XDrawPoints (dpy, win, gc, points, num_points, CoordModeOrigin);
169 /* if (delay) usleep (delay); */
170 /* XSync (dpy, True); */
176 for (i = 0; i < snum; i++)
179 /* Scale back when values get very large. Spot sez:
180 "I think this happens on HPUX. I think it's non-IEEE
181 to generate an exception instead of a silent NaN."
183 if ((abs(x) > 1.0E5) || (abs(y) > 1.0E5))
186 nx = f[0][0][i] * x + f[0][1][i] * y + f[0][2][i];
187 ny = f[1][0][i] * x + f[1][1][i] * y + f[1][2][i];
192 case 0: /* sinusoidal */
196 case 1: /* complex */
198 double r2 = nx * nx + ny * ny + 1e-6;
211 double r = (nx * nx + ny * ny); /* times k here is fun */
216 if (nx > 1e4 || nx < -1e4 || ny > 1e4 || ny < -1e4)
219 ny = c2 * t + c1 * ny;
220 nx = c1 * nx - c2 * ny;
223 case 4: /* horseshoe */
227 /* Avoid atan2: DOMAIN error message */
228 if (nx == 0.0 && ny == 0.0)
231 r = atan2(nx, ny); /* times k here is fun */
236 nx = c1 * nx - c2 * ny;
237 ny = c2 * t + c1 * ny;
244 /* Avoid atan2: DOMAIN error message */
245 if (nx == 0.0 && ny == 0.0)
248 t = atan2(nx, ny) / M_PI;
250 if (nx > 1e4 || nx < -1e4 || ny > 1e4 || ny < -1e4)
253 ny = sqrt(nx * nx + ny * ny) - 1.0;
267 case 7: /* spherical */
269 double r = 0.5 + sqrt(nx * nx + ny * ny + 1e-6);
276 nx = atan(nx) / M_PI_2;
277 ny = atan(ny) / M_PI_2;
279 /* #if 0 */ /* core dumps on some machines, why not all? */
280 case 9: /* complex sine */
285 double emv = exp(-v);
287 nx = (ev + emv) * sin(u) / 2.0;
288 ny = (ev - emv) * cos(u) / 2.0;
291 case 10: /* polynomial */
307 if (!recurse (nx, ny, l + 1, dpy, win))
316 flame (Display *dpy, Window window)
321 if (!(cur_level++ % max_levels))
323 if (delay2) usleep (delay2);
324 XClearWindow (dpy, window);
327 variation = random() % MAXKINDS;
333 XSetForeground (dpy, gc, colors [pixcol].pixel);
335 pixcol = ncolors - 1;
339 /* number of functions */
340 snum = 2 + (cur_level % (MAXLEV - 1));
342 /* how many of them are of alternate form */
346 anum = halfrandom (snum) + 2;
348 /* 6 coefs per function */
349 for (k = 0; k < snum; k++)
351 for (i = 0; i < 2; i++)
352 for (j = 0; j < 3; j++)
353 f[i][j][k] = ((double) (random() & 1023) / 512.0 - 1.0);
357 (void) recurse (0.0, 0.0, 0, dpy, window);
358 XDrawPoints (dpy, window, gc, points, num_points, CoordModeOrigin);
360 if (delay) usleep (delay);
365 /* I don't understand why this is necessary, but I'm told that this program
366 does nothing at all on HP-sUX without it.
372 register struct exception *x;
374 if (x->type == PLOSS) return 1;
381 char *progclass = "Flame";
383 char *defaults [] = {
384 "*background: black",
385 "*foreground: white",
394 XrmOptionDescRec options [] = {
395 { "-colors", ".colors", XrmoptionSepArg, 0 },
396 { "-iterations", ".iterations", XrmoptionSepArg, 0 },
397 { "-delay", ".delay", XrmoptionSepArg, 0 },
398 { "-delay2", ".delay2", XrmoptionSepArg, 0 },
399 { "-points", ".points", XrmoptionSepArg, 0 },
404 screenhack (Display *dpy, Window window)
406 init_flame (dpy, window);