summaryrefslogtreecommitdiffstats
path: root/hacks/eruption.c
blob: a4cfc254fe1752f2ca9f22f78261d3d23f852dd9 (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/* Eruption, Copyright (c) 2002-2003 W.P. van Paassen <peter@paassen.tmfweb.nl>
 *
 * 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.
 *
 * Module - "eruption.c"
 *
 * [01-2015] - Dave Odell <dmo2118@gmail.com>: Performance tweaks. Also, click-for-explosion.
 * [02-2003] - W.P. van Paassen: Improvements, added some code of jwz from the pyro hack for a spherical distribution of the particles
 * [01-2003] - W.P. van Paassen: Port to X for use with XScreenSaver, the shadebob hack by Shane Smit was used as a template
 * [04-2002] - W.P. van Paassen: Creation for the Demo Effects Collection (http://demo-effects.sourceforge.net)
 */

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

#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#else
typedef unsigned long uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;
#endif

/*#define VERBOSE*/ 

/* Slightly whacked, for better explosions
 */
#define PI_2000 6284
#define SPREAD 15

/*particle structure*/
typedef struct 
{
  short xpos, ypos, xdir, ydir;
  unsigned char colorindex;
  unsigned char dead;
} PARTICLE;

struct state {
  Display *dpy;
  Window window;

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

  PARTICLE *particles;
  unsigned short iWinWidth, iWinHeight;
  unsigned char *fire;
  unsigned short nParticleCount;
  unsigned char xdelta, ydelta, decay;
  signed char gravity;
  signed short heat;

  int cycles, delay;
  GC gc;
  signed short iColorCount;
  unsigned long *aiColorVals;
  XImage *pImage;
  XShmSegmentInfo shm_info;

  int draw_i;
};

static void
cache(struct state *st) /* jwz */
{               /*needs to be run once. Could easily be */
  int i;        /*reimplemented to run and cache at compile-time,*/
  double dA;    
  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]=-abs((int) (cos(((double)i)/1000.0)*dA*st->ydelta));
      st->sin_cache[i]=(int) (sin(((double)i)/1000.0)*dA*st->xdelta);
    }
}

static void init_particle(struct state *st, PARTICLE* particle, unsigned short xcenter, unsigned short ycenter)
{
  int v = random() % PI_2000;
  particle->xpos = xcenter - SPREAD + (random() % (SPREAD * 2));
  particle->ypos = ycenter - SPREAD + (random() % (SPREAD * 2));;
  particle->xdir = st->sin_cache[v];
  particle->ydir = st->cos_cache[v];
  particle->colorindex = st->iColorCount-1;
  particle->dead = 0;
}

#define X_PAD 1 /* Could be more if anybody wants the blur to fall off the left/right edges. */
#define Y_PAD 1


