summaryrefslogtreecommitdiffstats
path: root/hacks/pyro.c
blob: 442ea7f0da67ddb5a6a416b7793c088767bf5e1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* xscreensaver, Copyright (c) 1992-2008 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

/* Draw some fireworks.  Inspired from TI Explorer Lisp code by 
   John S. Pezaris <pz@hx.lcs.mit.edu>
 */

#include <math.h>
#include "screenhack.h"

struct projectile {
  int x, y;	/* position */
  int dx, dy;	/* velocity */
  int decay;
  int size;
  int fuse;
  Bool primary;
  Bool dead;
  XColor color;
  struct projectile *next_free;
};

#define PI_2000 6284


struct state {
  Display *dpy;
  Window window;

   struct projectile *projectiles, *free_projectiles;
   struct projectile **sorted_projectiles;

   GC draw_gc, erase_gc;
   unsigned int default_fg_pixel;
   Colormap cmap;

   int how_many, frequency, scatter, delay;

   int sin_cache[PI_2000];
   int cos_cache[PI_2000];

   int draw_xlim, draw_ylim, real_draw_xlim, real_draw_ylim;

   unsigned long last_pixel;
};



/* Slightly whacked, for better explosions
 */

static void
cache(struct state *st)
{               /*needs to be run once. Could easily be */
  int i;        /*reimplemented to run and cache at compile-time,*/
  double dA;    /*saving on init_pyro time */
  for (i=0; i<PI_2000; i++)
    {
      dA=sin(((double) (random() % (PI_2000/2)))/1000.0);
      /*Emulation of spherical distribution*/
      dA+=asin(frand(1.0))/M_PI_2*0.1;
      /*Approximating the integration of the binominal, for
        well-distributed randomness*/
      st->cos_cache[i]=(int) (cos(((double)i)/1000.0)*dA*2500.0);
      st->sin_cache[i]=(int) (sin(((double)i)/1000.0)*dA*2500.0);
    }
}


static struct projectile *
get_projectile (struct state *st)
{
  struct projectile *p;
  if (st->free_projectiles)
    {
      p = st->free_projectiles;
      st->free_projectiles = p->next_free;
      p->next_free = 0;
      p->dead = False;
      return p;
    }
  else
    return 0;
}

static void
free_projectile (struct state *st, struct projectile *p)
{
  p->next_free = st->free_projectiles;
  st->free_projectiles = p;
  p->dead = True;
}

static void
launch (struct state *st, 
        int xlim, int ylim, int g)
{
  struct projectile *p = get_projectile (st);
  int x, dx, xxx;
  if (! p) return;

  do {
    x = (random () % xlim);
    dx = 30000 - (random () % 60000);
    xxx = x + (dx * 200);
  } while (xxx <= 0 || xxx >= xlim);

  p->x = x;
  p->y = ylim;
  p->dx = dx;
  p->size = 8000;
  p->decay = 0;
  p->dy = (random () % 4000) - 13000;
  p->fuse = ((((random () % 500) + 500) * abs (p->dy / g)) / 1000);
  p->primary = True;

  /* cope with small windows -- those constants assume big windows. */
  {
    int dd = 1000000 / ylim;
    if (dd > 1)
      p->fuse /= dd;
  }

  if (! mono_p)
    {
      hsv_to_rgb (random () % 360, 1.0, 1.0,
		  &p->color.red, &p->color.green, &p->color.blue);
      p->color.flags = DoRed | DoGreen | DoBlue;
      if (!XAllocColor (st->dpy, st->cmap, &p->color))
	{
	  p->color.pixel = WhitePixel (st->dpy, DefaultScreen (st->dpy));
	  p->color.red = p->color.green = p->color.blue = 0xFFFF;
	}
    }
}

static struct projectile *
shrapnel (struct state *st, struct projectile *parent)
{
  struct projectile *p = get_projectile (st);
  int v;
  if (! p) return 0;
  p->x = parent->x;
  p->y = parent->y;
  v=random () % PI_2000;
  p->dx =(st->sin_cache[v]) + parent->dx;
  p->dy =(st->cos_cache[v]) + parent->dy;
  p->decay = (random () % 50) - 60;
  p->size = (parent->size * 2) / 3;
  p->fuse = 0;
  p->primary = False;

  p->color = parent->color;
  if (! mono_p)
    XAllocColor (st->dpy, st->cmap, &p->color); /* dup the lock */
  
  return p;
}

static void *
pyro_init (Display *dpy, Window window)
{
  struct state *st = (struct state *) calloc (1, sizeof(*st));
  int i;
  XGCValues gcv;
  XWindowAttributes xgwa;
  st->dpy = dpy;
  st->window = window;
  XGetWindowAttributes (st->dpy, st->window, &xgwa);
  st->last_pixel = ~0;
  st->cmap = xgwa.colormap;
  st->delay = get_integer_resource (st->dpy, "delay", "Integer");
  st->how_many = get_integer_resource (st->dpy, "count", "Integer");
  st->frequency = get_integer_resource (st->dpy, "frequency", "Integer");
  st->scatter = get_integer_resource (st->dpy, "scatter", "Integer");
  if (st->how_many <= 0) st->how_many = 100;
  if (st->frequency <= 0) st->frequency = 30;
  if (st->scatter <= 0) st->scatter = 20;
  st->projectiles = 0;
  st->free_projectiles = 0;
  st->projectiles = (struct projectile *)
    calloc (st->how_many, sizeof (*st->projectiles));
  st->sorted_projectiles = (struct projectile **)
    calloc (st->how_many, sizeof (*st->sorted_projectiles));
  for (i = 0; i < st->how_many; i++)
    free_projectile (st, &st->projectiles [i]);
  for (i = 0; i < st->how_many; i++)
    st->sorted_projectiles[i] = &st->projectiles[i];
  gcv.foreground = st->default_fg_pixel =
    get_pixel_resource (st->dpy, st->cmap, "foreground", "Foreground");
  st->draw_gc = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
  gcv.foreground = get_pixel_resource (st->dpy, st->cmap, "background", "Background");
  st->erase_gc = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
  XClearWindow (st->dpy, st->window);
  cache(st);  

  return st;
}


