This project has moved and is read-only. For the latest updates, please go here.

Access d3dDevice object from another class

Mar 24, 2015 at 4:20 PM
In the “Loading a texture” tutorial, the Game class holds the m_d3dDevice, and m_texture member variables.

The cat texture is then loaded by using CreateDDSTextureFromFile(m_d3dDevice.Get(), L"", nullptr, &m_texture);, where m_d3dDevice is the already created d3d device object.

I am wondering how one would go about creating a totally separate class to hold the cats texture and load it. For instance, I create
#include "Game.h"
class CatLoader
    ID3D11ShaderResourceView *m_texture;
    Microsoft::WRL::ComPtr<ID3D11Device> m_d3dDevice;

        HRESULT hr = DirectX::CreateDDSTextureFromFile(m_d3dDevice.Get(), L"", nullptr, &m_texture);

But this does not work, because the m_d3dDevice private member variable is not the same m_d3dDevice we initialized earlier.

In my game code, where I am tinkering with DirectX TK, the tutorial, and game states, essentially my CatLoader class is buried quite deep, e.g. I have
Game::Initialize() which calls gameManager.initialize(), then gameManager.initialize() calls CatCreator.initialize(), and CatCreator.initialize() calls CatLoader(), which is as above...

How would I go about getting the correct m_d3dDevice object into my CatLoader() private member fields, so when it calls

HRESULT hr = DirectX::CreateDDSTextureFromFile(m_d3dDevice.Get(), L"", nullptr, &m_texture);

It has the correct d3dDevice?

Thanks for your help.
Mar 24, 2015 at 6:53 PM
You need to pass a ID3D11Device* as part of your constructor.

If you actually need to keep a reference to the device for some other functionality, use:
    CatLoader( ID3D11Device* device ) : m_d3DDevice(device)
        HRESULT hr = DirectX::CreateDDSTextureFromFile(m_d3dDevice.Get(), L"", nullptr, &m_texture);
If you don't actually need the device for anything else, then just use it locally to your constructor:
// delete Microsoft::WRL::ComPtr<ID3D11Device> m_d3dDevice; from your class

    CatLoader( ID3D11Device* device )
        HRESULT hr = DirectX::CreateDDSTextureFromFile( device, L"", nullptr, &m_texture);
Also, you should be using a ComPtr for your texture object too:
    Microsoft::WRL:::ComPtr<ID3D11ShaderResourceView> m_texture;
Marked as answer by walbourn on 3/24/2015 at 10:53 AM
Mar 25, 2015 at 7:54 AM
Hi walbourn,

Thanks for your reply, I now understand that we must pass the created d3d11device object to any class that uses it for d3d11device functions such as load texture or draw...

I am just having a slight problem now, if I update CatLoader constructor to read:
    CatLoader(ID3D11Device* device)
        HRESULT hr = DirectX::CreateDDSTextureFromFile(device, L"", nullptr, &m_texture);
And then in Game::Initialize use
CatLoader ss(m_d3dDevice);
I get the error:
"no instance of constructor "CatLoader::CatLoader" matches the argument list argument types are: (Microsoft::WRL::ComPtr<ID3D11Device>)"

But if I change the constructor to expect a ComPtr (and m_texture) e.g.
    CatLoader(Microsoft::WRL::ComPtr<ID3D11Device> device)
        HRESULT hr = DirectX::CreateDDSTextureFromFile(device, L"", nullptr, &m_texture);
I get an intellisense error under CreateDDSTextureFromFile as
"no instance of overloaded function "DirectX::CreateDDSTextureFromFile" matches the argument list argument types are: (Microsoft::WRL::ComPtr<ID3D11Device>, const wchar_t [8], std::nullptr_t, Microsoft::WRL::Details::ComPtrRef<Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>>)

Thanks for your help on this issue.
Mar 25, 2015 at 6:34 PM
Edited Mar 25, 2015 at 6:35 PM
You should read my page on ComPtr usage.

The best solution is:
CatLoader(ID3D11Device* device) ...

CatLoader ss(m_d3dDevice.Get());
You certainly can use:
CatLoader(Microsoft::WRL::ComPtr<ID3D11Device> device) ...

CatLoader ss(m_d3dDevice);
But the second form is doing a useless reference-count increment followed by reference-count decrement which is unnecessary.

You may want to review the tutorial material as well.