Jump to content

  • Log In with Google      Sign In   
  • Create Account


We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!

Separating API specific code from the rest of the framework

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 molehill mountaineer   Members   -  Reputation: 595


Posted 10 September 2013 - 05:37 AM

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


#2 kunos   Crossbones+   -  Reputation: 2207


Posted 10 September 2013 - 05:45 AM

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, 10 September 2013 - 05:48 AM.

Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#3 molehill mountaineer   Members   -  Reputation: 595


Posted 10 September 2013 - 06:18 AM

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!

#4 Six222   Members   -  Reputation: 439


Posted 10 September 2013 - 07:54 AM

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.

#5 Wolftein   Members   -  Reputation: 167


Posted 10 September 2013 - 12:35 PM

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:



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

#6 molehill mountaineer   Members   -  Reputation: 595


Posted 10 September 2013 - 02:15 PM

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 

#7 Satharis   Members   -  Reputation: 1267


Posted 10 September 2013 - 02:37 PM

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.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.