summaryrefslogtreecommitdiffstats
path: root/hacks/thornbird.c
blob: 07e0cbde5e5dd06dc5ed6e366b97e962dc4e24b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* -*- Mode: C; tab-width: 4 -*- */
/* thornbird --- continuously varying Thornbird set */

#if 0
static const char sccsid[] = "@(#)thornbird.c	5.00 2000/11/01 xlockmore";
#endif

/*-
 * Copyright (c) 1996 by Tim Auckland <tda10.geo@yahoo.com>
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * This file is provided AS IS with no warranties of any kind.  The author
 * shall have no liability with respect to the infringement of copyrights,
 * trade secrets or any patents by this file or any part thereof.  In no
 * event will the author be liable for any lost revenue or profits or
 * other special, indirect and consequential damages.
 *
 * "thornbird" shows a view of the "Bird in a Thornbush" fractal,
 * continuously varying the three free parameters.
 *
 * Revision History:
 * 01-Nov-2000: Allocation checks
 * 04-Jun-1999: 3D tumble added by Tim Auckland
 * 31-Jul-1997: Adapted from discrete.c Copyright (c) 1996 by Tim Auckland
 */

#ifdef STANDALONE
# define MODE_thornbird
#define DEFAULTS	"*delay:    10000 \n" \
					"*count:    100   \n" \
					 "*cycles:  400   \n" \
					 "*ncolors: 64    \n" \
					 "*fpsSolid: true    \n" \
					"*ignoreRotation: True \n" \
				    "*lowrez: True \n" \

# define BRIGHT_COLORS
# define release_thornbird 0
# define reshape_thornbird 0
# define thornbird_handle_event 0
# include "xlockmore.h"		/* in xscreensaver distribution */
#else /* STANDALONE */
# include "xlock.h"		/* in xlockmore distribution */
#endif /* STANDALONE */

#ifdef MODE_thornbird

ENTRYPOINT ModeSpecOpt thornbird_opts =
{0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};

#ifdef USE_MODULES
ModStruct   thornbird_description =
{"thornbird", "init_thornbird", "draw_thornbird", (char *) NULL,
 "refresh_thornbird", "init_thornbird", "free_thornbird", &thornbird_opts,
 1000, 800, 16, 1, 64, 1.0, "",
 "Shows an animated Bird in a Thorn Bush fractal map", 0, NULL};

#endif

#define balance_rand(v)	((LRAND()/MAXRAND*(v))-((v)/2))	/* random around 0 */

typedef struct {
	int         maxx;
	int         maxy;	/* max of the screen */
	double      a;
	double      b;
	double      c;
	double      d;
	double      e;
	double      i;
	double      j;		/* thornbird parameters */
    struct {
	  double  f1;
	  double  f2;
	}           liss;
    struct {
	  double  theta;
	  double  dtheta;
	  double  phi;
	  double  dphi;
	}           tumble;
    int         inc;
	int         pix;
	int         count;
	int         nbuffers;
	XPoint    **pointBuffer;	/* pointer for XDrawPoints */
} thornbirdstruct;

static thornbirdstruct *thornbirds = (thornbirdstruct *) NULL;

ENTRYPOINT void
free_thornbird(ModeInfo * mi)
{
	thornbirdstruct *hp = &thornbirds[MI_SCREEN(mi)];
	if (hp->pointBuffer != NULL) {
		int         buffer;

		for (buffer = 0; buffer < hp->nbuffers; buffer++)
			if (hp->pointBuffer[buffer] != NULL)
				(void) free((void *) hp->pointBuffer[buffer]);
		(void) free((void *) hp->pointBuffer);
		hp->pointBuffer = (XPoint **) NULL;
	}
}

