ftp://ftp.krokus.ru/pub/OpenBSD/distfiles/xscreensaver-5.01.tar.gz
[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) ((x) ? (int)(random() % (x)) : 0)
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   if (!st->xim) return;
165   pm = st->palaka1;
166   po = st->palaka2;
167   for(n=0;n<w*4;n++) 
168   {pm[n]=0; po[n]=0;}    /* clean first line */
169   pm+=n; po+=n; h-=2; 
170   pa = pm-(w*4);
171   pb = pm+(w*4);
172   for(n=4;(signed) n< (signed) (w*h*4-4); n++){
173   q    = pm[n-4] + (pm[n]*8) + pm[n+4] + 
174          pa[n-4] + pa[n] + pa[n+4] + 
175          pb[n-4] + pb[n] + pb[n+4];
176   q    -= q>8 ? 8:q;
177   pm[n] = q/16;
178   q     = q/8;
179   if(q>255) q=255;
180   po[n] = q;}
181   pm+=n; po+=n;
182   for(n=0;n<w*4;n++)
183   {pm[n]=0; po[n]=0;}}   /* clean last line */
184
185 static void blur(struct state *st)
186 {
187   unsigned int n,q;
188   unsigned int w = st->width;
189   unsigned int h = st->height;
190   unsigned char *pa, *pb, *pm;
191   pm = st->palaka1;
192   pm += w*4; h-=2;  /* line 0&h */
193   pa = pm-(w*4);
194   pb = pm+(w*4);
195   for(n=4;n<w*h*4-4;n++){
196   q    = pm[n-4] + (pm[n]*8) + pm[n+4] + 
197          pa[n-4] + pa[n] + pa[n+4] + 
198          pb[n-4] + pb[n] + pb[n+4];
199   q    -= q>8 ? 8:q;
200   pm[n] = q>>4;}
201   pm += n-4;    /* last line */
202   for(n=0;n<w*4+4;n++) pm[n]=0;
203   pm = st->palaka1; /* first line */
204   for(n=0;n<w*4+4;n++) pm[n]=pm[n+w*4]; }
205
206 static void light_2x2(struct state *st, fireshell *fss)
207 {
208   unsigned int l,t,n,x,y;
209   float s;
210   int w = st->width;
211   int h = st->height;
212   unsigned char *dim = st->palaka2;
213   unsigned char *sim = st->palaka1;
214   int nl = w*4;
215   fireshell *f;
216   if(st->glow_on) sim=dim;
217   for(y=0;y<h;y+=2){
218   for(x=0;x<w;x+=2){
219   f = fss; s = 0;
220   for(n=SHELLCOUNT;n;n--,f++){
221   s += f->flash/(sqrt(1+(f->cx - x)*(f->cx - x)+
222                         (f->cy - y)*(f->cy - y)));}
223   l = s;
224
225   t = l + sim[0];
226   dim[0] = (t > 255 ? 255 : t); 
227   t = l + sim[1];
228   dim[1] = (t > 255 ? 255 : t);
229   t = l + sim[2];
230   dim[2] = (t > 255 ? 255 : t);
231
232   t = l + sim[4];
233   dim[4] = (t > 255 ? 255 : t);
234   t = l + sim[5];
235   dim[5] = (t > 255 ? 255 : t);
236   t = l + sim[6];
237   dim[6] = (t > 255 ? 255 : t);
238
239   t = l + sim[nl+0];
240   dim[nl+0] = (t > 255 ? 255 : t);
241   t = l + sim[nl+1];
242   dim[nl+1] = (t > 255 ? 255 : t);
243   t = l + sim[nl+2];
244   dim[nl+2] = (t > 255 ? 255 : t);
245
246   t = l + sim[nl+4];
247   dim[nl+4] = (t > 255 ? 255 : t);
248   t = l + sim[nl+5];
249   dim[nl+5] = (t > 255 ? 255 : t);
250   t = l + sim[nl+6];
251   dim[nl+6] = (t > 255 ? 255 : t);
252
253   sim += 8; dim += 8; } sim += nl; dim += nl;}}
254
255 static void resize(struct state *st)
256 {
257   XWindowAttributes xwa;
258   XGetWindowAttributes (st->dpy, st->window, &xwa);
259   xwa.width  -= xwa.width % 2;
260   xwa.height -= xwa.height % 2;
261   if(xwa.height != st->height || xwa.width != st->width) {
262   st->width  = xwa.width;
263   st->height = xwa.height;
264   if (st->verbose)
265   printf("sky size: %dx%d ------------------------------\n",st->width,st->height);
266   if (st->xim) {
267   if (st->xim->data==(char *)st->palaka2) st->xim->data=NULL;  
268   XDestroyImage(st->xim);
269   if (st->palaka2!=st->palaka1) free(st->palaka2 - 8);
270   free(st->palaka1 - 8); 
271   }
272   st->palaka1 = NULL;
273   st->palaka2 = NULL;
274   st->xim = XCreateImage(st->dpy, xwa.visual, xwa.depth, ZPixmap, 0, 0,
275                      st->width, st->height, 8, 0);
276   if (!st->xim) return;
277   st->palaka1 = (unsigned char *) calloc(st->xim->height+1,st->xim->width*4) + 8;
278   if(st->flash_on|st->glow_on)
279   st->palaka2 = (unsigned char *) calloc(st->xim->height+1,st->xim->width*4) + 8;
280   else
281   st->palaka2 = st->palaka1;
282   if (st->depth>=24)
283   st->xim->data = (char *)st->palaka2;
284   else
285   st->xim->data = calloc(st->xim->height,st->xim->bytes_per_line); }}
286
287 static void put_image(struct state *st)
288 {
289   int x,y,i,j;
290   unsigned char r, g, b;
291   if (!st->xim) return;
292   i = 0;
293   j = 0;
294   if (st->depth==16) {
295      if(st->bigendian)
296      for (y=0;y<st->xim->height; y++)
297      for (x=0;x<st->xim->width; x++) {
298      r = st->palaka2[j++];
299      g = st->palaka2[j++];
300      b = st->palaka2[j++];
301      j++;
302      st->xim->data[i++] = (g&224)>>5 | (r&248);
303      st->xim->data[i++] = (b&248)>>3 | (g&28)<<3;
304      }
305      else
306      for (y=0;y<st->xim->height; y++)
307      for (x=0;x<st->xim->width; x++) {
308      r = st->palaka2[j++];
309      g = st->palaka2[j++];
310      b = st->palaka2[j++];
311      j++;
312      st->xim->data[i++] = (b&248)>>3 | (g&28)<<3;
313      st->xim->data[i++] = (g&224)>>5 | (r&248);
314      }
315   }
316   if (st->depth==15) {
317      if(st->bigendian)
318      for (y=0;y<st->xim->height; y++)
319      for (x=0;x<st->xim->width; x++) {
320      r = st->palaka2[j++];
321      g = st->palaka2[j++];
322      b = st->palaka2[j++];
323      j++;
324      st->xim->data[i++] = (g&192)>>6 | (r&248)>>1;
325      st->xim->data[i++] = (b&248)>>3 | (g&56)<<2;
326      }
327      else
328      for (y=0;y<st->xim->height; y++)
329      for (x=0;x<st->xim->width; x++) {
330      r = st->palaka2[j++];
331      g = st->palaka2[j++];
332      b = st->palaka2[j++];
333      j++;
334      st->xim->data[i++] = (b&248)>>3 | (g&56)<<2;
335      st->xim->data[i++] = (g&192)>>6 | (r&248)>>1;
336      }
337   }
338   if (st->depth==8) {
339      for (y=0;y<st->xim->height; y++)
340      for (x=0;x<st->xim->width; x++) {
341      r = st->palaka2[j++];
342      g = st->palaka2[j++];
343      b = st->palaka2[j++];
344      j++;     
345      st->xim->data[i++] = (((7*g)/256)*36)+(((6*r)/256)*6)+((6*b)/256);
346      }
347   }
348   XPutImage(st->dpy,st->window,st->gc,st->xim,0,0,0,0,st->xim->width,st->xim->height); }
349
350
351 static void *
352 fireworkx_init (Display *dpy, Window win)
353 {
354   struct state *st = (struct state *) calloc (1, sizeof(*st));
355   unsigned int n;
356   Visual *vi;
357   Colormap cmap;
358   Bool writable;
359   XWindowAttributes xwa;
360   XGCValues gcv;
361   firepix *fpix, *ffpix;
362
363   st->dpy = dpy;
364   st->window = win;
365
366   st->glow_on  = get_boolean_resource(st->dpy, "glow"    , "Boolean");
367   st->flash_on = get_boolean_resource(st->dpy, "flash"   , "Boolean");
368   st->shoot    = get_boolean_resource(st->dpy, "shoot"   , "Boolean");
369   st->verbose  = get_boolean_resource(st->dpy, "verbose" , "Boolean");
370   st->rndlife  = get_integer_resource(st->dpy, "maxlife" , "Integer");
371   st->delay    = get_integer_resource(st->dpy, "delay"   , "Integer");
372   st->minlife  = st->rndlife/4;
373   if(st->rndlife<1000) st->flash_fade=0.98;
374   if(st->rndlife<500) st->flash_fade=0.97;
375   if(st->verbose){
376   printf("Fireworkx %s - pyrotechnics simulation program \n", FWXVERSION);
377   printf("Copyright (c) 1999-2005 Rony B Chandran <ronybc@asia.com> \n\n");
378   printf("url: http://www.ronybc.8k.com \n\n");}
379
380   XGetWindowAttributes(st->dpy,win,&xwa);
381   st->depth     = xwa.depth;
382   vi        = xwa.visual;
383   cmap      = xwa.colormap;
384   st->bigendian = (ImageByteOrder(st->dpy) == MSBFirst);
385  
386   if(st->depth==8){
387   if(st->verbose){
388   printf("Pseudocolor color: use '-no-flash' for better results.\n");}
389   st->colors = (XColor *) calloc(sizeof(XColor),st->ncolors+1);
390   writable = False;
391   make_smooth_colormap(st->dpy, vi, cmap, st->colors, &st->ncolors,
392                                 False, &writable, True);
393   }
394   st->gc = XCreateGC(st->dpy, win, 0, &gcv);
395
396   resize(st);   /* initialize palakas */ 
397   
398   ffpix = malloc(sizeof(firepix) * PIXCOUNT * SHELLCOUNT);
399   st->ffshell = malloc(sizeof(fireshell) * SHELLCOUNT);
400   st->fshell = st->ffshell;
401   fpix = ffpix;
402   for (n=0;n<SHELLCOUNT;n++){
403   st->fshell->fpix = fpix;
404   recycle (st, st->fshell,rnd(st->width),rnd(st->height));
405   st->fshell++; 
406   fpix += PIXCOUNT; }
407   
408   return st;
409 }
410
411
412 static unsigned long
413 fireworkx_draw (Display *dpy, Window win, void *closure)
414 {
415   struct state *st = (struct state *) closure;
416   int q,n;
417   for(q=FTWEAK;q;q--){
418     st->fshell=st->ffshell;
419     for(n=SHELLCOUNT;n;n--){
420       if (!explode(st, st->fshell)){
421         recycle(st, st->fshell,rnd(st->width),rnd(st->height)); }
422       st->fshell++; }}
423 #if HAVE_X86_MMX
424   if(st->glow_on) mmx_glow(st->palaka1,st->width,st->height,8,st->palaka2);
425 #else
426   if(st->glow_on) glow(st);
427 #endif
428   if(st->flash_on) light_2x2(st, st->ffshell);
429   put_image(st);
430 #if HAVE_X86_MMX
431   if(!st->glow_on) mmx_blur(st->palaka1,st->width,st->height,8);
432 #else
433   if(!st->glow_on) blur(st);
434 #endif
435
436   return st->delay;
437 }
438
439
440 static void
441 fireworkx_reshape (Display *dpy, Window window, void *closure, 
442                  unsigned int w, unsigned int h)
443 {
444   struct state *st = (struct state *) closure;
445   resize(st);
446 }
447
448 static Bool
449 fireworkx_event (Display *dpy, Window window, void *closure, XEvent *event)
450 {
451   struct state *st = (struct state *) closure;
452   if (event->type == ButtonPress) {
453     recycle(st, st->fshell, event->xbutton.x, event->xbutton.y);
454     return True;
455   }
456   return False;
457 }
458
459 static void
460 fireworkx_free (Display *dpy, Window window, void *closure)
461 {
462 }
463
464
465 static const char *fireworkx_defaults [] = {
466   ".background: black",
467   ".foreground: white",
468   "*delay:      20000",  /* never default to zero! */
469   "*maxlife:    2000",
470   "*flash:      True",
471   "*glow:       True",
472   "*shoot:      False",
473   "*verbose:    False",
474   0
475 };
476
477 static XrmOptionDescRec fireworkx_options [] = {
478   { "-delay",           ".delay",       XrmoptionSepArg, 0 },
479   { "-maxlife",         ".maxlife",     XrmoptionSepArg, 0 },
480   { "-no-flash",        ".flash",       XrmoptionNoArg, "False" },
481   { "-no-glow",         ".glow",        XrmoptionNoArg, "False" },
482   { "-shoot",           ".shoot",       XrmoptionNoArg, "True" },
483   { "-verbose",         ".verbose",     XrmoptionNoArg, "True" },
484   { 0, 0, 0, 0 }
485 };
486
487 XSCREENSAVER_MODULE ("Fireworkx", fireworkx)