#include "MLRHeaders.hpp"

//#############################################################################
//###########################    MLRCulturShape    ############################
//#############################################################################

MLRCulturShape::ClassData*
	MLRCulturShape::DefaultData = NULL;

MLRShape*
	MLRCulturShape::culturalRevolution = NULL;

bool
	MLRCulturShape::isDayTime = true;
Stuff::RGBAColor
	MLRCulturShape::dayColor = RGBAColor::White;
Stuff::RGBAColor
	MLRCulturShape::nightColor = RGBAColor::White;
Stuff::DynamicArrayOf<MLR_I_C_TMesh*> *MLRCulturShape::theNaturals = NULL;
Stuff::DynamicArrayOf<Stuff::Point3D> *MLRCulturShape::theNaturalCoords = NULL;
Stuff::DynamicArrayOf<Vector2DScalar> *MLRCulturShape::theNaturalTexCoords = NULL;
Stuff::DynamicArrayOf<BYTE> *MLRCulturShape::theNaturalIndicies = NULL;
Stuff::DynamicArrayOf<ColorType> *MLRCulturShape::theNaturalColors = NULL;

int MLRCulturShape::lastUsedMesh = 0, MLRCulturShape::lastUsedVertex = 0, MLRCulturShape::nrOfNaturalMeshes = 16;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MLRCulturShape::InitializeClass()
{
	Verify(!DefaultData);
	Verify(gos_GetCurrentHeap() == StaticHeap);
	DefaultData =
		new ClassData(
			MLRCulturShapeClassID,
			"MidLevelRenderer::MLRCulturShape",
			MLRShape::DefaultData
		);
	Register_Object(DefaultData);

	isDayTime = true;

	dayColor = RGBAColor::White;
	nightColor = RGBAColor(0.447059f, 0.55934f, 0.58317f, 1.0f);

	theNaturals = new DynamicArrayOf<MLR_I_C_TMesh*> (nrOfNaturalMeshes);

	for(int i=0;i<nrOfNaturalMeshes;i++)
	{
		(*theNaturals)[i] = new MLR_I_C_TMesh;
	}

	theNaturalCoords = new DynamicArrayOf<Point3D> (nrOfNaturalMeshes*Limits::Max_Number_Vertices_Per_Mesh);
	theNaturalTexCoords = new DynamicArrayOf<Vector2DScalar> (nrOfNaturalMeshes*Limits::Max_Number_Vertices_Per_Mesh);
	theNaturalIndicies = new DynamicArrayOf<BYTE> (nrOfNaturalMeshes*Limits::Max_Number_Vertices_Per_Mesh);
	theNaturalColors = new DynamicArrayOf<ColorType> (nrOfNaturalMeshes*Limits::Max_Number_Vertices_Per_Mesh);

	culturalRevolution = NULL;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MLRCulturShape::TerminateClass()
{
	if(culturalRevolution!=NULL)
	{
		culturalRevolution->DetachReference();
	}

	for(int i=0;i<nrOfNaturalMeshes;i++)
	{
		(*theNaturals)[i]->DetachReference();
	}

	theNaturals->SetLength(0);
	delete theNaturals;

	theNaturalCoords->SetLength(0);
	delete theNaturalCoords;

	theNaturalTexCoords->SetLength(0);
	delete theNaturalTexCoords;

	theNaturalIndicies->SetLength(0);
	delete theNaturalIndicies;

	theNaturalColors->SetLength(0);
	delete theNaturalColors;

	Unregister_Object(DefaultData);
	delete DefaultData;
	DefaultData = NULL;

	if(culturalRevolution!=NULL)
	{
		Unregister_Object(culturalRevolution);
		culturalRevolution->DetachReference();
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MLRCulturShape::MLRCulturShape(
	MemoryStream *stream,
	int version
) :
	MLRShape(DefaultData, stream, version)
{

	*stream >> centerInWorldX >> centerInWorldZ;
	*stream >> fadeOutStart >> fadeOutEnd;
	*stream >> radius;

	MemoryStreamIO_Read(stream, &culturals);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MLRCulturShape::MLRCulturShape():
	MLRShape(0, DefaultData)
{ 
	Verify(gos_GetCurrentHeap() == ShapeHeap);

	centerInWorldX = 0.0f;
	centerInWorldZ = 0.0f;
	fadeOutStart = 50.0f;
	fadeOutEnd = 80.0f;
	radius = 114.0f;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MLRCulturShape::~MLRCulturShape()
{
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MLRCulturShape*
	MLRCulturShape::Make(
		MemoryStream *stream,
		int version
	)
{
	Check_Object(stream);

	gos_PushCurrentHeap(ShapeHeap);
	MLRCulturShape *shape = new MLRCulturShape(stream, version);
	gos_PopCurrentHeap();

	return shape;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MLRCulturShape::Save(MemoryStream *stream)
{
	Check_Object(this);
	Check_Object(stream);

	MLRShape::Save(stream);

	*stream << centerInWorldX << centerInWorldZ;
	*stream << fadeOutStart << fadeOutEnd;
	*stream << radius;

	MemoryStreamIO_Write(stream, &culturals);
}

