• Advertisement
Sign in to follow this  

Separating API specific code from the rest of the framework

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

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

Share this post


Link to post
Share on other sites
Advertisement

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.

Edited by kunos

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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
 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement