#include "Terrain.h"

Terrain::Terrain( const unsigned int size )
: m_size( size ), m_nextAdd( 0 ), m_plot( 0x0 )
{
	m_plot = new b2Vec2[ size ];
}

Terrain::~Terrain()
{
	delete[] m_plot ;
	m_plot = 0x0 ;
}
	
void Terrain::addPlot( float x, float y )
{
	if ( m_nextAdd == m_size )
		return ;

	m_plot[ m_nextAdd ].Set( x, y );

	m_nextAdd ++;
}

void Terrain::build( b2World &world )
{
	b2BodyDef areaDef;
	areaDef.position.Set(0.0f, 0.0f);
	b2Body* areaBody = world.CreateBody(&areaDef);

	b2ChainShape chain;
	chain.CreateChain( m_plot, m_nextAdd, m_plot[0], m_plot[m_size - 1]  );
	areaBody->CreateFixture( &chain, 0.0f );
}

void Terrain::draw( SDL_Surface* screen, b2Vec2 origin )
{
	int x1, y1, x2, y2 ;
	for ( unsigned int i(0); i < m_nextAdd - 1 ; i++ )
	{
		x1 = ( m_plot[i].x - origin.x ) * MULTI ;
		y1 = ( m_plot[i].y - origin.y ) * MULTI ;
		x2 = ( m_plot[i + 1].x - origin.x ) * MULTI ;
		y2 = ( m_plot[i + 1].y - origin.y ) * MULTI ;


		lineRGBA (screen, x1, y1, x2, y2, 255, 0, 0, 255);
	}
}