http://www.jwz.org/xscreensaver/xscreensaver-5.13.tar.gz
[xscreensaver] / hacks / flame.c
1 /* xscreensaver, Copyright (c) 1993-2008 Jamie Zawinski <jwz@jwz.org>
2  *
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 
9  * implied warranty.
10  */
11
12 /* This file was ported from xlock for use in xscreensaver (and standalone)
13  * by jwz on 18-Oct-93.  (And again, 11-May-97.)  Original copyright reads:
14  *
15  *   static char sccsid[] = "@(#)flame.c 1.4 91/09/27 XLOCK";
16  *
17  * flame.c - recursive fractal cosmic flames.
18  *
19  * Copyright (c) 1991 by Patrick J. Naughton.
20  *
21  * Permission to use, copy, modify, and distribute this software and its
22  * documentation for any purpose and without fee is hereby granted,
23  * provided that the above copyright notice appear in all copies and that
24  * both that copyright notice and this permission notice appear in
25  * supporting documentation.
26  *
27  * This file is provided AS IS with no warranties of any kind.  The author
28  * shall have no liability with respect to the infringement of copyrights,
29  * trade secrets or any patents by this file or any part thereof.  In no
30  * event will the author be liable for any lost revenue or profits or
31  * other special, indirect and consequential damages.
32  *
33  * Comments and additions should be sent to the author:
34  *
35  *                     naughton@eng.sun.com
36  *
37  *                     Patrick J. Naughton
38  *                     MS 21-14
39  *                     Sun Laboritories, Inc.
40  *                     2550 Garcia Ave
41  *                     Mountain View, CA  94043
42  *
43  * Revision History:
44  * 01-Jun-95: This should look more like the original with some updates by
45  *            Scott Draves.
46  * 27-Jun-91: vary number of functions used.
47  * 24-Jun-91: fixed portability problem with integer mod (%).
48  * 06-Jun-91: Written. (received from Scott Draves, spot@cs.cmu.edu).
49  */
50
51 #include <math.h>
52 #include "screenhack.h"
53
54 #include <signal.h>             /* so we can ignore SIGFPE */
55
56 #define POINT_BUFFER_SIZE 10
57 #define MAXLEV 4
58 #define MAXKINDS  10
59
60 struct state {
61   Display *dpy;
62   Window window;
63
64   double f[2][3][MAXLEV];       /* three non-homogeneous transforms */
65   int max_total;
66   int max_levels;
67   int max_points;
68   int cur_level;
69   int variation;
70   int snum;
71   int anum;
72   int num_points;
73   int total_points;
74   int pixcol;
75   int ncolors;
76   XColor *colors;
77   XPoint points [POINT_BUFFER_SIZE];
78   GC gc;
79
80   int delay, delay2;
81   int width, height;
82
83   short lasthalf;
84
85   int flame_alt;
86   int do_reset;
87 };
88
89
90 static short
91 halfrandom (struct state *st, int mv)
92 {
93   unsigned long r;
94
95   if (st->lasthalf)
96     {
97       r = st->lasthalf;
98       st->lasthalf = 0;
99     }
100   else
101     {
102       r = random ();
103       st->lasthalf = r >> 16;
104     }
105   return (r % mv);
106 }
107
108 static void *
109 flame_init (Display *dpy, Window window)
110 {
111   struct state *st = (struct state *) calloc (1, sizeof(*st));
112   XGCValues gcv;
113   XWindowAttributes xgwa;
114   Colormap cmap;
115
116   st->dpy = dpy;
117   st->window = window;
118
119 #if defined(SIGFPE) && defined(SIG_IGN)
120   /* No doubt a better fix would be to track down where the NaN is coming
121      from, and code around that; but this should do.  Apparently most systems
122      (Linux, Solaris, Irix, ...) ignore FPE by default -- but FreeBSD dumps
123      core by default. */
124   signal (SIGFPE, SIG_IGN);
125 #endif
126
127   XGetWindowAttributes (st->dpy, st->window, &xgwa);
128   st->width = xgwa.width;
129   st->height = xgwa.height;
130   cmap = xgwa.colormap;
131
132   st->max_points = get_integer_resource (st->dpy, "iterations", "Integer");
133   if (st->max_points <= 0) st->max_points = 100;
134
135   st->max_levels = st->max_points;
136
137   st->max_total = get_integer_resource (st->dpy, "points", "Integer");
138   if (st->max_total <= 0) st->max_total = 10000;
139
140   st->delay = get_integer_resource (st->dpy, "delay", "Integer");
141   if (st->delay < 0) st->delay = 0;
142   st->delay2 = get_integer_resource (st->dpy, "delay2", "Integer");
143   if (st->delay2 < 0) st->delay2 = 0;
144
145   st->variation = random() % MAXKINDS;
146
147   if (mono_p)
148     st->ncolors = 0;
149   else
150     {
151       st->ncolors = get_integer_resource (st->dpy, "colors", "Integer");
152       if (st->ncolors <= 0) st->ncolors = 128;
153       st->colors = (XColor *) malloc ((st->ncolors+1) * sizeof (*st->colors));
154       make_smooth_colormap (st->dpy, xgwa.visual, xgwa.colormap, st->colors, &st->ncolors,
155                             True, 0, True);
156       if (st->ncolors <= 2)
157         mono_p = True, st->ncolors = 0;
158     }
159
160   gcv.foreground = get_pixel_resource (st->dpy, cmap, "foreground", "Foreground");
161   gcv.background = get_pixel_resource (st->dpy, cmap, "background", "Background");
162
163   if (! mono_p)
164     {
165       st->pixcol = halfrandom (st, st->ncolors);
166       gcv.foreground = (st->colors [st->pixcol].pixel);
167     }
168
169   st->gc = XCreateGC (st->dpy, st->window, GCForeground | GCBackground, &gcv);
170   return st;
171 }
172
173 static int
174 recurse (struct state *st, double x, double y, int l, Display *dpy, Window win)
175 {
176   int i;
177   double nx, ny;
178
179   if (l == st->max_levels)
180     {
181       st->total_points++;
182       if (st->total_points > st->max_total) /* how long each fractal runs */
183         return 0;
184
185       if (x > -1.0 && x < 1.0 && y > -1.0 && y < 1.0)
186         {
187           st->points[st->num_points].x = (int) ((st->width / 2) * (x + 1.0));
188           st->points[st->num_points].y = (int) ((st->height / 2) * (y + 1.0));
189           st->num_points++;
190           if (st->num_points >= POINT_BUFFER_SIZE)
191             {
192               XDrawPoints (st->dpy, win, st->gc, st->points, st->num_points, CoordModeOrigin);
193               st->num_points = 0;
194             }
195         }
196     }
197   else
198     {
199       for (i = 0; i < st->snum; i++)
200         {
201
202           /* Scale back when values get very large. Spot sez:
203              "I think this happens on HPUX.  I think it's non-IEEE
204              to generate an exception instead of a silent NaN."
205            */
206           if ((abs(x) > 1.0E5) || (abs(y) > 1.0E5))
207             x = x / y;
208
209           nx = st->f[0][0][i] * x + st->f[0][1][i] * y + st->f[0][2][i];
210           ny = st->f[1][0][i] * x + st->f[1][1][i] * y + st->f[1][2][i];
211           if (i < st->anum)
212             {
213               switch (st->variation)
214                 {
215                 case 0: /* sinusoidal */
216                   nx = sin(nx);
217                   ny = sin(ny);
218                   break;
219                 case 1: /* complex */
220                   {
221                     double r2 = nx * nx + ny * ny + 1e-6;
222                     nx = nx / r2;
223                     ny = ny / r2;
224                   }
225                   break;
226                 case 2: /* bent */
227                   if (nx < 0.0)
228                     nx = nx * 2.0;
229                   if (ny < 0.0)
230                     ny = ny / 2.0;
231                   break;
232                 case 3: /* swirl */
233                   {
234                     double r = (nx * nx + ny * ny);     /* times k here is fun */
235                     double c1 = sin(r);
236                     double c2 = cos(r);
237                     double t = nx;
238
239                     if (nx > 1e4 || nx < -1e4 || ny > 1e4 || ny < -1e4)
240                       ny = 1e4;
241                     else
242                       ny = c2 * t + c1 * ny;
243                     nx = c1 * nx - c2 * ny;
244                   }
245                   break;
246                 case 4: /* horseshoe */
247                   {
248                     double r, c1, c2, t;
249
250                     /* Avoid atan2: DOMAIN error message */
251                     if (nx == 0.0 && ny == 0.0)
252                       r = 0.0;
253                     else
254                       r = atan2(nx, ny);      /* times k here is fun */
255                     c1 = sin(r);
256                     c2 = cos(r);
257                     t = nx;
258
259                     nx = c1 * nx - c2 * ny;
260                     ny = c2 * t + c1 * ny;
261                   }
262                   break;
263                 case 5: /* drape */
264                   {
265                     double t;
266
267                     /* Avoid atan2: DOMAIN error message */
268                     if (nx == 0.0 && ny == 0.0)
269                       t = 0.0;
270                     else
271                       t = atan2(nx, ny) / M_PI;
272
273                     if (nx > 1e4 || nx < -1e4 || ny > 1e4 || ny < -1e4)
274                       ny = 1e4;
275                     else
276                       ny = sqrt(nx * nx + ny * ny) - 1.0;
277                     nx = t;
278                   }
279                   break;
280                 case 6: /* broken */
281                   if (nx > 1.0)
282                     nx = nx - 1.0;
283                   if (nx < -1.0)
284                     nx = nx + 1.0;
285                   if (ny > 1.0)
286                     ny = ny - 1.0;
287                   if (ny < -1.0)
288                     ny = ny + 1.0;
289                   break;
290                 case 7: /* spherical */
291                   {
292                     double r = 0.5 + sqrt(nx * nx + ny * ny + 1e-6);
293
294                     nx = nx / r;
295                     ny = ny / r;
296                   }
297                   break;
298                 case 8: /*  */
299                   nx = atan(nx) / M_PI_2;
300                   ny = atan(ny) / M_PI_2;
301                   break;
302 /* #if 0 */  /* core dumps on some machines, why not all? */
303                 case 9: /* complex sine */
304                   {
305                     double u = nx;
306                     double v = ny;
307                     double ev = exp(v);
308                     double emv = exp(-v);
309
310                     nx = (ev + emv) * sin(u) / 2.0;
311                     ny = (ev - emv) * cos(u) / 2.0;
312                   }
313                   break;
314                 case 10:        /* polynomial */
315                   if (nx < 0)
316                     nx = -nx * nx;
317                   else
318                     nx = nx * nx;
319                   if (ny < 0)
320                     ny = -ny * ny;
321                   else
322                     ny = ny * ny;
323                   break;
324 /* #endif */
325                 default:
326                   nx = sin(nx);
327                   ny = sin(ny);
328                 }
329             }
330           if (!recurse (st, nx, ny, l + 1, st->dpy, win))
331             return 0;
332         }
333     }
334   return 1;
335 }
336
337 static unsigned long
338 flame_draw (Display *dpy, Window window, void *closure)
339 {
340   struct state *st = (struct state *) closure;
341   int i, j, k;
342   unsigned long this_delay = st->delay;
343
344   if (st->do_reset)
345     {
346       st->do_reset = 0;
347       XClearWindow (st->dpy, st->window);
348     }
349
350   if (!(st->cur_level++ % st->max_levels))
351     {
352       st->do_reset = 1;
353       this_delay = st->delay2;
354       st->flame_alt = !st->flame_alt;
355       st->variation = random() % MAXKINDS;
356     }
357   else
358     {
359       if (st->ncolors > 2)
360         {
361           XSetForeground (st->dpy, st->gc, st->colors [st->pixcol].pixel);
362           if (--st->pixcol < 0)
363             st->pixcol = st->ncolors - 1;
364         }
365     }
366
367   /* number of functions */
368   st->snum = 2 + (st->cur_level % (MAXLEV - 1));
369
370   /* how many of them are of alternate form */
371   if (st->flame_alt)
372     st->anum = 0;
373   else
374     st->anum = halfrandom (st, st->snum) + 2;
375
376   /* 6 coefs per function */
377   for (k = 0; k < st->snum; k++)
378     {
379       for (i = 0; i < 2; i++)
380         for (j = 0; j < 3; j++)
381           st->f[i][j][k] = ((double) (random() & 1023) / 512.0 - 1.0);
382     }
383   st->num_points = 0;
384   st->total_points = 0;
385   recurse (st, 0.0, 0.0, 0, st->dpy, st->window);
386   XDrawPoints (st->dpy, st->window, st->gc, st->points, st->num_points, CoordModeOrigin);
387
388   return this_delay;
389 }
390
391
392 #if defined(__hpux) && defined(PLOSS)
393 /* I don't understand why this is necessary, but I'm told that this program
394    does nothing at all on HP-sUX without it.
395
396    I'm further told that HPUX 11.0 doesn't define PLOSS, and works ok without
397    this section.  Go figure.
398  */
399 #undef random
400 #undef srandom
401 #include <math.h>
402 int matherr(x)
403    register struct exception *x;
404 {
405   if (x->type == PLOSS) return 1;
406   else return 0;
407 }
408 #endif /* __hpux */
409
410
411 \f
412 static const char *flame_defaults [] = {
413   ".background: black",
414   ".foreground: white",
415   "*fpsSolid:   true",
416   "*colors:     64",
417   "*iterations: 25",
418   "*delay:      50000",
419   "*delay2:     2000000",
420   "*points:     10000",
421   0
422 };
423
424 static XrmOptionDescRec flame_options [] = {
425   { "-colors",          ".colors",      XrmoptionSepArg, 0 },
426   { "-iterations",      ".iterations",  XrmoptionSepArg, 0 },
427   { "-delay",           ".delay",       XrmoptionSepArg, 0 },
428   { "-delay2",          ".delay2",      XrmoptionSepArg, 0 },
429   { "-points",          ".points",      XrmoptionSepArg, 0 },
430   { 0, 0, 0, 0 }
431 };
432
433 static void
434 flame_reshape (Display *dpy, Window window, void *closure, 
435                  unsigned int w, unsigned int h)
436 {
437 }
438
439 static Bool
440 flame_event (Display *dpy, Window window, void *closure, XEvent *event)
441 {
442   return False;
443 }
444
445 static void
446 flame_free (Display *dpy, Window window, void *closure)
447 {
448   struct state *st = (struct state *) closure;
449   free (st);
450 }
451
452 XSCREENSAVER_MODULE ("Flame", flame)
453