From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / thornbird.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* thornbird --- continuously varying Thornbird set */
3
4 #if 0
5 static const char sccsid[] = "@(#)thornbird.c   5.00 2000/11/01 xlockmore";
6 #endif
7
8 /*-
9  * Copyright (c) 1996 by Tim Auckland <tda10.geo@yahoo.com>
10  *
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.
16  *
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.
22  *
23  * "thornbird" shows a view of the "Bird in a Thornbush" fractal,
24  * continuously varying the three free parameters.
25  *
26  * Revision History:
27  * 01-Nov-2000: Allocation checks
28  * 04-Jun-1999: 3D tumble added by Tim Auckland
29  * 31-Jul-1997: Adapted from discrete.c Copyright (c) 1996 by Tim Auckland
30  */
31
32 #ifdef STANDALONE
33 # define MODE_thornbird
34 #define DEFAULTS        "*delay:    10000 \n" \
35                                         "*count:    100   \n" \
36                                          "*cycles:  400   \n" \
37                                          "*ncolors: 64    \n" \
38                                          "*fpsSolid: true    \n" \
39                                         "*ignoreRotation: True \n" \
40
41 # define BRIGHT_COLORS
42 # define release_thornbird 0
43 # define thornbird_handle_event 0
44 # include "xlockmore.h"         /* in xscreensaver distribution */
45 #else /* STANDALONE */
46 # include "xlock.h"             /* in xlockmore distribution */
47 #endif /* STANDALONE */
48
49 #ifdef MODE_thornbird
50
51 ENTRYPOINT ModeSpecOpt thornbird_opts =
52 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
53
54 #ifdef USE_MODULES
55 ModStruct   thornbird_description =
56 {"thornbird", "init_thornbird", "draw_thornbird", (char *) NULL,
57  "refresh_thornbird", "init_thornbird", (char *) NULL, &thornbird_opts,
58  1000, 800, 16, 1, 64, 1.0, "",
59  "Shows an animated Bird in a Thorn Bush fractal map", 0, NULL};
60
61 #endif
62
63 #define balance_rand(v) ((LRAND()/MAXRAND*(v))-((v)/2)) /* random around 0 */
64
65 typedef struct {
66         int         maxx;
67         int         maxy;       /* max of the screen */
68         double      a;
69         double      b;
70         double      c;
71         double      d;
72         double      e;
73         double      i;
74         double      j;          /* thornbird parameters */
75     struct {
76           double  f1;
77           double  f2;
78         }           liss;
79     struct {
80           double  theta;
81           double  dtheta;
82           double  phi;
83           double  dphi;
84         }           tumble;
85     int         inc;
86         int         pix;
87         int         count;
88         int         nbuffers;
89         XPoint    **pointBuffer;        /* pointer for XDrawPoints */
90 } thornbirdstruct;
91
92 static thornbirdstruct *thornbirds = (thornbirdstruct *) NULL;
93
94 static void
95 free_thornbird(ModeInfo * mi)
96 {
97         thornbirdstruct *hp = &thornbirds[MI_SCREEN(mi)];
98         if (hp->pointBuffer != NULL) {
99                 int         buffer;
100
101                 for (buffer = 0; buffer < hp->nbuffers; buffer++)
102                         if (hp->pointBuffer[buffer] != NULL)
103                                 (void) free((void *) hp->pointBuffer[buffer]);
104                 (void) free((void *) hp->pointBuffer);
105                 hp->pointBuffer = (XPoint **) NULL;
106         }
107 }
108
109 ENTRYPOINT void
110 init_thornbird (ModeInfo * mi)
111 {
112         thornbirdstruct *hp;
113
114         MI_INIT (mi, thornbirds, free_thornbird);
115         hp = &thornbirds[MI_SCREEN(mi)];
116
117
118         hp->maxx = MI_WIDTH(mi);
119         hp->maxy = MI_HEIGHT(mi);
120
121         hp->b = 0.1;
122         hp->i = hp->j = 0.1;
123
124         hp->pix = 0;
125         hp->inc = 0;
126
127         hp->nbuffers = MI_CYCLES(mi);
128
129         if (hp->pointBuffer == NULL)
130                 if ((hp->pointBuffer = (XPoint **) calloc(MI_CYCLES(mi),
131                                 sizeof (XPoint *))) == NULL) {
132                         free_thornbird(mi);
133                         return;
134                 }
135
136         if (hp->pointBuffer[0] == NULL)
137                 if ((hp->pointBuffer[0] = (XPoint *) malloc(MI_COUNT(mi) *
138                                 sizeof (XPoint))) == NULL) {
139                         free_thornbird(mi);
140                         return;
141                 }
142
143         /* select frequencies for parameter variation */
144         hp->liss.f1 = LRAND() % 5000;
145         hp->liss.f2 = LRAND() % 2000;
146
147         /* choose random 3D tumbling */
148         hp->tumble.theta = 0;
149         hp->tumble.phi = 0;
150         hp->tumble.dtheta = balance_rand(0.001);
151         hp->tumble.dphi = balance_rand(0.005);
152
153         /* Clear the background. */
154         MI_CLEARWINDOW(mi);
155
156         hp->count = 0;
157 }
158
159
160 ENTRYPOINT void
161 draw_thornbird(ModeInfo * mi)
162 {
163         Display    *dsp = MI_DISPLAY(mi);
164         Window      win = MI_WINDOW(mi);
165         double      oldj, oldi;
166         int         batchcount = MI_COUNT(mi);
167         int         k;
168         XPoint     *xp;
169         GC          gc = MI_GC(mi);
170         int         erase;
171         int         current;
172
173         double      sint, cost, sinp, cosp;
174         thornbirdstruct *hp;
175
176         if (thornbirds == NULL)
177                 return;
178         hp = &thornbirds[MI_SCREEN(mi)];
179         if (hp->pointBuffer == NULL)
180                 return;
181
182         erase = (hp->inc + 1) % MI_CYCLES(mi);
183         current = hp->inc % MI_CYCLES(mi);
184         k = batchcount;
185
186
187         xp = hp->pointBuffer[current];
188
189         /* vary papameters */
190         hp->a = 1.99 + (0.4 * sin(hp->inc / hp->liss.f1) +
191                                         0.05 * cos(hp->inc / hp->liss.f2));
192         hp->c = 0.80 + (0.15 * cos(hp->inc / hp->liss.f1) +
193                                         0.05 * sin(hp->inc / hp->liss.f2));
194
195         /* vary view */
196         hp->tumble.theta += hp->tumble.dtheta;
197         hp->tumble.phi += hp->tumble.dphi;
198         sint = sin(hp->tumble.theta);
199         cost = cos(hp->tumble.theta);
200         sinp = sin(hp->tumble.phi);
201         cosp = cos(hp->tumble.phi);
202
203         while (k--) {
204                 oldj = hp->j;
205                 oldi = hp->i;
206
207                 hp->j = oldi;
208                 hp->i = (1 - hp->c) * cos(M_PI * hp->a * oldj) + hp->c * hp->b;
209                 hp->b = oldj;
210
211                 xp->x = (short)
212                   (hp->maxx / 2 * (1
213                                                    + sint*hp->j + cost*cosp*hp->i - cost*sinp*hp->b));
214                 xp->y = (short)
215                   (hp->maxy / 2 * (1
216                                                    - cost*hp->j + sint*cosp*hp->i - sint*sinp*hp->b));
217                 xp++;
218         }
219
220         MI_IS_DRAWN(mi) = True;
221
222     if (MI_COUNT(mi) < 1) MI_COUNT(mi) = 1;
223         if (hp->pointBuffer[erase] == NULL) {
224                 if ((hp->pointBuffer[erase] = (XPoint *) malloc(MI_COUNT(mi) *
225                                 sizeof (XPoint))) == NULL) {
226                         free_thornbird(mi);
227                         return;
228                 }
229         } else {
230                 XSetForeground(dsp, gc, MI_BLACK_PIXEL(mi));
231                 XDrawPoints(dsp, win, gc, hp->pointBuffer[erase],
232                             batchcount, CoordModeOrigin);
233         }
234         if (MI_NPIXELS(mi) > 2) {
235                 XSetForeground(dsp, gc, MI_PIXEL(mi, hp->pix));
236 #if 0
237                 if (erase == 0) /* change colours after "cycles" cycles */
238 #else
239         if (!((hp->inc + 1) % (1 + (MI_CYCLES(mi) / 3)))) /* jwz: sooner */
240 #endif
241                         if (++hp->pix >= MI_NPIXELS(mi))
242                                 hp->pix = 0;
243         } else
244                 XSetForeground(dsp, gc, MI_WHITE_PIXEL(mi));
245
246         XDrawPoints(dsp, win, gc, hp->pointBuffer[current],
247                     batchcount, CoordModeOrigin);
248         hp->inc++;
249 }
250
251 ENTRYPOINT void
252 reshape_thornbird(ModeInfo * mi, int width, int height)
253 {
254   XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
255   init_thornbird (mi);
256 }
257
258 ENTRYPOINT void
259 refresh_thornbird (ModeInfo * mi)
260 {
261         MI_CLEARWINDOW(mi);
262 }
263
264
265 XSCREENSAVER_MODULE ("Thornbird", thornbird)
266
267 #endif /* MODE_thornbird */