view LightClone/Source/GuiImage.cpp @ 25:eae13b04b06f

Working on Gui drag and drop
author koryspansel <koryspansel@bendbroadband.com>
date Fri, 16 Sep 2011 13:11:35 -0700
parents a785b0aaf004
children 3a63df04f3c0
line wrap: on
line source

/*
 * GuiImage
 */

#include "GuiImage.h"
#include "VertexTypes.h"

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

/*
 * Initialize
 */
ErrorCode GuiImage::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 GuiImage::Terminate()
{
	if(pTexture)
	{
		pTexture->Release();
		pTexture = NULL;
	}

	if(pVertexBuffer)
	{
		pVertexBuffer->Release();
		pVertexBuffer = NULL;
	}

	if(pEffect)
	{
		pEffect->Release();
		pEffect = NULL;
	}
}

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

/*
 * Render
 */
void GuiImage::Render(RenderContext& kContext, Camera& kCamera)
{
	if(bVisible)
	{
		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);

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

			const D3DXVECTOR2& kLocation = GetPosition();

			D3DXMATRIX kTranslate;
			D3DXMatrixTranslation(&kTranslate, fOffsetX + kLocation.x + 0.5f, fOffsetY - kLocation.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->SetTechnique(pEffect->GetTechnique(0));
			pEffect->Begin(&nPasses, 0);
			pEffect->BeginPass(0);

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

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

		GuiElement::Render(kContext, kCamera);
	}
}

/*
 * SetTexture
 */
ErrorCode GuiImage::SetTexture(const char* pName, bool bResize)
{
	if(pTexture)
	{
		pTexture->Release();
		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 GuiImage::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;
}