#define sgn(x) ((x<0)?-1:((x>0)?1:0))
#define MAX_SPRITE_FRAMES 10
#define	SPRITE_DEAD 0
#define	SPRITE_ALIVE   1
#define	SPRITE_KILLED -1
#define	ANIMATION      3

unsigned int GameLoopCount=0;



BITMAP Exhaust[2];
// the sprite data structure
class csprite
{


      protected:

      // no assignments made currently..
      public:

	int x,y;             //the sprite
	int x_old,y_old;     //the old coordinates
	int width,height;    //dimensions

	// array of ptrs to image
		unsigned char far *frames[MAX_SPRITE_FRAMES];
	// the current frame
		int curr_frame;
	// total frames
		int num_frames;
	// sprite condition
		int state;

	// the sprite speed
		float speed;


// prototypes
void csprite::initialize(int xx,int yy);
void csprite::LExhaustAir (void);
void csprite::RExhaustAir (void);
void csprite::deltree(void);
void csprite::paste_x_db(void);
void csprite::paste_y_db(void);
void csprite::paste_db(void);
int csprite::collision   (csprite *sprite2);
int csprite::OutOfScreen (void);
int csprite::LeftBehindScreen (void);
};/////////////////////////// class end

///////////////////////////////////////////////////////////////////////////
//////////////////   sprite data structure completed..


void csprite::initialize(int xx,int yy)
{
  // this function initializes the sprite with the sent data
  int index;

	x           =xx;
	y           =yy;
	x_old       =xx;
	y_old       =yy;
	width       =0;
	height      =0;
	curr_frame  =0;
	state       =SPRITE_ALIVE;
	num_frames  =0;

	// set all bmp ptrs to null
	for (index=0;index<MAX_SPRITE_FRAMES;index++)
		frames[index]=NULL;

}// end sprite_init

// fire exhaust systems
///////////////////////////////////////////////////////////////////////////

    void csprite::LExhaustAir (void)
    {

    if (state == SPRITE_DEAD)return;
    if (GameLoopCount%ANIMATION)
    bitmap_show_tranparency_inscreen(&Exhaust[0],
			    x,
			    y+(height>>2));
    }

    void csprite::RExhaustAir (void)
    {
    if (state == SPRITE_DEAD)return;
    if (GameLoopCount%ANIMATION)
    bitmap_show_tranparency_inscreen(&Exhaust[1],
			    x+(width),
			    y+(height>>2));
    }
////////////////////////////////////////////////////////////////////////////

void csprite::deltree(void)
{
  // this function deletes the memory associated with a sprite
  int index;

	//farfree (background);
	// deallocate animation
	for (index=0;index<MAX_SPRITE_FRAMES;index++)
		farfree(frames[index]);

}// end sprite delete

// this function draw sprite row by row using bitwise operators
void csprite::paste_x_db(void)

{
  // this function draw sprite row by row using bitwise operators
  int offset;
  int a,b;
  unsigned char *data = NULL;
  long bitmap_offset = 0;

  int i=0,j=0;


    if (state == SPRITE_DEAD)return;
	// alias a ptr. to the sprite


  data = frames[curr_frame];
  if(data == (unsigned char*) 0x00000096) return;	// HACK
  for(j=0;j<height;j++)
  {
    b=y+j;
    for (i=0;i<width;i++)
    {
    a=x+i;

    //transparency
    if(data[bitmap_offset+i])
      {
	if (a>=319)   break;
	if (a> 0)
	{
	// the function plot a pixel on the double buffer
	double_buffer[((b<<8) + (b<<6)) + a] = data[bitmap_offset+i];
	}

      }

    }
    bitmap_offset+=width;
  }


} // end paste_sprite
///////////////////////////////////////////////////////////////////////////

void csprite::paste_y_db(void)

