#include "dllinterface.h"
#include "MeshContainer.h"

MeshContainer::MeshContainer(unsigned VertexCount, DXVector3* pVertices, unsigned TriangleCount, unsigned* pTriangles)
{
	m_VertexCount = VertexCount;
	m_pVertices = pVertices;
	m_TriangleCount = TriangleCount;
	m_pTriangles = pTriangles;
	m_pAttributes = NULL;
	m_AttributeCount = 0;
	m_VertexBufferSize = VertexCount;
	m_TriangleBufferSize = TriangleCount;
}

MeshContainer::~MeshContainer(void)
{
	delete [] m_pVertices;
	delete [] m_pTriangles;

	for (unsigned i = 0; i < m_AttributeCount; i++)
		delete m_pAttributes[i];
	
	delete [] m_pAttributes; 
}

void MeshContainer::AddAttribute(MeshAttribute* const pAttrib)
{
	MeshAttribute** const pNewAttribArray = new MeshAttribute*[m_AttributeCount + 1];

	if(m_pAttributes)
	{
		memcpy(pNewAttribArray, m_pAttributes, sizeof(MeshAttribute*) * m_AttributeCount);
		delete [] m_pAttributes;
	}	

	pNewAttribArray[m_AttributeCount] = pAttrib;

	m_pAttributes = pNewAttribArray;
	m_AttributeCount++;
}

void MeshContainer::ReallocBuffers(unsigned VertexCount, unsigned TriangleCount)
{
	DXVector3* pNewVertices = new DXVector3[VertexCount];
	unsigned* pNewTriangles = new unsigned[TriangleCount * 3];

	memcpy(pNewVertices, m_pVertices, sizeof(DXVector3) * m_VertexCount);
	memcpy(pNewTriangles, m_pTriangles, sizeof(unsigned) * m_TriangleCount * 3);

	for (unsigned i = 0; i < m_AttributeCount; i++)
	{
		if (m_pAttributes[i]->Mapping == AM_TRIANGLE)
		{
			
		}
		else if(m_pAttributes[i]->Mapping == AM_TRIANGLEINDEXED)
		{

		}
		else if(m_pAttributes[i]->Mapping == AM_VERTEX)
		{

		}
		else if(m_pAttributes[i]->Mapping == AM_TRIANGLEVERTEXINDEXED)
		{

		}
	}

	delete [] m_pVertices;
	delete [] m_pTriangles;
	
	m_pVertices = pNewVertices;
	m_pTriangles = pNewTriangles;
}