static void Execute( struct state *st )
{
  XImage *img = st->pImage;
  int i, j;
  size_t fire_pitch = st->iWinWidth + X_PAD * 2;
  unsigned char *line0, *line1, *line2;

  /* move particles */
  
  for (i = 0; i < st->nParticleCount; i++)
    {
      if (!st->particles[i].dead)
	{
	  st->particles[i].xpos += st->particles[i].xdir;
	  st->particles[i].ypos += st->particles[i].ydir;
	  
	  /* is particle dead? */
	  
	  if (st->particles[i].colorindex == 0)
	    {
	      st->particles[i].dead = 1;
	      continue;
	    }
	  
	  if (st->particles[i].xpos < 1)
	    {
	      st->particles[i].xpos = 1;
	      st->particles[i].xdir = -st->particles[i].xdir - 4;
	      st->particles[i].colorindex = st->iColorCount;
	    }
	  else if (st->particles[i].xpos >= st->iWinWidth - 2)
	    {
	      st->particles[i].xpos = st->iWinWidth - 2;
	      if (st->particles[i].xpos < 1) st->particles[i].xpos = 1;
	      st->particles[i].xdir = -st->particles[i].xdir + 4;
	      st->particles[i].colorindex = st->iColorCount;
	    }
	  
	  if (st->particles[i].ypos < 1)
	    {
	      st->particles[i].ypos = 1;
	      st->particles[i].ydir = -st->particles[i].ydir;
	      st->particles[i].colorindex = st->iColorCount;
	    }
	  else if (st->particles[i].ypos >= st->iWinHeight - 3)
	    {
	      st->particles[i].ypos = st->iWinHeight- 3;
	      if (st->particles[i].ypos < 1) st->particles[i].ypos = 1;
	      st->particles[i].ydir = (-st->particles[i].ydir >> 2) - (random() % 2);
	      st->particles[i].colorindex = st->iColorCount;
	    }

	  
	  /* st->gravity kicks in */
	  st->particles[i].ydir += st->gravity;
	  
	  /* particle cools off */
	  st->particles[i].colorindex--;
	}
    }

  /* draw particles into st->fire array */
  for( i = 0; i < st->nParticleCount; i++ )
    {
      PARTICLE *p = &st->particles[i];
      if( !p->dead && p->ypos >= -Y_PAD + 1 && p->ypos < st->iWinHeight + Y_PAD - 1 )
        {
          /* draw particle */
          unsigned char *center = st->fire + (p->ypos - -Y_PAD) * fire_pitch + (p->xpos + X_PAD);
          unsigned char color = p->colorindex;
          *center = color;
          center[-1] = color;
          if (p->ypos < st->iWinHeight + Y_PAD - 2)
            center[fire_pitch] = color;
          if (p->ypos >= -Y_PAD + 2)
            center[-fire_pitch] = color;
          center[1] = color;
        }
    }

  line0 = st->fire + X_PAD;
  line1 = line0 + fire_pitch;
  line2 = line1 + fire_pitch;

  /* create st->fire effect */

  switch( img->bits_per_pixel )
    {
    case 8:
    case 16:
    case 32:
      break;
    default:
      memset( img->data, 0, img->bytes_per_line * img->height );
      break;
    }

  for( i = -Y_PAD + 1; i < st->iWinHeight + Y_PAD - 1; i++ )
    {
      const int j0 = -X_PAD + 1;

      unsigned
        t0 = line0[j0 - 1] + line1[j0 - 1] + line2[j0 - 1],
        t1 = line0[j0] + line1[j0] + line2[j0];

      unsigned j1 = st->iWinWidth + X_PAD;

      /* This is basically like the GIMP's "Convolution Matrix" filter, with
         the following settings:
         Matrix:
         | 1 1 1 |    Divisor = 8
         | 1 0 1 |    Offset = -eruption.cooloff
         | 1 1 1 |
         Except that the effect is applied to each pixel individually, in order
         from left-to-right, then top-to-bottom, resulting in a slight smear
         going rightwards and downwards.
       */

      for( j = j0 + 1; j != j1; j++ )
        {
          unsigned t2 = line0[j] + line1[j] + line2[j];
          unsigned char *px = line1 + j - 1;
          int temp;
          t1 -= *px;
          temp = t0 + t1 + t2 - st->decay;
          temp = temp >= 0 ? temp >> 3 : 0;
          *px = temp;
          t0 = t1 + temp;
          t1 = t2;
        }

      if( i >= 0 && i < st->iWinHeight )
        {
          /* draw st->fire array to screen */
          void *out_ptr = img->data + img->bytes_per_line * i;

          switch( img->bits_per_pixel )
            {
            case 32:
              {
                uint32_t *out = (uint32_t *)out_ptr;
                for( j = 0; j < st->iWinWidth; ++j )
                  out[j] = st->aiColorVals[ line1[j] ];
              }
              break;
            case 16:
              {
                uint16_t *out = (uint16_t *)out_ptr;
                for( j = 0; j < st->iWinWidth; ++j )
                  out[j] = st->aiColorVals[ line1[j] ];
              }
              break;
            case 8:
              {
                uint8_t *out = (uint8_t *)out_ptr;
                for( j = 0; j < st->iWinWidth; ++j )
                  out[j] = st->aiColorVals[ line1[j] ];
              }
              break;
            default:
              for( j = 0; j < st->iWinWidth; ++j )
                {
                  if( line1[j] )
                    XPutPixel( img, j, i, st->aiColorVals[ line1[j] ] );
                }
              break;
            }
        }

      line0 += fire_pitch;
      line1 += fire_pitch;
      line2 += fire_pitch;
    }
  put_xshm_image( st->dpy, st->window, st->gc, st->pImage,
                  0,0,0,0, st->iWinWidth, st->iWinHeight, &st->shm_info );
}

