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