Sign in to follow this  

Interface Question

This topic is 3741 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

Hiya, I'm having some trouble designing the parts of my game that use interfaces. The problem is that some game components need to use functions/data which are not declared in other interfaces, only in classes that derive from them. It's difficult to explain, so here's a simpler example:
// mesh interface - this is what the client code uses
class IMesh;

class Mesh : public IMesh
{
private:
    ID3DXMesh *m_pMesh; // or whatever format the mesh is stored as - this
                        //shouldn't be exposed to the client
};

// renderer interface - this is what the client code uses
class IRenderer
{
public:
    void Draw( const IMesh *pMesh ) = 0;
};

// renderer class
class Renderer : public IRenderer
{
public:
    void Draw( const IMesh *pMesh )
    {
        // this is where the problem is - the DirectX mesh object is not
        // exposed by the IMesh interface, but renderer needs it to draw...
        // how can I get around this?
    }
private:
    IDirect3DDevice9 *pDevice;
};

// usage would be something like this - only the inerfaces are used
void main()
{
    // use factory functions which return the 'concrete' classes
    IRenderer *pRenderer = CreateRenderer();

    IMesh = pRenderer->CreateMesh();
    pRenderer->Draw( pMesh );
}

I guess I'm not sure how to keep the implementation hidden, whilst still having each class be able to access the data it needs. Are there any common ways around this, or perhaps is my design just wrong? I don't see how libraries which only publish interfaces can work. Any help or suggestions would be very much appreciated [smile] James

Share this post


Link to post
Share on other sites
I think what you're asking for is something akin to the Bridge design pattern. Unfortunately the Bridge design pattern can be quite difficult to understand. Generally this involves working out what these implementation details that you require are, and moving those into the interface, instead of relying on low-level details.

Ultimately though, if you know that wherever you'll have a Renderer, you'll have a Mesh, you can just cast it... do some sort of check first however. :)

What you have is a bit of an edge case where you have parallel hierarchies that rely on each other to operate. Rendering is the canonical example of this because performance is tied closely to the implementation. But in 95% of other situations simple interfaces are typically sufficient for what you need to do and nothing requires the lower level details to operate.

Share this post


Link to post
Share on other sites

This topic is 3741 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.

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