1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* grav --- planets spinning around a pulsar */
5 static const char sccsid[] = "@(#)grav.c 5.00 2000/11/01 xlockmore";
9 * Copyright (c) 1993 by Greg Boewring <gb@pobox.com>
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 * 10-May-1997: Compatible with xscreensaver
26 * 11-Jul-1994: color version
27 * 06-Oct-1993: Written by Greg Bowering <gb@pobox.com>
32 #define DEFAULTS "*delay: 10000 \n" \
35 "*fpsSolid: true \n" \
38 # define grav_handle_event 0
39 # include "xlockmore.h" /* in xscreensaver distribution */
40 #else /* STANDALONE */
41 # include "xlock.h" /* in xlockmore distribution */
42 #endif /* STANDALONE */
46 #define DEF_DECAY "True" /* Damping for decaying orbits */
47 #define DEF_TRAIL "True" /* For trails (works good in mono only) */
52 static XrmOptionDescRec opts[] =
54 {"-decay", ".grav.decay", XrmoptionNoArg, "on"},
55 {"+decay", ".grav.decay", XrmoptionNoArg, "off"},
56 {"-trail", ".grav.trail", XrmoptionNoArg, "on"},
57 {"+trail", ".grav.trail", XrmoptionNoArg, "off"}
59 static argtype vars[] =
61 {&decay, "decay", "Decay", DEF_DECAY, t_Bool},
62 {&trail, "trail", "Trail", DEF_TRAIL, t_Bool}
64 static OptionStruct desc[] =
66 {"-/+decay", "turn on/off decaying orbits"},
67 {"-/+trail", "turn on/off trail dots"}
70 ENTRYPOINT ModeSpecOpt grav_opts =
71 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
74 ModStruct grav_description =
75 {"grav", "init_grav", "draw_grav", "release_grav",
76 "refresh_grav", "init_grav", (char *) NULL, &grav_opts,
77 10000, -12, 1, 1, 64, 1.0, "",
78 "Shows orbiting planets", 0, NULL};
82 #define GRAV -0.02 /* Gravitational constant */
84 #define COLLIDE 0.0001
87 /* #define INTRINSIC_RADIUS 200.0 */
88 #define INTRINSIC_RADIUS ((float) (gp->height/5))
89 #define STARRADIUS (unsigned int)(gp->height/(2*DIST))
90 #define AVG_RADIUS (INTRINSIC_RADIUS/DIST)
91 #define RADIUS (unsigned int)(INTRINSIC_RADIUS/(POS(Z)+DIST))
93 #define XR HALF*ALMOST
94 #define YR HALF*ALMOST
95 #define ZR HALF*ALMOST
104 #define DAMP 0.999999
105 #define MaxA 0.1 /* Maximum acceleration (w/ damping) */
107 #define POS(c) planet->P[c]
108 #define VEL(c) planet->V[c]
109 #define ACC(c) planet->A[c]
112 if ((x) >= 0 && (y) >= 0 && (x) <= gp->width && (y) <= gp->height) {\
114 XDrawPoint(display, window, gc, (x), (y));\
116 XFillArc(display, window, gc,\
117 (x) - planet->ri / 2, (y) - planet->ri / 2, planet->ri, planet->ri,\
121 #define FLOATRAND(min,max) ((min)+(LRAND()/MAXRAND)*((max)-(min)))
124 double P[DIMENSIONS], V[DIMENSIONS], A[DIMENSIONS];
126 unsigned long colors;
131 int x, y, sr, nplanets;
132 unsigned long starcolor;
133 planetstruct *planets;
136 static gravstruct *gravs = (gravstruct *) NULL;
139 init_planet(ModeInfo * mi, planetstruct * planet)
141 Display *display = MI_DISPLAY(mi);
142 Window window = MI_WINDOW(mi);
144 gravstruct *gp = &gravs[MI_SCREEN(mi)];
147 jwxyz_XSetAntiAliasing (MI_DISPLAY(mi), MI_GC(mi), False);
150 if (MI_NPIXELS(mi) > 2)
151 planet->colors = MI_PIXEL(mi, NRAND(MI_NPIXELS(mi)));
153 planet->colors = MI_WHITE_PIXEL(mi);
154 /* Initialize positions */
155 POS(X) = FLOATRAND(-XR, XR);
156 POS(Y) = FLOATRAND(-YR, YR);
157 POS(Z) = FLOATRAND(-ZR, ZR);
159 if (POS(Z) > -ALMOST) {
161 ((double) gp->width * (HALF + POS(X) / (POS(Z) + DIST)));
163 ((double) gp->height * (HALF + POS(Y) / (POS(Z) + DIST)));
165 planet->xi = planet->yi = -1;
168 /* Initialize velocities */
169 VEL(X) = FLOATRAND(-VR, VR);
170 VEL(Y) = FLOATRAND(-VR, VR);
171 VEL(Z) = FLOATRAND(-VR, VR);
174 Planet(planet->xi, planet->yi);
178 draw_planet(ModeInfo * mi, planetstruct * planet)
180 Display *display = MI_DISPLAY(mi);
181 Window window = MI_WINDOW(mi);
183 gravstruct *gp = &gravs[MI_SCREEN(mi)];
184 double D; /* A distance variable to work with */
185 register unsigned char cmpt;
187 D = POS(X) * POS(X) + POS(Y) * POS(Y) + POS(Z) * POS(Z);
192 for (cmpt = X; cmpt < DIMENSIONS; cmpt++) {
193 ACC(cmpt) = POS(cmpt) * GRAV / D;
195 if (ACC(cmpt) > MaxA)
197 else if (ACC(cmpt) < -MaxA)
199 VEL(cmpt) = VEL(cmpt) + ACC(cmpt);
202 /* update velocity */
203 VEL(cmpt) = VEL(cmpt) + ACC(cmpt);
205 /* update position */
206 POS(cmpt) = POS(cmpt) + VEL(cmpt);
212 if (POS(Z) > -ALMOST) {
214 ((double) gp->width * (HALF + POS(X) / (POS(Z) + DIST)));
216 ((double) gp->height * (HALF + POS(Y) / (POS(Z) + DIST)));
218 planet->xi = planet->yi = -1;
221 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
222 Planet(gp->x, gp->y);
224 XSetForeground(display, gc, planet->colors);
225 XDrawPoint(display, MI_WINDOW(mi), gc, gp->x, gp->y);
233 XSetForeground(display, gc, planet->colors);
234 Planet(gp->x, gp->y);
238 init_grav(ModeInfo * mi)
240 Display *display = MI_DISPLAY(mi);
246 if ((gravs = (gravstruct *) calloc(MI_NUM_SCREENS(mi),
247 sizeof (gravstruct))) == NULL)
250 gp = &gravs[MI_SCREEN(mi)];
252 gp->width = MI_WIDTH(mi);
253 gp->height = MI_HEIGHT(mi);
257 gp->nplanets = MI_COUNT(mi);
258 if (gp->nplanets < 0) {
260 (void) free((void *) gp->planets);
261 gp->planets = (planetstruct *) NULL;
263 gp->nplanets = NRAND(-gp->nplanets) + 1; /* Add 1 so its not too boring */
265 if (gp->planets == NULL) {
266 if ((gp->planets = (planetstruct *) calloc(gp->nplanets,
267 sizeof (planetstruct))) == NULL)
273 if (MI_NPIXELS(mi) > 2)
274 gp->starcolor = MI_PIXEL(mi, NRAND(MI_NPIXELS(mi)));
276 gp->starcolor = MI_WHITE_PIXEL(mi);
277 for (ball = 0; ball < (unsigned char) gp->nplanets; ball++)
278 init_planet(mi, &gp->planets[ball]);
280 /* Draw centrepoint */
281 XDrawArc(display, MI_WINDOW(mi), gc,
282 gp->width / 2 - gp->sr / 2, gp->height / 2 - gp->sr / 2, gp->sr, gp->sr,
287 draw_grav(ModeInfo * mi)
289 Display *display = MI_DISPLAY(mi);
290 Window window = MI_WINDOW(mi);
292 register unsigned char ball;
297 gp = &gravs[MI_SCREEN(mi)];
298 if (gp->planets == NULL)
301 MI_IS_DRAWN(mi) = True;
302 /* Mask centrepoint */
303 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
304 XDrawArc(display, window, gc,
305 gp->width / 2 - gp->sr / 2, gp->height / 2 - gp->sr / 2, gp->sr, gp->sr,
308 /* Resize centrepoint */
311 if (gp->sr < (int) STARRADIUS)
319 /* Draw centrepoint */
320 XSetForeground(display, gc, gp->starcolor);
321 XDrawArc(display, window, gc,
322 gp->width / 2 - gp->sr / 2, gp->height / 2 - gp->sr / 2, gp->sr, gp->sr,
325 for (ball = 0; ball < (unsigned char) gp->nplanets; ball++)
326 draw_planet(mi, &gp->planets[ball]);
330 reshape_grav(ModeInfo * mi, int width, int height)
332 gravstruct *gp = &gravs[MI_SCREEN(mi)];
335 XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
339 release_grav(ModeInfo * mi)
344 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
345 gravstruct *gp = &gravs[screen];
348 (void) free((void *) gp->planets);
350 (void) free((void *) gravs);
351 gravs = (gravstruct *) NULL;
356 refresh_grav(ModeInfo * mi)
361 XSCREENSAVER_MODULE ("Grav", grav)
363 #endif /* MODE_grav */