2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: pyro.c /main/3 1995/11/02 16:07:59 rswiston $ */
27 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
28 * (c) Copyright 1993, 1994 International Business Machines Corp. *
29 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
30 * (c) Copyright 1993, 1994 Novell, Inc. *
33 * pyro.c - Fireworks for dtscreen, the X Window System lockscreen.
35 * Copyright (c) 1991 by Patrick J. Naughton.
37 * See dtscreen.c for copying information.
40 * 16-Mar-91: Written. (received from David Brooks, brooks@osf.org).
43 /* The physics of the rockets is a little bogus, but it looks OK. Each is
44 * given an initial velocity impetus. They decelerate slightly (gravity
45 * overcomes the rocket's impulse) and explode as the rocket's main fuse
46 * gives out (we could add a ballistic stage, maybe). The individual
47 * stars fan out from the rocket, and they decelerate less quickly.
48 * That's called bouyancy, but really it's again a visual preference.
53 #define TWOPI 6.2831853
55 /* Define this >1 to get small rectangles instead of points */
62 #define BURSTINGINAIR 2
66 /* Clearly other types and other fascinating visual effects could be added...*/
68 /* P_xxx parameters represent the reciprocal of the probability... */
69 #define P_IGNITE 5000 /* ...of ignition per cycle */
70 #define P_DOUBLECLOUD 10 /* ...of an ignition being double */
71 #define P_MULTI 75 /* ...of an ignition being several @ once */
72 #define P_FUSILLADE 250 /* ...of an ignition starting a fusillade */
74 #define ROCKETW 2 /* Dimensions of rocket */
76 #define XVELFACTOR 0.0025 /* Max horizontal velocity / screen width */
77 #define MINYVELFACTOR 0.016 /* Min vertical velocity / screen height */
78 #define MAXYVELFACTOR 0.018
79 #define GRAVFACTOR 0.0002 /* delta v / screen height */
80 #define MINFUSE 50 /* range of fuse lengths for rocket */
83 #define FUSILFACTOR 10 /* Generate fusillade by reducing P_IGNITE */
84 #define FUSILLEN 100 /* Length of fusillade, in ignitions */
86 #define SVELFACTOR 0.1 /* Max star velocity / yvel */
87 #define BOUYANCY 0.2 /* Reduction in grav deceleration for stars */
88 #define MAXSTARS 75 /* Number of stars issued from a shell */
90 #define MINSFUSE 50 /* Range of fuse lengths for stars */
93 #define INTRAND(min,max) (random()%((max+1)-(min))+(min))
94 #define FLOATRAND(min,max) ((min)+(random()/MAXRAND)*((max)-(min)))
97 static void animate();
98 static void shootup();
110 XRectangle Xpoints[MAXSTARS];
111 XRectangle Xpoints2[MAXSTARS];
113 XPoint Xpoints[MAXSTARS];
114 XPoint Xpoints2[MAXSTARS];
116 float sx[MAXSTARS], sy[MAXSTARS]; /* Distance from notional
118 float sxvel[MAXSTARS], syvel[MAXSTARS]; /* Relative to notional
126 unsigned long bgpixel;
127 unsigned long fgpixel;
128 unsigned long rockpixel;
132 int width, lmargin, rmargin, height;
133 float minvelx, maxvelx;
134 float minvely, maxvely;
136 float rockdecel, stardecel;
140 static int orig_p_ignite;
141 static int just_started = True;/* Greet the user right away */
149 XWindowAttributes xwa;
151 int rockn, starn, bsize;
153 if (pwin->data) free(pwin->data);
154 pwin->data = (pyrostruct *)malloc(sizeof(pyrostruct));
155 memset(pwin->data, '\0', sizeof(pyrostruct));
156 pp = (pyrostruct *)pwin->data;
157 XGetWindowAttributes(dsp, pwin->w, &xwa);
159 orig_p_ignite = P_IGNITE / batchcount;
160 if (orig_p_ignite <= 0)
162 pp->p_ignite = orig_p_ignite;
164 pp->rockq = (rocket *) malloc(batchcount * sizeof(rocket));
166 pp->nflying = pp->fusilcount = 0;
168 bsize = (xwa.height <= 64) ? 1 : STARSIZE;
169 for (rockn = 0, rp = pp->rockq; rockn < batchcount; rockn++, rp++) {
172 for (starn = 0; starn < MAXSTARS; starn++) {
173 rp->Xpoints[starn].width = rp->Xpoints[starn].height =
174 rp->Xpoints2[starn].width = rp->Xpoints2[starn].height = bsize;
179 pp->width = xwa.width;
180 pp->lmargin = xwa.width / 16;
181 pp->rmargin = xwa.width - pp->lmargin;
182 pp->height = xwa.height;
183 pp->scr = pwin->perscreen->screen;
184 pp->cmap = DefaultColormapOfScreen(pp->scr);
186 pp->fgpixel = WhitePixelOfScreen(pp->scr);
187 pp->bgpixel = BlackPixelOfScreen(pp->scr);
188 if (!mono && pwin->perscreen->npixels > 3)
189 pp->rockpixel = pwin->perscreen->pixels[3];/* Just the right shade of
192 pp->rockpixel = pp->fgpixel;
194 xgcv.foreground = pp->bgpixel;
195 pp->bgGC = XCreateGC(dsp, pwin->w, GCForeground, &xgcv);
197 /* Geometry-dependent physical data: */
198 pp->maxvelx = (float) (xwa.width) * XVELFACTOR;
199 pp->minvelx = -pp->maxvelx;
200 pp->minvely = -(float) (xwa.height) * MINYVELFACTOR;
201 pp->maxvely = -(float) (xwa.height) * MAXYVELFACTOR;
202 pp->maxsvel = pp->minvely * SVELFACTOR;
203 pp->rockdecel = (float) (pp->height) * GRAVFACTOR;
204 pp->stardecel = pp->rockdecel * BOUYANCY;
206 XFillRectangle(dsp, pwin->w, pp->bgGC, 0, 0, xwa.width, xwa.height);
218 pp = (pyrostruct *)pwin->data;
219 if (just_started || (random() % pp->p_ignite == 0)) {
220 just_started = False;
221 if (random() % P_FUSILLADE == 0) {
222 pp->p_ignite = orig_p_ignite / FUSILFACTOR;
223 pp->fusilcount = INTRAND(FUSILLEN * 9 / 10, FUSILLEN * 11 / 10);
226 if (pp->fusilcount > 0) {
227 if (--pp->fusilcount == 0)
228 pp->p_ignite = orig_p_ignite;
231 for (rockn = pp->nflying, rp = pp->rockq; rockn > 0; rp++) {
232 if (rp->state != SILENT) {
233 animate(pwin, pp, rp);
245 int multi, shelltype, nstars, fuse, npix, pix, color1, color2;
248 x = random() % pp->width;
249 xvel = FLOATRAND(-pp->maxvelx, pp->maxvelx);
250 /* All this to stop too many rockets going offscreen: */
251 if (x < pp->lmargin && xvel < 0.0 || x > pp->rmargin && xvel > 0.0)
253 yvel = FLOATRAND(pp->minvely, pp->maxvely);
254 fuse = INTRAND(MINFUSE, MAXFUSE);
255 nstars = INTRAND(MINSTARS, MAXSTARS);
256 if (!mono && (npix = pwin->perscreen->npixels) > 2) {
257 color1 = pwin->perscreen->pixels[pix = random() % npix];
258 color2 = pwin->perscreen->pixels[(pix + (npix / 2)) % npix];
260 color1 = color2 = WhitePixelOfScreen(pwin->perscreen->screen);
264 if (random() % P_DOUBLECLOUD == 0)
265 shelltype = DOUBLECLOUD;
268 if (random() % P_MULTI == 0)
269 multi = INTRAND(5, 15);
274 if (pp->nflying >= batchcount)
276 while (rp->state != SILENT)
279 rp->shelltype = shelltype;
280 rp->state = REDGLARE;
284 rp->yvel = FLOATRAND(yvel * 0.97, yvel * 1.03);
285 rp->fuse = INTRAND((fuse * 90) / 100, (fuse * 110) / 100);
286 rp->x = x + FLOATRAND(multi * 7.6, multi * 8.4);
287 rp->y = pp->height - 1;
293 animate(pwin, pp, rp)
301 if (rp->state == REDGLARE) {
302 shootup(pwin, pp, rp);
304 /* Handle setup for explosion */
305 if (rp->state == BURSTINGINAIR) {
306 for (starn = 0; starn < rp->nstars; starn++) {
307 rp->sx[starn] = rp->sy[starn] = 0.0;
308 rp->Xpoints[starn].x = (int) rp->x;
309 rp->Xpoints[starn].y = (int) rp->y;
310 if (rp->shelltype == DOUBLECLOUD) {
311 rp->Xpoints2[starn].x = (int) rp->x;
312 rp->Xpoints2[starn].y = (int) rp->y;
314 /* This isn't accurate solid geometry, but it looks OK. */
316 r = FLOATRAND(0.0, pp->maxsvel);
317 theta = FLOATRAND(0.0, TWOPI);
318 rp->sxvel[starn] = r * cos(theta);
319 rp->syvel[starn] = r * sin(theta);
321 rp->fuse = INTRAND(MINSFUSE, MAXSFUSE);
324 if (rp->state == BURSTINGINAIR) {
330 shootup(pwin, pp, rp)
335 XFillRectangle(dsp, pwin->w, pp->bgGC, (int) (rp->x), (int) (rp->y),
336 ROCKETW, ROCKETH + 3);
338 if (rp->fuse-- <= 0) {
339 rp->state = BURSTINGINAIR;
344 rp->yvel += pp->rockdecel;
345 XSetForeground(dsp, pwin->gc, pp->rockpixel);
346 XFillRectangle(dsp, pwin->w, pwin->gc, (int) (rp->x), (int) (rp->y),
347 ROCKETW, ROCKETH + random() % 4);
357 register int nstars, stype;
358 register float rx, ry, sd; /* Help compiler optimize :-) */
359 register float sx, sy;
362 stype = rp->shelltype;
365 XFillRectangles(dsp, pwin->w, pp->bgGC, rp->Xpoints, nstars);
366 if (stype == DOUBLECLOUD)
367 XFillRectangles(dsp, pwin->w, pp->bgGC, rp->Xpoints2, nstars);
369 XDrawPoints(dsp, pwin->w, pp->bgGC, rp->Xpoints, nstars, CoordModeOrigin);
370 if (stype == DOUBLECLOUD)
371 XDrawPoints(dsp, pwin->w, pp->bgGC, rp->Xpoints2, nstars, CoordModeOrigin);
374 if (rp->fuse-- <= 0) {
379 /* Stagger the stars' decay */
381 if ((rp->nstars = nstars = nstars * 90 / 100) == 0)
387 for (starn = 0; starn < nstars; starn++) {
388 sx = rp->sx[starn] += rp->sxvel[starn];
389 sy = rp->sy[starn] += rp->syvel[starn];
390 rp->syvel[starn] += sd;
391 rp->Xpoints[starn].x = (int) (rx + sx);
392 rp->Xpoints[starn].y = (int) (ry + sy);
393 if (stype == DOUBLECLOUD) {
394 rp->Xpoints2[starn].x = (int) (rx + 1.7 * sx);
395 rp->Xpoints2[starn].y = (int) (ry + 1.7 * sy);
398 rp->x = rx + rp->xvel;
399 rp->y = ry + rp->yvel;
402 XSetForeground(dsp, pwin->gc, rp->color1);
404 XFillRectangles(dsp, pwin->w, pwin->gc, rp->Xpoints, nstars);
405 if (stype == DOUBLECLOUD) {
406 XSetForeground(dsp, pwin->gc, rp->color2);
407 XFillRectangles(dsp, pwin->w, pwin->gc, rp->Xpoints2, nstars);
410 XDrawPoints(dsp, pwin->w, pwin->gc, rp->Xpoints, nstars, CoordModeOrigin);
411 if (stype == DOUBLECLOUD) {
412 XSetForeground(dsp, pwin->gc, rp->color2);
413 XDrawPoints(dsp, pwin->w, pwin->gc, rp->Xpoints2, nstars,