///////////////////////////////////////////////////////////////////////////////
/// @file RenderState.cpp
///
/// @brief class for driver renderstate settings
///
/// @author Lightning
///
/// 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 Aug 2001
/// @author Lightning
/// @remarks Creation
///
///////////////////////////////////////////////////////////////////////////////

#include "deRenderStates_priv.hpp"
#include "deWorld.hpp"

static long s_RenderStateWOInterfaceID = -1;

#pragma todo(Serialize and Deserialize in all RenderStates SUCK. FIX! - Lightning)

deBoolean IdeRenderState_Initialize()
{
	IdeWorldManager * manager = IdeWorld_GetManager();
	s_RenderStateWOInterfaceID = manager->GetUniqueWOInterfaceID();

	if (!WorldObjectRegister(deRenderLight))
		DEBUGRETURN(deFALSE, "IdeRenderState_Initialize: deRenderLight");
	if (!WorldObjectRegister(deRenderMaterial))	
		DEBUGRETURN(deFALSE, "IdeRenderState_Initialize: deRenderMaterial");
	if (!WorldObjectRegister(deRenderTexture))	
		DEBUGRETURN(deFALSE, "IdeRenderState_Initialize: deRenderTexture");

	return deTRUE;
}

long IdeRenderState_GetWOInterfaceID()
{
	return s_RenderStateWOInterfaceID;
}

IdeRenderTexture* IdeRenderState_CreateRenderTexture()
{
	deRenderTexture* retval = new deRenderTexture;
	return retval;
}

IdeRenderMaterial* IdeRenderState_CreateRenderMaterial()
{
	deRenderMaterial* retval = new deRenderMaterial;
	return retval;
}

IdeRenderLight* IdeRenderState_CreateRenderLight()
{
	deRenderLight* retval = new deRenderLight;
	return retval;
}

deRenderState::deRenderState()
{
	m_LockFlag = deFALSE;
}

deRenderState::~deRenderState()
{
}

deBoolean deRenderState::Lock()
{
	//default lock function
	
	//if already locked, fail
#pragma note (this could be adjusted to spinlock for multi-threaded usage)
	if(m_LockFlag)
		DEBUGRETURN(deFALSE, "Lock: Already Locked");
	
	//set it and return true
	m_LockFlag = deTRUE;
	return deTRUE;
}

deBoolean deRenderState::Unlock()
{
	//default unlock function
	
	//if it isn't locked, fail
	if(!m_LockFlag)
		DEBUGRETURN(deFALSE, "Unlock: Not Locked");
	
	//unlock it and return true
	m_LockFlag = deFALSE;
	return deTRUE;
}

void* deRenderState::GetWOInterface(long interface_id)
{
	if (interface_id == s_RenderStateWOInterfaceID)
		return (IdeRenderState*)(this);
	if (interface_id == 0)
		return (IdeWorldObject*)(this);
	return NULL;
}

