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