2 * Fireworkx 1.5 - pyrotechnics simulation program
3 * Copyright (c) 1999-2005 Rony B Chandran <ronybc@asia.com>
7 * url: http://www.ronybc.8k.com
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
17 * Additional programming:
18 * ------------------------
19 * Support for different dpy color modes:
20 * Jean-Pierre Demailly <Jean-Pierre.Demailly@ujf-grenoble.fr>
22 * Fixed array access problems by beating on it with a large hammer.
23 * Nicholas Miell <nmiell@gmail.com>
25 * Freed 'free'ing up of memory by adding the forgotten 'XSync's
26 * Renuka S <renuka@local.net>
31 #include "screenhack.h"
33 #define FWXVERSION "1.5"
35 #define SHELLCOUNT 3 /* 3 or 5 */
36 #define PIXCOUNT 500 /* 500 */
37 #define POWER 5 /* 5 */
38 #define FTWEAK 12 /* 12 */
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);
45 #define rnd(x) ((x) ? (int)(random() % (x)) : 0)
62 firepix *fpix; }fireshell;
68 fireshell *fshell, *ffshell;
83 unsigned char *palaka1;
84 unsigned char *palaka2;
90 static int explode(struct state *st, fireshell *fs)
92 float air,adg = 0.001; /* gravity */
94 unsigned int h = st->height;
95 unsigned int w = st->width;
97 unsigned char *palaka = st->palaka1;
98 firepix *fp = fs->fpix;
100 if(--fs->cy == fs->shy){
102 fs->flash = rnd(30000)+15000;}
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;
108 if(fs->cshift) --fs->cshift;
109 if((fs->cshift+1)%50==0) fs->color = ~fs->color;
112 fs->flash *= st->flash_fade;
113 for(n=PIXCOUNT;n;n--){
114 if(fp->burn){ --fp->burn;
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; }
119 fp->x += fp->xv = fp->xv * air + (float)(rnd(200)-100)/20000;
120 fp->y += fp->yv = fp->yv * air + adg; }
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);
128 } return(--fs->life); }
130 static void recycle(struct state *st, fireshell *fs,int x,int y)
132 unsigned int n,pixlife;
133 firepix *fp = fs->fpix;
137 fs->cy = st->shoot ? st->height : y ;
138 fs->color = (rnd(155)+100) <<16 |
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;
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;
158 static void glow(struct state *st)
161 unsigned int w = st->width;
162 unsigned int h = st->height;
163 unsigned char *pa, *pb, *pm, *po;
164 if (!st->xim) return;
168 {pm[n]=0; po[n]=0;} /* clean first line */
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];
183 {pm[n]=0; po[n]=0;}} /* clean last line */
185 static void blur(struct state *st)
188 unsigned int w = st->width;
189 unsigned int h = st->height;
190 unsigned char *pa, *pb, *pm;
192 pm += w*4; h-=2; /* line 0&h */
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];
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]; }
206 static void light_2x2(struct state *st, fireshell *fss)
208 unsigned int l,t,n,x,y;
212 unsigned char *dim = st->palaka2;
213 unsigned char *sim = st->palaka1;
216 if(st->glow_on) sim=dim;
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)));}
226 dim[0] = (t > 255 ? 255 : t);
228 dim[1] = (t > 255 ? 255 : t);
230 dim[2] = (t > 255 ? 255 : t);
233 dim[4] = (t > 255 ? 255 : t);
235 dim[5] = (t > 255 ? 255 : t);
237 dim[6] = (t > 255 ? 255 : t);
240 dim[nl+0] = (t > 255 ? 255 : t);
242 dim[nl+1] = (t > 255 ? 255 : t);
244 dim[nl+2] = (t > 255 ? 255 : t);
247 dim[nl+4] = (t > 255 ? 255 : t);
249 dim[nl+5] = (t > 255 ? 255 : t);
251 dim[nl+6] = (t > 255 ? 255 : t);
253 sim += 8; dim += 8; } sim += nl; dim += nl;}}
255 static void resize(struct state *st)
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;
265 printf("sky size: %dx%d ------------------------------\n",st->width,st->height);
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);
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;
281 st->palaka2 = st->palaka1;
283 st->xim->data = (char *)st->palaka2;
285 st->xim->data = calloc(st->xim->height,st->xim->bytes_per_line); }}
287 static void put_image(struct state *st)
290 unsigned char r, g, b;
291 if (!st->xim) return;
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++];
302 st->xim->data[i++] = (g&224)>>5 | (r&248);
303 st->xim->data[i++] = (b&248)>>3 | (g&28)<<3;
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++];
312 st->xim->data[i++] = (b&248)>>3 | (g&28)<<3;
313 st->xim->data[i++] = (g&224)>>5 | (r&248);
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++];
324 st->xim->data[i++] = (g&192)>>6 | (r&248)>>1;
325 st->xim->data[i++] = (b&248)>>3 | (g&56)<<2;
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++];
334 st->xim->data[i++] = (b&248)>>3 | (g&56)<<2;
335 st->xim->data[i++] = (g&192)>>6 | (r&248)>>1;
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++];
345 st->xim->data[i++] = (((7*g)/256)*36)+(((6*r)/256)*6)+((6*b)/256);
348 XPutImage(st->dpy,st->window,st->gc,st->xim,0,0,0,0,st->xim->width,st->xim->height); }
352 fireworkx_init (Display *dpy, Window win)
354 struct state *st = (struct state *) calloc (1, sizeof(*st));
359 XWindowAttributes xwa;
361 firepix *fpix, *ffpix;
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;
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");}
380 XGetWindowAttributes(st->dpy,win,&xwa);
381 st->depth = xwa.depth;
384 st->bigendian = (ImageByteOrder(st->dpy) == MSBFirst);
388 printf("Pseudocolor color: use '-no-flash' for better results.\n");}
389 st->colors = (XColor *) calloc(sizeof(XColor),st->ncolors+1);
391 make_smooth_colormap(st->dpy, vi, cmap, st->colors, &st->ncolors,
392 False, &writable, True);
394 st->gc = XCreateGC(st->dpy, win, 0, &gcv);
396 resize(st); /* initialize palakas */
398 ffpix = malloc(sizeof(firepix) * PIXCOUNT * SHELLCOUNT);
399 st->ffshell = malloc(sizeof(fireshell) * SHELLCOUNT);
400 st->fshell = st->ffshell;
402 for (n=0;n<SHELLCOUNT;n++){
403 st->fshell->fpix = fpix;
404 recycle (st, st->fshell,rnd(st->width),rnd(st->height));
413 fireworkx_draw (Display *dpy, Window win, void *closure)
415 struct state *st = (struct state *) closure;
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)); }
424 if(st->glow_on) mmx_glow(st->palaka1,st->width,st->height,8,st->palaka2);
426 if(st->glow_on) glow(st);
428 if(st->flash_on) light_2x2(st, st->ffshell);
431 if(!st->glow_on) mmx_blur(st->palaka1,st->width,st->height,8);
433 if(!st->glow_on) blur(st);
441 fireworkx_reshape (Display *dpy, Window window, void *closure,
442 unsigned int w, unsigned int h)
444 struct state *st = (struct state *) closure;
449 fireworkx_event (Display *dpy, Window window, void *closure, XEvent *event)
451 struct state *st = (struct state *) closure;
452 if (event->type == ButtonPress) {
453 recycle(st, st->fshell, event->xbutton.x, event->xbutton.y);
460 fireworkx_free (Display *dpy, Window window, void *closure)
465 static const char *fireworkx_defaults [] = {
466 ".background: black",
467 ".foreground: white",
468 "*delay: 20000", /* never default to zero! */
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" },
487 XSCREENSAVER_MODULE ("Fireworkx", fireworkx)