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) ((int)(random() % (x)))
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;
167 {pm[n]=0; po[n]=0;} /* clean first line */
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];
182 {pm[n]=0; po[n]=0;}} /* clean last line */
184 static void blur(struct state *st)
187 unsigned int w = st->width;
188 unsigned int h = st->height;
189 unsigned char *pa, *pb, *pm;
191 pm += w*4; h-=2; /* line 0&h */
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];
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]; }
205 static void light_2x2(struct state *st, fireshell *fss)
207 unsigned int l,t,n,x,y;
211 unsigned char *dim = st->palaka2;
212 unsigned char *sim = st->palaka1;
215 if(st->glow_on) sim=dim;
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)));}
225 dim[0] = (t > 255 ? 255 : t);
227 dim[1] = (t > 255 ? 255 : t);
229 dim[2] = (t > 255 ? 255 : t);
232 dim[4] = (t > 255 ? 255 : t);
234 dim[5] = (t > 255 ? 255 : t);
236 dim[6] = (t > 255 ? 255 : t);
239 dim[nl+0] = (t > 255 ? 255 : t);
241 dim[nl+1] = (t > 255 ? 255 : t);
243 dim[nl+2] = (t > 255 ? 255 : t);
246 dim[nl+4] = (t > 255 ? 255 : t);
248 dim[nl+5] = (t > 255 ? 255 : t);
250 dim[nl+6] = (t > 255 ? 255 : t);
252 sim += 8; dim += 8; } sim += nl; dim += nl;}}
254 static void resize(struct state *st)
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;
264 printf("sky size: %dx%d ------------------------------\n",st->width,st->height);
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);
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;
279 st->palaka2 = st->palaka1;
281 st->xim->data = (char *)st->palaka2;
283 st->xim->data = calloc(st->xim->height,st->xim->bytes_per_line); }}
285 static void put_image(struct state *st)
288 unsigned char r, g, b;
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++];
299 st->xim->data[i++] = (g&224)>>5 | (r&248);
300 st->xim->data[i++] = (b&248)>>3 | (g&28)<<3;
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++];
309 st->xim->data[i++] = (b&248)>>3 | (g&28)<<3;
310 st->xim->data[i++] = (g&224)>>5 | (r&248);
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++];
321 st->xim->data[i++] = (g&192)>>6 | (r&248)>>1;
322 st->xim->data[i++] = (b&248)>>3 | (g&56)<<2;
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++];
331 st->xim->data[i++] = (b&248)>>3 | (g&56)<<2;
332 st->xim->data[i++] = (g&192)>>6 | (r&248)>>1;
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++];
342 st->xim->data[i++] = (((7*g)/256)*36)+(((6*r)/256)*6)+((6*b)/256);
345 XPutImage(st->dpy,st->window,st->gc,st->xim,0,0,0,0,st->xim->width,st->xim->height); }
349 fireworkx_init (Display *dpy, Window win)
351 struct state *st = (struct state *) calloc (1, sizeof(*st));
356 XWindowAttributes xwa;
358 firepix *fpix, *ffpix;
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;
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");}
377 XGetWindowAttributes(st->dpy,win,&xwa);
378 st->depth = xwa.depth;
381 st->bigendian = (ImageByteOrder(st->dpy) == MSBFirst);
385 printf("Pseudocolor color: use '-no-flash' for better results.\n");}
386 st->colors = (XColor *) calloc(sizeof(XColor),st->ncolors+1);
388 make_smooth_colormap(st->dpy, vi, cmap, st->colors, &st->ncolors,
389 False, &writable, True);
391 st->gc = XCreateGC(st->dpy, win, 0, &gcv);
393 resize(st); /* initialize palakas */
395 ffpix = malloc(sizeof(firepix) * PIXCOUNT * SHELLCOUNT);
396 st->ffshell = malloc(sizeof(fireshell) * SHELLCOUNT);
397 st->fshell = st->ffshell;
399 for (n=0;n<SHELLCOUNT;n++){
400 st->fshell->fpix = fpix;
401 recycle (st, st->fshell,rnd(st->width),rnd(st->height));
410 fireworkx_draw (Display *dpy, Window win, void *closure)
412 struct state *st = (struct state *) closure;
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)); }
421 if(st->glow_on) mmx_glow(st->palaka1,st->width,st->height,8,st->palaka2);
423 if(st->glow_on) glow(st);
425 if(st->flash_on) light_2x2(st, st->ffshell);
428 if(!st->glow_on) mmx_blur(st->palaka1,st->width,st->height,8);
430 if(!st->glow_on) blur(st);
438 fireworkx_reshape (Display *dpy, Window window, void *closure,
439 unsigned int w, unsigned int h)
441 struct state *st = (struct state *) closure;
446 fireworkx_event (Display *dpy, Window window, void *closure, XEvent *event)
448 struct state *st = (struct state *) closure;
449 if (event->type == ButtonPress) {
450 recycle(st, st->fshell, event->xbutton.x, event->xbutton.y);
457 fireworkx_free (Display *dpy, Window window, void *closure)
462 static const char *fireworkx_defaults [] = {
463 ".background: black",
464 ".foreground: white",
465 "*delay: 20000", /* never default to zero! */
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" },
484 XSCREENSAVER_MODULE ("Fireworkx", fireworkx)