1
/* -*- Mode: C; tab-width: 4 -*-
2
* grav --- simulation of a planetary system.
4
#if !defined( lint ) && !defined( SABER )
5
static const char sccsid[] = "@(#)grav.c 4.00 97/01/01 xlockmore";
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";
8
/* Copyright (c) 1993 Greg Bowering <gb@pobox.com>
9
* Copyright (c) 1993 by Greg Boewring <gb@pobox.com>
10
11
* Permission to use, copy, modify, and distribute this software and its
11
12
* documentation for any purpose and without fee is hereby granted,
19
20
* event will the author be liable for any lost revenue or profits or
20
21
* other special, indirect and consequential damages.
23
* 10-May-97: jwz@jwz.org: turned into a standalone program.
24
* 11-Jul-94: color version
25
* 06-Oct-93: by Greg Bowering <gb@pobox.com>
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>
29
# define PROGCLASS "Grav"
30
# define HACK_INIT init_grav
31
# define HACK_DRAW draw_grav
32
# define grav_opts xlockmore_opts
33
# define DEFAULTS "*count: 12 \n" \
36
# define BRIGHT_COLORS
37
# include "xlockmore.h" /* from the xscreensaver distribution */
38
#else /* !STANDALONE */
39
# include "xlock.h" /* from the xlockmore distribution */
40
#endif /* !STANDALONE */
32
#define PROGCLASS "Grav"
33
#define HACK_INIT init_grav
34
#define HACK_DRAW draw_grav
35
#define grav_opts xlockmore_opts
36
#define DEFAULTS "*delay: 10000 \n" \
40
#include "xlockmore.h" /* in xscreensaver distribution */
41
#else /* STANDALONE */
42
#include "xlock.h" /* in xlockmore distribution */
44
#endif /* STANDALONE */
48
#define DEF_DECAY "False" /* Damping for decaying orbits */
49
#define DEF_TRAIL "False" /* For trails (works good in mono only) */
54
static XrmOptionDescRec opts[] =
56
{"-decay", ".grav.decay", XrmoptionNoArg, "on"},
57
{"+decay", ".grav.decay", XrmoptionNoArg, "off"},
58
{"-trail", ".grav.trail", XrmoptionNoArg, "on"},
59
{"+trail", ".grav.trail", XrmoptionNoArg, "off"}
61
static argtype vars[] =
63
{&decay, "decay", "Decay", DEF_DECAY, t_Bool},
64
{&trail, "trail", "Trail", DEF_TRAIL, t_Bool}
66
static OptionStruct desc[] =
68
{"-/+decay", "turn on/off decaying orbits"},
69
{"-/+trail", "turn on/off trail dots"}
72
ModeSpecOpt grav_opts =
73
{sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
76
ModStruct grav_description =
77
{"grav", "init_grav", "draw_grav", "release_grav",
78
"refresh_grav", "init_grav", (char *) NULL, &grav_opts,
79
10000, -12, 1, 1, 64, 1.0, "",
80
"Shows orbiting planets", 0, NULL};
42
84
#define GRAV -0.02 /* Gravitational constant */
81
123
#define FLOATRAND(min,max) ((min)+(LRAND()/MAXRAND)*((max)-(min)))
83
#define DEF_DECAY "False" /* Damping for decaying orbits */
84
#define DEF_TRAIL "False" /* For trails (works good in mono only) */
89
static XrmOptionDescRec opts[] =
91
{"-decay", ".grav.decay", XrmoptionNoArg, (caddr_t) "on"},
92
{"+decay", ".grav.decay", XrmoptionNoArg, (caddr_t) "off"},
93
{"-trail", ".grav.trail", XrmoptionNoArg, (caddr_t) "on"},
94
{"+trail", ".grav.trail", XrmoptionNoArg, (caddr_t) "off"}
96
static argtype vars[] =
98
{(caddr_t *) & decay, "decay", "Decay", DEF_DECAY, t_Bool},
99
{(caddr_t *) & trail, "trail", "Trail", DEF_TRAIL, t_Bool}
101
static OptionStruct desc[] =
103
{"-/+decay", "turn on/off decaying orbits"},
104
{"-/+trail", "turn on/off trail dots"}
107
ModeSpecOpt grav_opts = { 4, opts, 2, vars, desc };
110
double P[DIMENSIONS], V[DIMENSIONS], A[DIMENSIONS];
126
double P[DIMENSIONS], V[DIMENSIONS], A[DIMENSIONS];
112
128
unsigned long colors;
232
248
gp = &gravs[MI_SCREEN(mi)];
234
gp->width = MI_WIN_WIDTH(mi);
235
gp->height = MI_WIN_HEIGHT(mi);
250
gp->width = MI_WIDTH(mi);
251
gp->height = MI_HEIGHT(mi);
237
253
gp->sr = STARRADIUS;
239
gp->nplanets = MI_BATCHCOUNT(mi);
255
gp->nplanets = MI_COUNT(mi);
240
256
if (gp->nplanets < 0) {
241
257
if (gp->planets) {
242
258
(void) free((void *) gp->planets);
259
gp->planets = (planetstruct *) NULL;
245
261
gp->nplanets = NRAND(-gp->nplanets) + 1; /* Add 1 so its not too boring */
248
gp->planets = (planetstruct *) calloc(gp->nplanets, sizeof (planetstruct));
263
if (gp->planets == NULL) {
264
if ((gp->planets = (planetstruct *) calloc(gp->nplanets,
265
sizeof (planetstruct))) == NULL)
250
XClearWindow(display, MI_WINDOW(mi));
252
271
if (MI_NPIXELS(mi) > 2)
253
272
gp->starcolor = MI_PIXEL(mi, NRAND(MI_NPIXELS(mi)));
255
gp->starcolor = MI_WIN_WHITE_PIXEL(mi);
274
gp->starcolor = MI_WHITE_PIXEL(mi);
256
275
for (ball = 0; ball < (unsigned char) gp->nplanets; ball++)
257
276
init_planet(mi, &gp->planets[ball]);
268
287
Display *display = MI_DISPLAY(mi);
269
288
Window window = MI_WINDOW(mi);
270
289
GC gc = MI_GC(mi);
271
gravstruct *gp = &gravs[MI_SCREEN(mi)];
272
290
register unsigned char ball;
295
gp = &gravs[MI_SCREEN(mi)];
296
if (gp->planets == NULL)
299
MI_IS_DRAWN(mi) = True;
274
300
/* Mask centrepoint */
275
XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
301
XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
276
302
XDrawArc(display, window, gc,
277
303
gp->width / 2 - gp->sr / 2, gp->height / 2 - gp->sr / 2, gp->sr, gp->sr,