From http://www.jwz.org/xscreensaver/xscreensaver-5.38.tar.gz
[xscreensaver] / hacks / sphere.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* sphere --- a bunch of shaded spheres */
3
4 #if 0
5 static const char sccsid[] = "@(#)sphere.c      5.00 2000/11/01 xlockmore";
6 #endif
7
8 /*-
9  * Copyright (c) 1988 by Sun Microsystems
10  *
11  * Permission to use, copy, modify, and distribute this software and its
12  * documentation for any purpose and without fee is hereby granted,
13  * provided that the above copyright notice appear in all copies and that
14  * both that copyright notice and this permission notice appear in
15  * supporting documentation.
16  *
17  * This file is provided AS IS with no warranties of any kind.  The author
18  * shall have no liability with respect to the infringement of copyrights,
19  * trade secrets or any patents by this file or any part thereof.  In no
20  * event will the author be liable for any lost revenue or profits or
21  * other special, indirect and consequential damages.
22  *
23  * Revision History:
24  * 01-Nov-2000: Allocation checks
25  * 30-May-1997: <jwz@jwz.org> made it go vertically as well as horizontally.
26  * 27-May-1997: <jwz@jwz.org> turned into a standalone program.
27  * 02-Sep-1993: xlock version David Bagley <bagleyd@tux.org>
28  * 1988: Revised to use SunView canvas instead of gfxsw Sun Microsystems
29  * 1982: Orignal Algorithm Tom Duff Lucasfilm Ltd.
30  */
31
32 /*-
33  * original copyright
34  * **************************************************************************
35  * Copyright 1988 by Sun Microsystems, Inc. Mountain View, CA.
36  *
37  * All Rights Reserved
38  *
39  * Permission to use, copy, modify, and distribute this software and its
40  * documentation for any purpose and without fee is hereby granted, provided
41  * that the above copyright notice appear in all copies and that both that
42  * copyright notice and this permission notice appear in supporting
43  * documentation, and that the names of Sun or MIT not be used in advertising
44  * or publicity pertaining to distribution of the software without specific
45  * prior written permission. Sun and M.I.T. make no representations about the
46  * suitability of this software for any purpose. It is provided "as is"
47  * without any express or implied warranty.
48  *
49  * SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
51  * IN NO EVENT SHALL SUN BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
52  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
53  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
54  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
55  * SOFTWARE.
56  * ***************************************************************************
57  */
58
59 #ifdef STANDALONE
60 # define MODE_sphere
61 #define DEFAULTS        "*delay: 20000 \n" \
62                                         "*cycles: 20 \n" \
63                                         "*size: 0 \n" \
64                                         "*ncolors: 64 \n" \
65                                         "*fpsSolid: true \n" \
66
67 # define BRIGHT_COLORS
68 # define release_sphere 0
69 # define reshape_sphere 0
70 # define sphere_handle_event 0
71 # include "xlockmore.h"         /* from the xscreensaver distribution */
72 #else /* !STANDALONE */
73 # include "xlock.h"             /* from the xlockmore distribution */
74 #endif /* !STANDALONE */
75
76 #ifdef MODE_sphere
77
78 ENTRYPOINT ModeSpecOpt sphere_opts =
79 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
80
81 #ifdef USE_MODULES
82 ModStruct   sphere_description =
83 {"sphere", "init_sphere", "draw_sphere", (char *) NULL,
84  "refresh_sphere", "init_sphere", "free_sphere", &sphere_opts,
85  5000, 1, 20, 0, 64, 1.0, "",
86  "Shows a bunch of shaded spheres", 0, NULL};
87
88 #endif
89
90 /*-
91  * (NX, NY, NZ) is the light source vector -- length should be 100
92  */
93 #define NX 48
94 #define NY (-36)
95 #define NZ 80
96 #define NR 100
97 #define SQRT(a) ((int)sqrt((double)(a)))
98
99 typedef struct {
100         int         width, height;
101         int         radius;
102         int         x0;         /* x center */
103         int         y0;         /* y center */
104         int         color;
105         int         x, y;
106         int         dirx, diry;
107         int         shadowx, shadowy;
108         int         maxx, maxy;
109         XPoint     *points;
110 } spherestruct;
111
112 static spherestruct *spheres = (spherestruct *) NULL;
113
114 ENTRYPOINT void
115 init_sphere(ModeInfo * mi)
116 {
117         spherestruct *sp;
118
119         MI_INIT (mi, spheres);
120         sp = &spheres[MI_SCREEN(mi)];
121
122         if (sp->points != NULL) {
123                 (void) free((void *) sp->points);
124                 sp->points = (XPoint *) NULL;
125         }
126         sp->width = MAX(MI_WIDTH(mi), 4);
127         sp->height = MAX(MI_HEIGHT(mi), 4);
128         if ((sp->points = (XPoint *) malloc(MIN(sp->width, sp->height) *
129                          sizeof (XPoint))) == NULL) {
130                 return;
131         }
132
133         MI_CLEARWINDOW(mi);
134
135         sp->dirx = 1;
136         sp->x = sp->radius;
137         sp->shadowx = (LRAND() & 1) ? 1 : -1;
138         sp->shadowy = (LRAND() & 1) ? 1 : -1;
139 }
140
141 ENTRYPOINT void
142 draw_sphere(ModeInfo * mi)
143 {
144         Display    *display = MI_DISPLAY(mi);
145         GC          gc = MI_GC(mi);
146         int         sqrd, nd;
147         register int minx = 0, maxx = 0, miny = 0, maxy = 0, npts = 0;
148         spherestruct *sp;
149
150         if (spheres == NULL)
151                 return;
152         sp = &spheres[MI_SCREEN(mi)];
153         if (sp->points == NULL)
154                 return;
155
156         MI_IS_DRAWN(mi) = True;
157         if ((sp->dirx && ABS(sp->x) >= sp->radius) ||
158             (sp->diry && ABS(sp->y) >= sp->radius)) {
159                 sp->radius = NRAND(MIN(sp->width / 2, sp->height / 2) - 1) + 1;
160
161                 if (LRAND() & 1) {
162                         sp->dirx = (int) (LRAND() & 1) * 2 - 1;
163                         sp->diry = 0;
164                 } else {
165                         sp->dirx = 0;
166                         sp->diry = (int) (LRAND() & 1) * 2 - 1;
167                 }
168                 sp->x0 = NRAND(sp->width);
169                 sp->y0 = NRAND(sp->height);
170
171                 sp->x = -sp->radius * sp->dirx;
172                 sp->y = -sp->radius * sp->diry;
173
174                 if (MI_NPIXELS(mi) > 2)
175                         sp->color = NRAND(MI_NPIXELS(mi));
176         }
177         if (sp->dirx == 1) {
178                 if (sp->x0 + sp->x < 0)
179                         sp->x = -sp->x0;
180         } else if (sp->dirx == -1) {
181                 if (sp->x0 + sp->x >= sp->width)
182                         sp->x = sp->width - sp->x0 - 1;
183         }
184         if (sp->diry == 1) {
185                 if (sp->y0 + sp->y < 0)
186                         sp->y = -sp->y0;
187         } else if (sp->diry == -1) {
188                 if (sp->y0 + sp->y >= sp->height)
189                         sp->y = sp->height - sp->y0 - 1;
190         }
191         if (sp->dirx) {
192                 sp->maxy = SQRT(sp->radius * sp->radius - sp->x * sp->x);
193                 miny = -sp->maxy;
194                 if (sp->y0 - sp->maxy < 0)
195                         miny = -sp->y0;
196                 maxy = sp->maxy;
197         }
198         if (sp->diry) {
199                 sp->maxx = SQRT(sp->radius * sp->radius - sp->y * sp->y);
200                 minx = -sp->maxx;
201                 if (sp->x0 - sp->maxx < 0)
202                         minx = -sp->x0;
203                 maxx = sp->maxx;
204         }
205         if (sp->dirx) {
206                 if (sp->y0 + sp->maxy >= sp->height)
207                         maxy = sp->height - sp->y0;
208         }
209         if (sp->diry) {
210                 if (sp->x0 + sp->maxx >= sp->width)
211                         maxx = sp->width - sp->x0;
212         }
213         XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
214
215         if (sp->dirx)
216                 XDrawLine(display, MI_WINDOW(mi), gc,
217                 sp->x0 + sp->x, sp->y0 + miny, sp->x0 + sp->x, sp->y0 + maxy);
218         if (sp->diry)
219                 XDrawLine(display, MI_WINDOW(mi), gc,
220                 sp->x0 + minx, sp->y0 + sp->y, sp->x0 + maxx, sp->y0 + sp->y);
221
222         if (MI_NPIXELS(mi) > 2)
223                 XSetForeground(display, gc, MI_PIXEL(mi, sp->color));
224         else
225                 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
226
227         if (sp->dirx) {
228                 sqrd = sp->radius * sp->radius - sp->x * sp->x;
229                 nd = NX * sp->shadowx * sp->x;
230                 for (sp->y = miny; sp->y <= maxy; sp->y++)
231                         if ((NRAND(sp->radius * NR)) <= nd + NY * sp->shadowy * sp->y +
232                             NZ * SQRT(sqrd - sp->y * sp->y)) {
233                                 sp->points[npts].x = sp->x + sp->x0;
234                                 sp->points[npts].y = sp->y + sp->y0;
235                                 npts++;
236                         }
237         }
238         if (sp->diry) {
239                 sqrd = sp->radius * sp->radius - sp->y * sp->y;
240                 nd = NY * sp->shadowy * sp->y;
241                 for (sp->x = minx; sp->x <= maxx; sp->x++)
242                         if ((NRAND(sp->radius * NR)) <= NX * sp->shadowx * sp->x + nd +
243                             NZ * SQRT(sqrd - sp->x * sp->x)) {
244                                 sp->points[npts].x = sp->x + sp->x0;
245                                 sp->points[npts].y = sp->y + sp->y0;
246                                 npts++;
247                         }
248         }
249         XDrawPoints(display, MI_WINDOW(mi), gc, sp->points, npts, CoordModeOrigin);
250         if (sp->dirx == 1) {
251                 sp->x++;
252                 if (sp->x0 + sp->x >= sp->width)
253                         sp->x = sp->radius;
254         } else if (sp->dirx == -1) {
255                 sp->x--;
256                 if (sp->x0 + sp->x < 0)
257                         sp->x = -sp->radius;
258         }
259         if (sp->diry == 1) {
260                 sp->y++;
261                 if (sp->y0 + sp->y >= sp->height)
262                         sp->y = sp->radius;
263         } else if (sp->diry == -1) {
264                 sp->y--;
265                 if (sp->y0 + sp->y < 0)
266                         sp->y = -sp->radius;
267         }
268 }
269
270 ENTRYPOINT void
271 free_sphere(ModeInfo * mi)
272 {
273         spherestruct *sp;
274
275         if (spheres == NULL)
276                 return;
277         sp = &spheres[MI_SCREEN(mi)];
278
279         if (sp->points) {
280                 (void) free((void *) sp->points);
281                 /* sp->points = NULL; */
282         }
283 }
284
285 #ifndef STANDALONE
286 ENTRYPOINT void
287 refresh_sphere(ModeInfo * mi)
288 {
289         spherestruct *sp;
290
291         if (spheres == NULL)
292                 return;
293         sp = &spheres[MI_SCREEN(mi)];
294
295         MI_CLEARWINDOW(mi);
296
297         sp->x = -sp->radius;
298 }
299 #endif
300
301 XSCREENSAVER_MODULE ("Sphere", sphere)
302
303 #endif /* MODE_sphere */