#include <stdlib.h> // NULL
#include "bitmap.h"
#include "notice.h"

Bitmap::Bitmap( Screen* newScreen, int x0, int y0, int w, int h )
 : GObject( newScreen, x0, y0 )
{
  width  = w;
  height = h;
  cOldX  = cOldY  = cOldWidth = cOldHeight = -1;
  savedBackground = 0;
  bBuffer = 0;
}

Bitmap::Bitmap( Bitmap& b )
 : GObject( b.screen, b.getX(), b.getY() )
{
  width       = b.getWidth();
  height      = b.getHeight();
  cOldX       = b.cOldX;
  cOldY       = b.cOldY;
  cOldWidth   = b.cOldWidth;
  cOldHeight  = b.cOldHeight;
  savedBackground = b.savedBackground;
  if( savedBackground )
  {
    bBuffer = screen->cloneArea( b.bBuffer );
  }
  else
    bBuffer = NULL;
}

Bitmap& Bitmap::operator= ( Bitmap& b )
{
  width       = b.getWidth();
  height      = b.getHeight();
  cOldX       = b.cOldX;
  cOldY       = b.cOldY;
  cOldWidth   = b.cOldWidth;
  cOldHeight  = b.cOldHeight;

  if( bBuffer )
  {
    screen->freeArea( bBuffer );
    bBuffer = NULL;
  }

  savedBackground = b.savedBackground;
  if( savedBackground )
  {
    bBuffer = screen->cloneArea( b.bBuffer );
  }
  else
    bBuffer = NULL;
  return *this;
}

Bitmap::~Bitmap( void )
{
  if( bBuffer )
    screen->freeArea( bBuffer );
}

void Bitmap::draw( void )
{
}

void Bitmap::saveBackground()
{
  cOldX = getX();
  cOldY = getY();
  cOldWidth = width;
  cOldHeight = height;
  if( bBuffer )
  {
    screen->freeArea( bBuffer );
    bBuffer = NULL;
    savedBackground = 0;
  }
  bBuffer = screen->getArea( x, y, width, height );
  savedBackground = 1;
}

void Bitmap::restoreBackground()
{
  if( savedBackground )
  {
    screen->putArea( cOldX, cOldY, bBuffer );
  }
}

Bool Bitmap::isOver( int x0, int y0 )
{
  int xOk = 0; int yOk = 0;

  if(( West() <= x0 ) && ( x0 <= East() ) )
    xOk = 1;
  if(( North() <= y0 ) && ( y0 <= South() ) )
    yOk = 1;

  return xOk&yOk;
}

int Bitmap::checkAndSetBounds( int& x0, int& y0 )
{
  int notDraw = 0;
  if( x0 < West() )
  {
    x0 = West();
    notDraw++;
  }
  if( x0 > East() )
  {
    x0 = East();
    notDraw++;
  }
  if( y0 < North() )
  {
    y0 = North();
    notDraw++;
  }
  if( y0 > South() )
  {
    y0 = South();
    notDraw++;
  }
  return notDraw;
}

int Bitmap::checkAndSetBounds( int& x2, int& y2, GObject* object )
{
  int notDraw = 0;
  int fx    = object->getX();
  int fy    = object->getY();
  int north = object->North();
  int south = object->South();
  int west  = object->West();
  int east  = object->East();
  int dx = fx - x2;
  int dy = fy - y2;
  int dxl = west  - fx;
  int dxr = east  - fx;
  int dyu = north - fy;
  int dyl = south - fy;

  if( north-dy < North() )
  {
    y2 = North() - dyu;
    notDraw++;
  }
  if( south-dy > South() )
  {
    y2 = South() - dyl;
    notDraw++;
  }
  if( west-dx < West() )
  {
    x2 = West() - dxl;
    notDraw++;
  }
  if( east-dx > East() )
  {
    x2 = East() - dxr;
    notDraw++;
  }
  return notDraw;
}

inline int Bitmap::West( void )
{
  return getX();
}

inline int Bitmap::East( void )
{
  return getX()+getWidth();
}

inline int Bitmap::North( void )
{
  return getY();
}

inline int Bitmap::South( void )
{
  return getY()+getHeight();
}

inline void Bitmap::setWidth( int w )
{
  width = w;
}

inline void Bitmap::setHeight( int h )
{
  height = h;
}

inline int Bitmap::getWidth( void )
{
  return width;
}

inline int Bitmap::getHeight( void )
{
  return height;
}
