
#include "image.h"



//---- IMAGE CLASS -----------------------------------------------------------//

Image::Image( int w, int h )
{
  this->w = w;
  this->h = h;
  buf = new int[ size = w*h ];
}

Image::~Image()
{
  delete[] this->buf;
}

void Image::clear( int col )
{
  for( int i=0; i<size; i++ ) buf[i] = col;
}

void Image::checks( )
{
  int i, j;

  int *p = buf;

  for(j=0; j<h; j++) for(i=0; i<w; i++)
  {
    *p++ = (((i>>4)^(j>>4))&1) * 0x222222 + 0xDDDDDD;
  }
}

void Image::drawPolygon( Poly *p, int col )
{
  Scanline sl;

  //  Render each scanline

  for( int i=0; i<h; i++ )
  {
    p->getScanline( &sl, i );
    drawScanline( i, &sl, col );
  }
}

void Image::drawScanline( int y, Scanline *sl, int col )
{
  float x1, x2;

  sl->firstPair();

  while( sl->nextPair( &x1, &x2 ) )
  {
    if( x1 < 0 ) x1 = 0;
    if( x2 > w ) x2 = w;

    for( int i=x1+0.9999; i<x2; i++ )
    {
      buf[ y*w + i ] = col;
    }
  }
}


