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