d723f055e8cee09572da6171878936590a477f99
[xscreensaver] / hacks / flame.c
1 /* xscreensaver, Copyright (c) 1993, 1995, 1996
2  *  Jamie Zawinski <jwz@netscape.com>
3  *
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 
10  * implied warranty.
11  */
12
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:
15  *
16  *   static char sccsid[] = "@(#)flame.c 1.4 91/09/27 XLOCK";
17  *
18  * flame.c - recursive fractal cosmic flames.
19  *
20  * Copyright (c) 1991 by Patrick J. Naughton.
21  *
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.
27  *
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.
33  *
34  * Comments and additions should be sent to the author:
35  *
36  *                     naughton@eng.sun.com
37  *
38  *                     Patrick J. Naughton
39  *                     MS 21-14
40  *                     Sun Laboritories, Inc.
41  *                     2550 Garcia Ave
42  *                     Mountain View, CA  94043
43  *
44  * Revision History:
45  * 01-Jun-95: This should look more like the original with some updates by
46  *            Scott Draves.
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).
50  */
51
52 #include <math.h>
53 #include "screenhack.h"
54
55 #define POINT_BUFFER_SIZE 10
56 #define MAXLEV 4
57 #define MAXKINDS  10
58
59 static double f[2][3][MAXLEV];  /* three non-homogeneous transforms */
60 static int max_total;
61 static int max_levels;
62 static int max_points;
63 static int cur_level;
64 static int variation;
65 static int snum;
66 static int anum;
67 static int num_points;
68 static int total_points;
69 static int pixcol;
70 static int ncolors;
71 static XColor *colors;
72 static XPoint points [POINT_BUFFER_SIZE];
73 static GC gc;
74
75 static int delay, delay2;
76 static int width, height;
77
78 static short
79 halfrandom (int mv)
80 {
81   static short lasthalf = 0;
82   unsigned long r;
83
84   if (lasthalf)
85     {
86       r = lasthalf;
87       lasthalf = 0;
88     }
89   else
90     {
91       r = random ();
92       lasthalf = r >> 16;
93     }
94   return (r % mv);
95 }
96
97 static void
98 init_flame (Display *dpy, Window window)
99 {
100   XGCValues gcv;
101   XWindowAttributes xgwa;
102   Colormap cmap;
103   XGetWindowAttributes (dpy, window, &xgwa);
104   width = xgwa.width;
105   height = xgwa.height;
106   cmap = xgwa.colormap;
107
108   max_points = get_integer_resource ("iterations", "Integer");
109   if (max_points <= 0) max_points = 100;
110
111   max_levels = max_points;
112
113   max_total = get_integer_resource ("points", "Integer");
114   if (max_total <= 0) max_total = 10000;
115
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;
120
121   variation = random() % MAXKINDS;
122
123   if (mono_p)
124     ncolors = 0;
125   else
126     {
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,
131                             True, 0, True);
132       if (ncolors <= 2)
133         mono_p = True, ncolors = 0;
134     }
135
136   gcv.foreground = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
137   gcv.background = get_pixel_resource ("background", "Background", dpy, cmap);
138
139   if (! mono_p)
140     {
141       pixcol = halfrandom (ncolors);
142       gcv.foreground = (colors [pixcol].pixel);
143     }
144
145   gc = XCreateGC (dpy, window, GCForeground | GCBackground, &gcv);
146 }
147
148 static int
149 recurse (double x, double y, int l, Display *dpy, Window win)
150 {
151   int xp, yp, i;
152   double nx, ny;
153
154   if (l == max_levels)
155     {
156       total_points++;
157       if (total_points > max_total) /* how long each fractal runs */
158         return 0;
159
160       if (x > -1.0 && x < 1.0 && y > -1.0 && y < 1.0)
161         {
162           xp = points[num_points].x = (int) ((width / 2) * (x + 1.0));
163           yp = points[num_points].y = (int) ((height / 2) * (y + 1.0));
164           num_points++;
165           if (num_points >= POINT_BUFFER_SIZE)
166             {
167               XDrawPoints (dpy, win, gc, points, num_points, CoordModeOrigin);
168               num_points = 0;
169               /* if (delay) usleep (delay); */
170               /* XSync (dpy, True); */
171             }
172         }
173     }
174   else
175     {
176       for (i = 0; i < snum; i++)
177         {
178           nx = f[0][0][i] * x + f[0][1][i] * y + f[0][2][i];
179           ny = f[1][0][i] * x + f[1][1][i] * y + f[1][2][i];
180           if (i < anum)
181             {
182               switch (variation)
183                 {
184                 case 0: /* sinusoidal */
185                   nx = sin(nx);
186                   ny = sin(ny);
187                   break;
188                 case 1: /* complex */
189                   {
190                     double r2 = nx * nx + ny * ny + 1e-6;
191                     nx = nx / r2;
192                     ny = ny / r2;
193                   }
194                   break;
195                 case 2: /* bent */
196                   if (nx < 0.0)
197                     nx = nx * 2.0;
198                   if (ny < 0.0)
199                     ny = ny / 2.0;
200                   break;
201                 case 3: /* swirl */
202                   {
203                     double r = (nx * nx + ny * ny);     /* times k here is fun */
204                     double c1 = sin(r);
205                     double c2 = cos(r);
206                     double t = nx;
207
208                     if (nx > 1e4 || nx < -1e4 || ny > 1e4 || ny < -1e4)
209                       ny = 1e4;
210                     else
211                       ny = c2 * t + c1 * ny;
212                     nx = c1 * nx - c2 * ny;
213                   }
214                   break;
215                 case 4: /* horseshoe */
216                   {
217                     double r, c1, c2, t;
218
219                     /* Avoid atan2: DOMAIN error message */
220                     if (nx == 0.0 && ny == 0.0)
221                       r = 0.0;
222                     else
223                       r = atan2(nx, ny);      /* times k here is fun */
224                     c1 = sin(r);
225                     c2 = cos(r);
226                     t = nx;
227
228                     nx = c1 * nx - c2 * ny;
229                     ny = c2 * t + c1 * ny;
230                   }
231                   break;
232                 case 5: /* drape */
233                   {
234                     double t;
235
236                     /* Avoid atan2: DOMAIN error message */
237                     if (nx == 0.0 && ny == 0.0)
238                       t = 0.0;
239                     else
240                       t = atan2(nx, ny) / M_PI;
241
242                     if (nx > 1e4 || nx < -1e4 || ny > 1e4 || ny < -1e4)
243                       ny = 1e4;
244                     else
245                       ny = sqrt(nx * nx + ny * ny) - 1.0;
246                     nx = t;
247                   }
248                   break;
249                 case 6: /* broken */
250                   if (nx > 1.0)
251                     nx = nx - 1.0;
252                   if (nx < -1.0)
253                     nx = nx + 1.0;
254                   if (ny > 1.0)
255                     ny = ny - 1.0;
256                   if (ny < -1.0)
257                     ny = ny + 1.0;
258                   break;
259                 case 7: /* spherical */
260                   {
261                     double r = 0.5 + sqrt(nx * nx + ny * ny + 1e-6);
262
263                     nx = nx / r;
264                     ny = ny / r;
265                   }
266                   break;
267                 case 8: /*  */
268                   nx = atan(nx) / M_PI_2;
269                   ny = atan(ny) / M_PI_2;
270                   break;
271 /* #if 0 */  /* core dumps on some machines, why not all? */
272                 case 9: /* complex sine */
273                   {
274                     double u = nx;
275                     double v = ny;
276                     double ev = exp(v);
277                     double emv = exp(-v);
278
279                     nx = (ev + emv) * sin(u) / 2.0;
280                     ny = (ev - emv) * cos(u) / 2.0;
281                   }
282                   break;
283                 case 10:        /* polynomial */
284                   if (nx < 0)
285                     nx = -nx * nx;
286                   else
287                     nx = nx * nx;
288                   if (ny < 0)
289                     ny = -ny * ny;
290                   else
291                     ny = ny * ny;
292                   break;
293 /* #endif */
294                 default:
295                   nx = sin(nx);
296                   ny = sin(ny);
297                 }
298             }
299           if (!recurse (nx, ny, l + 1, dpy, win))
300             return 0;
301         }
302     }
303   return 1;
304 }
305
306
307 static void
308 flame (Display *dpy, Window window)
309 {
310   int i, j, k;
311   static int alt = 0;
312
313   if (!(cur_level++ % max_levels))
314     {
315       if (delay2) usleep (delay2);
316       XClearWindow (dpy, window);
317       alt = !alt;
318
319       variation = random() % MAXKINDS;
320     }
321   else
322     {
323       if (ncolors > 2)
324         {
325           XSetForeground (dpy, gc, colors [pixcol].pixel);
326           if (--pixcol < 0)
327             pixcol = ncolors - 1;
328         }
329     }
330
331   /* number of functions */
332   snum = 2 + (cur_level % (MAXLEV - 1));
333
334   /* how many of them are of alternate form */
335   if (alt)
336     anum = 0;
337   else
338     anum = halfrandom (snum) + 2;
339
340   /* 6 coefs per function */
341   for (k = 0; k < snum; k++)
342     {
343       for (i = 0; i < 2; i++)
344         for (j = 0; j < 3; j++)
345           f[i][j][k] = ((double) (random() & 1023) / 512.0 - 1.0);
346     }
347   num_points = 0;
348   total_points = 0;
349   (void) recurse (0.0, 0.0, 0, dpy, window);
350   XDrawPoints (dpy, window, gc, points, num_points, CoordModeOrigin);
351   XSync (dpy, True);
352   if (delay) usleep (delay);
353 }
354
355
356 #ifdef __hpux
357 /* I don't understand why this is necessary, but I'm told that this program
358    does nothing at all on HP-sUX without it.
359  */
360 #undef random
361 #undef srandom
362 #include <math.h>
363 int matherr(x)
364    register struct exception *x;
365 {
366   if (x->type == PLOSS) return 1;
367   else return 0;
368 }
369 #endif /* __hpux */
370
371
372 \f
373 char *progclass = "Flame";
374
375 char *defaults [] = {
376   "Flame.background:    black",         /* to placate SGI */
377   "Flame.foreground:    white",
378   "*colors:     64",
379   "*iterations: 25",
380   "*delay:      50000",
381   "*delay2:     2000000",
382   "*points:     10000",
383   0
384 };
385
386 XrmOptionDescRec options [] = {
387   { "-colors",          ".colors",      XrmoptionSepArg, 0 },
388   { "-iterations",      ".iterations",  XrmoptionSepArg, 0 },
389   { "-delay",           ".delay",       XrmoptionSepArg, 0 },
390   { "-delay2",          ".delay2",      XrmoptionSepArg, 0 },
391   { "-points",          ".points",      XrmoptionSepArg, 0 },
392   { 0, 0, 0, 0 }
393 };
394
395 void
396 screenhack (Display *dpy, Window window)
397 {
398   init_flame (dpy, window);
399   while (1)
400     flame (dpy, window);
401 }