Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making my game independent from graphic API in C++

I would like to make my game independant from the underlying graphic API used (in order to reduce coupling in case I want to port it to another platform).

I'm currently using SDL, and initially I wanted to encapsulate the SDL_Renderer in a GraphicAPI_SDL class, implementing my generic GraphicAPI interface. This API would have create Image_SDL (encapsulating an SDL_Texture), which implements my generic Image interface.

My problem is that, when I want to render an Image using my GraphicAPI instance, the underlying GraphicAPI_SDL has to cast the Image into an Image_SDL, in order to obtain the SDL_Texture. And such a cast is ugly.

What is the best way to deal with that type of situation ? I've though about storing every SDL_Texture loaded in a vector inside my GraphicAPI_SDL, with every instance of Image having just an integer corresponding to the index of the texture in the vector, so that only the GraphicAPI_SDL class uses SDL directly, but is there a better way to proceed (with a pattern for instance) ?

like image 918
thomasc Avatar asked Dec 13 '13 21:12

thomasc


1 Answers

If you are using SDL 2.0 (and are just making a 2D game) then you are already decoupled from the underlying Graphics API to a large extent. SDL 2.0 can automatically select the correct renderer (DirectX or OpenGL) for your system when you set up the window, your code shouldn't need to worry about it at all. However, if your game requires a lower level of drawing control (i.e, you're making a 3D game), you will need to handle the renderer by yourself.

For more information, see here: http://wiki.libsdl.org/MigrationGuide#Video

like image 152
redsoxfantom Avatar answered Nov 01 '22 09:11

redsoxfantom