static unsigned long * SetPalette(struct state *st)
{
	XWindowAttributes XWinAttribs;
	XColor Color, *aColors;
	signed short iColor;

	XGetWindowAttributes( st->dpy, st->window, &XWinAttribs );
	
	st->iColorCount = get_integer_resource(st->dpy,  "ncolors", "Integer" );
	if( st->iColorCount < 16 )	st->iColorCount = 16;
	if( st->iColorCount > 255 )	st->iColorCount = 256;

	aColors    = calloc( st->iColorCount, sizeof(XColor) );
	st->aiColorVals = calloc( st->iColorCount, sizeof(unsigned long) );
	
	Color.red = Color.green = Color.blue = 65535 / st->iColorCount;

	/* create st->fire palette */
	for( iColor=0; iColor < st->iColorCount; iColor++ )
	{
	  if (iColor < st->iColorCount >> 3)
	    {
	      /* black to blue */
	      aColors[iColor].red = 0;
	      aColors[iColor].green = 0;
	      aColors[iColor].blue = Color.blue * (iColor << 1);
	    }
	  else if (iColor < st->iColorCount >> 2)
	    {
	      /* blue to red */
	      signed short temp = (iColor - (st->iColorCount >> 3));
	      aColors[iColor].red = Color.red * (temp << 3);
	      aColors[iColor].green = 0;
	      aColors[iColor].blue = 16383 - Color.blue * (temp << 1);
	    }
	  else if (iColor < (st->iColorCount >> 2) + (st->iColorCount >> 3))
	    {
	      /* red to yellow */
	      signed short temp = (iColor - (st->iColorCount >> 2)) << 3;
	      aColors[iColor].red = 65535;
	      aColors[iColor].green = Color.green * temp;
	      aColors[iColor].blue = 0;
	    }
	  else if (iColor < st->iColorCount >> 1)
	    {
	      /* yellow to white */
	      signed int temp = (iColor - ((st->iColorCount >> 2) + (st->iColorCount >> 3))) << 3;
	      aColors[iColor].red = 65535;
	      aColors[iColor].green = 65535;
	      aColors[iColor].blue = Color.blue * temp;
	    }
	  else
	    {
	      /* white */
	      aColors[iColor].red = aColors[iColor].green = aColors[iColor].blue = 65535;
	    }

	  if( !XAllocColor( st->dpy, XWinAttribs.colormap, &aColors[ iColor ] ) )
	    {
	      /* start all over with less colors */	
	      XFreeColors( st->dpy, XWinAttribs.colormap, st->aiColorVals, iColor, 0 );
	      free( aColors );
	      free( st->aiColorVals );
	      (st->iColorCount)--;
	      aColors     = calloc( st->iColorCount, sizeof(XColor) );
	      st->aiColorVals = calloc( st->iColorCount, sizeof(unsigned long) );
	      iColor = -1;
	    }
	  else
	    st->aiColorVals[ iColor ] = aColors[ iColor ].pixel;
	}

	if (st->heat < st->iColorCount)
	  st->iColorCount = st->heat;
	
	free( aColors );

	XSetWindowBackground( st->dpy, st->window, st->aiColorVals[ 0 ] );

	return st->aiColorVals;
}


static void DestroyImage (struct state *st)
{
  free (st->fire);
  destroy_xshm_image (st->dpy, st->pImage, &st->shm_info);
}

static void CreateImage( struct state *st, XWindowAttributes *XWinAttribs )
{
  /* Create the Image for drawing */

  /* Must be NULL because of how DestroyImage works. */
  assert( !st->fire );

  st->pImage = create_xshm_image( st->dpy, XWinAttribs->visual,
                                  XWinAttribs->depth, ZPixmap, &st->shm_info,
                                  XWinAttribs->width, XWinAttribs->height );

  memset( (st->pImage)->data, 0,
          (st->pImage)->bytes_per_line * (st->pImage)->height);

  st->iWinWidth = XWinAttribs->width;
  st->iWinHeight = XWinAttribs->height;

  /* create st->fire array */
  st->fire = calloc( (st->iWinHeight + Y_PAD * 2) * (st->iWinWidth + X_PAD * 2), sizeof(unsigned char));
}

static void Initialize( struct state *st, XWindowAttributes *XWinAttribs )
{
	XGCValues gcValues;

	/*  Create the GC. */
	st->gc = XCreateGC( st->dpy, st->window, 0, &gcValues );

	CreateImage (st, XWinAttribs);

	/*create st->particles */
	st->particles = malloc (st->nParticleCount * sizeof(PARTICLE));
}

