3271212b4648f17336c6778e1b4bbfe16ebdd408
[xscreensaver] / hacks / fireworkx.c
1 /*
2  * Fireworkx 1.5 - pyrotechnics simulation program
3  * Copyright (c) 1999-2005 Rony B Chandran <ronybc@asia.com>
4  *
5  * From Kerala, INDIA
6  * 
7  * url: http://www.ronybc.8k.com
8  *
9  * Permission to use, copy, modify, distribute, and sell this software and its
10  * documentation for any purpose is hereby granted without fee, provided that
11  * the above copyright notice appear in all copies and that both that
12  * copyright notice and this permission notice appear in supporting
13  * documentation.  No representations are made about the suitability of this
14  * software for any purpose.  It is provided "as is" without express or 
15  * implied warranty.
16  *
17  * Additional programming: 
18  * ------------------------
19  * Support for different dpy color modes: 
20  * Jean-Pierre Demailly <Jean-Pierre.Demailly@ujf-grenoble.fr>
21  *
22  * Fixed array access problems by beating on it with a large hammer.
23  * Nicholas Miell <nmiell@gmail.com>
24  *
25  * Freed 'free'ing up of memory by adding the forgotten 'XSync's
26  * Renuka S <renuka@local.net>
27  *
28  */
29
30 #include <math.h>
31 #include "screenhack.h"
32
33 #define FWXVERSION "1.5"
34
35 #define SHELLCOUNT 3                   /* 3 or 5  */
36 #define PIXCOUNT 500                   /* 500     */
37 #define POWER 5                        /* 5       */
38 #define FTWEAK 12                      /* 12      */
39
40 #if HAVE_X86_MMX
41 void mmx_blur(char *a, int b, int c, int d); 
42 void mmx_glow(char *a, int b, int c, int d, char *e);
43 #endif
44
45 #define rnd(x) ((int)(random() % (x)))
46
47 typedef struct {
48   unsigned int burn;
49   float x;
50   float y;
51   float xv;
52   float yv;}firepix;
53
54 typedef struct {
55   unsigned int cx,cy;
56   unsigned int life;
57   unsigned int color;
58   unsigned int special;
59   unsigned int cshift;
60   unsigned int vgn,shy;
61   float air,flash;
62   firepix *fpix; }fireshell;
63
64 struct state {
65   Display *dpy;
66   Window window;
67
68   fireshell *fshell, *ffshell;
69   GC gc;
70
71   int depth;
72   int bigendian;
73   Bool flash_on;
74   Bool glow_on;
75   Bool verbose;
76   Bool shoot;
77   int width;
78   int height;
79   int rndlife;
80   int minlife;
81   int delay;
82   float flash_fade;
83   unsigned char *palaka1;
84   unsigned char *palaka2;
85   XImage *xim;
86   XColor *colors;
87   int ncolors;
88 };
89
90 static int explode(struct state *st, fireshell *fs)
91 {
92   float air,adg = 0.001;         /* gravity */
93   unsigned int n,c;
94   unsigned int h = st->height;
95   unsigned int w = st->width;
96   unsigned int *prgb;
97   unsigned char *palaka = st->palaka1;
98   firepix *fp = fs->fpix;
99   if(fs->vgn){
100     if(--fs->cy == fs->shy){  
101       fs->vgn   = 0;
102       fs->flash = rnd(30000)+15000;}
103     else{  
104       fs->flash = 50+(fs->cy - fs->shy)*2;
105       prgb=(unsigned int *)(palaka + (fs->cy * w + fs->cx + rnd(5)-2)*4);
106      *prgb=(rnd(8)+8)*0x000f0f10;
107       return(1);}}    
108   if(fs->cshift) --fs->cshift;
109   if((fs->cshift+1)%50==0) fs->color = ~fs->color;
110   c = fs->color;
111   air = fs->air;
112   fs->flash *= st->flash_fade;
113   for(n=PIXCOUNT;n;n--){
114   if(fp->burn){ --fp->burn; 
115   if(fs->special){
116   fp->x += fp->xv = fp->xv * air + (float)(rnd(200)-100)/2000;
117   fp->y += fp->yv = fp->yv * air + (float)(rnd(200)-100)/2000 + adg; }
118   else{
119   fp->x += fp->xv = fp->xv * air + (float)(rnd(200)-100)/20000;
120   fp->y += fp->yv = fp->yv * air + adg; }
121   if(fp->y > h){
122   if(rnd(5)==3) {fp->yv *= -0.24; fp->y = h;}
123   else fp->burn=0;} /* touch muddy ground :) */
124   if(fp->x < w && fp->x > 0 && fp->y < h && fp->y > 0){
125      prgb = (unsigned int *)(palaka + ((int)fp->y * w + (int)fp->x)*4);
126     *prgb = c; }
127   } fp++;
128   } return(--fs->life); }
129
130 static void recycle(struct state *st, fireshell *fs,int x,int y)
131 {
132   unsigned int n,pixlife;
133   firepix *fp = fs->fpix;
134   fs->vgn = st->shoot;
135   fs->shy = y;
136   fs->cx = x;
137   fs->cy = st->shoot ? st->height : y ;
138   fs->color = (rnd(155)+100) <<16 |
139               (rnd(155)+100) <<8  |
140                rnd(255);
141   fs->life = rnd(st->rndlife)+st->minlife;
142   fs->air  = 1-(float)(rnd(200))/10000;
143   fs->flash   = rnd(30000)+15000;        /* million jouls */
144   fs->cshift  = !rnd(5) ? 120:0; 
145   fs->special = !rnd(10) ? 1:0; 
146   if(st->verbose)
147   printf("recycle(): color = %x air = %f life = %d \n",fs->color,fs->air,fs->life);
148   pixlife = rnd(fs->life)+fs->life/10+1;    /* ! */
149   for(n=0;n<PIXCOUNT;n++){
150   fp->burn = rnd(pixlife)+32;
151   fp->xv = POWER*(float)(rnd(20000)-10000)/10000;
152   fp->yv = sqrt(POWER*POWER - fp->xv * fp->xv) *
153                (float)(rnd(20000)-10000)/10000;
154   fp->x = x;
155   fp->y = y; 
156   fp++;             }}
157
158 static void glow(struct state *st)
159 {
160   unsigned int n,q;
161   unsigned int w = st->width;
162   unsigned int h = st->height;
163   unsigned char *pa, *pb, *pm, *po;
164   pm = st->palaka1;
165   po = st->palaka2;
166   for(n=0;n<w*4;n++) 
167   {pm[n]=0; po[n]=0;}    /* clean first line */
168   pm+=n; po+=n; h-=2; 
169   pa = pm-(w*4);
170   pb = pm+(w*4);
171   for(n=4;n<w*h*4-4;n++){
172   q    = pm[n-4] + (pm[n]*8) + pm[n+4] + 
173          pa[n-4] + pa[n] + pa[n+4] + 
174          pb[n-4] + pb[n] + pb[n+4];
175   q    -= q>8 ? 8:q;
176   pm[n] = q/16;
177   q     = q/8;
178   if(q>255) q=255;
179   po[n] = q;}
180   pm+=n; po+=n;
181   for(n=0;n<w*4;n++)
182   {pm[n]=0; po[n]=0;}}   /* clean last line */
183
184 static void blur(struct state *st)
185 {
186   unsigned int n,q;
187   unsigned int w = st->width;
188   unsigned int h = st->height;
189   unsigned char *pa, *pb, *pm;
190   pm = st->palaka1;
191   pm += w*4; h-=2;  /* line 0&h */
192   pa = pm-(w*4);
193   pb = pm+(w*4);
194   for(n=4;n<w*h*4-4;n++){
195   q    = pm[n-4] + (pm[n]*8) + pm[n+4] + 
196          pa[n-4] + pa[n] + pa[n+4] + 
197          pb[n-4] + pb[n] + pb[n+4];
198   q    -= q>8 ? 8:q;
199   pm[n] = q>>4;}
200   pm += n-4;    /* last line */
201   for(n=0;n<w*4+4;n++) pm[n]=0;
202   pm = st->palaka1; /* first line */
203   for(n=0;n<w*4+4;n++) pm[n]=pm[n+w*4]; }
204
205 static void light_2x2(struct state *st, fireshell *fss)
206 {
207   unsigned int l,t,n,x,y;
208   float s;
209   int w = st->width;
210   int h = st->height;
211   unsigned char *dim = st->palaka2;
212   unsigned char *sim = st->palaka1;
213   int nl = w*4;
214   fireshell *f;
215   if(st->glow_on) sim=dim;
216   for(y=0;y<h;y+=2){
217   for(x=0;x<w;x+=2){
218   f = fss; s = 0;
219   for(n=SHELLCOUNT;n;n--,f++){
220   s += f->flash/(sqrt(1+(f->cx - x)*(f->cx - x)+
221                         (f->cy - y)*(f->cy - y)));}
222   l = s;
223
224   t = l + sim[0];
225   dim[0] = (t > 255 ? 255 : t); 
226   t = l + sim[1];
227   dim[1] = (t > 255 ? 255 : t);
228   t = l + sim[2];
229   dim[2] = (t > 255 ? 255 : t);
230
231   t = l + sim[4];
232   dim[4] = (t > 255 ? 255 : t);
233   t = l + sim[5];
234   dim[5] = (t > 255 ? 255 : t);
235   t = l + sim[6];
236   dim[6] = (t > 255 ? 255 : t);
237
238   t = l + sim[nl+0];
239   dim[nl+0] = (t > 255 ? 255 : t);
240   t = l + sim[nl+1];
241   dim[nl+1] = (t > 255 ? 255 : t);
242   t = l + sim[nl+2];
243   dim[nl+2] = (t > 255 ? 255 : t);
244
245   t = l + sim[nl+4];
246   dim[nl+4] = (t > 255 ? 255 : t);
247   t = l + sim[nl+5];
248   dim[nl+5] = (t > 255 ? 255 : t);
249   t = l + sim[nl+6];
250   dim[nl+6] = (t > 255 ? 255 : t);
251
252   sim += 8; dim += 8; } sim += nl; dim += nl;}}
253
254 static void resize(struct state *st)
255 {
256   XWindowAttributes xwa;
257   XGetWindowAttributes (st->dpy, st->window, &xwa);
258   xwa.width  -= xwa.width % 2;
259   xwa.height -= xwa.height % 2;
260   if(xwa.height != st->height || xwa.width != st->width) {
261   st->width  = xwa.width;
262   st->height = xwa.height;
263   if (st->verbose)
264   printf("sky size: %dx%d ------------------------------\n",st->width,st->height);
265   if (st->xim) {
266   if (st->xim->data==(char *)st->palaka2) st->xim->data=NULL;  
267   XDestroyImage(st->xim);
268   if (st->palaka2!=st->palaka1) free(st->palaka2 - 8);
269   free(st->palaka1 - 8); 
270   }
271   st->palaka1 = NULL;
272   st->palaka2 = NULL;
273   st->xim = XCreateImage(st->dpy, xwa.visual, xwa.depth, ZPixmap, 0, 0,
274                      st->width, st->height, 8, 0);
275   st->palaka1 = (unsigned char *) calloc(st->xim->height+1,st->xim->width*4) + 8;
276   if(st->flash_on|st->glow_on)
277   st->palaka2 = (unsigned char *) calloc(st->xim->height+1,st->xim->width*4) + 8;
278   else
279   st->palaka2 = st->palaka1;
280   if (st->depth>=24)
281   st->xim->data = (char *)st->palaka2;
282   else
283   st->xim->data = calloc(st->xim->height,st->xim->bytes_per_line); }}
284
285 static void put_image(struct state *st)
286 {
287   int x,y,i,j;
288   unsigned char r, g, b;
289   i = 0;
290   j = 0;
291   if (st->depth==16) {
292      if(st->bigendian)
293      for (y=0;y<st->xim->height; y++)
294      for (x=0;x<st->xim->width; x++) {
295      r = st->palaka2[j++];
296      g = st->palaka2[j++];
297      b = st->palaka2[j++];
298      j++;
299      st->xim->data[i++] = (g&224)>>5 | (r&248);
300      st->xim->data[i++] = (b&248)>>3 | (g&28)<<3;
301      }
302      else
303      for (y=0;y<st->xim->height; y++)
304      for (x=0;x<st->xim->width; x++) {
305      r = st->palaka2[j++];
306      g = st->palaka2[j++];
307      b = st->palaka2[j++];
308      j++;
309      st->xim->data[i++] = (b&248)>>3 | (g&28)<<3;
310      st->xim->data[i++] = (g&224)>>5 | (r&248);
311      }
312   }
313   if (st->depth==15) {
314      if(st->bigendian)
315      for (y=0;y<st->xim->height; y++)
316      for (x=0;x<st->xim->width; x++) {
317      r = st->palaka2[j++];
318      g = st->palaka2[j++];
319      b = st->palaka2[j++];
320      j++;
321      st->xim->data[i++] = (g&192)>>6 | (r&248)>>1;
322      st->xim->data[i++] = (b&248)>>3 | (g&56)<<2;
323      }
324      else
325      for (y=0;y<st->xim->height; y++)
326      for (x=0;x<st->xim->width; x++) {
327      r = st->palaka2[j++];
328      g = st->palaka2[j++];
329      b = st->palaka2[j++];
330      j++;
331      st->xim->data[i++] = (b&248)>>3 | (g&56)<<2;
332      st->xim->data[i++] = (g&192)>>6 | (r&248)>>1;
333      }
334   }
335   if (st->depth==8) {
336      for (y=0;y<st->xim->height; y++)
337      for (x=0;x<st->xim->width; x++) {
338      r = st->palaka2[j++];
339      g = st->palaka2[j++];
340      b = st->palaka2[j++];
341      j++;     
342      st->xim->data[i++] = (((7*g)/256)*36)+(((6*r)/256)*6)+((6*b)/256);
343      }
344   }
345   XPutImage(st->dpy,st->window,st->gc,st->xim,0,0,0,0,st->xim->width,st->xim->height); }
346
347
348 static void *
349 fireworkx_init (Display *dpy, Window win)
350 {
351   struct state *st = (struct state *) calloc (1, sizeof(*st));
352   unsigned int n;
353   Visual *vi;
354   Colormap cmap;
355   Bool writable;
356   XWindowAttributes xwa;
357   XGCValues gcv;
358   firepix *fpix, *ffpix;
359
360   st->dpy = dpy;
361   st->window = win;
362
363   st->glow_on  = get_boolean_resource(st->dpy, "glow"    , "Boolean");
364   st->flash_on = get_boolean_resource(st->dpy, "flash"   , "Boolean");
365   st->shoot    = get_boolean_resource(st->dpy, "shoot"   , "Boolean");
366   st->verbose  = get_boolean_resource(st->dpy, "verbose" , "Boolean");
367   st->rndlife  = get_integer_resource(st->dpy, "maxlife" , "Integer");
368   st->delay    = get_integer_resource(st->dpy, "delay"   , "Integer");
369   st->minlife  = st->rndlife/4;
370   if(st->rndlife<1000) st->flash_fade=0.98;
371   if(st->rndlife<500) st->flash_fade=0.97;
372   if(st->verbose){
373   printf("Fireworkx %s - pyrotechnics simulation program \n", FWXVERSION);
374   printf("Copyright (c) 1999-2005 Rony B Chandran <ronybc@asia.com> \n\n");
375   printf("url: http://www.ronybc.8k.com \n\n");}
376
377   XGetWindowAttributes(st->dpy,win,&xwa);
378   st->depth     = xwa.depth;
379   vi        = xwa.visual;
380   cmap      = xwa.colormap;
381   st->bigendian = (ImageByteOrder(st->dpy) == MSBFirst);
382  
383   if(st->depth==8){
384   if(st->verbose){
385   printf("Pseudocolor color: use '-no-flash' for better results.\n");}
386   st->colors = (XColor *) calloc(sizeof(XColor),st->ncolors+1);
387   writable = False;
388   make_smooth_colormap(st->dpy, vi, cmap, st->colors, &st->ncolors,
389                                 False, &writable, True);
390   }
391   st->gc = XCreateGC(st->dpy, win, 0, &gcv);
392
393   resize(st);   /* initialize palakas */ 
394   
395   ffpix = malloc(sizeof(firepix) * PIXCOUNT * SHELLCOUNT);
396   st->ffshell = malloc(sizeof(fireshell) * SHELLCOUNT);
397   st->fshell = st->ffshell;
398   fpix = ffpix;
399   for (n=0;n<SHELLCOUNT;n++){
400   st->fshell->fpix = fpix;
401   recycle (st, st->fshell,rnd(st->width),rnd(st->height));
402   st->fshell++; 
403   fpix += PIXCOUNT; }
404   
405   return st;
406 }
407
408
409 static unsigned long
410 fireworkx_draw (Display *dpy, Window win, void *closure)
411 {
412   struct state *st = (struct state *) closure;
413   int q,n;
414   for(q=FTWEAK;q;q--){
415     st->fshell=st->ffshell;
416     for(n=SHELLCOUNT;n;n--){
417       if (!explode(st, st->fshell)){
418         recycle(st, st->fshell,rnd(st->width),rnd(st->height)); }
419       st->fshell++; }}
420 #if HAVE_X86_MMX
421   if(st->glow_on) mmx_glow(st->palaka1,st->width,st->height,8,st->palaka2);
422 #else
423   if(st->glow_on) glow(st);
424 #endif
425   if(st->flash_on) light_2x2(st, st->ffshell);
426   put_image(st);
427 #if HAVE_X86_MMX
428   if(!st->glow_on) mmx_blur(st->palaka1,st->width,st->height,8);
429 #else
430   if(!st->glow_on) blur(st);
431 #endif
432
433   return st->delay;
434 }
435
436
437 static void
438 fireworkx_reshape (Display *dpy, Window window, void *closure, 
439                  unsigned int w, unsigned int h)
440 {
441   struct state *st = (struct state *) closure;
442   resize(st);
443 }
444
445 static Bool
446 fireworkx_event (Display *dpy, Window window, void *closure, XEvent *event)
447 {
448   struct state *st = (struct state *) closure;
449   if (event->type == ButtonPress) {
450     recycle(st, st->fshell, event->xbutton.x, event->xbutton.y);
451     return True;
452   }
453   return False;
454 }
455
456 static void
457 fireworkx_free (Display *dpy, Window window, void *closure)
458 {
459 }
460
461
462 static const char *fireworkx_defaults [] = {
463   ".background: black",
464   ".foreground: white",
465   "*delay:      20000",  /* never default to zero! */
466   "*maxlife:    2000",
467   "*flash:      True",
468   "*glow:       True",
469   "*shoot:      False",
470   "*verbose:    False",
471   0
472 };
473
474 static XrmOptionDescRec fireworkx_options [] = {
475   { "-delay",           ".delay",       XrmoptionSepArg, 0 },
476   { "-maxlife",         ".maxlife",     XrmoptionSepArg, 0 },
477   { "-no-flash",        ".flash",       XrmoptionNoArg, "False" },
478   { "-no-glow",         ".glow",        XrmoptionNoArg, "False" },
479   { "-shoot",           ".shoot",       XrmoptionNoArg, "True" },
480   { "-verbose",         ".verbose",     XrmoptionNoArg, "True" },
481   { 0, 0, 0, 0 }
482 };
483
484 XSCREENSAVER_MODULE ("Fireworkx", fireworkx)