//
// Asura_Env_System.cpp
//
// Created by Kevin Lea - 12/06/2001 11:15:27
// 
// Copyright (C) 2001 Rebellion, All Rights Reserved.
//

// I N C L U D E S ///////////////////////////////////////////////////////////

#include "Core/Asura.h"

// This file's header.
#include "Mesh/Asura_Env_System.h"

// Engine independent.
#include "Camera/Asura_Camera.h"
#include "Core/Asura_String.h"
#include "Debugging/Asura_Assert.h"
#include "Mesh/Asura_Env.h"

// C L A S S   I M P L E M E N T A T I O N S /////////////////////////////////

Asura_TemplateHashTree<Asura_Environment>	Asura_Environment_System::s_xEnvironmentTree;
Asura_Environment*							Asura_Environment_System::s_pxCurrentEnvironment	= NULL;

#if defined(ASURA_TOOLS)
// Variables used to start entities at the camera position from Tools...
bool				Asura_Environment_System::s_bUseCameraPositionAsStartPoint	= false;
Asura_Vector_3		Asura_Environment_System::s_xCameraPositionForStartPoint;
Asura_Matrix_3x3	Asura_Environment_System::s_xCameraOrientationForStartPoint;
#endif	// defined(ASURA_TOOLS)


Asura_Environment* Asura_Environment_System::GetEnvironment( const char* szName )
{
	const Asura_Hash_ID	uHash			= Asura_GetHashID(szName);
	Asura_Environment*	pxEnvironment	= GetEnvironment(uHash);

	// Did we find the environment?
    if(pxEnvironment == NULL)
	{
		// No - create one then...
		return(CreateEnvironment(szName));
	}
	else
    {
		// Yes - use this environment...
		return(pxEnvironment);
    }
}

void Asura_Environment_System::Initialise(void)
{
    Platform_Initialise();
}

void Asura_Environment_System::Reset(void)
{
    Platform_Reset();

	// Reset the entire handler tree...
	s_xEnvironmentTree.Reset();

	// Remove the current handler...
	s_pxCurrentEnvironment	= NULL;
}

void Asura_Environment_System::Shutdown(void)
{
    Platform_Shutdown();

	// Reset the entire handler tree...
	s_xEnvironmentTree.Reset();

	// Remove the current handler...
	s_pxCurrentEnvironment	= NULL;
}

void Asura_Environment_System::Update( void )
{
    Platform_Update();

	if(s_pxCurrentEnvironment != NULL)
	{
		// Now that the platform-specific code has processed the required regions
		// from last frame (or the initially required ones if this is the first
		// frame) we can reset ready for when we evaluate visibilities for this
		// frame.
		s_pxCurrentEnvironment->ResetReqdAndVisRgnsForThisFrame();
	}
}

Asura_Environment* Asura_Environment_System::CreateEnvironment (const char* szName)
{
	const Asura_Hash_ID	uHash			= Asura_GetHashID(szName);
	Asura_Environment*	pxEnvironment	= s_xEnvironmentTree.GetDataByHash(uHash);

	// Do we already have this handler?
	if(pxEnvironment == NULL)
	{
		// No - create one...
		pxEnvironment		= s_xEnvironmentTree.AddNode(uHash);
		pxEnvironment->Name	= szName;
	}
	else
	{
		// Yes - this shouldn't happen...
		Asura_Assert(false, "Already have an environment-system with this hash-ID.");
	}

	// Do we have a current environment?
	if(s_pxCurrentEnvironment == NULL)
	{
		s_pxCurrentEnvironment	= pxEnvironment;
	}

	// Return the handler...
	return(pxEnvironment);
}

bool Asura_Environment_System::DestroyEnvironment (const Asura_Hash_ID uHash)
{
	// Call the delete function...
	return(s_xEnvironmentTree.DeleteNodeByHash(uHash));
}


#if defined(ASURA_TOOLS)

////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_Environment_System::SetUseCameraAsStartPoint
// Author       : Andy Tate
// Date         : 16-Mar-2007
////////////////////////////////////////////////////////////////////////////////

void Asura_Environment_System::SetUseCameraAsStartPoint(void)
{
	s_bUseCameraPositionAsStartPoint	= true;
	s_xCameraPositionForStartPoint		= *Asura_Camera::GetPosition();
	s_xCameraOrientationForStartPoint	= *Asura_Camera::GetOrientation();
}

/// End of function 'Asura_Environment_System::SetUseCameraAsStartPoint'. ///

#endif	// defined(ASURA_TOOLS)


/// End of file. ///
