How do I create modular/replaceable game/engine code interfaces with C++?

I've spent some time when I can spare it experimenting with c++ and at the same time learning a bit about game programming. I like most thought I'd start with creating a simple game engine but with some experimenting and plenty of advice along the lines of create a game, not an engine I forgot about the engine and just experimented with building a game.

Suffice to say in the process of doing that I hacked together rendering code (ogre3d) and physics (bullet) and input/output (OIS) and a couple of times over experimented with basic game ideas to the point of loading a map and being able to walk around it. Nothing special I know but it's an achievement for me.

Moving on from that now I would like to learn more about creating reusable code that falls more under engine programming than just a game. I'm sure the skills will be transferable to more than just games.

Basically I'd like to move from having everything intertwined to being able to separate and replace the individual parts. For example, build a 3d renderer dll/so in OpenGL and another with DirectX (as a dll). Or the same for other aspects such as Physics, sound, input, networking.

I'm learning about general dll programming and shared libraries on Linux but would like some more information with regards to game specific interfaces. I am also aware that Ogre3d which I previously mentioned is already cross platform and capable of both opengl and direct-x... I'm not saying I want to replace a library like that, more make it able to be unplugged and have something else plugged in instead.

So you are aware. My programming experience has been C with some more recent exposure to C++. Mostly recently I have been doing web development with both PHP and C#.NET (mostly php). I've also got average experience with c#, python, perl, etc. I'm a bit of a jack of all trades and I pick up things pretty quickly. What I'm hoping for is not to be spoon fed the code but rather some good places to research the concepts etc. (Although I won't turn down code if you're offering) ;)

You are answering pretty much to you own question. You write to, or create your own interfaces. And then implement them using different methods.

For exmaple:

interface IDraw
{
public void DrawPoint(int x, int y);
}

class DirectXDraw : IDraw
{
public void DrawPoint(int x, int y)
{
// direct x code
}
}

class OpenGLDraw : IDraw
{
public void DrawPoint(int x, int y)
{
// open gl code
}
}

class Game
{
private IDraw drawEngine;

public Game(IDraw drawEngine)
{
this.drawEngine = drawEngine;
}

public void Draw()
{
// use drawEngine here
}
}


basically this is not much different from any other area, this is a common approach in object-oriented programming. In C++ AFAIK abstract classes with pure virtual functions are used in place of interfaces.

• Thanks. Sounds like I'm thinking on the right track anyway. :) – Tim May 1 '12 at 9:18
• This is a terrible paradigm for C++ because now every call goes through a virtual function even though you don't need any runtime polymorphism - a choice at compile or link time (which includes dynamic linking at program startup) is enough. – user744 May 1 '12 at 16:33
• @JoeWreschnig is it terrible only in performance aspect? Unfortunatelly I don't really know C++ to provide some equivalent to C# interfaces. – Petr Abdulin May 1 '12 at 16:51
• AFAIK, yes, in performance aspect. Given your pattern, I think every language (including C#) will suffer the same performance issue (but in different scale). It is OK for most programs, but unfortunately, CPU-intensive game is not one of them. – javaLover May 1 '19 at 5:18