ENTRYPOINT void
init_thornbird (ModeInfo * mi)
{
	thornbirdstruct *hp;

	MI_INIT (mi, thornbirds);
	hp = &thornbirds[MI_SCREEN(mi)];


	hp->maxx = MI_WIDTH(mi);
	hp->maxy = MI_HEIGHT(mi);

	hp->b = 0.1;
	hp->i = hp->j = 0.1;

	hp->pix = 0;
	hp->inc = 0;

	hp->nbuffers = MI_CYCLES(mi);

	if (hp->pointBuffer == NULL)
		if ((hp->pointBuffer = (XPoint **) calloc(MI_CYCLES(mi),
				sizeof (XPoint *))) == NULL) {
			free_thornbird(mi);
			return;
		}

	if (hp->pointBuffer[0] == NULL)
		if ((hp->pointBuffer[0] = (XPoint *) malloc(MI_COUNT(mi) *
				sizeof (XPoint))) == NULL) {
			free_thornbird(mi);
			return;
		}

	/* select frequencies for parameter variation */
	hp->liss.f1 = LRAND() % 5000;
	hp->liss.f2 = LRAND() % 2000;

	/* choose random 3D tumbling */
	hp->tumble.theta = 0;
	hp->tumble.phi = 0;
	hp->tumble.dtheta = balance_rand(0.001);
	hp->tumble.dphi = balance_rand(0.005);

	/* Clear the background. */
	MI_CLEARWINDOW(mi);

	hp->count = 0;
}


ENTRYPOINT void
draw_thornbird(ModeInfo * mi)
{
	Display    *dsp = MI_DISPLAY(mi);
	Window      win = MI_WINDOW(mi);
	double      oldj, oldi;
	int         batchcount = MI_COUNT(mi);
	int         k;
	XPoint     *xp;
	GC          gc = MI_GC(mi);
	int         erase;
	int         current;

	double      sint, cost, sinp, cosp;
	thornbirdstruct *hp;

	if (thornbirds == NULL)
		return;
	hp = &thornbirds[MI_SCREEN(mi)];
	if (hp->pointBuffer == NULL)
		return;

	erase = (hp->inc + 1) % MI_CYCLES(mi);
	current = hp->inc % MI_CYCLES(mi);
	k = batchcount;


	xp = hp->pointBuffer[current];

	/* vary papameters */
	hp->a = 1.99 + (0.4 * sin(hp->inc / hp->liss.f1) +
					0.05 * cos(hp->inc / hp->liss.f2));
	hp->c = 0.80 + (0.15 * cos(hp->inc / hp->liss.f1) +
					0.05 * sin(hp->inc / hp->liss.f2));

	/* vary view */
	hp->tumble.theta += hp->tumble.dtheta;
	hp->tumble.phi += hp->tumble.dphi;
	sint = sin(hp->tumble.theta);
	cost = cos(hp->tumble.theta);
	sinp = sin(hp->tumble.phi);
	cosp = cos(hp->tumble.phi);

	while (k--) {
		oldj = hp->j;
		oldi = hp->i;

		hp->j = oldi;
		hp->i = (1 - hp->c) * cos(M_PI * hp->a * oldj) + hp->c * hp->b;
		hp->b = oldj;

		xp->x = (short)
		  (hp->maxx / 2 * (1
						   + sint*hp->j + cost*cosp*hp->i - cost*sinp*hp->b));
		xp->y = (short)
		  (hp->maxy / 2 * (1
						   - cost*hp->j + sint*cosp*hp->i - sint*sinp*hp->b));
		xp++;
	}

	MI_IS_DRAWN(mi) = True;

    if (MI_COUNT(mi) < 1) MI_COUNT(mi) = 1;
	if (hp->pointBuffer[erase] == NULL) {
		if ((hp->pointBuffer[erase] = (XPoint *) malloc(MI_COUNT(mi) *
				sizeof (XPoint))) == NULL) {
			free_thornbird(mi);
			return;
		}
	} else {
		XSetForeground(dsp, gc, MI_BLACK_PIXEL(mi));
		XDrawPoints(dsp, win, gc, hp->pointBuffer[erase],
			    batchcount, CoordModeOrigin);
	}
	if (MI_NPIXELS(mi) > 2) {
		XSetForeground(dsp, gc, MI_PIXEL(mi, hp->pix));
#if 0
		if (erase == 0) /* change colours after "cycles" cycles */
#else
        if (!((hp->inc + 1) % (1 + (MI_CYCLES(mi) / 3)))) /* jwz: sooner */
#endif
			if (++hp->pix >= MI_NPIXELS(mi))
				hp->pix = 0;
	} else
		XSetForeground(dsp, gc, MI_WHITE_PIXEL(mi));

	XDrawPoints(dsp, win, gc, hp->pointBuffer[current],
		    batchcount, CoordModeOrigin);
	hp->inc++;
}

#ifndef STANDALONE
ENTRYPOINT void
refresh_thornbird (ModeInfo * mi)
{
	MI_CLEARWINDOW(mi);
}
#endif


XSCREENSAVER_MODULE ("Thornbird", thornbird)

#endif /* MODE_thornbird */