class Renderer { public: virtual void RenderText(std::string text, int x, int y, float rotation) = 0; virtual void RenderSprite(Sprite& sprite, int x, int y, float rotation) = 0; virtual void Resize(int x, int y) = 0; }; class SFMLRenderer : Renderer { public: virtual void RenderText(std::string text, int x, int y, float rotation) { /* implementation */ } virtual void RenderSprite(Sprite& sprite, int x, int y, float rotation) { /* implementation */ } virtual void Resize(int x, int y) { /* implementation */ } } class AnotherRenderer : Renderer { public: virtual void RenderText(std::string text, int x, int y, float rotation) { /* implementation */ } virtual void RenderSprite(Sprite& sprite, int x, int y, float rotation) { /* implementation */ } virtual void Resize(int x, int y) { /* implementation */ } };
enum RenderType { SFML; Another; }; Renderer* CreateRenderer(RenderType type /* and whatever other params you want */) { switch(type) { case SFML: return new SFMLRenderer(/*params*/); case Another: return new AnotherRenderer(/*params*/); default: /* throw exception */ } };
Renderer* myRenderer = CreateRenderer(SFML); for(/*all of the sprites in my game*/) { myRenderer.RenderSprite(/*params*/); }
Call Me Jimmy wrote: » I'm creating a game in C++ but I have designed the logical classes so they are independent of the renderer (in this case a 2d library, SFML). I did this so I can easily port the game to a 3d engine later without having to change the game code.
Call Me Jimmy wrote: » My question is, whether I should implement wrapper classes on top of the renderable game objects. At the moment, I am writing SFML specific classes which derive from my base classes. However all these really do is implement one function - Render(), which does the SFML specific rendering. Is this the best way to do things? Can anyone with cross-platform development experience shed light on what the best practice is? Thanks