#include "GameGraphics.h"

#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

GameGraphics::GameGraphics(void)
{
	m_d3d = NULL;
	m_d3dDevice = NULL;
	m_d3dSprite = NULL;
}

GameGraphics::~GameGraphics(void)
{
	Destroy();
}

void GameGraphics::Create(HWND hWnd)
{
	m_d3d = Direct3DCreate9(D3D_SDK_VERSION);

	D3DCAPS9 caps; m_d3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
	DWORD createflag;
	if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
		createflag = D3DCREATE_HARDWARE_VERTEXPROCESSING;
	else
		createflag = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

	D3DPRESENT_PARAMETERS pp = {0, };
	pp.BackBufferWidth = 400;
	pp.BackBufferHeight = 400;
	pp.BackBufferCount = 1;
	pp.BackBufferFormat = D3DFMT_A8R8G8B8;
	pp.hDeviceWindow = hWnd;
	pp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
	pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	pp.Windowed = true;

	m_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, createflag, &pp, &m_d3dDevice);

	D3DXCreateSprite(m_d3dDevice, &m_d3dSprite);
}

void GameGraphics::Destroy()
{
	SAFE_RELEASE(m_d3dSprite);
	SAFE_RELEASE(m_d3dDevice);
	SAFE_RELEASE(m_d3d);
}

void GameGraphics::Clear()
{
	m_d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, 0xff000000, 1, NULL);
}

void GameGraphics::Present()
{
	m_d3dDevice->Present(NULL, NULL, NULL, NULL);
}

void GameGraphics::Begin() { m_d3dDevice->BeginScene(); }
void GameGraphics::End() { m_d3dDevice->EndScene(); }

