1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* sphere --- a bunch of shaded spheres */
5 static const char sccsid[] = "@(#)sphere.c 5.00 2000/11/01 xlockmore";
9 * Copyright (c) 1988 by Sun Microsystems
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.
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.
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.
34 * **************************************************************************
35 * Copyright 1988 by Sun Microsystems, Inc. Mountain View, CA.
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.
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
56 * ***************************************************************************
61 #define DEFAULTS "*delay: 1000 \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 */
75 ENTRYPOINT ModeSpecOpt sphere_opts =
76 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
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};
88 * (NX, NY, NZ) is the light source vector -- length should be 100
94 #define SQRT(a) ((int)sqrt((double)(a)))
99 int x0; /* x center */
100 int y0; /* y center */
104 int shadowx, shadowy;
109 static spherestruct *spheres = (spherestruct *) NULL;
112 init_sphere(ModeInfo * mi)
116 if (spheres == NULL) {
117 if ((spheres = (spherestruct *) calloc(MI_NUM_SCREENS(mi),
118 sizeof (spherestruct))) == NULL)
121 sp = &spheres[MI_SCREEN(mi)];
123 if (sp->points != NULL) {
124 (void) free((void *) sp->points);
125 sp->points = (XPoint *) NULL;
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) {
138 sp->shadowx = (LRAND() & 1) ? 1 : -1;
139 sp->shadowy = (LRAND() & 1) ? 1 : -1;
143 draw_sphere(ModeInfo * mi)
145 Display *display = MI_DISPLAY(mi);
148 register int minx = 0, maxx = 0, miny = 0, maxy = 0, npts = 0;
153 sp = &spheres[MI_SCREEN(mi)];
154 if (sp->points == NULL)
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;
163 sp->dirx = (int) (LRAND() & 1) * 2 - 1;
167 sp->diry = (int) (LRAND() & 1) * 2 - 1;
169 sp->x0 = NRAND(sp->width);
170 sp->y0 = NRAND(sp->height);
172 sp->x = -sp->radius * sp->dirx;
173 sp->y = -sp->radius * sp->diry;
175 if (MI_NPIXELS(mi) > 2)
176 sp->color = NRAND(MI_NPIXELS(mi));
179 if (sp->x0 + sp->x < 0)
181 } else if (sp->dirx == -1) {
182 if (sp->x0 + sp->x >= sp->width)
183 sp->x = sp->width - sp->x0 - 1;
186 if (sp->y0 + sp->y < 0)
188 } else if (sp->diry == -1) {
189 if (sp->y0 + sp->y >= sp->height)
190 sp->y = sp->height - sp->y0 - 1;
193 sp->maxy = SQRT(sp->radius * sp->radius - sp->x * sp->x);
195 if (sp->y0 - sp->maxy < 0)
200 sp->maxx = SQRT(sp->radius * sp->radius - sp->y * sp->y);
202 if (sp->x0 - sp->maxx < 0)
207 if (sp->y0 + sp->maxy >= sp->height)
208 maxy = sp->height - sp->y0;
211 if (sp->x0 + sp->maxx >= sp->width)
212 maxx = sp->width - sp->x0;
214 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
217 XDrawLine(display, MI_WINDOW(mi), gc,
218 sp->x0 + sp->x, sp->y0 + miny, sp->x0 + sp->x, sp->y0 + maxy);
220 XDrawLine(display, MI_WINDOW(mi), gc,
221 sp->x0 + minx, sp->y0 + sp->y, sp->x0 + maxx, sp->y0 + sp->y);
223 if (MI_NPIXELS(mi) > 2)
224 XSetForeground(display, gc, MI_PIXEL(mi, sp->color));
226 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
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;
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;
250 XDrawPoints(display, MI_WINDOW(mi), gc, sp->points, npts, CoordModeOrigin);
253 if (sp->x0 + sp->x >= sp->width)
255 } else if (sp->dirx == -1) {
257 if (sp->x0 + sp->x < 0)
262 if (sp->y0 + sp->y >= sp->height)
264 } else if (sp->diry == -1) {
266 if (sp->y0 + sp->y < 0)
272 release_sphere(ModeInfo * mi)
274 if (spheres != NULL) {
277 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
278 spherestruct *sp = &spheres[screen];
281 (void) free((void *) sp->points);
282 /* sp->points = NULL; */
285 (void) free((void *) spheres);
286 spheres = (spherestruct *) NULL;
291 refresh_sphere(ModeInfo * mi)
297 sp = &spheres[MI_SCREEN(mi)];
304 XSCREENSAVER_MODULE ("Sphere", sphere)
306 #endif /* MODE_sphere */