I want to create an array that will contain all the sprites, text and shapes that will be drawn onto the window and my question is how can I make this array both sf::Drawable and sf::Transformable?
Asked
Active
Viewed 347 times
0
-
Possible duplicate: http://stackoverflow.com/questions/17910297/inheriting-from-transformable-and-drawable-in-sfml?rq=1 – Greg M May 01 '16 at 12:30
-
While not a solution for your coding problem, you may want to take a look at Entity systems (EntityX for example) . Those solve the problems you get when you use inheritance for this problems. – nvoigt May 01 '16 at 16:44
1 Answers
2
You would need to create a class inheriting both Drawable
and Transformable
. Then you would be able to create an array of that class.
class Obj : public sf::Drawable, public sf::Transformable
{
// class code
}
// somewhere in code...
std::array<Obj, ARRAY_SIZE> arr;
Make sure you correctly implement Drawable
and Transformable
.
Here is a link to the official documentation.
One way to implement these classes would be something along the lines of:
class Obj : public sf::Drawable, public sf::Transformable
{
public:
sf::Sprite sprite;
sf::Texture texture;
// implement sf::Drawable
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(sprite, states); // draw sprite
}
// implement sf::Transformable
virtual void SetPosition(const MyVector& v) const
{
sprite.setPosition(v.x(), v.y());
}
}
And then in your code you can directly draw and transform the class.
// somewhere in code
// arr = std::array<Obj, ARRAY_SIZE>
for (auto s : arr) {
window.draw(s);
}

Greg M
- 954
- 1
- 8
- 20