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