dimanche 1 février 2015

Design pattern to handle Direct3D COM object initialization and usage

I'm looking for a design pattern that will handle Direct3D efficiently and gracefully. Am I on the right track or does anyone have any other suggestions


Making the assumption that the class referred to, cD3D, handles set up and disposal of a set of COM objects relating to Direct3D functionality, including but not limited to ID3D11Device, ID3D11DeviceContext, IDXGISwapChain and ID3D11RenderTargetView, is using the following structure memory safe? Is it efficient? Is it even a sensible way of doing this?



//I've emitted a few things that do exist for readability, including but not limited to
// the destructor and copy constructor

class cMain {
public:
cMain(HINSTANCE hInstance, LPTSTR applicationName);
private:
void Render();
ID3D11DeviceContext& m_devCon;
IDXGISwapChain& m_swapChain;

HINSTANCE m_hInstance;
HWND m_hWnd;
LPTSTR m_applicationName;

cD3D m_d3d;
};

cMain::cMain(HINSTANCE hInstance, LPTSTR applicationName)
:
m_hInstance(hInstance),
m_applicationName (applicationName),
m_hWnd(CreateNewWindow(hInstance, applicationName)),
m_d3d(m_hWnd)
{
m_devCon = m_d3d.GetDevCon();
m_swapChain = m_d3d.GetSwapChain();
}

void cMain::Render()
{
float colour[4] = {0.2f, 0.4f, 1.0f, 1.0f};
devCon.ClearRenderTargetView(&renderTarget, colour);
swapChain.Present(0, 0);
}


I have several worries - including but not limited to reference counts, making sure objects are disposed of correctly, look ups (I don't want an endless stream of look ups I'm constantly having to do). I guess I'm looking for a little bit of guidance on sensible, robust, efficient and safe ways to handle these elements of the game, that stray away from the raw pointers local to WinMain() found in a lot of DirectX programming guides.


Is the above a good way of doing it? If so, why? If not, why not? I'm hoping for reusable knowledge that I can apply in future.


Aucun commentaire:

Enregistrer un commentaire