Advertisement

Separating API specific code from the rest of the framework

Started by September 10, 2013 11:37 AM
5 comments, last by Satharis 11 years, 5 months ago

Hi guys,

So for the past few months I've been developing a little "pong meets arkanoid" game, learning directx and building a little framework as I go along. (I hesitate to call it an engine)

Though frustrating at times, I do find it immensely satisfying and educational to work with something I built, so I hope to keep exanding (and correcting) this code for future projects, which brings me to my question: I would like to make the code less dependent on API specific stuff (directx in particular) so that I could, for example, render the game in openGL should I wish to do so.

The trouble is that I'm not quite sure how to seperate the rendering code from the game objects.

I gather I would have a class called "render manager" or something like that, with a pointer to a "renderer" base class - similar to the 'strategy' design pattern. How does one isolate the actual drawing of the object from the game code? Right now my game objects all contain a pointer to a sprite object, which is rife with directX stuff.

I'm also now quite sure how to phrase this in a google query so I thought I would ask you guys.

Thanks for reading

it's quite simple actually.. all you do is identifying the minimum "graphics concepts" that your game need without calling the native API and then "wrap" these behind classes that expose the functionality you need.

So, for example, you might end up with a VertexBuffer class that wraps whatever calls to the API you need to manage vertex buffers.

At that point it's all about deciding how to implement the 2 versions... you can have the implementations deriving from a common interface.. ie. IVertexBuffer and residing in a dll.. or you can have static libs.

IMO, most of the times, there is no need for the dll approach.. when you build for a target system you just link the most appropriate static lib and call it a day.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

Advertisement

I think I get it - instead of having the sprite class contain a bunch of DX references it would contain pointers to interface classes? The actual implementation would then depend on the API I choose to render with. That's actually pretty simple. Thanks!

The key thing is to wrap it all around in a nice interface, going back to the vertex buffer you could have an interface that defines how to access it (create, map data, etc.) then for example you could have a GLVertexBuffer and an DXVertexBuffer class that derives from the interface. This way you hide the implementation and let your render system decide which one it wants to use.

You should separate implementation from interface. DirectX and OpenGL are implementation so should be in a file named like GLVertexBuffer, DXVertexBuffer or simple as GLMesh, DXMesh, etc. That allows you to create an attachable library, you could add dx or opengl shared library at runtime. The only downside is that interfaces will produce vtables, but thats not a big deal.

A can give you an example of how i do it:

(Interface)
https://github.com/Ghrum/Ghrum/blob/master/include/Event/IEventManager.hpp

(Implementation)
https://github.com/Ghrum/Ghrum/blob/master/include/Event/EventManager.hpp


Feel free to comment and star my project! <https://github.com/Ghrum/Ghrum>

Thanks guys - I really should have been able to think of this myself. I guess I was too busy figuring out how the dx stuff worked biggrin.png

Advertisement
As others have stated the way to do it is simply to abstract, think of the rendering like a big wall with a bunch of mail slots in it with labels over them. You know if you shove an object in a certain slot it will get handled appropriately, your code doesn't care who is behind the wall.

Then all you have to do is create the back room yourself, it can be as simple as going to where your engine creates a renderer object and change the type to one of multiple different ones. Thus you seperate the concrete implementation from the interface you use to work with it.

This topic is closed to new replies.

Advertisement