• Advertisement
Sign in to follow this  

Sharing the LPDIRECT3DDEVICE9 across a project

This topic is 2514 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement