Make your own free website on Tripod.com

Fire.

Prev || Home

bar.gif (11170 bytes)

/*
** Fire special effect
** -------------------
** routine by KR Ranjith ( krranjith@crosswinds.net )
*/

#include<dos.h>
#include<sys/nearptr.h>

typedef unsigned char byte;

void setrgb( byte c, byte r, byte g, byte b )
{
outportb(0x3C8,c);
outportb(0x3C9,r);
outportb(0x3C9,g);
outportb(0x3C9,b);
}

void set_palette(int type)
{
int x=0;

if( type==1 )
for (x=1; x<=32; x++) {
setrgb(x, x*2-1, 0, 0 ); /* red */
setrgb(x+32, 63, x*2-1, 0 );
setrgb(x+64, 63, 63, x*2-1);
setrgb(x+96, 63, 63, 63);
}
else if( type==2 )
for (x=1; x<=32; x++) {
setrgb(x, 0, 0, x*2-1 ); /* blue */
setrgb(x+32, x*2-1, 0, 63 );
setrgb(x+64, 63, x*2-1, 63);
setrgb(x+96, 63, 63, 63);
}
else
for (x=1; x<=32; x++) {
setrgb(x, 0, x*2-1, 0 ); /* green */
setrgb(x+32, 0, 63, x*2-1 );
setrgb(x+64, x*2-1, 63, 63);
setrgb(x+96, 63, 63, 63);
}

}

int main()
{
union REGS s;

byte *scr;
byte *vrscr;
byte *t;
byte *val;
int key;

vrscr = (char*) calloc(320*200,1);

if( vrscr == 0 )
{
printf("Not enough memory\n");
return 0;
}

printf("This simple fire effect was written entirely in DJGPP C compiler\nwith no assembly language.\n\n"
"Try pressing 'r','g' or 'b' for different colored flames.\n\nprog. by Ranjith K ( ranjithk@crosswinds.net ) \n\n"
"press a key to continue\n");

getch();

/* Set graphics mode */
s.w.ax = 0x13;
int86(0x10, &s,&s);

/* start with the real red */
set_palette(1);

__djgpp_nearptr_enable();

scr = (byte*)0xA0000 + __djgpp_conventional_base;

while( 1 )
{
val = vrscr+320*200;

/* fill the bottom line with random values */
for( t=val-640; t<val; t++ )
{
*t = (rand()%2)==0 ? 120 : 0;
}

val = vrscr+320*200-320;

/* calculate pixel values */
for( t=vrscr+320*100; t<val; t++ )
    {
*(t-320) = (*t + *(t+1) + *(t-1) + *(t+320) + *(t+321) + *(t+319) ) / 6;

/* The fire needs to fade at the top */
if( *(t-320) > 0 )
*(t-320) = *(t-320) - 1;

    }

    /* retrace sync */
    while( inportb(0x3Da) & 0x8 );
    while( ! (inportb(0x3Da) & 0x8) );

/* blit buffer to screen */
memcpy( scr+(320*4), vrscr, 320*197 );

if( kbhit() )
if( (key = getch()) == 27 )
break;
else if( key == 'r' )
set_palette(1);
else if( key == 'g' )
set_palette(3);
else if( key == 'b' )
set_palette(2);
}

free(vrscr);

__djgpp_nearptr_disable();

/* Set text mode */
s.w.ax = 0x3;
int86(0x10, &s,&s);

return 0;
}

bar.gif (11170 bytes)