ftp://ftp.demon.nl/disk1/redhat-contrib/libc5/SRPMS/xscreensaver-2.14-1.src.rpm
[xscreensaver] / hacks / hopalong.c
1 /* -*- Mode: C; tab-width: 4 -*-
2  * hop --- real plane fractals.
3  */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)hop.c 4.02 97/04/01 xlockmore";
6 #endif
7
8 /* Copyright (c) 1988-91 by Patrick J. Naughton.
9  *
10  * Permission to use, copy, modify, and distribute this software and its
11  * documentation for any purpose and without fee is hereby granted,
12  * provided that the above copyright notice appear in all copies and that
13  * both that copyright notice and this permission notice appear in
14  * supporting documentation.
15  *
16  * This file is provided AS IS with no warranties of any kind.  The author
17  * shall have no liability with respect to the infringement of copyrights,
18  * trade secrets or any patents by this file or any part thereof.  In no
19  * event will the author be liable for any lost revenue or profits or
20  * other special, indirect and consequential damages.
21  *
22  * Revision History:
23  * Changes of David Bagley <bagleyd@bigfoot.com>
24  * 10-May-97: jwz@netscape.com: ported from xlockmore 4.03a10 to be a 
25  *                        standalone program and thus usable with xscreensaver
26  *                        (I threw away my 1992 port and started over.)
27  * 27-Jul-95: added Peter de Jong's hop from Scientific American
28  *            July 87 p. 111.  Sometimes they are amazing but there are a
29  *            few duds (I did not see a pattern in the parameters).
30  * 29-Mar-95: changed name from hopalong to hop
31  * 09-Dec-94: added sine hop
32  *
33  * (12-Aug-92: jwz@lucid.com: made xlock version run standalone.)
34  *
35  * Changes of Patrick J. Naughton
36  * 29-Oct-90: fix bad (int) cast.
37  * 29-Jul-90: support for multiple screens.
38  * 08-Jul-90: new timing and colors and new algorithm for fractals.
39  * 15-Dec-89: Fix for proper skipping of {White,Black}Pixel() in colors.
40  * 08-Oct-89: Fixed long standing typo bug in RandomInitHop();
41  *            Fixed bug in memory allocation in init_hop();
42  *            Moved seconds() to an extern.
43  *            Got rid of the % mod since .mod is slow on a sparc.
44  * 20-Sep-89: Lint.
45  * 31-Aug-88: Forked from xlock.c for modularity.
46  * 23-Mar-88: Coded HOPALONG routines from Scientific American Sept. 86 p. 14.
47  */
48
49 #ifdef STANDALONE
50 # define PROGCLASS                                      "Hopalong"
51 # define HACK_INIT                                      init_hop
52 # define HACK_DRAW                                      draw_hop
53 # define HACK_FREE                                      release_hop
54 # define hop_opts                                       xlockmore_opts
55 # define DEFAULTS       "*count:                1000    \n"                     \
56                                         "*cycles:               2500    \n"                     \
57                                         "*delay:                10000   \n"                     \
58                                         "*ncolors:              200     \n"                     \
59                                         "*eraseSpeed:   400 \n"                         \
60                                         "*eraseMode:    -1 \n"
61 # define SMOOTH_COLORS
62 # include "xlockmore.h"                         /* from the xscreensaver distribution */
63 # include "erase.h"
64 #else  /* !STANDALONE */
65 # include "xlock.h"                                     /* from the xlockmore distribution */
66 #endif /* !STANDALONE */
67
68 static Bool jong;
69 static Bool sine;
70
71 #define DEF_JONG "False"
72 #define DEF_SINE "False"
73
74 static XrmOptionDescRec opts[] =
75 {
76         {"-jong", ".hop.jong", XrmoptionNoArg, (caddr_t) "on"},
77         {"+jong", ".hop.jong", XrmoptionNoArg, (caddr_t) "off"},
78         {"-sine", ".hop.sine", XrmoptionNoArg, (caddr_t) "on"},
79         {"+sine", ".hop.sine", XrmoptionNoArg, (caddr_t) "off"}
80 };
81 static argtype vars[] =
82 {
83         {(caddr_t *) & jong, "jong", "Jong", DEF_JONG, t_Bool},
84         {(caddr_t *) & sine, "sine", "Sine", DEF_SINE, t_Bool}
85 };
86 static OptionStruct desc[] =
87 {
88         {"-/+jong", "turn on/off jong format"},
89         {"-/+sine", "turn on/off sine format"}
90 };
91
92 ModeSpecOpt hop_opts = { 4, opts, 2, vars, desc };
93
94
95 #define SQRT 0
96 #define JONG 1
97 #define SINE 2
98 #ifdef OFFENDING
99 #define OPS 2                   /* Sine might be too close to a Swastika for some... */
100 #else
101 #define OPS 3
102 #endif
103
104 typedef struct {
105         int         centerx;
106         int         centery;    /* center of the screen */
107         double      a;
108         double      b;
109         double      c;
110         double      d;
111         double      i;
112         double      j;          /* hopalong parameters */
113         int         inc;
114         int         pix;
115         int         op;
116         int         count;
117         int         bufsize;
118 } hopstruct;
119
120 static hopstruct *hops = NULL;
121 static XPoint *pointBuffer = 0; /* pointer for XDrawPoints */
122
123 void
124 init_hop(ModeInfo * mi)
125 {
126         Display    *display = MI_DISPLAY(mi);
127         GC          gc = MI_GC(mi);
128         hopstruct  *hp;
129         double      range;
130
131         if (hops == NULL) {
132                 if ((hops = (hopstruct *) calloc(MI_NUM_SCREENS(mi),
133                                                  sizeof (hopstruct))) == NULL)
134                         return;
135         }
136         hp = &hops[MI_SCREEN(mi)];
137
138         hp->centerx = MI_WIN_WIDTH(mi) / 2;
139         hp->centery = MI_WIN_HEIGHT(mi) / 2;
140         /* Make the other operations less common since they are less interesting */
141         if (MI_WIN_IS_FULLRANDOM(mi)) {
142           hp->op = NRAND(OPS+2); /* jwz: make the others a bit more likely. */
143           if (hp->op >= OPS)
144                 hp->op = SQRT;
145         } else {
146                 hp->op = SQRT;
147                 if (jong)
148                         hp->op = JONG;
149                 else if (sine)
150                         hp->op = SINE;
151         }
152         switch (hp->op) {
153                 case SQRT:
154                         range = sqrt((double) hp->centerx * hp->centerx +
155                                      (double) hp->centery * hp->centery) / (10.0 + NRAND(10));
156
157                         hp->a = (LRAND() / MAXRAND) * range - range / 2.0;
158                         hp->b = (LRAND() / MAXRAND) * range - range / 2.0;
159                         hp->c = (LRAND() / MAXRAND) * range - range / 2.0;
160                         if (LRAND() & 1)
161                                 hp->c = 0.0;
162                         break;
163                 case JONG:
164                         hp->a = (LRAND() / MAXRAND) * 2.0 * M_PI - M_PI;
165                         hp->b = (LRAND() / MAXRAND) * 2.0 * M_PI - M_PI;
166                         hp->c = (LRAND() / MAXRAND) * 2.0 * M_PI - M_PI;
167                         hp->d = (LRAND() / MAXRAND) * 2.0 * M_PI - M_PI;
168                         break;
169                 case SINE:
170                         hp->a = M_PI + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.7;
171                         break;
172         }
173         if (MI_NPIXELS(mi) > 2)
174                 hp->pix = NRAND(MI_NPIXELS(mi));
175         hp->i = hp->j = 0.0;
176         hp->inc = (int) ((LRAND() / MAXRAND) * 200) - 100;
177         hp->bufsize = MI_BATCHCOUNT(mi);
178
179         if (!pointBuffer)
180                 pointBuffer = (XPoint *) malloc(hp->bufsize * sizeof (XPoint));
181
182         XClearWindow(display, MI_WINDOW(mi));
183
184         XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
185         hp->count = 0;
186 }
187
188
189 void
190 draw_hop(ModeInfo * mi)
191 {
192         hopstruct  *hp = &hops[MI_SCREEN(mi)];
193         double      oldj, oldi;
194         XPoint     *xp = pointBuffer;
195         int         k = hp->bufsize;
196
197         hp->inc++;
198         if (MI_NPIXELS(mi) > 2) {
199                 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_PIXEL(mi, hp->pix));
200                 if (++hp->pix >= MI_NPIXELS(mi))
201                         hp->pix = 0;
202         }
203         while (k--) {
204                 oldj = hp->j;
205                 switch (hp->op) {
206                         case SQRT:
207                                 oldi = hp->i + hp->inc;
208                                 hp->j = hp->a - hp->i;
209                                 hp->i = oldj + ((hp->i < 0)
210                                            ? sqrt(fabs(hp->b * oldi - hp->c))
211                                         : -sqrt(fabs(hp->b * oldi - hp->c)));
212                                 xp->x = hp->centerx + (int) (hp->i + hp->j);
213                                 xp->y = hp->centery - (int) (hp->i - hp->j);
214                                 break;
215                         case JONG:
216                                 if (hp->centerx > 0)
217                                         oldi = hp->i + 4 * hp->inc / hp->centerx;
218                                 else
219                                         oldi = hp->i;
220                                 hp->j = sin(hp->c * hp->i) - cos(hp->d * hp->j);
221                                 hp->i = sin(hp->a * oldj) - cos(hp->b * oldi);
222                                 xp->x = hp->centerx + (int) (hp->centerx * (hp->i + hp->j) / 4.0);
223                                 xp->y = hp->centery - (int) (hp->centery * (hp->i - hp->j) / 4.0);
224                                 break;
225                         case SINE:
226                                 oldi = hp->i + hp->inc;
227                                 hp->j = hp->a - hp->i;
228                                 hp->i = oldj - sin(oldi);
229                                 xp->x = hp->centerx + (int) (hp->i + hp->j);
230                                 xp->y = hp->centery - (int) (hp->i - hp->j);
231                                 break;
232                 }
233                 xp++;
234         }
235         XDrawPoints(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
236                     pointBuffer, hp->bufsize, CoordModeOrigin);
237         if (++hp->count > MI_CYCLES(mi)) {
238 #ifdef STANDALONE
239           erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
240 #endif /* STANDALONE */
241           init_hop(mi);
242         }
243 }
244
245 void
246 release_hop(ModeInfo * mi)
247 {
248         if (hops != NULL) {
249                 (void) free((void *) hops);
250                 hops = NULL;
251         }
252         if (pointBuffer) {
253                 (void) free((void *) pointBuffer);
254                 pointBuffer = NULL;
255         }
256 }
257
258 void
259 refresh_hop(ModeInfo * mi)
260 {
261         XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
262 }