view LightClone/Source/GuiCursor.cpp @ 41:a986355cca5f

Move Tower structure into Environment
author koryspansel
date Thu, 22 Sep 2011 10:25:35 -0700
parents 5656c8e382fc
children 7ff46a00bcd3
line wrap: on
line source

/*
 * GuiCursor
 */

#include "GuiCursor.h"
#include "VertexTypes.h"

/*
 * GuiCursor
 */
GuiCursor::GuiCursor() : pEffect(NULL), pVertexBuffer(NULL), pTexture(NULL)
{
}

/*
 * Initialize
 */
ErrorCode GuiCursor::Initialize(ResourceManager* pManager)
{
	pResourceManager = pManager;

	ErrorCode eCode = pResourceManager->CreateEffectFromFile("Data\\Shaders\\TexturedQuad.fx", &pEffect);
	if(eCode == Error_Success)
	{
		eCode = pResourceManager->CreateVertexBuffer(6 * sizeof(Vertex::Quad), D3DUSAGE_WRITEONLY, D3DPOOL_MANAGED, &pVertexBuffer);
		if(eCode == Error_Success)
		{
			eCode = SetupVertexBuffer();
		}
	}

	return eCode;
}

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

/*
 * Update
 */
void GuiCursor::Update(float fElapsed)
{
}

/*
 * Render
 */
void GuiCursor::Render(RenderContext& kContext, Camera& kCamera)
{
	if(pTexture)
	{
		uint32 nPasses = 0;

		const float fOffsetX = -0.5f * ScreenSizeX;
		const float fOffsetY = +0.5f * ScreenSizeY;

		const D3DXVECTOR4 kColorVector(1.0f, 1.0f, 1.0f, 1.0f);

		kContext.ApplyCameraToEffect(kCamera, pEffect);

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

		D3DXMATRIX kScale;
		D3DXMatrixScaling(&kScale, kDimensions.x, kDimensions.y, 1.0f);

		D3DXMATRIX kTranslate;
		D3DXMatrixTranslation(&kTranslate, fOffsetX + kPosition.x + 0.5f, fOffsetY - kPosition.y + 0.5f, 0.0f);

		D3DXMATRIX kWorldMatrix;
		D3DXMatrixMultiply(&kWorldMatrix, &kScale, &kTranslate);

		pEffect->SetMatrix(pEffect->GetParameterByName(NULL, "kWorld"), &kWorldMatrix);
		pEffect->SetVector(pEffect->GetParameterByName(NULL, "kColor"), &kColorVector);
		pEffect->SetTexture(pEffect->GetParameterByName(NULL, "kTexture"), pTexture);
		pEffect->CommitChanges();

		kContext.DrawTriangles(Vertex::Quad::Declaration, pVertexBuffer, sizeof(Vertex::Quad), TrianglesPerFace);

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

/*
 * SetTexture
 */
ErrorCode GuiCursor::SetTexture(const char* pName, bool bResize)
{
	pTexture = NULL;

	ErrorCode eCode = pResourceManager->CreateTextureFromFile(pName, &pTexture);
	if(eCode == Error_Success)
	{
		if(bResize)
		{
			D3DSURFACE_DESC kDescriptor;
			pTexture->GetLevelDesc(0, &kDescriptor);

			kDimensions.x = (float)kDescriptor.Width;
			kDimensions.y = (float)kDescriptor.Height;
		}
	}

	return eCode;
}

/*
 * SetupVertexBuffer
 */
ErrorCode GuiCursor::SetupVertexBuffer()
{
	Vertex::Quad* pVertices = NULL;

	HRESULT hResult = pVertexBuffer->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD);
	if(FAILED(hResult))
	{
		return Error_Fail;
	}

	pVertices[0]	= Vertex::Quad(+0.0f, -1.0f, 1.0f, 0.0f, 1.0f);
	pVertices[1]	= Vertex::Quad(+0.0f, +0.0f, 1.0f, 0.0f, 0.0f);
	pVertices[2]	= Vertex::Quad(+1.0f, +0.0f, 1.0f, 1.0f, 0.0f);
	pVertices[3]	= Vertex::Quad(+0.0f, -1.0f, 1.0f, 0.0f, 1.0f);
	pVertices[4]	= Vertex::Quad(+1.0f, +0.0f, 1.0f, 1.0f, 0.0f);
	pVertices[5]	= Vertex::Quad(+1.0f, -1.0f, 1.0f, 1.0f, 1.0f);

	pVertexBuffer->Unlock();

	return Error_Success;
}