#include "Gos.pch"
#include "Graphics.h"

GosStateBlock*	(*GosStateBlock::CreateCurrent)( bool, bool ) = StateBlock::CreateCurrent;
GosStateBlock*	(*GosStateBlock::CreateRecording)( ) = StateBlock::CreateRecording;



GosStateBlock* StateBlock::CreateCurrent( bool GetPixelState, bool GetVertexState )
{
	LOG_API("StateBlock::CreateCurrent");
	DWORD Token;
	HRESULT rc;
	D3DSTATEBLOCKTYPE Type = (GetPixelState)?(GetVertexState?D3DSBT_ALL:D3DSBT_PIXELSTATE):(D3DSBT_VERTEXSTATE);

	if( !GetPixelState && !GetVertexState )
		STOP(("Must have GetPixelState or GetVertexState set!"));

	rc = Graphics::D3DDevice()->CreateStateBlock(Type,&Token);
	if( FAILED(rc) )
		STOP(("Failed to create D3D state block"));
	StateBlock *pStateBlock = new StateBlock(Token);
	if( !pStateBlock )
		STOP(("Failed to create state block object"));
	return (GosStateBlock *)pStateBlock;	
}
GosStateBlock* StateBlock::CreateRecording()
{
	LOG_API("StateBlock::CreateRecording");
	HRESULT rc;
	rc = Graphics::D3DDevice()->BeginStateBlock();
	if( FAILED(rc) )
		STOP(("Failed to start recording state block"));
	StateBlock *pStateBlock = new StateBlock(0);
	if( !pStateBlock )
		STOP(("Failed to create state block object"));
	return (GosStateBlock *)pStateBlock;	
}
void StateBlock::EndRecording()
{
	LOG_API("StateBlock::EndRecording");
	HRESULT rc;
	rc = Graphics::D3DDevice()->EndStateBlock(&m_Token);
	if( FAILED(rc) )
		STOP(("Failed to stop recording state block"));
}
void StateBlock::Update()
{
	LOG_API("StateBlock::Update");
	HRESULT rc;
	rc = Graphics::D3DDevice()->CaptureStateBlock(m_Token);
	if( FAILED(rc) )
		STOP(("Failed to update state block"));
}
StateBlock::StateBlock( DWORD Token )
{
	LOG_API("StateBlock::StateBlock");
	m_Token = Token;
}
StateBlock::~StateBlock()
{
	LOG_API("StateBlock::~StateBlock");
	HRESULT rc;
	rc = Graphics::D3DDevice()->DeleteStateBlock(m_Token);
	if( FAILED(rc) )
		STOP(("Failed to delete state block"));
}

GosStateBlock::~GosStateBlock()
{
	LOG_API("GosStateBlock::~GosStateBlock");
	;
}

