///////////////////////////////////////////////////////////////////////////////
/// @file LineBrush.cpp
///
/// @brief Creation of a line
///
/// @author Assassin 
///
/// This file is the intellectual property of Novus Delta, LLC.. Usage of the
/// contents of this file is subject to the Destiny3D Member License which
/// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
///
/// This file is distributed "AS IS" without warranty of any kind.  Novus
/// Delta, LLC. does not guarantee the fitness of the contents of this file
/// for any particular purpose.
///
/// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
///
/// <hr>
///                                 Change History
/// <hr>
///
/// @date Mar 2003
/// @author Assassin
/// @remarks Creation
///
///////////////////////////////////////////////////////////////////////////////
#include "deBrush_priv.hpp"
#include "deDriver.hpp"

WorldObjectClassImp(deLineBrush);

static const deBrush::DataQuantity_t s_LineQuantities = 
{
	deBrush::DataQuantity_t::Quantity_PerCV, 
	1,	//CVNum
	0,	//NumFaces
	0	//CoordsPerFace
};

deLineBrush::deLineBrush()
:deBrush(Brush_Line, s_LineQuantities)
{
	m_MainVBuffer->Lock();
	//allocate memory for the points
	if(!m_MainVBuffer->CreateBuffer(IdeVertexBuffer::BUFFER_POSITION, deFALSE))
	{
		//something failed, destroy and exit
		throw;
	}

	//a buffer for the color
	if(!m_MainVBuffer->CreateBuffer(IdeVertexBuffer::BUFFER_DIFFUSE, deFALSE))
	{
		throw;
	}
	m_MainVBuffer->Unlock();
}

deLineBrush::~deLineBrush()
{
}

void deLineBrush::UpdateBrush()
{
	deVertex *			Position;
	deARGB *			Color;
	const int			PointCount = m_CVNum;
	int					i;

	m_SubVBs.Resize(1);
	IdeVertexBuffer* VBuffer = m_SubVBs[0];

	//lock it
	VBuffer->Lock();

	if (!VBuffer->SetBufferSize(PointCount))
	{
		VBuffer->Unlock();
		DEBUGMSG0("UpdateBrush: unable to set buffer size");
		return;
	}

	//grab the buffers now that we created and fill them
	Position = (deVertex *)VBuffer->GetBuffer(IdeVertexBuffer::BUFFER_POSITION);
	Color = (deARGB *)VBuffer->GetBuffer(IdeVertexBuffer::BUFFER_DIFFUSE);

	for (i = 0; i < PointCount; i++)
	{
		Position[i] = deMake_Vertex(m_CVList[i]);
		Color[i] = 0xffffffff;
	}

	//make sure we are doing a tri list
	VBuffer->SetRenderType(IdeVertexBuffer::RENDER_LINESTRIP);

	//make sure the buffers are dirtied
	VBuffer->DirtyBuffer(IdeVertexBuffer::BUFFER_POSITION);
	VBuffer->DirtyBuffer(IdeVertexBuffer::BUFFER_DIFFUSE);

	VBuffer->Unlock();
}

deBrush * deLineBrush::CopyBrush()
{
	deLineBrush * b = NULL;
	b = new deLineBrush;
//	*b = *this;
	return b;
}

deBoolean deLineBrush::Serialize(IdeFile* FS)
{
	if (!deBrush::Serialize(FS))
		return deFALSE;
	return deTRUE;
}

deBoolean deLineBrush::DeSerialize(IdeFile* FS, long DataLength)
{
	if (!deBrush::DeSerialize(FS, DataLength))
		return deFALSE;
	return deTRUE;
}

