view LightClone/Source/RenderContext.cpp @ 8:968341ab1fb2

First pass world re-structuring
author koryspansel
date Fri, 09 Sep 2011 16:18:41 -0700
parents 88b5c4d51c68
children e494c4295dba
line wrap: on
line source

/*
 * RenderContext
 */

#include "RenderContext.h"
#include "VertexTypes.h"

/*
 * RenderContext
 */
RenderContext::RenderContext() : pGraphicsDevice(NULL)
{
}

/*
 * Initialize
 */
ErrorCode RenderContext::Initialize(GraphicsDevice* pDevice)
{
	return pGraphicsDevice = pDevice, Error_Success;
}

/* 
 * Terminate
 */
void RenderContext::Terminate()
{
	pGraphicsDevice = NULL;
}

/*
 * CreateTextureFromFile
 */
ErrorCode RenderContext::CreateTextureFromFile(const char* pName, IDirect3DTexture9** pTexture)
{
	ErrorCode eCode = Error_Fail;

	if(pGraphicsDevice)
	{
		const uint32 nSizeX		= D3DX_DEFAULT_NONPOW2;
		const uint32 nSizeY		= D3DX_DEFAULT_NONPOW2;
		const uint32 nFilter	= D3DX_DEFAULT;
		const uint32 nFilterMip	= D3DX_DEFAULT;

		HRESULT hResult = D3DXCreateTextureFromFileExA(*pGraphicsDevice, pName, nSizeX, nSizeY, 0, 0, D3DFMT_FROM_FILE, D3DPOOL_MANAGED, nFilter, nFilterMip, 0, NULL, NULL, pTexture);
		if(SUCCEEDED(hResult))
		{
			eCode = Error_Success;
		}
	}

	return eCode;
}

/*
 * CreateEffectFromFile
 */
ErrorCode RenderContext::CreateEffectFromFile(const char* pName, ID3DXEffect** pEffect)
{
	ID3DXBuffer* pBuffer = NULL;

	HRESULT hResult = D3DXCreateEffectFromFileA(*pGraphicsDevice, pName, NULL, NULL, 0, NULL, pEffect, &pBuffer);
	if(FAILED(hResult))
	{
		if(pBuffer)
		{
			const char* pError = (const char*)pBuffer->GetBufferPointer();
		}

		return Error_Fail;
	}

	return Error_Success;
}

/*
 * CreateVertexBuffer
 */
ErrorCode RenderContext::CreateVertexBuffer(uint32 nSize, uint32 nUsage, uint32 nPool, IDirect3DVertexBuffer9** pBuffer)
{
	HRESULT hResult = ((IDirect3DDevice9*)*pGraphicsDevice)->CreateVertexBuffer(nSize, nUsage, 0, (D3DPOOL)nPool, pBuffer, NULL);
	if(FAILED(hResult))
	{
		return Error_Fail;
	}

	return Error_Success;
}

/*
 * CreateFontFromName
 */
ErrorCode RenderContext::CreateFontFromName(const char* pName, uint32 nSize, uint32 nWeight, ID3DXFont** pFont)
{
	HRESULT hResult = D3DXCreateFontA(*pGraphicsDevice, nSize, 0, nWeight, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, pName, pFont);
	if(FAILED(hResult))
	{
		return Error_Fail;
	}

	return Error_Success;
}

/*
 * Begin
 */
void RenderContext::Begin(uint32 nColor)
{
	pGraphicsDevice->Begin(nColor);
}

/*
 * End
 */
void RenderContext::End()
{
	pGraphicsDevice->End();
}

/*
 * SetupCamera3D
 */
void RenderContext::SetupCamera3D(ID3DXEffect* pEffect, const D3DXVECTOR3& kLocation, const D3DXVECTOR3& kTarget, float fViewAngle)
{
	D3DVIEWPORT9 kViewport;
	if(SUCCEEDED(pGraphicsDevice->GetViewport(&kViewport)))
	{
		D3DXHANDLE kHandleProj	= pEffect->GetParameterByName(NULL, "kProjection");
		D3DXHANDLE kHandleView	= pEffect->GetParameterByName(NULL, "kView");

		// setup projection matrix
		D3DXMatrixPerspectiveFovLH(&kProjection, fViewAngle, (float)kViewport.Width / (float)kViewport.Height, 1.0f, 1024.0f);

		// setup view matrix
		D3DXMatrixLookAtLH(&kView, &kLocation, &kTarget, &D3DXVECTOR3(0.0f, 1.0f, 0.0f));

		if(kHandleProj && kHandleView)
		{
			// apply parameters to shader
			pEffect->SetMatrix(kHandleProj, &kProjection);
			pEffect->SetMatrix(kHandleView, &kView);
		}
	}
}

/*
 * SetupCamera2D
 */
void RenderContext::SetupCamera2D(ID3DXEffect* pEffect)
{
	D3DVIEWPORT9 kViewport;
	if(SUCCEEDED(pGraphicsDevice->GetViewport(&kViewport)))
	{
		D3DXHANDLE kHandle = pEffect->GetParameterByName(NULL, "kProjection");
		if(kHandle)
		{
			// setup projection matrix
			D3DXMatrixOrthoLH(&kProjection, (float)kViewport.Width, (float)kViewport.Height, 1.0f, 1024.0f);

			// setup view matrix
			D3DXMatrixIdentity(&kView);

			pEffect->SetMatrix(kHandle, &kProjection);
		}
	}
}

/*
 * DrawTriangles
 */
void RenderContext::DrawTriangles(IDirect3DVertexDeclaration9* pDeclaration, IDirect3DVertexBuffer9* pBuffer, uint32 nSize, uint32 nCount)
{
	pGraphicsDevice->DrawTriangles(pDeclaration, pBuffer, nSize, nCount);
}

/*
 * GetViewport
 */
ErrorCode RenderContext::GetViewport(D3DVIEWPORT9* pViewport)
{
	return pGraphicsDevice->GetViewport(pViewport);
}

/*
 * GetProjection
 */
const D3DXMATRIX& RenderContext::GetProjection() const
{
	return kProjection;
}

/*
 * GetView
 */
const D3DXMATRIX& RenderContext::GetView() const
{
	return kView;
}