{
  // this function draw sprite row by row using bitwise operators
  int offset;
  int a,b;
  unsigned char *data = NULL;
  long bitmap_offset = 0;

  int i=0,j=0;


    if (state == SPRITE_DEAD)return;
	// alias a ptr. to the sprite


  data = frames[curr_frame];
  for(j=0;j<height;j++)
  {
    b=y+j;
    for (i=0;i<width;i++)
    {
    a=x+i;

    //transparency
    if(data[bitmap_offset+i])
      {
	if (b>=199)   break;
	if (b> 0)
	{
	// the function plot a pixel on the double buffer
	double_buffer[((b<<8) + (b<<6)) + a] = data[bitmap_offset+i];
	}

      }

    }
    bitmap_offset+=width;
  }
} // end paste_sprite

   void csprite::paste_db(void)

{
  // this function draw sprite row by row using bitwise operators
  int offset;
  int a,b;
  unsigned char *data;
  long bitmap_offset = 0;

  int i=0,j=0;


    if (state == SPRITE_DEAD)return;
	// alias a ptr. to the sprite


  data = frames[curr_frame];
  for(j=0;j<height;j++)
  {
    b=y+j;
    for (i=0;i<width;i++)
    {
    a=x+i;
    //transparency
    if(data[bitmap_offset+i])
      {
	if (b>199)   break;
	if (a>319)   break;
	if (a< 0)    break;
	if (b< 0)    break;

	// the function plot a pixel on the double buffer
	double_buffer[((b<<8) + (b<<6)) + a] = data[bitmap_offset+i];


      }

    }
    bitmap_offset+=width;
  }


} // end paste_sprite

typedef csprite *csprite_ptr;

    int  csprite::OutOfScreen 	(void)

    {
	    if (/****/(x+width) < 0 )
		/****/return (1);
	    else
	    if (/****/x > Getmaxx)
		/****/return (1);

	   if (/****/ (y+height) < 0 )
		/****/return (1);
	    else
	    if (/****/y > Getmaxy)
		/****/return (1);

	       return(0);

     }


    int  csprite::LeftBehindScreen 	(void)

    {
	    if (/****/(x+width) < 0 )
		/****/return (1);

	    return(0);

     }


int csprite::collision(csprite *sprite2)
{

  if((state==SPRITE_DEAD) || (sprite2->state==SPRITE_DEAD)) return(0);

	if ((x+width< sprite2->x))
	{
		return(0);
	}
	if ((x> sprite2->x+sprite2->width))
	{
		return(0);
	}
		if ((y> sprite2->y+sprite2->height))
	{
		return(0);
	}
	if ((y+height< sprite2->y))
	{
		return(0);
	}
	else
		return(1);
}// end collide




/*
int csprite::collision(csprite *sprite2)
{

  if((state==SPRITE_DEAD) || (sprite2->state==SPRITE_DEAD)) return(0);
  // function test bounding boxes to test collision
  // 1=collision,0=no collision
  int dx,dy;
  // compute the amount of over lap test the x and y extents note

    dx = abs (x - sprite2->x);
    dy = abs (y - sprite2->y);

  // how the width and height are decreased this is to make
  // the bounding box smaller

	if ((dx< width-(sprite2->width   >>  3)) &&
	   (dy<  height-(sprite2->height >> 3)))
	{
		return(1);
	}
	else
		return(0);
}// end collide
*/



//////////////	grab bitmap into sprite data structure ////////////////
void bmp_grab(char *file,csprite_ptr sprite,int pal_choice)

{
  // this function grabs a bmp from the frame buffer
  BITMAP bitmap;
    bitmap_load(file,&bitmap);

      if (pal_choice)
	set_palette(bitmap.palette);

	// allocate memory for sprite
	sprite->frames[sprite->num_frames]=(unsigned char far *)
	farmalloc(sizeof(bitmap.data));

	// create an alias to the sprite for easy access
	sprite->frames[sprite->num_frames]=bitmap.data;




	sprite->width=bitmap.width;
	sprite->height=bitmap.height;

	// increment the number of frames
	sprite->num_frames++;



}// end
