Sharing the LPDIRECT3DDEVICE9 across a project

Started by
4 comments, last by martinperry 13 years ago
Evening people, I am currently revisiting my old renderer to optimise its performance and came across something i remembered was a sticking point back when i first designed it, the sharing of the device across classes in a project. I.e. loading meshes, textures etc.. Currently its all done through pointers and parameters. But was just wondering how anyone else has approached this.
---Terence Burns---
Advertisement
i have a global namespace for these sort of things

globals.h:namespace Engine{    namespace DX    {	extern LPDIRECT3DDEVICE9 device;	extern D3DCAPS9 caps;    }    ... more global stuff}globals.cpp:    LPDIRECT3DDEVICE9 Engine::DX::device = NULL;    D3DCAPS9 Engine::DX::caps;


now you can access it from anywhere u include the header like this:

Engine::DX::device


auto-complete feature makes it easy to type. downside is the external declaration which is a little more typing. i would love to find a way to do it without.
I prefer to just pass in dependencies to a constructor or initialize function. It lets you avoid the various nastiness that can occur when use globals, and it makes it easy to keep track of a class's dependencies.
*reviving thread because i'm interested in the answers*
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
I also prefer to pass the device pointer around to classes constructors, and have the class AddRef() and Release() in the c'tor and d'tor. At work, we have a singleton for the renderer which contains the device pointer, which isn't much better than a global IMO.
I am trying to avoid global variables as much as possible... So everything is passed as pointers. I use Singleton fo some special classes like TextureManager or EffectManager, which are shared within whole engine... but DX device is created only on one place and then passed everywhere by pointers... Its better, you can have more devices, not just one, If you have more GPUs.

This topic is closed to new replies.

Advertisement