static void *
eruption_init (Display *dpy, Window window)
{
  struct state *st = (struct state *) calloc (1, sizeof(*st));
	XWindowAttributes XWinAttribs;
	unsigned short sum = 0;
#ifdef VERBOSE
	time_t nTime = time( NULL );
	unsigned short iFrame = 0;
#endif  /*  VERBOSE */

  st->dpy = dpy;
  st->window = window;

	st->nParticleCount = get_integer_resource(st->dpy,  "particles", "Integer" );
	if (st->nParticleCount < 100)
	  st->nParticleCount = 100;
	if (st->nParticleCount > 2000)
	  st->nParticleCount = 2000;

	st->decay = get_integer_resource(st->dpy,  "cooloff", "Integer" );
	if (st->decay <= 0)
	  st->decay = 0;
	if (st->decay > 10)
	  st->decay = 10;
	st->decay <<= 3;

	st->gravity = get_integer_resource(st->dpy,  "gravity", "Integer" );
	if (st->gravity < -5)
	  st->gravity = -5;
	if (st->gravity > 5)
	  st->gravity = 5;

	st->heat = get_integer_resource(st->dpy,  "heat", "Integer" );
	if (st->heat < 64)
	  st->heat = 64;
	if (st->heat > 256)
	  st->heat = 256;

#ifdef VERBOSE 
	printf( "%s: Allocated %d st->particles\n", progclass, st->nParticleCount );
#endif  /*  VERBOSE */

	XGetWindowAttributes( st->dpy, st->window, &XWinAttribs );

	Initialize( st, &XWinAttribs );

	st->ydelta = 0;
	while (sum < (st->iWinHeight >> 1) - SPREAD)
	  {
	    st->ydelta++;
	    sum += st->ydelta;
	  }

	sum = 0;
	while (sum < (st->iWinWidth >> 3))
	  {
	    st->xdelta++;
	    sum += st->xdelta;
	  }

	st->delay = get_integer_resource(st->dpy,  "delay", "Integer" );
	st->cycles = get_integer_resource(st->dpy,  "cycles", "Integer" );

	cache(st);
	
	XFreeColors( st->dpy, XWinAttribs.colormap, st->aiColorVals, st->iColorCount, 0 );
	free( st->aiColorVals );
	st->aiColorVals = SetPalette( st );
	XClearWindow( st->dpy, st->window );

        st->draw_i = -1;

        return st;
}


static void
new_eruption (struct state *st, unsigned short xcenter, unsigned short ycenter)
{
  for (st->draw_i = 0; st->draw_i < st->nParticleCount; st->draw_i++)
    init_particle(st, st->particles + st->draw_i, xcenter, ycenter);
  st->draw_i = 0;
}


static void
random_eruption (struct state *st)
{
  /* compute random center */
  new_eruption (st, random() % st->iWinWidth, random() % st->iWinHeight);
}


static unsigned long
eruption_draw (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
	    
  if( st->draw_i < 0 || st->draw_i++ >= st->cycles )
    {
      random_eruption (st);
    }
	    
  Execute( st );
	    
#ifdef VERBOSE
  iFrame++;
  if( nTime - time( NULL ) )
    {
      printf( "%s: %d FPS\n", progclass, iFrame );
      nTime = time( NULL );
      iFrame = 0;
    }
#endif  /*  VERBOSE */

  return st->delay;
}
	

static void
eruption_reshape (Display *dpy, Window window, void *closure, 
                 unsigned int w, unsigned int h)
{
  struct state *st = (struct state *) closure;
  XWindowAttributes XWinAttribs;

  DestroyImage (st);
  st->fire = NULL;

  XGetWindowAttributes( st->dpy, st->window, &XWinAttribs );
  XWinAttribs.width = w;
  XWinAttribs.height = h;
  CreateImage (st, &XWinAttribs);
  st->draw_i = -1;
}

static Bool
eruption_event (Display *dpy, Window window, void *closure, XEvent *event)
{
  struct state *st = (struct state *) closure;

  if (event->type == ButtonPress)
    {
      new_eruption (st, event->xbutton.x, event->xbutton.y);
      return True;
    }
  else if (screenhack_event_helper (dpy, window, event))
    {
      random_eruption (st);
      return True;
    }

  return False;
}

static void
eruption_free (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
  DestroyImage (st);
  free (st->aiColorVals);
  free (st->particles);
  XFreeGC (dpy, st->gc);
  free (st);
}


static const char *eruption_defaults [] = {
  ".background: black",
  ".foreground: white",
  "*fpsTop:	true",
  "*cycles:   80",
  "*ncolors:  256",
  "*delay:    10000",
  "*particles: 300",
  "*cooloff: 2",
  "*gravity: 1",
  "*heat: 256",
  ".lowrez: true",  /* Too slow on Retina screens otherwise */
  "*useSHM: True",
  0
};

static XrmOptionDescRec eruption_options [] = {
  { "-ncolors", ".ncolors", XrmoptionSepArg, 0 },
  { "-delay",   ".delay",   XrmoptionSepArg, 0 },
  { "-cycles",  ".cycles",  XrmoptionSepArg, 0 },
  { "-particles",  ".particles",  XrmoptionSepArg, 0 },
  { "-cooloff",  ".cooloff",  XrmoptionSepArg, 0 },
  { "-gravity",  ".gravity",  XrmoptionSepArg, 0 },
  { "-heat",  ".heat",  XrmoptionSepArg, 0 },
#ifdef HAVE_XSHM_EXTENSION
  { "-shm", ".useSHM", XrmoptionNoArg, "True" },
  { "-no-shm",  ".useSHM", XrmoptionNoArg, "False" },
#endif
  { 0, 0, 0, 0 }
};


XSCREENSAVER_MODULE ("Eruption", eruption)

/* End of Module - "eruption.c" */