Making my game independent from graphic API in C++

Viewed 1706

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) ?

4 Answers
Related