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