Hey guys, I would like to start a discussion about why all things drawable inherit from transformable. To me it makes sense to use composition to give drawables a transform. E.g. sf::Sprite HAS A transform rather than sf::Sprite IS A transform.
First my use case:
- I want to have a hierarchy of transforms for my sprites or drawables.
- But I don't want that hierarchy to control the draw order
To that end I re-wrote those classes into what I call 'SmartDrawables and SmartTransformables'. They aren't really smart they just allow me to write this:
sf::SmartTransformable rootTransform;
sf::SmartTransformable someTransform;
sf::Sprite rootSprite(rootTransform, texture);
sf::Sprite someSprite(someTransform, texture);
rootTransform.addChild(someTransform);
// In an update somewhere.
rootTransform.rotate(0.1f);
rootTransform.updateTransforms(); // Goes through the heirarchy.
window.draw(someSprite); // Draw in whatever order we want, regardless of parent->child relationship.
window.draw(rootSprite);
So my questions:
- Is this a dumb idea and have I missed something obvious?
- Are there better ways to do this?
- Should SFML follow the 'Prefer composition over inheritance rule' in this situation?
A pic is attached, the blue one represents the child and is being rendered below the parent.