view LightClone/Source/GraphicsDevice.cpp @ 61:1fe27776627e

Added asserts
author koryspansel
date Mon, 03 Oct 2011 15:05:09 -0700
parents efd2b1ca5b77
children
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)
{
	TRACE("GraphicsDevice::Create\n");

	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_NONE,
			0,
			D3DSWAPEFFECT_DISCARD,
			kWindow,
			TRUE,
			TRUE,
			D3DFMT_D24S8,
			0,
			0,
			D3DPRESENT_INTERVAL_DEFAULT,
		};

		TRACE("  Creating DirectX context\n");
		IDirect3D9* pContext = Direct3DCreate9(D3D_SDK_VERSION);
		if(pContext)
		{
			D3DCAPS9 kDeviceCaps;
			HRESULT hResult = pContext->GetDeviceCaps(nAdapter, D3DDEVTYPE_HAL, &kDeviceCaps);
			if(SUCCEEDED(hResult))
			{
				if(!(kDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY))
				{
					TRACE("Device does not support anistropic filtering\n");
				}

				if(kDeviceCaps.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY)
				{
					TRACE("Device does not support non-square textures\n");
				}

				if(!(kDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2))
				{
					if(!(kDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL))
					{
						TRACE("Device may not support non-power-of-two textures\n");
					}
					else
					{
						TRACE("Device does not support non-power-of-two textures\n");
					}
				}

				TRACE("Vertex Shader Version: %d\n", kDeviceCaps.VertexShaderVersion);
				TRACE("Pixel Shader Version: %d\n", kDeviceCaps.PixelShaderVersion);

				D3DDISPLAYMODE kMode;
				hResult = pContext->GetAdapterDisplayMode(nAdapter, &kMode);
				if(SUCCEEDED(hResult))
				{
					kParameters.BackBufferFormat = kMode.Format;

					DWORD nQuality = 0;
					hResult = pContext->CheckDeviceMultiSampleType(nAdapter, D3DDEVTYPE_HAL, kMode.Format, TRUE, D3DMULTISAMPLE_4_SAMPLES, &nQuality);
					if(SUCCEEDED(hResult))
					{
						kParameters.MultiSampleType		= D3DMULTISAMPLE_4_SAMPLES;
						kParameters.MultiSampleQuality	= nQuality - 1;
					}
					else
					{
						TRACE("4X FSAA is not supported\n");
					}

					TRACE("    Success!\n");
					IDirect3DDevice9* pDevice = NULL;

					TRACE("  Creating DirectX device\n");
					HRESULT hResult = pContext->CreateDevice(nAdapter, D3DDEVTYPE_HAL, kWindow, nFlags, &kParameters, &pDevice);
					if(SUCCEEDED(hResult))
					{
						TRACE("    Success!\n");
						TRACE("  Initializing vertex types\n");

						eCode = InitializeVertexTypes(pDevice);
						if(eCode == Error_Success)
						{
							TRACE("    Success!\n");
							*pInstance = new GraphicsDevice(pContext, pDevice, kParameters);
						}

						pDevice->Release();
					}
				}
				else
				{
					TRACE("Failed to acquire device capabilities\n");

				}
			}

			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);
	pDevice->SetStreamSource(0, NULL, 0, 0);
}