Sign in to follow this  
Tispe

[DX9] Copy LPDIRECT3DDEVICE9 adress

Recommended Posts

Hello :)

I have two classes, one Device class and one SpriteHandling class.

My LPDIRECT3DDEVICE9 device interface is a member of the device class, in this class I also have the LPD3DXSPRITE interface to initialize the sprite COM using:
[code]
D3DXCreateSprite(device, &sprite)
[/code]

Now my SpriteHandler class will load all textures and meta data., however the D3DXCreateTextureFromFile() requires that I pass the device. Instead of having to keep passing my GraphicsDevice object into the SpirteHandler object I would much rather have the device adress as a member available.

Which brings the question if this is proper:
WinMain:
[code]
DEVICEOBJECT GraphicsDevice();
SPRITEHANDLER Sprites(GraphicsDevice.GetDevice());
[/code]

[code]
LPDIRECT3DDEVICE9 DEVICEOBJECT::GetDevice()
{
return device;
}
[/code]

[code]
void SPRITEHANDLER::InitHandler(LPDIRECT3DDEVICE9 _device)
{
device = _device;
return;
}
[/code]

Class:
[code]
class SPRITEHANDLER
{
public:
SPRITEHANDLER(LPDIRECT3DDEVICE9 device)
{
//init stuff
InitHandler(device);
}
~SPRITEHANDLER()
{
//release stuff
if(TestSprite)
TestSprite->Release();
}

void InitHandler(LPDIRECT3DDEVICE9 device);
void LoadSprite();


protected:
LPDIRECT3DDEVICE9 device; //Copy device adress in here
LPDIRECT3DTEXTURE9 TestSprite;
};
[/code]

where device is a LPDIRECT3DDEVICE9 member in the SpriteHandler class. The passed _device is from the GraphicsDevice class where it is initialized.

Share this post


Link to post
Share on other sites
One thing I would add to this is to AddRef your device in SPRITEHANDLER::InitHandler and Release it in the appropriate place (probably the destructor for your SPRITEHANDLER). This would follow proper COM convention and make sure that the reference to device in SPRITEHANDLER remains valid even if something else releases it.

Don't forget OnLostDevice and OnResetDevice for your ID3DXSprite - it creates some resources of it's own behind the scenes (namely a vertex buffer and an index buffer).

Share this post


Link to post
Share on other sites
Alternately, rather than use raw pointers, use a smart pointer like [url=http://msdn.microsoft.com/en-us/library/ezzw7k98(v=VS.100).aspx]CComPtr[/url] and don't worry about explicit AddRef() and Release() calls.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this