1 /* xscreensaver, Copyright (c) 1992-2008 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
12 /* Draw some fireworks. Inspired from TI Explorer Lisp code by
13 John S. Pezaris <pz@hx.lcs.mit.edu>
17 #include "screenhack.h"
20 int x, y; /* position */
21 int dx, dy; /* velocity */
28 struct projectile *next_free;
38 struct projectile *projectiles, *free_projectiles;
39 struct projectile **sorted_projectiles;
42 unsigned int default_fg_pixel;
45 int how_many, frequency, scatter, delay;
47 int sin_cache[PI_2000];
48 int cos_cache[PI_2000];
50 int draw_xlim, draw_ylim, real_draw_xlim, real_draw_ylim;
52 unsigned long last_pixel;
57 /* Slightly whacked, for better explosions
61 cache(struct state *st)
62 { /*needs to be run once. Could easily be */
63 int i; /*reimplemented to run and cache at compile-time,*/
64 double dA; /*saving on init_pyro time */
65 for (i=0; i<PI_2000; i++)
67 dA=sin(((double) (random() % (PI_2000/2)))/1000.0);
68 /*Emulation of spherical distribution*/
69 dA+=asin(frand(1.0))/M_PI_2*0.1;
70 /*Approximating the integration of the binominal, for
71 well-distributed randomness*/
72 st->cos_cache[i]=(int) (cos(((double)i)/1000.0)*dA*2500.0);
73 st->sin_cache[i]=(int) (sin(((double)i)/1000.0)*dA*2500.0);
78 static struct projectile *
79 get_projectile (struct state *st)
82 if (st->free_projectiles)
84 p = st->free_projectiles;
85 st->free_projectiles = p->next_free;
95 free_projectile (struct state *st, struct projectile *p)
97 p->next_free = st->free_projectiles;
98 st->free_projectiles = p;
103 launch (struct state *st,
104 int xlim, int ylim, int g)
106 struct projectile *p = get_projectile (st);
111 x = (random () % xlim);
112 dx = 30000 - (random () % 60000);
113 xxx = x + (dx * 200);
114 } while (xxx <= 0 || xxx >= xlim);
121 p->dy = (random () % 4000) - 13000;
122 p->fuse = ((((random () % 500) + 500) * abs (p->dy / g)) / 1000);
125 /* cope with small windows -- those constants assume big windows. */
127 int dd = 1000000 / ylim;
134 hsv_to_rgb (random () % 360, 1.0, 1.0,
135 &p->color.red, &p->color.green, &p->color.blue);
136 p->color.flags = DoRed | DoGreen | DoBlue;
137 if (!XAllocColor (st->dpy, st->cmap, &p->color))
139 p->color.pixel = WhitePixel (st->dpy, DefaultScreen (st->dpy));
140 p->color.red = p->color.green = p->color.blue = 0xFFFF;
145 static struct projectile *
146 shrapnel (struct state *st, struct projectile *parent)
148 struct projectile *p = get_projectile (st);
153 v=random () % PI_2000;
154 p->dx =(st->sin_cache[v]) + parent->dx;
155 p->dy =(st->cos_cache[v]) + parent->dy;
156 p->decay = (random () % 50) - 60;
157 p->size = (parent->size * 2) / 3;
161 p->color = parent->color;
163 XAllocColor (st->dpy, st->cmap, &p->color); /* dup the lock */
169 pyro_init (Display *dpy, Window window)
171 struct state *st = (struct state *) calloc (1, sizeof(*st));
174 XWindowAttributes xgwa;
177 XGetWindowAttributes (st->dpy, st->window, &xgwa);
179 st->cmap = xgwa.colormap;
180 st->delay = get_integer_resource (st->dpy, "delay", "Integer");
181 st->how_many = get_integer_resource (st->dpy, "count", "Integer");
182 st->frequency = get_integer_resource (st->dpy, "frequency", "Integer");
183 st->scatter = get_integer_resource (st->dpy, "scatter", "Integer");
184 if (st->how_many <= 0) st->how_many = 100;
185 if (st->frequency <= 0) st->frequency = 30;
186 if (st->scatter <= 0) st->scatter = 20;
188 st->free_projectiles = 0;
189 st->projectiles = (struct projectile *)
190 calloc (st->how_many, sizeof (*st->projectiles));
191 st->sorted_projectiles = (struct projectile **)
192 calloc (st->how_many, sizeof (*st->sorted_projectiles));
193 for (i = 0; i < st->how_many; i++)
194 free_projectile (st, &st->projectiles [i]);
195 for (i = 0; i < st->how_many; i++)
196 st->sorted_projectiles[i] = &st->projectiles[i];
197 gcv.foreground = st->default_fg_pixel =
198 get_pixel_resource (st->dpy, st->cmap, "foreground", "Foreground");
199 st->draw_gc = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
200 gcv.foreground = get_pixel_resource (st->dpy, st->cmap, "background", "Background");
201 st->erase_gc = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
202 XClearWindow (st->dpy, st->window);
210 projectile_pixel_sorter (const void *a, const void *b)
212 struct projectile *pa = *(struct projectile **) a;
213 struct projectile *pb = *(struct projectile **) b;
214 if (pa->color.pixel == pb->color.pixel) return 0;
215 else if (pa->color.pixel < pb->color.pixel) return -1;
220 sort_by_pixel (struct state *st, int length)
222 qsort ((void *) st->sorted_projectiles,
224 sizeof(*st->sorted_projectiles),
225 projectile_pixel_sorter);
230 pyro_draw (Display *dpy, Window window, void *closure)
232 struct state *st = (struct state *) closure;
233 XWindowAttributes xgwa;
238 for (i = 0; i < st->how_many; i++)
240 struct projectile *p = st->sorted_projectiles [i];
241 int old_x, old_y, old_size;
243 if (p->dead) continue;
246 old_size = p->size >> 10;
247 size = (p->size += p->decay) >> 10;
248 x = (p->x += p->dx) >> 10;
249 y = (p->y += p->dy) >> 10;
250 p->dy += (p->size >> 6);
251 if (p->primary) p->fuse--;
257 XDrawPoint (st->dpy, st->window, st->erase_gc, old_x, old_y);
259 XFillRectangle (st->dpy, st->window, st->erase_gc, old_x, old_y,
263 if ((p->primary ? (p->fuse > 0) : (p->size > 0)) &&
264 x < st->real_draw_xlim &&
265 y < st->real_draw_ylim &&
273 if (mono_p || p->primary)
274 pixel = st->default_fg_pixel;
276 pixel = p->color.pixel;
278 if (pixel != st->last_pixel)
280 st->last_pixel = pixel;
281 XSetForeground (st->dpy, st->draw_gc, pixel);
285 XDrawPoint (st->dpy, st->window, st->draw_gc, x, y);
287 XFillRectangle (st->dpy, st->window, st->draw_gc, x, y, size, size);
289 XFillArc (st->dpy, st->window, st->draw_gc, x, y, size, size, 0, 360*64);
294 free_projectile (st, p);
296 if (p->color.pixel != WhitePixel (st->dpy, DefaultScreen (st->dpy)))
297 XFreeColors (st->dpy, st->cmap, &p->color.pixel, 1, 0);
300 if (p->primary && p->fuse <= 0)
302 int j = (random () % st->scatter) + (st->scatter/2);
309 if ((random () % st->frequency) == 0)
311 XGetWindowAttributes (st->dpy, st->window, &xgwa);
312 st->real_draw_xlim = xgwa.width;
313 st->real_draw_ylim = xgwa.height;
314 st->draw_xlim = st->real_draw_xlim * 1000;
315 st->draw_ylim = st->real_draw_ylim * 1000;
316 launch (st, st->draw_xlim, st->draw_ylim, g);
320 /* being sorted lets us avoid changing the GC's foreground color as often. */
322 sort_by_pixel (st, st->how_many);
328 pyro_reshape (Display *dpy, Window window, void *closure,
329 unsigned int w, unsigned int h)
334 pyro_event (Display *dpy, Window window, void *closure, XEvent *event)
340 pyro_free (Display *dpy, Window window, void *closure)
342 struct state *st = (struct state *) closure;
348 static const char *pyro_defaults [] = {
349 ".background: black",
350 ".foreground: white",
356 "*geometry: 800x500",
360 static XrmOptionDescRec pyro_options [] = {
361 { "-delay", ".delay", XrmoptionSepArg, 0 },
362 { "-count", ".count", XrmoptionSepArg, 0 },
363 { "-frequency", ".frequency", XrmoptionSepArg, 0 },
364 { "-scatter", ".scatter", XrmoptionSepArg, 0 },
368 XSCREENSAVER_MODULE ("Pyro", pyro)