Search code examples
c++c++14sfml

How to set `sf::Drawable` positions sfml


I'm trying to declare a sf::Drawable * property inside my class body.

the code i already wrote:

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

class View{
protected:
    sf::Drawable *view;
};

and inside the class constructor, I want to use the view properties:

class View{
public:
    View(){ view->...}
protected:
    sf::Drawable *view;
}

but I cannot access any of the sf::Drawable methods. I get the No member named 'setPosition' in 'sf::Drawable' warning from IDE.

the only code suggestion i get from code completer is:

draw(RenderTarget& target, RenderStates states)


Solution

  • Indeed, sf::Drawable doesn't have a setPosition method. You could instead use sf::Transformable*, which does have setPosition.

    If you need to have drawable properties, then...

    1. If all your items are either shapes/sprites/texts, consider walking down the inheritance chain and use sf::Shape, sf::Sprite, or sf::Text.
    2. Make a separate class for each (ShapeView, SpriteView, TextView).
    3. Use dynamic_cast, as suggested by TheMedicineSeller. But this will incur a small runtime cost.