From http://www.jwz.org/xscreensaver/xscreensaver-5.38.tar.gz
[xscreensaver] / hacks / grav.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* grav --- planets spinning around a pulsar */
3
4 #if 0
5 static const char sccsid[] = "@(#)grav.c        5.00 2000/11/01 xlockmore";
6 #endif
7
8 /*-
9  * Copyright (c) 1993 by Greg Boewring <gb@pobox.com>
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  * 10-May-1997: Compatible with xscreensaver
26  * 11-Jul-1994: color version
27  * 06-Oct-1993: Written by Greg Bowering <gb@pobox.com>
28  */
29
30 #ifdef STANDALONE
31 #define MODE_grav
32 #define DEFAULTS        "*delay: 10000 \n" \
33                                         "*count: 12 \n" \
34                                         "*ncolors: 64 \n" \
35                                         "*fpsSolid: true \n" \
36                                         "*ignoreRotation: True \n" \
37                                     "*lowrez: True \n" \
38
39 #define BRIGHT_COLORS
40 # define release_grav 0
41 # define grav_handle_event 0
42 # include "xlockmore.h"         /* in xscreensaver distribution */
43 #else /* STANDALONE */
44 # include "xlock.h"             /* in xlockmore distribution */
45 #endif /* STANDALONE */
46
47 #ifdef MODE_grav
48
49 #define DEF_DECAY "True"        /* Damping for decaying orbits */
50 #define DEF_TRAIL "True"        /* For trails (works good in mono only) */
51
52 static Bool decay;
53 static Bool trail;
54
55 static XrmOptionDescRec opts[] =
56 {
57         {"-decay", ".grav.decay", XrmoptionNoArg, "on"},
58         {"+decay", ".grav.decay", XrmoptionNoArg, "off"},
59         {"-trail", ".grav.trail", XrmoptionNoArg, "on"},
60         {"+trail", ".grav.trail", XrmoptionNoArg, "off"}
61 };
62 static argtype vars[] =
63 {
64         {&decay, "decay", "Decay", DEF_DECAY, t_Bool},
65         {&trail, "trail", "Trail", DEF_TRAIL, t_Bool}
66 };
67 static OptionStruct desc[] =
68 {
69         {"-/+decay", "turn on/off decaying orbits"},
70         {"-/+trail", "turn on/off trail dots"}
71 };
72
73 ENTRYPOINT ModeSpecOpt grav_opts =
74 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
75
76 #ifdef USE_MODULES
77 ModStruct   grav_description =
78 {"grav", "init_grav", "draw_grav", (char *) NULL,
79  "refresh_grav", "init_grav", "free_grav", &grav_opts,
80  10000, -12, 1, 1, 64, 1.0, "",
81  "Shows orbiting planets", 0, NULL};
82
83 #endif
84
85 #define GRAV                    -0.02   /* Gravitational constant */
86 #define DIST                    16.0
87 #define COLLIDE                 0.0001
88 #define ALMOST                  15.99
89 #define HALF                    0.5
90 /* #define INTRINSIC_RADIUS     200.0 */
91 #define INTRINSIC_RADIUS        ((float) (gp->height/5))
92 #define STARRADIUS              (unsigned int)(gp->height/(2*DIST))
93 #define AVG_RADIUS              (INTRINSIC_RADIUS/DIST)
94 #define RADIUS                  (unsigned int)(INTRINSIC_RADIUS/(POS(Z)+DIST))
95
96 #define XR                      HALF*ALMOST
97 #define YR                      HALF*ALMOST
98 #define ZR                      HALF*ALMOST
99
100 #define VR                      0.04
101
102 #define DIMENSIONS              3
103 #define X                       0
104 #define Y                       1
105 #define Z                       2
106
107 #define DAMP                    0.999999
108 #define MaxA                    0.1     /* Maximum acceleration (w/ damping) */
109
110 #define POS(c) planet->P[c]
111 #define VEL(c) planet->V[c]
112 #define ACC(c) planet->A[c]
113
114 #define Planet(x,y)\
115   if ((x) >= 0 && (y) >= 0 && (x) <= gp->width && (y) <= gp->height) {\
116     if (planet->ri < 2)\
117      XDrawPoint(display, window, gc, (x), (y));\
118     else\
119      XFillArc(display, window, gc,\
120       (x) - planet->ri / 2, (y) - planet->ri / 2, planet->ri, planet->ri,\
121       0, 23040);\
122    }
123
124 #define FLOATRAND(min,max)      ((min)+(LRAND()/MAXRAND)*((max)-(min)))
125
126 typedef struct {
127         double      P[DIMENSIONS], V[DIMENSIONS], A[DIMENSIONS];
128         int         xi, yi, ri;
129         unsigned long colors;
130 } planetstruct;
131
132 typedef struct {
133         int         width, height;
134         int         x, y, sr, nplanets;
135         unsigned long starcolor;
136         planetstruct *planets;
137 } gravstruct;
138
139 static gravstruct *gravs = (gravstruct *) NULL;
140
141 static void
142 init_planet(ModeInfo * mi, planetstruct * planet)
143 {
144         gravstruct *gp = &gravs[MI_SCREEN(mi)];
145
146 # ifdef HAVE_JWXYZ
147     jwxyz_XSetAntiAliasing (MI_DISPLAY(mi), MI_GC(mi), False);
148 # endif
149
150         if (MI_NPIXELS(mi) > 2)
151                 planet->colors = MI_PIXEL(mi, NRAND(MI_NPIXELS(mi)));
152         else
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);
158
159         if (POS(Z) > -ALMOST) {
160                 planet->xi = (int)
161                         ((double) gp->width * (HALF + POS(X) / (POS(Z) + DIST)));
162                 planet->yi = (int)
163                         ((double) gp->height * (HALF + POS(Y) / (POS(Z) + DIST)));
164         } else
165                 planet->xi = planet->yi = -1;
166         planet->ri = RADIUS;
167
168         /* Initialize velocities */
169         VEL(X) = FLOATRAND(-VR, VR);
170         VEL(Y) = FLOATRAND(-VR, VR);
171         VEL(Z) = FLOATRAND(-VR, VR);
172 }
173
174 static void
175 draw_planet(ModeInfo * mi, planetstruct * planet)
176 {
177         Display    *display = MI_DISPLAY(mi);
178         Window      window = MI_WINDOW(mi);
179         GC          gc = MI_GC(mi);
180         gravstruct *gp = &gravs[MI_SCREEN(mi)];
181         double      D;          /* A distance variable to work with */
182         register unsigned char cmpt;
183
184         D = POS(X) * POS(X) + POS(Y) * POS(Y) + POS(Z) * POS(Z);
185         if (D < COLLIDE)
186                 D = COLLIDE;
187         D = sqrt(D);
188         D = D * D * D;
189         for (cmpt = X; cmpt < DIMENSIONS; cmpt++) {
190                 ACC(cmpt) = POS(cmpt) * GRAV / D;
191                 if (decay) {
192                         if (ACC(cmpt) > MaxA)
193                                 ACC(cmpt) = MaxA;
194                         else if (ACC(cmpt) < -MaxA)
195                                 ACC(cmpt) = -MaxA;
196                         VEL(cmpt) = VEL(cmpt) + ACC(cmpt);
197                         VEL(cmpt) *= DAMP;
198                 } else {
199                         /* update velocity */
200                         VEL(cmpt) = VEL(cmpt) + ACC(cmpt);
201                 }
202                 /* update position */
203                 POS(cmpt) = POS(cmpt) + VEL(cmpt);
204         }
205
206         gp->x = planet->xi;
207         gp->y = planet->yi;
208
209         if (POS(Z) > -ALMOST) {
210                 planet->xi = (int)
211                         ((double) gp->width * (HALF + POS(X) / (POS(Z) + DIST)));
212                 planet->yi = (int)
213                         ((double) gp->height * (HALF + POS(Y) / (POS(Z) + DIST)));
214         } else
215                 planet->xi = planet->yi = -1;
216
217         /* Mask */
218         XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
219         Planet(gp->x, gp->y);
220         if (trail) {
221                 XSetForeground(display, gc, planet->colors);
222                 XDrawPoint(display, MI_WINDOW(mi), gc, gp->x, gp->y);
223         }
224         /* Move */
225         gp->x = planet->xi;
226         gp->y = planet->yi;
227         planet->ri = RADIUS;
228
229         /* Redraw */
230         XSetForeground(display, gc, planet->colors);
231         Planet(gp->x, gp->y);
232 }
233
234 ENTRYPOINT void
235 init_grav(ModeInfo * mi)
236 {
237         unsigned char ball;
238         gravstruct *gp;
239
240         MI_INIT (mi, gravs);
241         gp = &gravs[MI_SCREEN(mi)];
242
243         gp->width = MI_WIDTH(mi);
244         gp->height = MI_HEIGHT(mi);
245
246         gp->sr = STARRADIUS;
247
248         gp->nplanets = MI_COUNT(mi);
249         if (gp->nplanets < 0) {
250                 if (gp->planets) {
251                         (void) free((void *) gp->planets);
252                         gp->planets = (planetstruct *) NULL;
253                 }
254                 gp->nplanets = NRAND(-gp->nplanets) + 1;        /* Add 1 so its not too boring */
255         }
256         if (gp->planets == NULL) {
257                 if ((gp->planets = (planetstruct *) calloc(gp->nplanets,
258                                 sizeof (planetstruct))) == NULL)
259                         return;
260         }
261
262         MI_CLEARWINDOW(mi);
263
264         if (MI_NPIXELS(mi) > 2)
265                 gp->starcolor = MI_PIXEL(mi, NRAND(MI_NPIXELS(mi)));
266         else
267                 gp->starcolor = MI_WHITE_PIXEL(mi);
268         for (ball = 0; ball < (unsigned char) gp->nplanets; ball++)
269                 init_planet(mi, &gp->planets[ball]);
270 }
271
272 ENTRYPOINT void
273 draw_grav(ModeInfo * mi)
274 {
275         Display    *display = MI_DISPLAY(mi);
276         Window      window = MI_WINDOW(mi);
277         GC          gc = MI_GC(mi);
278         register unsigned char ball;
279         gravstruct *gp;
280
281         if (gravs == NULL)
282                         return;
283         gp = &gravs[MI_SCREEN(mi)];
284         if (gp->planets == NULL)
285                 return;
286
287         if (!MI_IS_DRAWN(mi)) {
288                 for (ball = 0; ball < (unsigned char) gp->nplanets; ball++) {
289                         planetstruct *planet = &gp->planets[ball];
290
291                         /* Draw planets */
292                         Planet(planet->xi, planet->yi);
293                 }
294
295                 /* Draw centrepoint */
296                 XDrawArc(display, MI_WINDOW(mi), gc,
297                          gp->width / 2 - gp->sr / 2, gp->height / 2 - gp->sr / 2, gp->sr, gp->sr,
298                          0, 23040);
299         }
300
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,
306                  0, 23040);
307
308         /* Resize centrepoint */
309         switch (NRAND(4)) {
310                 case 0:
311                         if (gp->sr < (int) STARRADIUS)
312                                 gp->sr++;
313                         break;
314                 case 1:
315                         if (gp->sr > 2)
316                                 gp->sr--;
317         }
318
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,
323                  0, 23040);
324
325         for (ball = 0; ball < (unsigned char) gp->nplanets; ball++)
326                 draw_planet(mi, &gp->planets[ball]);
327 }
328
329 ENTRYPOINT void
330 reshape_grav(ModeInfo * mi, int width, int height)
331 {
332         gravstruct *gp = &gravs[MI_SCREEN(mi)];
333         gp->width  = width;
334         gp->height = height;
335     XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
336 }
337
338 ENTRYPOINT void
339 free_grav(ModeInfo * mi)
340 {
341         gravstruct *gp = &gravs[MI_SCREEN(mi)];
342         if (gp->planets)
343                 (void) free((void *) gp->planets);
344 }
345
346 #ifndef STANDALONE
347 ENTRYPOINT void
348 refresh_grav(ModeInfo * mi)
349 {
350         MI_CLEARWINDOW(mi);
351 }
352 #endif
353
354 XSCREENSAVER_MODULE ("Grav", grav)
355
356 #endif /* MODE_grav */