direct3d device in a texture manager

Started by
6 comments, last by paic 18 years, 10 months ago
Hi, I'm currently implementing a texture manager, and I'm stuck with this question : how to handle the pointer to the device ? at first, I did have a LPDIRECT3DDEVICE9 m_pd3dDevice member in the texture manager, with the 4 functions like OnCreate, etc. The problem is that even if I have only 1 instance of the manager (I use a singleton) the OnCreateDevice, OnDestroy will be called many times, and i'm not sure if it's right ... the second option I thought about is : I pass the pointer to the device every time I call the Load function (e.g. instead of m_pTexture = m_pTexManager->Load("texture.jpg"); I'll have m_pTexture = m_pTexManager->Load(m_pd3dDevice, "texture.jpg"); What do you think it's best ?
Advertisement
Passing a device pointer for each function call gets pretty annoying after awhile. Also, if you are trying to make a wrapper for D3D, it doesn't work too well, since all of the D3D stuff is still exposed.

I recommend that you just store it once (in your constructor or init() function), so that you can internally use it whenever you need it. Some D3DX interfaces to this - like ID3DXSprite and ID3DXFont.
Dustin Franklin ( circlesoft :: KBase :: Mystic GD :: ApolloNL )
hi piac,
what circlesoft says is really true. It gets annoying.
There are really a few ways you could do this... the elegant ways and then the messy ways.. having a copy of the device all over the place is really a bad idea and ideally you must only have one instance of the device.

With this said. It would be nice to just pass it as a parameter... if it's only 1 - 3 parameters. Look at how direct3d handles then ... in direct3d you will see that most of the methods take parameters and that's a good start.

I would suggest passing it down as a parameter
Quote:Original post by Armadon
There are really a few ways you could do this... the elegant ways and then the messy ways.. having a copy of the device all over the place is really a bad idea and ideally you must only have one instance of the device.

Remember, you are not having more than one device - just multiple pointers to that device. It's not a bad idea to keep pointers to the device - just look at how all of the D3D functions work. The interfaces store references internally.
Dustin Franklin ( circlesoft :: KBase :: Mystic GD :: ApolloNL )
I am not sure how your graphic layer is set up but you can use a static function in your wrapped D3D class which can then be called anywhere without having to instance the class.

What I do to solve that problem as I hated having to pass it all over the place is I created a singleton graphic layer class. That class contains a function call Graphics() which is static which reports back to me a LPDIRECT3DDEVICE7 through a pointer to the instance of the singleton. This way anywhere in my engine all I need is:

#include <cGraphicLayer.h>

void SomeRenderFunctionForAUnit()
{
::Graphics()->DrawPrimitives(...)
}
The way that I'm currently implementing variables (like the Direct3D device and such) is like this:
#define TMERR_FAILEDLOAD -1class TextureManager{    static IDirect3DDevice9* m_pGfxDevice;    std::vector<IDirect3DTexture9*> m_Textures;public:    TextureManager(){}    ~TextureManager(){}    void RegisterGfxDevice(IDirect3DDevice9* pDevice)    {        m_pGfxDevice = pDevice;    }    HANDLE Load(const char* pFileName, TEXTURE_CREATION_INFO* pInfo)    {        IDirect3DTexture9* pTexture = 0;        D3DXCreateTextureFromFile(m_pGfxDevice, pFileName, &pTexture);        m_Textures.push_back(pTexture);        if(pTexture)            return m_Textures.size() - 1;        else            return TMERR_FAILEDLOAD;        return 0;    }    // other functions here};
Well thats how I used to do it, I found that method a waste of time compared to what I tried to explain above.
cool, first time I get so much explicit replies, thanks a lot everyone ^^

I like the method where the texture manager keep a copy of the device internally, and you register it with a register(IDirect3DDevice9 *device) method. I think I'll go this way, it just leave me we the choice : where do I handle the registering of my manager, but that's not really a problem ^^

Anyway, thanks again ^^

This topic is closed to new replies.

Advertisement