2 * Fireworkx 1.3 - pyrotechnics simulation program
3 * Copyright (c) 1999-2004 Rony B Chandran <ronybc@asia.com>
5 * url: http://www.ronybc.8k.com
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation. No representations are made about the suitability of this
12 * software for any purpose. It is provided "as is" without express or
15 * Additional programming:
16 * ------------------------
17 * Support for different display color modes:
18 * Jean-Pierre Demailly <Jean-Pierre.Demailly@ujf-grenoble.fr>
23 #include "screenhack.h"
24 #include <X11/Xutil.h>
26 #define FWXVERSION "1.3"
31 #define PIXCOUNT 500 /* 500 */
32 #define RNDLIFE0 250 /* violent */
33 #define RNDLIFE1 1200 /* 1200 */
34 #define MINLIFE0 50 /* violent */
35 #define MINLIFE1 500 /* 500 */
36 #define POWER 5 /* 5 */
37 #define FTWEAK 12 /* 12 */
41 static Bool light_on = True;
42 static Bool fade_on = False;
43 static Bool shoot = False;
44 static Bool verbose = False;
46 static int fsc_width = 0;
47 static int fsc_height= 0;
48 static int rndlife = RNDLIFE1;
49 static int minlife = MINLIFE1;
50 static float light_fade = 0.99;
51 static unsigned char *palaka1=NULL;
52 static unsigned char *palaka2=NULL;
53 static XImage *xim=NULL;
54 static XColor *colors;
55 static int ncolors = 255;
72 firepix *fpix; }fireshell;
77 if ((seed = seed % 44488 * 48271 - seed / 44488 * 3399) < 0)
82 int explode(fireshell *fs)
84 float air,adg = 0.001; /* gravity */
86 unsigned int h = fsc_height;
87 unsigned int w = fsc_width;
89 unsigned char *palaka = palaka1;
90 firepix *fp = fs->fpix;
92 if(--fs->cy == fs->shy){
94 fs->lum = 20000;} /* millions of jouls..! */
96 fs->lum = 50+(fs->cy - fs->shy)*2;
98 if(fs->cshift) --fs->cshift;
99 if((fs->cshift+1)%50==0) fs->color = ~fs->color;
102 fs->lum *= light_fade;
103 for(n=PIXCOUNT;n;n--){
104 if(fp->burn){ --fp->burn;
106 fp->x += fp->xv = fp->xv * air + (float)(rnd(200)-100)/2000;
107 fp->y += fp->yv = fp->yv * air + (float)(rnd(200)-100)/2000 +adg; }
109 fp->x += fp->xv = fp->xv * air + (float)(rnd(200)-100)/20000;
110 fp->y += fp->yv = fp->yv * air + adg; }
112 if(rnd(5)==3) {fp->yv *= -0.24; fp->y = h;}
113 else fp->burn=0;} /* touch muddy ground :) */
114 if(fp->x < w && fp->x > 0 && fp->y < h && fp->y > 0){
115 prgb = (unsigned int *)(palaka + ((int)fp->y * w + (int)fp->x)*4);
118 } return(--(fs->life));
121 void recycle(fireshell *fs,int x,int y)
123 unsigned int n,pixlife;
124 firepix *fp = fs->fpix;
128 fs->cy = shoot ? fsc_height : y ;
129 fs->color = (rnd(155)+100) <<16 |
132 fs->life = rnd(rndlife)+minlife;
133 fs->air = 1-(float)(rnd(200))/10000;
135 fs->cshift = !rnd(5) ? 120:0;
136 fs->special = !rnd(10) ? 1:0;
137 pixlife = rnd(fs->life)+fs->life/10+1; /* ! */
138 for(n=0;n<PIXCOUNT;n++){
139 fp->burn = rnd(pixlife)+32;
140 fp->xv = POWER*(float)(rnd(20000)-10000)/10000;
141 fp->yv = sqrt(POWER*POWER - fp->xv * fp->xv) *
142 (float)(rnd(20000)-10000)/10000;
151 unsigned int w = fsc_width;
152 unsigned int h = fsc_height;
153 unsigned char *pa, *pb, *pm;
155 for(n=0;n<w*4;n++) pm[n]=0; /* clean first line */
161 for(n=0;n<w*h*4;n++){
162 pm[n]=(pm[n-4] + pm[n] + pm[n+4] +
163 pa[n-4] + pa[n] + pa[n+4] +
164 pb[n-4] + pb[n] + pb[n+4] +
165 pm[n] + pm[n] + (pm[n]<<2))/ 16;}}
167 for(n=0;n<w*h*4;n++){
168 pm[n]=(pm[n-4] + pm[n] + pm[n+4] +
169 pa[n-4] + pa[n] + pa[n+4] +
170 pb[n-4] + pb[n] + pb[n+4])/ 9;}}
172 for(n=0;n<w*4;n++) pm[n]=0; /* clean last line */
175 void light_2x2(fireshell *fss)
177 unsigned int l,t,n,x,y;
181 unsigned char *dim = palaka2;
182 unsigned char *sim = palaka1;
188 for(n=SHELLCOUNT;n;n--,f++){
189 s += f->lum / ( 1 + sqrt((f->cx - x)*(f->cx - x)+(f->cy - y)*(f->cy - y))); }
193 dim[0] = (t > 255 ? 255 : t); /* cmov's */
195 dim[1] = (t > 255 ? 255 : t);
197 dim[2] = (t > 255 ? 255 : t);
200 dim[4] = (t > 255 ? 255 : t);
202 dim[5] = (t > 255 ? 255 : t);
204 dim[6] = (t > 255 ? 255 : t);
207 dim[nl+0] = (t > 255 ? 255 : t);
209 dim[nl+1] = (t > 255 ? 255 : t);
211 dim[nl+2] = (t > 255 ? 255 : t);
214 dim[nl+4] = (t > 255 ? 255 : t);
216 dim[nl+5] = (t > 255 ? 255 : t);
218 dim[nl+6] = (t > 255 ? 255 : t);
220 sim += 8; dim += 8; } sim += nl; dim += nl;}
223 void resize(Display *display, Window win)
225 XWindowAttributes xwa;
226 XGetWindowAttributes (display, win, &xwa);
227 xwa.width -= xwa.width % 4;
228 xwa.height -= xwa.height % 4;
229 if(xwa.height != fsc_height || xwa.width != fsc_width) {
230 fsc_width = xwa.width;
231 fsc_height = xwa.height;
233 if (xim->data==(char *)palaka2) xim->data=NULL;
235 if (palaka2!=palaka1) free(palaka2);
240 xim = XCreateImage(display, xwa.visual, xwa.depth, ZPixmap, 0, 0,
241 fsc_width, fsc_height, 32, 0);
242 palaka1 = calloc(xim->height,xim->width*4);
244 palaka2 = calloc(xim->height,xim->width*4);
248 xim->data = (char *)palaka2;
250 xim->data = calloc(xim->height,xim->bytes_per_line);
254 void put_image(Display *display, Window win, GC gc, XImage *xim)
257 unsigned char r, g, b;
262 for (y=0;y<xim->height; y++)
263 for (x=0;x<xim->width; x++) {
268 xim->data[i++] = (g&224)>>5 | (r&248);
269 xim->data[i++] = (b&248)>>3 | (g&28)<<3;
272 for (y=0;y<xim->height; y++)
273 for (x=0;x<xim->width; x++) {
278 xim->data[i++] = (b&248)>>3 | (g&28)<<3;
279 xim->data[i++] = (g&224)>>5 | (r&248);
284 for (y=0;y<xim->height; y++)
285 for (x=0;x<xim->width; x++) {
290 xim->data[i++] = (g&192)>>6 | (r&248)>>1;
291 xim->data[i++] = (b&248)>>3 | (g&56)<<2;
294 for (y=0;y<xim->height; y++)
295 for (x=0;x<xim->width; x++) {
300 xim->data[i++] = (b&248)>>3 | (g&56)<<2;
301 xim->data[i++] = (g&192)>>6 | (r&248)>>1;
305 for (y=0;y<xim->height; y++)
306 for (x=0;x<xim->width; x++) {
311 xim->data[i++] = (((7*g)/256)*36)+(((6*r)/256)*6)+((6*b)/256);
314 XPutImage(display,win,gc,xim,0,0,0,0,xim->width,xim->height);
317 void sniff_events(Display *dis, Window win, fireshell *fss)
320 while (XPending(dis)){
321 XNextEvent (dis, &e);
322 if (e.type == ConfigureNotify) resize(dis,win);
323 if (e.type == ButtonPress) recycle(fss,e.xbutton.x, e.xbutton.y);
324 screenhack_handle_event(dis,&e);}
328 char *progclass = "Fireworkx";
330 char *defaults [] = {
331 ".background: black",
332 ".foreground: white",
342 XrmOptionDescRec options [] = {
343 { "-delay", ".delay", XrmoptionSepArg, 0 },
344 { "-maxlife", ".maxlife", XrmoptionSepArg, 0 },
345 { "-nolight", ".light", XrmoptionNoArg, "False" },
346 { "-fade", ".fade", XrmoptionNoArg, "True" },
347 { "-shoot", ".shoot", XrmoptionNoArg, "True" },
348 { "-verbose", ".verbose", XrmoptionNoArg, "True" },
353 screenhack (Display *display, Window win)
359 XWindowAttributes xwa;
362 firepix *fpixs, *fpix;
363 fireshell *fshells, *fshell;
364 fade_on = get_boolean_resource("fade" , "Boolean");
365 light_on = get_boolean_resource("light" , "Boolean");
366 shoot = get_boolean_resource("shoot" , "Boolean");
367 verbose = get_boolean_resource("verbose" , "Boolean");
368 rndlife = get_integer_resource("maxlife" , "Integer");
369 delay = get_integer_resource("delay" , "Integer");
371 if(rndlife<1000) light_fade=0.98;
372 if(rndlife<500) light_fade=0.97;
373 if(fade_on) light_fade=0.97;
375 printf("Fireworkx %s - pyrotechnics simulation program \n", FWXVERSION);
376 printf("Copyright (c) 1999-2004 Rony B Chandran <ronybc@asia.com> \n\n");
377 printf("url: http://www.ronybc.8k.com \n\n");}
379 XGetWindowAttributes(display,win,&xwa);
383 bigendian = (ImageByteOrder(display) == MSBFirst);
387 printf("Pseudocolor color: use '-fade' & '-nolight' for better results.\n");}
388 colors = (XColor *) calloc(sizeof(XColor),ncolors+1);
390 make_smooth_colormap(display, vi, cmap, colors, &ncolors,
391 False, &writable, True);
393 gc = XCreateGC(display, win, 0, &gcv);
395 resize(display,win); /* initialize palakas */
398 fpixs = malloc(sizeof(firepix) * PIXCOUNT * SHELLCOUNT);
399 fshells = malloc(sizeof(fireshell) * SHELLCOUNT);
402 for (n=0;n<SHELLCOUNT;n++){
404 recycle (fshell,rnd(fsc_width),rnd(fsc_height));
411 for(n=SHELLCOUNT;n;n--){
412 if (!explode(fshell)){
413 recycle(fshell,rnd(fsc_width),rnd(fsc_height)); }
415 if(light_on) light_2x2(fshells);
416 put_image(display,win,gc,xim);
419 sniff_events(display, win, fshells);