cb663ac33a0603791ed884131df85b6b080ccce6
[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 # define SMOOTH_COLORS
60 # include "xlockmore.h"                         /* from the xscreensaver distribution */
61 # include "erase.h"
62 #else  /* !STANDALONE */
63 # include "xlock.h"                                     /* from the xlockmore distribution */
64 #endif /* !STANDALONE */
65
66 static Bool jong;
67 static Bool sine;
68
69 #define DEF_JONG "False"
70 #define DEF_SINE "False"
71
72 static XrmOptionDescRec opts[] =
73 {
74         {"-jong", ".hop.jong", XrmoptionNoArg, (caddr_t) "on"},
75         {"+jong", ".hop.jong", XrmoptionNoArg, (caddr_t) "off"},
76         {"-sine", ".hop.sine", XrmoptionNoArg, (caddr_t) "on"},
77         {"+sine", ".hop.sine", XrmoptionNoArg, (caddr_t) "off"}
78 };
79 static argtype vars[] =
80 {
81         {(caddr_t *) & jong, "jong", "Jong", DEF_JONG, t_Bool},
82         {(caddr_t *) & sine, "sine", "Sine", DEF_SINE, t_Bool}
83 };
84 static OptionStruct desc[] =
85 {
86         {"-/+jong", "turn on/off jong format"},
87         {"-/+sine", "turn on/off sine format"}
88 };
89
90 ModeSpecOpt hop_opts = { 4, opts, 2, vars, desc };
91
92
93 #define SQRT 0
94 #define JONG 1
95 #define SINE 2
96 #ifdef OFFENDING
97 #define OPS 2                   /* Sine might be too close to a Swastika for some... */
98 #else
99 #define OPS 3
100 #endif
101
102 typedef struct {
103         int         centerx;
104         int         centery;    /* center of the screen */
105         double      a;
106         double      b;
107         double      c;
108         double      d;
109         double      i;
110         double      j;          /* hopalong parameters */
111         int         inc;
112         int         pix;
113         int         op;
114         int         count;
115         int         bufsize;
116 } hopstruct;
117
118 static hopstruct *hops = NULL;
119 static XPoint *pointBuffer = 0; /* pointer for XDrawPoints */
120
121 void
122 init_hop(ModeInfo * mi)
123 {
124         Display    *display = MI_DISPLAY(mi);
125         GC          gc = MI_GC(mi);
126         hopstruct  *hp;
127         double      range;
128
129         if (hops == NULL) {
130                 if ((hops = (hopstruct *) calloc(MI_NUM_SCREENS(mi),
131                                                  sizeof (hopstruct))) == NULL)
132                         return;
133         }
134         hp = &hops[MI_SCREEN(mi)];
135
136         hp->centerx = MI_WIN_WIDTH(mi) / 2;
137         hp->centery = MI_WIN_HEIGHT(mi) / 2;
138         /* Make the other operations less common since they are less interesting */
139         if (MI_WIN_IS_FULLRANDOM(mi)) {
140           hp->op = NRAND(OPS+2); /* jwz: make the others a bit more likely. */
141           if (hp->op >= OPS)
142                 hp->op = SQRT;
143         } else {
144                 hp->op = SQRT;
145                 if (jong)
146                         hp->op = JONG;
147                 else if (sine)
148                         hp->op = SINE;
149         }
150         switch (hp->op) {
151                 case SQRT:
152                         range = sqrt((double) hp->centerx * hp->centerx +
153                                      (double) hp->centery * hp->centery) / (10.0 + NRAND(10));
154
155                         hp->a = (LRAND() / MAXRAND) * range - range / 2.0;
156                         hp->b = (LRAND() / MAXRAND) * range - range / 2.0;
157                         hp->c = (LRAND() / MAXRAND) * range - range / 2.0;
158                         if (LRAND() & 1)
159                                 hp->c = 0.0;
160                         break;
161                 case JONG:
162                         hp->a = (LRAND() / MAXRAND) * 2.0 * M_PI - M_PI;
163                         hp->b = (LRAND() / MAXRAND) * 2.0 * M_PI - M_PI;
164                         hp->c = (LRAND() / MAXRAND) * 2.0 * M_PI - M_PI;
165                         hp->d = (LRAND() / MAXRAND) * 2.0 * M_PI - M_PI;
166                         break;
167                 case SINE:
168                         hp->a = M_PI + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.7;
169                         break;
170         }
171         if (MI_NPIXELS(mi) > 2)
172                 hp->pix = NRAND(MI_NPIXELS(mi));
173         hp->i = hp->j = 0.0;
174         hp->inc = (int) ((LRAND() / MAXRAND) * 200) - 100;
175         hp->bufsize = MI_BATCHCOUNT(mi);
176
177         if (!pointBuffer)
178                 pointBuffer = (XPoint *) malloc(hp->bufsize * sizeof (XPoint));
179
180         XClearWindow(display, MI_WINDOW(mi));
181
182         XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
183         hp->count = 0;
184 }
185
186
187 void
188 draw_hop(ModeInfo * mi)
189 {
190         hopstruct  *hp = &hops[MI_SCREEN(mi)];
191         double      oldj, oldi;
192         XPoint     *xp = pointBuffer;
193         int         k = hp->bufsize;
194
195         hp->inc++;
196         if (MI_NPIXELS(mi) > 2) {
197                 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_PIXEL(mi, hp->pix));
198                 if (++hp->pix >= MI_NPIXELS(mi))
199                         hp->pix = 0;
200         }
201         while (k--) {
202                 oldj = hp->j;
203                 switch (hp->op) {
204                         case SQRT:
205                                 oldi = hp->i + hp->inc;
206                                 hp->j = hp->a - hp->i;
207                                 hp->i = oldj + ((hp->i < 0)
208                                            ? sqrt(fabs(hp->b * oldi - hp->c))
209                                         : -sqrt(fabs(hp->b * oldi - hp->c)));
210                                 xp->x = hp->centerx + (int) (hp->i + hp->j);
211                                 xp->y = hp->centery - (int) (hp->i - hp->j);
212                                 break;
213                         case JONG:
214                                 if (hp->centerx > 0)
215                                         oldi = hp->i + 4 * hp->inc / hp->centerx;
216                                 else
217                                         oldi = hp->i;
218                                 hp->j = sin(hp->c * hp->i) - cos(hp->d * hp->j);
219                                 hp->i = sin(hp->a * oldj) - cos(hp->b * oldi);
220                                 xp->x = hp->centerx + (int) (hp->centerx * (hp->i + hp->j) / 4.0);
221                                 xp->y = hp->centery - (int) (hp->centery * (hp->i - hp->j) / 4.0);
222                                 break;
223                         case SINE:
224                                 oldi = hp->i + hp->inc;
225                                 hp->j = hp->a - hp->i;
226                                 hp->i = oldj - sin(oldi);
227                                 xp->x = hp->centerx + (int) (hp->i + hp->j);
228                                 xp->y = hp->centery - (int) (hp->i - hp->j);
229                                 break;
230                 }
231                 xp++;
232         }
233         XDrawPoints(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
234                     pointBuffer, hp->bufsize, CoordModeOrigin);
235         if (++hp->count > MI_CYCLES(mi)) {
236 #ifdef STANDALONE
237           erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
238 #endif /* STANDALONE */
239           init_hop(mi);
240         }
241 }
242
243 void
244 release_hop(ModeInfo * mi)
245 {
246         if (hops != NULL) {
247                 (void) free((void *) hops);
248                 hops = NULL;
249         }
250         if (pointBuffer) {
251                 (void) free((void *) pointBuffer);
252                 pointBuffer = NULL;
253         }
254 }
255
256 void
257 refresh_hop(ModeInfo * mi)
258 {
259         XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
260 }