view LightClone/Source/GuiRenderContext.cpp @ 63:44dcff5abf12

Work on ServiceProvider
author koryspansel
date Tue, 04 Oct 2011 12:04:09 -0700
parents 1fe27776627e
children 3507bd831c7f
line wrap: on
line source

/*
 * GuiRenderContext
 */

#include "GuiRenderContext.h"

/*
 * GuiRenderContext
 */
GuiRenderContext::GuiRenderContext() : pContext(NULL), pCamera(NULL), pEffect(NULL), pTexture(NULL), pVertices(NULL), nPrimitiveCount(0)
{
}

/*
 * Initialize
 */
ErrorCode GuiRenderContext::Initialize(ResourceManager* pResourceManager)
{
	ErrorCode eCode = pResourceManager->CreateEffectFromFile("Data\\Shaders\\TexturedQuad.fx", &pEffect);
	if(eCode == Error_Success)
	{
		eCode = pResourceManager->CreateVertexBuffer(VertexBufferSize, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, D3DPOOL_DEFAULT, &pVertexBuffer);
	}

	return eCode;
}

/*
 * Terminate
 */
void GuiRenderContext::Terminate()
{
	if(pVertexBuffer)
	{
		pVertexBuffer->Release();
		pVertexBuffer = NULL;
	}

	pEffect = NULL;
}

/*
 * Begin
 */
void GuiRenderContext::Begin(RenderContext* pRenderContext, Camera* pRenderCamera)
{
	if(pRenderContext && pRenderCamera)
	{
		pContext = pRenderContext;
		pCamera = pRenderCamera;

		uint32 nPasses = 0;			

		D3DXHANDLE kTechnique = pEffect->GetTechniqueByName("Color");
		ASSERT(kTechnique != NULL);

		pEffect->SetTechnique(kTechnique);

		pEffect->Begin(&nPasses, 0);
		pEffect->BeginPass(0);

		pContext->ApplyCameraToEffect(*pCamera, pEffect);

		nPrimitiveCount = 0;
		pVertices		= NULL;

		pVertexBuffer->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD);
	}
}

/*
 * End
 */
void GuiRenderContext::End()
{
	if(pVertices)
	{
		if(nPrimitiveCount > 0)
		{
			pVertexBuffer->Unlock();

			D3DXHANDLE kTextureHandle = pEffect->GetParameterByName(NULL, "kTexture");
			ASSERT(kTextureHandle != NULL);

			pEffect->SetTexture(kTextureHandle, pTexture);
			pEffect->CommitChanges();

			pContext->DrawTriangles(Vertex::ColorQuad::Declaration, pVertexBuffer, sizeof(Vertex::ColorQuad), nPrimitiveCount);

			nPrimitiveCount = 0;
			pVertices		= NULL;
			pTexture		= NULL;
		}
	}

	pEffect->EndPass();
	pEffect->End();

	pContext = NULL;
	pCamera	= NULL;
}

/*
 * Add
 */
ErrorCode GuiRenderContext::Add(IDirect3DTexture9* pInstance, const D3DXVECTOR2& kPosition, float fDepth, const D3DXVECTOR2& kSize, D3DCOLOR kColor)
{
	if(pVertices)
	{
		if(pInstance != pTexture)
		{
			if(nPrimitiveCount > 0)
			{
				pVertexBuffer->Unlock();

				D3DXHANDLE kTextureHandle = pEffect->GetParameterByName(NULL, "kTexture");
				ASSERT(kTextureHandle != NULL);

				pEffect->SetTexture(kTextureHandle, pTexture);
				pEffect->CommitChanges();

				pContext->DrawTriangles(Vertex::ColorQuad::Declaration, pVertexBuffer, sizeof(Vertex::ColorQuad), nPrimitiveCount);

				nPrimitiveCount = 0;
				pVertices		= NULL;

				pVertexBuffer->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD);
			}

			pTexture = pInstance;
		}

		if(nPrimitiveCount < QuadsPerBatch)
		{
			const float fOffsetX = 0.0f;//(nFlags & GuiRenderBatchFlag_CenterX) ? 0.5f : 0.0f;
			const float fOffsetY = 0.0f;//(nFlags & GuiRenderBatchFlag_CenterY) ? 0.5f : 0.0f;

			const float fX0 = -0.5f * ScreenSizeX + (kPosition.x + fOffsetX + 0.5f);
			const float fY0 = +0.5f * ScreenSizeY - (kPosition.y + fOffsetY + 0.5f);
			const float fX1 = -0.5f * ScreenSizeX + (kPosition.x + fOffsetX + kSize.x + 0.5f);
			const float fY1 = +0.5f * ScreenSizeY - (kPosition.y + fOffsetY + kSize.y + 0.5f);

			*pVertices++ = Vertex::ColorQuad(fX0, fY1, 1.0f, 0.0f, 1.0f, kColor);
			*pVertices++ = Vertex::ColorQuad(fX0, fY0, 1.0f, 0.0f, 0.0f, kColor);
			*pVertices++ = Vertex::ColorQuad(fX1, fY0, 1.0f, 1.0f, 0.0f, kColor);
			*pVertices++ = Vertex::ColorQuad(fX0, fY1, 1.0f, 0.0f, 1.0f, kColor);
			*pVertices++ = Vertex::ColorQuad(fX1, fY0, 1.0f, 1.0f, 0.0f, kColor);
			*pVertices++ = Vertex::ColorQuad(fX1, fY1, 1.0f, 1.0f, 1.0f, kColor);

			nPrimitiveCount += 2;
		}

		return Error_Success;
	}

	return Error_Fail;
}

/*
 * Flush
 */
void GuiRenderContext::Flush()
{
	if(pVertices)
	{
		if(nPrimitiveCount > 0)
		{
			pVertexBuffer->Unlock();

			D3DXHANDLE kTextureHandle = pEffect->GetParameterByName(NULL, "kTexture");
			ASSERT(kTextureHandle != NULL);

			pEffect->SetTexture(kTextureHandle, pTexture);
			pEffect->CommitChanges();

			pContext->DrawTriangles(Vertex::ColorQuad::Declaration, pVertexBuffer, sizeof(Vertex::ColorQuad), nPrimitiveCount);

			nPrimitiveCount = 0;
			pVertices		= NULL;
			pTexture		= NULL;

			pVertexBuffer->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD);
		}
	}
}

/*
 * GetRenderContext
 */
RenderContext* GuiRenderContext::GetRenderContext()
{
	return pContext;
}

/*
 * GetCamera
 */
Camera* GuiRenderContext::GetCamera()
{
	return pCamera;
}