38 SDL_Surface *thescreen;
39 unsigned char *vmem1, *vmem2;
41 SDL_Color themap[256];
45 if(SDL_MUSTLOCK(thescreen))
47 if ( SDL_LockSurface(thescreen) < 0 )
49 fprintf(stderr,
"Couldn't lock display surface: %s\n",
58 if(SDL_MUSTLOCK(thescreen))
59 SDL_UnlockSurface(thescreen);
60 SDL_UpdateRect(thescreen, 0, 0, 0, 0);
71 #define ABS(x) ((x)<0 ? -(x) : (x)) 75 char sizes[]={2,3,4,5,8,5,4,3};
79 struct blob *blobnext;
86 } *blobs,*freeblobs,*activeblobs;
89 unsigned char **mul640;
93 0,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,
94 4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,
95 5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,
96 6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
97 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
98 8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
99 9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,
100 10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,
101 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
102 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
103 12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,
104 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
105 13,13,13,13,14,14,14,14,14,14,14,14,14,14,14,14,
106 14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
107 14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
108 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15
114 printf(
"Not enough low memory!\n");
121 void fire(
unsigned char *p1,
unsigned char *p2,
int pitch,
char *map)
124 unsigned char *p3, *p4;
132 *p4=map[*p3+p3[-XSIZE]+p3[-XSIZE-1]+p3[-XSIZE+1]+p3[-1]+p3[1]+p3[-XSIZE-XSIZE-1]+p3[-XSIZE-XSIZE]+p3[-XSIZE-XSIZE+1]];
145 for(j=-rad;j<=rad;j++)
154 while(k--) {*p++=255-(ABS(i)<<2)-aj;i++;}
160 if(mousex>10 && mousex<XSIZE-10 && mousey>10 && mousey<YSIZE-10)
161 disk(mousex,mousey,8);
169 if(!freeblobs)
return;
173 freeblobs=freeblobs->blobnext;
174 ablob->bloblife=(rand()&511)+256;
177 ablob->blobx=(256+(rand()&127))<<BLOBFRAC;
178 ablob->bloby=2<<BLOBFRAC;
179 ablob->blobnext=activeblobs;
180 ablob->blobsize=BIGSIZE;
185 struct blob **lastblob,*ablob;
188 lastblob=&activeblobs;
189 while(ablob=*lastblob)
191 x=ablob->blobx>>BLOBFRAC;
192 y=ablob->bloby>>BLOBFRAC;
193 if(!--ablob->bloblife || y<0 || x<10 || x>XSIZE-10)
195 *lastblob=ablob->blobnext;
196 ablob->blobnext=freeblobs;
200 ablob->blobx+=ablob->blobdx;
201 ablob->bloby+=ablob->blobdy;
202 ablob->blobdy-=BLOBGRAVITY;
203 lastblob=&ablob->blobnext;
208 struct blob *ablob,*ablob2,*temp;
218 if(ablob->blobsize!=SMALLSIZE && (dy>-THRESHOLD && dy<THRESHOLD && !(rand()&7) || (rand()&127)==63))
221 while(i-- && freeblobs)
224 freeblobs=freeblobs->blobnext;
225 ablob2->blobx=ablob->blobx;
226 ablob2->bloby=ablob->bloby;
232 if(vel>0x3000 && vel<0x10000L)
break;
234 ablob2->blobdx=ablob->blobdx+x2;
235 ablob2->blobdy=ablob->blobdy+y2;
236 ablob2->bloblife=16+(rand()&31);
237 ablob2->blobsize=SMALLSIZE;
238 ablob2->blobnext=activeblobs;
243 x=ablob->blobx>>BLOBFRAC;
244 y=ablob->bloby>>BLOBFRAC;
245 size=ablob->blobsize;
246 if(size==BIGSIZE && ablob->blobdy>0 && ablob->blobdy<200)
247 size=sizes[ablob->bloblife&7];
248 if(x>10 && x<XSIZE-10 && y>10 && y<YSIZE-10)
249 disk(x,YSIZE-1-y,size);
251 ablob=ablob->blobnext;
252 temp->blobnext=activeblobs;
260 void normal(
char *map)
266 map[i]=j<256 ? (j>=RATE ? j-RATE : 0) : 255;
269 void bright(
char *map)
272 for(i=0;i<8192;i++) map[i]=i>>3<255 ? (i>>3) : 255;
277 SDL_SetColors(thescreen, themap, 0, 256);
281 void loadcolor(
int n,
int r,
int g,
int b)
289 void loadcolors(
unsigned int which)
300 if(i<64) loadcolor(i,0,0,0);
301 else if(i<128) loadcolor(i,i-64,0,0);
302 else if(i<192) loadcolor(i,63,i-128,0);
303 else loadcolor(i,63,63,i-192);
306 if(i<64) loadcolor(i,0,0,0);
307 else if(i<128) loadcolor(i,0,0,i-64);
308 else loadcolor(i,(i-128)>>1,(i-128)>>1,63);
311 loadcolor(i,i>>2,i>>2,i>>2);
317 loadcolor(i,r*i>>8,g*i>>8,b*i>>8);
320 loadcolor(i,i>>2,0,0);
323 loadcolor(i,0,i>>2,0);
326 loadcolor(i,0,0,i>>2);
336 if(i>8 && i<128) j=63;
359 main(
int argc,
char *argv[])
363 unsigned char *p1, *p2;
369 unsigned long videoflags;
377 if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
379 fprintf(stderr,
"Couldn't initialize SDL: %s\n",SDL_GetError());
382 videoflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_HWPALETTE;
384 thescreen = SDL_SetVideoMode(XSIZE, YSIZE, 8, videoflags);
385 if ( thescreen == NULL )
387 fprintf(stderr,
"Couldn't set display mode: %s\n",
394 vmem2=malloc(XSIZE*YSIZE);
396 mul640=malloc(YSIZE*
sizeof(
char *));
400 remap2=malloc(16384);
402 blobs=malloc(MAXBLOBS*
sizeof(
struct blob));
405 puts(
"Fire demo by David Ashley (dash@xdr.com)");
406 puts(
"1 = Change color map");
407 puts(
"2 = Randomly change color map");
411 puts(
"Left mouse button = paint");
412 puts(
"Right mouse button, CR = ignite atmosphere");
414 freeblobs=activeblobs=0;
415 for(i=0;i<MAXBLOBS;i++)
417 blobs[i].blobnext=freeblobs;
426 loadcolors(whichmap);
432 starttime=SDL_GetTicks();
438 if ( scrlock() < 0 )
continue;
440 if ( vmem1 != (
unsigned char *)thescreen->pixels )
442 p1=vmem1=thescreen->pixels;
443 for (i=0;i<YSIZE;i++)
445 mul640[i]=i*thescreen->pitch+vmem1;
447 p1+=thescreen->pitch;
455 if(explodenum>96 && explodenum<160 && !(rand()&511) || (buttonstate&8))
458 explodenum=(now>>4)+1;
if(explodenum==320) now=0;
459 if(explodenum>256) explodenum=256;
464 if(buttonstate&2) trydisk();
474 fire(vmem2,vmem1,k,flash ? remap2 :remap);
478 while(SDL_PollEvent(&event))
482 case SDL_MOUSEBUTTONDOWN:
483 case SDL_MOUSEBUTTONUP:
484 if ( event.button.state == SDL_PRESSED )
485 buttonstate|=1<<
event.button.button;
487 buttonstate&=~(1<<
event.button.button);
488 mousex=
event.button.x;
489 mousey=
event.button.y;
490 if(!ispaused && buttonstate&2) trydisk();
492 case SDL_MOUSEMOTION:
493 mousex=
event.motion.x;
494 mousey=
event.motion.y;
495 if(!ispaused && buttonstate&2) trydisk();
498 key=
event.key.keysym.sym;
499 if(key==SDLK_RETURN) {flash=60;
break;}
500 if(key==SDLK_1 || key==SDLK_2)
506 loadcolors(whichmap);
509 if(key==SDLK_ESCAPE) {done=1;
break;}
510 if(key==SDLK_SPACE && !ispaused) {addblob();
break;}
511 if(key==SDLK_p) {ispaused=!ispaused;
break;}
522 starttime=SDL_GetTicks()-starttime;
523 if(!starttime) starttime=1;
525 printf(
"fps = %d\n",1000*frames/starttime);