1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* thornbird --- continuously varying Thornbird set */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)thornbird.c 5.00 2000/11/01 xlockmore";
10 * Copyright (c) 1996 by Tim Auckland <Tim.Auckland@Procket.com>
12 * Permission to use, copy, modify, and distribute this software and its
13 * documentation for any purpose and without fee is hereby granted,
14 * provided that the above copyright notice appear in all copies and that
15 * both that copyright notice and this permission notice appear in
16 * supporting documentation.
18 * This file is provided AS IS with no warranties of any kind. The author
19 * shall have no liability with respect to the infringement of copyrights,
20 * trade secrets or any patents by this file or any part thereof. In no
21 * event will the author be liable for any lost revenue or profits or
22 * other special, indirect and consequential damages.
24 * "thornbird" shows a view of the "Bird in a Thornbush" fractal,
25 * continuously varying the three free parameters.
28 * 01-Nov-2000: Allocation checks
29 * 04-Jun-1999: 3D tumble added by Tim Auckland
30 * 31-Jul-1997: Adapted from discrete.c Copyright (c) 1996 by Tim Auckland
34 #define MODE_thornbird
35 #define PROGCLASS "Thornbird"
36 #define HACK_INIT init_thornbird
37 #define HACK_DRAW draw_thornbird
38 #define thornbird_opts xlockmore_opts
39 #define DEFAULTS "*delay: 10000 \n" \
44 #include "xlockmore.h" /* in xscreensaver distribution */
45 #else /* STANDALONE */
46 #include "xlock.h" /* in xlockmore distribution */
47 #endif /* STANDALONE */
51 ModeSpecOpt thornbird_opts =
52 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
55 ModStruct thornbird_description =
56 {"thornbird", "init_thornbird", "draw_thornbird", "release_thornbird",
57 "refresh_thornbird", "init_thornbird", (char *) NULL, þbird_opts,
58 1000, 800, 16, 1, 64, 1.0, "",
59 "Shows an animated Bird in a Thorn Bush fractal map", 0, NULL};
63 #define balance_rand(v) ((LRAND()/MAXRAND*(v))-((v)/2)) /* random around 0 */
67 int maxy; /* max of the screen */
74 double j; /* thornbird parameters */
89 XPoint **pointBuffer; /* pointer for XDrawPoints */
92 static thornbirdstruct *thornbirds = (thornbirdstruct *) NULL;
95 free_thornbird(thornbirdstruct *hp)
97 if (hp->pointBuffer != NULL) {
100 for (buffer = 0; buffer < hp->nbuffers; buffer++)
101 if (hp->pointBuffer[buffer] != NULL)
102 (void) free((void *) hp->pointBuffer[buffer]);
103 (void) free((void *) hp->pointBuffer);
104 hp->pointBuffer = (XPoint **) NULL;
109 init_thornbird(ModeInfo * mi)
113 if (thornbirds == NULL) {
115 (thornbirdstruct *) calloc(MI_NUM_SCREENS(mi),
116 sizeof (thornbirdstruct))) == NULL)
119 hp = þbirds[MI_SCREEN(mi)];
122 hp->maxx = MI_WIDTH(mi);
123 hp->maxy = MI_HEIGHT(mi);
131 hp->nbuffers = MI_CYCLES(mi);
133 if (hp->pointBuffer == NULL)
134 if ((hp->pointBuffer = (XPoint **) calloc(MI_CYCLES(mi),
135 sizeof (XPoint *))) == NULL) {
140 if (hp->pointBuffer[0] == NULL)
141 if ((hp->pointBuffer[0] = (XPoint *) malloc(MI_COUNT(mi) *
142 sizeof (XPoint))) == NULL) {
147 /* select frequencies for parameter variation */
148 hp->liss.f1 = LRAND() % 5000;
149 hp->liss.f2 = LRAND() % 2000;
151 /* choose random 3D tumbling */
152 hp->tumble.theta = 0;
154 hp->tumble.dtheta = balance_rand(0.001);
155 hp->tumble.dphi = balance_rand(0.005);
157 /* Clear the background. */
165 draw_thornbird(ModeInfo * mi)
167 Display *dsp = MI_DISPLAY(mi);
168 Window win = MI_WINDOW(mi);
170 int batchcount = MI_COUNT(mi);
177 double sint, cost, sinp, cosp;
180 if (thornbirds == NULL)
182 hp = þbirds[MI_SCREEN(mi)];
183 if (hp->pointBuffer == NULL)
186 erase = (hp->inc + 1) % MI_CYCLES(mi);
187 current = hp->inc % MI_CYCLES(mi);
191 xp = hp->pointBuffer[current];
193 /* vary papameters */
194 hp->a = 1.99 + (0.4 * sin(hp->inc / hp->liss.f1) +
195 0.05 * cos(hp->inc / hp->liss.f2));
196 hp->c = 0.80 + (0.15 * cos(hp->inc / hp->liss.f1) +
197 0.05 * sin(hp->inc / hp->liss.f2));
200 hp->tumble.theta += hp->tumble.dtheta;
201 hp->tumble.phi += hp->tumble.dphi;
202 sint = sin(hp->tumble.theta);
203 cost = cos(hp->tumble.theta);
204 sinp = sin(hp->tumble.phi);
205 cosp = cos(hp->tumble.phi);
212 hp->i = (1 - hp->c) * cos(M_PI * hp->a * oldj) + hp->c * hp->b;
217 + sint*hp->j + cost*cosp*hp->i - cost*sinp*hp->b));
220 - cost*hp->j + sint*cosp*hp->i - sint*sinp*hp->b));
224 MI_IS_DRAWN(mi) = True;
226 if (hp->pointBuffer[erase] == NULL) {
227 if ((hp->pointBuffer[erase] = (XPoint *) malloc(MI_COUNT(mi) *
228 sizeof (XPoint))) == NULL) {
233 XSetForeground(dsp, gc, MI_BLACK_PIXEL(mi));
234 XDrawPoints(dsp, win, gc, hp->pointBuffer[erase],
235 batchcount, CoordModeOrigin);
237 if (MI_NPIXELS(mi) > 2) {
238 XSetForeground(dsp, gc, MI_PIXEL(mi, hp->pix));
239 if (erase == 0) /* change colours after "cycles" cycles */
240 if (++hp->pix >= MI_NPIXELS(mi))
243 XSetForeground(dsp, gc, MI_WHITE_PIXEL(mi));
245 XDrawPoints(dsp, win, gc, hp->pointBuffer[current],
246 batchcount, CoordModeOrigin);
252 release_thornbird(ModeInfo * mi)
254 if (thornbirds != NULL) {
257 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
258 free_thornbird(þbirds[screen]);
259 (void) free((void *) thornbirds);
260 thornbirds = (thornbirdstruct *) NULL;
265 refresh_thornbird(ModeInfo * mi)
270 #endif /* MODE_thornbird */