view LightClone/Source/GraphicsDevice.cpp @ 33:06b151afc8d0

Cleaned up UI art; Completed drag and drop; Centered grid
author koryspansel <koryspansel@bendbroadband.com>
date Wed, 21 Sep 2011 00:50:20 -0700
parents 968341ab1fb2
children 4663f93aefc4
line wrap: on
line source

/*
 * GraphicsDevice
 */

#include "GraphicsDevice.h"
#include "VertexTypes.h"

/*
 * GraphicsDevice
 */
GraphicsDevice::GraphicsDevice(IDirect3D9* pContext, IDirect3DDevice9* pInstance, const D3DPRESENT_PARAMETERS& kPresentParameters)
	: pDirect3D(pContext), pDevice(pInstance), kParameters(kPresentParameters)
{
	//ASSERT(pDirect3D);
	pDirect3D->AddRef();

	//ASSERT(pDevice);
	pDevice->AddRef();
}

/*
 * ~GraphicsDevice
 */
GraphicsDevice::~GraphicsDevice()
{
	if(pDevice)
	{
		pDevice->Release();
		pDevice = NULL;
	}

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

/*
 * Create
 */
ErrorCode GraphicsDevice::Create(HWND kWindow, uint32 nWidth, uint32 nHeight, GraphicsDevice** pInstance)
{
	ErrorCode eCode = Error_Fail;

	if(pInstance)
	{
		const uint32 nAdapter	= D3DADAPTER_DEFAULT;
		const uint32 nFlags		= D3DCREATE_HARDWARE_VERTEXPROCESSING;

		D3DPRESENT_PARAMETERS kParameters = {
			nWidth,
			nHeight,
			D3DFMT_UNKNOWN,
			1,
			D3DMULTISAMPLE_4_SAMPLES,
			1,
			D3DSWAPEFFECT_DISCARD,
			kWindow,
			TRUE,
			TRUE,
			D3DFMT_D24S8,
			0,
			0,
			D3DPRESENT_INTERVAL_DEFAULT,
		};

		IDirect3D9* pContext = Direct3DCreate9(D3D_SDK_VERSION);
		if(pContext)
		{
			IDirect3DDevice9* pDevice = NULL;

			HRESULT hResult = pContext->CreateDevice(nAdapter, D3DDEVTYPE_HAL, kWindow, nFlags, &kParameters, &pDevice);
			if(SUCCEEDED(hResult))
			{
				eCode = InitializeVertexTypes(pDevice);
				if(eCode == Error_Success)
				{
					*pInstance = new GraphicsDevice(pContext, pDevice, kParameters);
				}

				pDevice->Release();
			}

			pContext->Release();
		}
	}

	return eCode;
}

/*
 * Destroy
 */
void GraphicsDevice::Destroy(GraphicsDevice*& pDevice)
{
	//NOTE: If I ever want to have more than one device this would be an issue
	TerminateVertexTypes();

	delete pDevice;
	pDevice = NULL;
}

/*
 * SetViewport
 */
ErrorCode GraphicsDevice::SetViewport(const D3DVIEWPORT9& kViewport)
{
	return SUCCEEDED(pDevice->SetViewport(&kViewport)) ? Error_Success : Error_Fail;
}

/*
 * GetViewport
 */
ErrorCode GraphicsDevice::GetViewport(D3DVIEWPORT9* pViewport)
{
	return SUCCEEDED(pDevice->GetViewport(pViewport)) ? Error_Success : Error_Fail;
}

/*
 * Begin
 */
void GraphicsDevice::Begin(uint32 nColor, float fDepth, uint32 nStencil)
{
	pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, nColor, fDepth, nStencil);
	pDevice->BeginScene();
}

/*
 * End
 */
void GraphicsDevice::End()
{
	pDevice->EndScene();
	pDevice->Present(NULL, NULL, NULL, NULL);
}

/*
 * DrawTriangles
 */
void GraphicsDevice::DrawTriangles(IDirect3DVertexDeclaration9* pDeclaration, IDirect3DVertexBuffer9* pBuffer, uint32 nSize, uint32 nCount)
{
	pDevice->SetVertexDeclaration(pDeclaration);
	pDevice->SetStreamSource(0, pBuffer, 0, nSize);
	pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, nCount);
}