views:

80

answers:

1

This is kind of a COM question to do with DirectX.

So, both ID3DXSprite and ID3DXFont and a bunch of the other ID3DX* objects require you to call OnLostDevice() when the d3d device is lost AND OnResetDevice() when the device is reset.

What I want to do is maintain an array of all ID3DX* objects and simply call OnResetDevice() and OnLostDevice() on each whenever the device is lost or reset.

However I can't seem to find a BASE CLASS for the ID3DX* classes... they all seem to COM-ually inherit from IUnknown.

Is there a way to do this or do I have to maintain separate arrays of ID3DXFont* pointers, ID3DXSprite* pointers, etc?

+1  A: 

There isn't a common base class, sorry.

You could use multiple inheritance and templates to sort of achive what you want. Something like this (untested but hopefully you'll get the idea)...

#include <d3dx9.h>
#include <vector>

using namespace std;

class DeviceLostInterface
{
public:
    virtual void onLost() = 0;
    virtual void onReset() = 0;
};

template <typename Base>
class D3DXWrapper : public Base, public DeviceLostInterface
{
public:
    virtual void onLost() { Base::OnLostDevice(); }
    virtual void onReset() { Base::OnResetDevice(); }
};


int main()
{
    // Wouldn't be set to null in real program
    D3DXWrapper<ID3DXSprite>* sprite = 0;
    D3DXWrapper<ID3DXFont>* font = 0;

    vector<DeviceLostInterface*> things;
    things.push_back(sprite);
    things.push_back(font);

    // This would be a loop...
    things[0]->onLost();
    things[1]->onLost();

}

This sort of meets your requirements, but to be honest I don't really think it's very useful. You'd either need some way to know what to cast each item back to, or keep the pointers in a type specific list anyway and then you might as well just write separate code to reset each type anyway.

John Burton