/**
*
* \file Asura_Platform_ProgressBar.cpp
*
* Created by Richard May - 20/05/2004 17:24:10
*
* Copyright (C) 2002 Rebellion, All Rights Reserved.
*
* \brief
*
**/



/************/
/* Includes.*/
/************/

// main asura header
#include "Core/Asura.h"

// local header
#include "Menus/Asura_ProgressBar.h"

// asura
#include "Rendering/Asura_Direct3d.h"
#include "Rendering/Asura_Direct3d_Shaders.h"

/******************************************************************************/
/* Types                                                                      */
/******************************************************************************/

struct ColouredVertex
{
	float x, y, z, rhw;
	DWORD uColour;
};

const DWORD uFVF_COLOURED_VERTEX = D3DFVF_XYZRHW | D3DFVF_DIFFUSE;

/**************/
/* Functions. */
/**************/

void Asura_ProgressDisplay_BackgroundFiller::Platform_Render(void)
{
	// SNH - 20/07/2004 15:58:22 - this is stolen from the xbox version, minus the back buffer reading stuff.
	//	NB for destination alpha to work, I had to change the requested back buffer format to A8R8G8B8 -
	//	see Asura_Direct3D::Initialise

	Asura_Render::CurrentStates.RequestAlphaTest(false);
	Asura_Render::CurrentStates.RequestZBufferEnabled(false);
	Asura_Render::CurrentStates.RequestZBufferWriteEnabled(false);
	Asura_Render::CurrentStates.RequestCullMode(ASURA_CULLMODE_NONE);
	
	// Colour layout is broken, so have to swizzle
	const D3DCOLOR uColour = D3DCOLOR_RGBA(m_xFillColour.ColourComponents.B, m_xFillColour.ColourComponents.G, m_xFillColour.ColourComponents.R, m_xFillColour.ColourComponents.A);

	const ColouredVertex axColouredVertex[4] =
	{
		{ m_xCurTL.x,						m_xCurTL.y,							0.0f, 1.0f, uColour },
		{ m_xCurTL.x + m_xCurDimensions.x,	m_xCurTL.y,							0.0f, 1.0f, uColour },
		{ m_xCurTL.x + m_xCurDimensions.x,	m_xCurTL.y + m_xCurDimensions.y,	0.0f, 1.0f, uColour },
		{ m_xCurTL.x,						m_xCurTL.y + m_xCurDimensions.y,	0.0f, 1.0f, uColour },
	};

	Asura_Direct3D_Shaders::RequestFixedFunctionVertexShader(uFVF_COLOURED_VERTEX);

	// Save old multitexture cascade so we can restore it afterwards. There's no Asura texture op for just colour

	DWORD uOldColourOp0, uOldColourArg0, uOldAlphaOp0, uOldAlphaArg0, uOldColourOp1, uOldAlphaOp1;
	Asura_Direct3D::D3DDevice->GetTextureStageState(0, D3DTSS_COLOROP, &uOldColourOp0);
	Asura_Direct3D::D3DDevice->GetTextureStageState(0, D3DTSS_COLORARG1, &uOldColourArg0);
	Asura_Direct3D::D3DDevice->GetTextureStageState(0, D3DTSS_ALPHAOP, &uOldAlphaOp0);
	Asura_Direct3D::D3DDevice->GetTextureStageState(0, D3DTSS_ALPHAARG1, &uOldAlphaArg0);
	Asura_Direct3D::D3DDevice->GetTextureStageState(1, D3DTSS_COLOROP, &uOldColourOp1);
	Asura_Direct3D::D3DDevice->GetTextureStageState(1, D3DTSS_ALPHAOP, &uOldAlphaOp1);

	Asura_Direct3D::D3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	Asura_Direct3D::D3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE);
	Asura_Direct3D::D3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
	Asura_Direct3D::D3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
	Asura_Direct3D::D3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
	Asura_Direct3D::D3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

	// Asura has no definition for the alpha blend we need, so have to do it manually

	DWORD uOldAlphaBlend, uOldSrcBlend, uOldDestBlend;
	Asura_Direct3D::D3DDevice->GetRenderState(D3DRS_ALPHABLENDENABLE, &uOldAlphaBlend);
	Asura_Direct3D::D3DDevice->GetRenderState(D3DRS_SRCBLEND, &uOldSrcBlend);
	Asura_Direct3D::D3DDevice->GetRenderState(D3DRS_DESTBLEND, &uOldDestBlend);

	Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_DESTALPHA);
	Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVDESTALPHA);

	// Draw the triangles

	Asura_Direct3D::D3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, axColouredVertex, sizeof(ColouredVertex));

	// Clean up

	Asura_Render::CurrentStates.RequestZBufferEnabled(true);

	Asura_Direct3D::D3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, uOldColourOp0);
	Asura_Direct3D::D3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, uOldColourArg0);
	Asura_Direct3D::D3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, uOldAlphaOp0);
	Asura_Direct3D::D3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, uOldAlphaArg0);
	Asura_Direct3D::D3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, uOldColourOp1);
	Asura_Direct3D::D3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, uOldAlphaOp1);

	Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, uOldAlphaBlend);
	Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_SRCBLEND, uOldSrcBlend);
	Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_DESTBLEND, uOldDestBlend);
}

void Asura_ProgressDisplay_BackgroundFiller::Platform_Reset(void)
{

}

/****************/
/* End of file. */
/****************/
