From http://www.jwz.org/xscreensaver/xscreensaver-5.37.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 reshape_sphere 0
69 # define sphere_handle_event 0
70 # include "xlockmore.h"         /* from the xscreensaver distribution */
71 #else /* !STANDALONE */
72 # include "xlock.h"             /* from the xlockmore distribution */
73 #endif /* !STANDALONE */
74
75 #ifdef MODE_sphere
76
77 ENTRYPOINT ModeSpecOpt sphere_opts =
78 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
79
80 #ifdef USE_MODULES
81 ModStruct   sphere_description =
82 {"sphere", "init_sphere", "draw_sphere", "release_sphere",
83  "refresh_sphere", "init_sphere", (char *) NULL, &sphere_opts,
84  5000, 1, 20, 0, 64, 1.0, "",
85  "Shows a bunch of shaded spheres", 0, NULL};
86
87 #endif
88
89 /*-
90  * (NX, NY, NZ) is the light source vector -- length should be 100
91  */
92 #define NX 48
93 #define NY (-36)
94 #define NZ 80
95 #define NR 100
96 #define SQRT(a) ((int)sqrt((double)(a)))
97
98 typedef struct {
99         int         width, height;
100         int         radius;
101         int         x0;         /* x center */
102         int         y0;         /* y center */
103         int         color;
104         int         x, y;
105         int         dirx, diry;
106         int         shadowx, shadowy;
107         int         maxx, maxy;
108         XPoint     *points;
109 } spherestruct;
110
111 static spherestruct *spheres = (spherestruct *) NULL;
112
113 ENTRYPOINT void
114 init_sphere(ModeInfo * mi)
115 {
116         spherestruct *sp;
117
118         MI_INIT (mi, spheres, 0);
119         sp = &spheres[MI_SCREEN(mi)];
120
121         if (sp->points != NULL) {
122                 (void) free((void *) sp->points);
123                 sp->points = (XPoint *) NULL;
124         }
125         sp->width = MAX(MI_WIDTH(mi), 4);
126         sp->height = MAX(MI_HEIGHT(mi), 4);
127         if ((sp->points = (XPoint *) malloc(MIN(sp->width, sp->height) *
128                          sizeof (XPoint))) == NULL) {
129                 return;
130         }
131
132         MI_CLEARWINDOW(mi);
133
134         sp->dirx = 1;
135         sp->x = sp->radius;
136         sp->shadowx = (LRAND() & 1) ? 1 : -1;
137         sp->shadowy = (LRAND() & 1) ? 1 : -1;
138 }
139
140 ENTRYPOINT void
141 draw_sphere(ModeInfo * mi)
142 {
143         Display    *display = MI_DISPLAY(mi);
144         GC          gc = MI_GC(mi);
145         int         sqrd, nd;
146         register int minx = 0, maxx = 0, miny = 0, maxy = 0, npts = 0;
147         spherestruct *sp;
148
149         if (spheres == NULL)
150                 return;
151         sp = &spheres[MI_SCREEN(mi)];
152         if (sp->points == NULL)
153                 return;
154
155         MI_IS_DRAWN(mi) = True;
156         if ((sp->dirx && ABS(sp->x) >= sp->radius) ||
157             (sp->diry && ABS(sp->y) >= sp->radius)) {
158                 sp->radius = NRAND(MIN(sp->width / 2, sp->height / 2) - 1) + 1;
159
160                 if (LRAND() & 1) {
161                         sp->dirx = (int) (LRAND() & 1) * 2 - 1;
162                         sp->diry = 0;
163                 } else {
164                         sp->dirx = 0;
165                         sp->diry = (int) (LRAND() & 1) * 2 - 1;
166                 }
167                 sp->x0 = NRAND(sp->width);
168                 sp->y0 = NRAND(sp->height);
169
170                 sp->x = -sp->radius * sp->dirx;
171                 sp->y = -sp->radius * sp->diry;
172
173                 if (MI_NPIXELS(mi) > 2)
174                         sp->color = NRAND(MI_NPIXELS(mi));
175         }
176         if (sp->dirx == 1) {
177                 if (sp->x0 + sp->x < 0)
178                         sp->x = -sp->x0;
179         } else if (sp->dirx == -1) {
180                 if (sp->x0 + sp->x >= sp->width)
181                         sp->x = sp->width - sp->x0 - 1;
182         }
183         if (sp->diry == 1) {
184                 if (sp->y0 + sp->y < 0)
185                         sp->y = -sp->y0;
186         } else if (sp->diry == -1) {
187                 if (sp->y0 + sp->y >= sp->height)
188                         sp->y = sp->height - sp->y0 - 1;
189         }
190         if (sp->dirx) {
191                 sp->maxy = SQRT(sp->radius * sp->radius - sp->x * sp->x);
192                 miny = -sp->maxy;
193                 if (sp->y0 - sp->maxy < 0)
194                         miny = -sp->y0;
195                 maxy = sp->maxy;
196         }
197         if (sp->diry) {
198                 sp->maxx = SQRT(sp->radius * sp->radius - sp->y * sp->y);
199                 minx = -sp->maxx;
200                 if (sp->x0 - sp->maxx < 0)
201                         minx = -sp->x0;
202                 maxx = sp->maxx;
203         }
204         if (sp->dirx) {
205                 if (sp->y0 + sp->maxy >= sp->height)
206                         maxy = sp->height - sp->y0;
207         }
208         if (sp->diry) {
209                 if (sp->x0 + sp->maxx >= sp->width)
210                         maxx = sp->width - sp->x0;
211         }
212         XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
213
214         if (sp->dirx)
215                 XDrawLine(display, MI_WINDOW(mi), gc,
216                 sp->x0 + sp->x, sp->y0 + miny, sp->x0 + sp->x, sp->y0 + maxy);
217         if (sp->diry)
218                 XDrawLine(display, MI_WINDOW(mi), gc,
219                 sp->x0 + minx, sp->y0 + sp->y, sp->x0 + maxx, sp->y0 + sp->y);
220
221         if (MI_NPIXELS(mi) > 2)
222                 XSetForeground(display, gc, MI_PIXEL(mi, sp->color));
223         else
224                 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
225
226         if (sp->dirx) {
227                 sqrd = sp->radius * sp->radius - sp->x * sp->x;
228                 nd = NX * sp->shadowx * sp->x;
229                 for (sp->y = miny; sp->y <= maxy; sp->y++)
230                         if ((NRAND(sp->radius * NR)) <= nd + NY * sp->shadowy * sp->y +
231                             NZ * SQRT(sqrd - sp->y * sp->y)) {
232                                 sp->points[npts].x = sp->x + sp->x0;
233                                 sp->points[npts].y = sp->y + sp->y0;
234                                 npts++;
235                         }
236         }
237         if (sp->diry) {
238                 sqrd = sp->radius * sp->radius - sp->y * sp->y;
239                 nd = NY * sp->shadowy * sp->y;
240                 for (sp->x = minx; sp->x <= maxx; sp->x++)
241                         if ((NRAND(sp->radius * NR)) <= NX * sp->shadowx * sp->x + nd +
242                             NZ * SQRT(sqrd - sp->x * sp->x)) {
243                                 sp->points[npts].x = sp->x + sp->x0;
244                                 sp->points[npts].y = sp->y + sp->y0;
245                                 npts++;
246                         }
247         }
248         XDrawPoints(display, MI_WINDOW(mi), gc, sp->points, npts, CoordModeOrigin);
249         if (sp->dirx == 1) {
250                 sp->x++;
251                 if (sp->x0 + sp->x >= sp->width)
252                         sp->x = sp->radius;
253         } else if (sp->dirx == -1) {
254                 sp->x--;
255                 if (sp->x0 + sp->x < 0)
256                         sp->x = -sp->radius;
257         }
258         if (sp->diry == 1) {
259                 sp->y++;
260                 if (sp->y0 + sp->y >= sp->height)
261                         sp->y = sp->radius;
262         } else if (sp->diry == -1) {
263                 sp->y--;
264                 if (sp->y0 + sp->y < 0)
265                         sp->y = -sp->radius;
266         }
267 }
268
269 ENTRYPOINT void
270 release_sphere(ModeInfo * mi)
271 {
272         if (spheres != NULL) {
273                 int         screen;
274
275                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
276                         spherestruct *sp = &spheres[screen];
277
278                         if (sp->points) {
279                                 (void) free((void *) sp->points);
280                                 /* sp->points = NULL; */
281                         }
282                 }
283                 (void) free((void *) spheres);
284                 spheres = (spherestruct *) NULL;
285         }
286 }
287
288 ENTRYPOINT void
289 refresh_sphere(ModeInfo * mi)
290 {
291         spherestruct *sp;
292
293         if (spheres == NULL)
294                 return;
295         sp = &spheres[MI_SCREEN(mi)];
296
297         MI_CLEARWINDOW(mi);
298
299         sp->x = -sp->radius;
300 }
301
302 XSCREENSAVER_MODULE ("Sphere", sphere)
303
304 #endif /* MODE_sphere */