I am writing an engine for a 2D platformer based on an entity-component-system approach using SFML. Until now, I had a Graphics component with a pointer to sf::Sprite. This sf::Sprite would get drawn by the Rendering System on each update.
Recently I realized that I might want to draw other kinds of elements (Shapes and Text) during gameplay, ideally without changing the Rendering System. So, instead of a pointer to sf::Sprite, I decided that the Graphics component should have a pointer to sf::Drawable. The problem is that in order to draw these elements in the right location I need to invoke their setPosition() method. This is not possible polymorphically because that method belongs to class Transformable.
This could be easily solved. We could have a new class, say Node, that inherits from Drawable and Transformable and stands above Shape, Sprite and Text on the hierarchy.
class Node : public Drawable, public Transformable {
}
Instead of inheriting from Drawable and Transformable, classes Shape, Sprite and Text would then inherit from Node.
class SFML_GRAPHICS_API Shape : public Node{
//...
class SFML_GRAPHICS_API Sprite : public Node{
//...
class SFML_GRAPHICS_API Text : public Node{
//...
This way, the Graphics component can hold a pointer to sf::Node, which can behave polymorphically as a Shape, a Sprite or Text, exposing the properties that they have in common as subclasses of Drawable and Transformable.
Of course, the drawback is that I have to modify the code of SFML itself. I've tested it and so far I've found no problems, but you can never be sure...
So the questions are:
- Do you think this is a good solution?
- Could this change to the source code bring problems that are not evident at first sight?
- Would this be a good addition to the SFML Graphics architecture?
Edit: I just saw that a very similar proposal was posted here:
http://en.sfml-dev.org/forums/index.php?topic=14157.msg99316#msg99316What eXpl0it3r proposes might be a good alternative, although it could take gymnastics to implement cleanly in an entity-component framework.