static int
projectile_pixel_sorter (const void *a, const void *b)
{
  struct projectile *pa = *(struct projectile **) a;
  struct projectile *pb = *(struct projectile **) b;
  if (pa->color.pixel == pb->color.pixel) return 0;
  else if (pa->color.pixel < pb->color.pixel) return -1;
  else return 1;
}

static void
sort_by_pixel (struct state *st, int length)
{
  qsort ((void *) st->sorted_projectiles,
         length,
         sizeof(*st->sorted_projectiles),
         projectile_pixel_sorter);
}


static unsigned long
pyro_draw (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
  XWindowAttributes xgwa;
  int g = 100;
  int resort = 0;
  int i;
  
  for (i = 0; i < st->how_many; i++)
    {
      struct projectile *p = st->sorted_projectiles [i];
      int old_x, old_y, old_size;
      int size, x, y;
      if (p->dead) continue;
      old_x = p->x >> 10;
      old_y = p->y >> 10;
      old_size = p->size >> 10;
      size = (p->size += p->decay) >> 10;
      x = (p->x += p->dx) >> 10;
      y = (p->y += p->dy) >> 10;
      p->dy += (p->size >> 6);
      if (p->primary) p->fuse--;

      /* erase old one */
      if (old_size > 0)
        {
          if (old_size == 1)
	    XDrawPoint (st->dpy, st->window, st->erase_gc, old_x, old_y);
          else
            XFillRectangle (st->dpy, st->window, st->erase_gc, old_x, old_y,
                            old_size, old_size);
        }

      if ((p->primary ? (p->fuse > 0) : (p->size > 0)) &&
	  x < st->real_draw_xlim &&
	  y < st->real_draw_ylim &&
	  x > 0 &&
	  y > 0)
	{
          if (size > 0)
            {
              unsigned long pixel;

              if (mono_p || p->primary)
                pixel = st->default_fg_pixel;
              else
                pixel = p->color.pixel;

              if (pixel != st->last_pixel)
                {
                  st->last_pixel = pixel;
                  XSetForeground (st->dpy, st->draw_gc, pixel);
                }

              if (size == 1)
                XDrawPoint (st->dpy, st->window, st->draw_gc, x, y);
              else if (size < 4)
                XFillRectangle (st->dpy, st->window, st->draw_gc, x, y, size, size);
              else
                XFillArc (st->dpy, st->window, st->draw_gc, x, y, size, size, 0, 360*64);
            }
        }
      else
	{
	  free_projectile (st, p);
	  if (! mono_p)
	    if (p->color.pixel != WhitePixel (st->dpy, DefaultScreen (st->dpy)))
	      XFreeColors (st->dpy, st->cmap, &p->color.pixel, 1, 0);
	}

      if (p->primary && p->fuse <= 0)
	{
	  int j = (random () % st->scatter) + (st->scatter/2);
	  while (j--)
	    shrapnel (st, p);
          resort = 1;
	}
    }

  if ((random () % st->frequency) == 0)
    {
      XGetWindowAttributes (st->dpy, st->window, &xgwa);
      st->real_draw_xlim = xgwa.width;
      st->real_draw_ylim = xgwa.height;
      st->draw_xlim = st->real_draw_xlim * 1000;
      st->draw_ylim = st->real_draw_ylim * 1000;
      launch (st, st->draw_xlim, st->draw_ylim, g);
      resort = 1;
    }

  /* being sorted lets us avoid changing the GC's foreground color as often. */
  if (resort)
    sort_by_pixel (st, st->how_many);

  return st->delay;
}

static void
pyro_reshape (Display *dpy, Window window, void *closure, 
              unsigned int w, unsigned int h)
{
}

static Bool
pyro_event (Display *dpy, Window window, void *closure, XEvent *event)
{
  return False;
}

static void
pyro_free (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
  free (st);
}



static const char *pyro_defaults [] = {
  ".lowrez:     true",
  ".background:	black",
  ".foreground:	white",
  "*fpsSolid:	true",
  "*count:	600",
  "*delay:	10000",
  "*frequency:	30",
  "*scatter:	100",
  0
};

static XrmOptionDescRec pyro_options [] = {
  { "-delay",		".delay",	XrmoptionSepArg, 0 },
  { "-count",		".count",	XrmoptionSepArg, 0 },
  { "-frequency",	".frequency",	XrmoptionSepArg, 0 },
  { "-scatter",		".scatter",	XrmoptionSepArg, 0 },
  { 0, 0, 0, 0 }
};

XSCREENSAVER_MODULE ("Pyro", pyro)