Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Well I'm learning DX11 and I hope someday I can start a simple 3D engine. I'm following tutorial series and I realised that I have no need to create any class to mantain my code (the tutorials also uses this procedural approach but I don't copy-paste the tutorial code, I try to understand the tutorial and then try to code the new stuff in my way), I can have the code 'understable' and working well with just functions and global variables. So I wonder, when I have enough knowledge of the API and I want to start creating my own library, could this procedural approach have some downside? should I start writing some classes?

Thanks.

share|improve this question

1 Answer 1

The only real reason to write API code in classes vs. free functions (I hope you're grouping them in namespaces!) is so that you can swap in concrete API code if you decide to switch implementations.

To do this, you have interfaces for all your API-specific objects (renderer, mesh, texture, etc.).Program all your high-level code (like models, lights, materials) to these interfaces.

// The renderer interface.
// Your game code should work with this and not the concrete implementations.
class IRenderer
{
public:
    virtual void Render() = 0;
};

Now, write concrete implementations for these interfaces, one implementation per API.

// The Direct3D 11 renderer.
class D3DRenderer : public IRenderer
{
public:
    virtual void Render()
    {
        // Clears the screen.
        context->ClearRenderTargetView(renderTargetView, { 0.0f, 0.5f, 1.0f, 1.0f });

        // Render the rest of the scene...
    }

private:
    ID3D11DeviceContext *context;
    ID3D11RenderTargetView *renderTargetView;
    // Other API-specific data...
};

// The OpenGL renderer.
class OpenGLRenderer : public IRenderer
{
public:
    virtual void Render()
    {
        // Clears the screen
        glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Render the rest of the scene...
    }
};

Somewhere in your engine, you also have some sort of abstract factory that is able to create all of these concrete implementation objects for you. When initializing the game, you pick which API to use and set up your factory to create the objects that conform to that API. Your high-level code can now work with objects like meshes and textures without needing to know which specific API you're using.

// The abstract factory.
class IEngineFactory
{
    virtual IRenderer *CreateRenderer() = 0;
};

// The D3D11 factory.
class D3DEngineFactory
{
    virtual IRenderer *CreateRenderer()
    {
        return new D3DRenderer();
    }
};

// The OpenGL factory.
class OpenGLEngineFactory
{
    virtual IRenderer *CreateRenderer()
    {
        return new OpenGLRenderer();
    }
};

This is useful for porting your code to other platforms. For example, your Windows binary uses DirectX and your Unix binary uses OpenGL.

share|improve this answer
    
I really enjoy your explanation because I didn't use this approach and, although I've heard about the abstract factory pattern, I haven't used it yet... but, if I want to (in the future obviously) create an engine that works just for DX11 then what would be the difference between using classes and just functions? –  German Dec 23 '12 at 8:17
    
If you know you're only going to be using DX11, I'd favour writing all your API code in free functions inside namespaces. There's no real difference, but I prefer just being able to call all my API functions without worrying about passing around an object everywhere. –  Boreal Dec 23 '12 at 15:18
    
that's all what I need to know, thanks! –  German Dec 23 '12 at 15:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.