2017-02-14 59 views
0

我有一组简单的类。其中一个是ButtonComponent,我希望在视觉方面实质上使用sf::RectangleShape,然后ButtonComponent的其余部分将处理诸如事件监听器之类的事情。问题的形式是,如果我从父类继承sf::Drawable我必须实现draw()Component类),这意味着子类实现父母draw()而不是SFML形状draw()函数。有没有重组两种课程的方式或调用sf::RectangleShapes绘图函数的方法?调用sf :: RectangleShapes绘制函数?

Component.h

class Component : public sf::Drawable { 
public: 
    Component(); 
    virtual ~Component(); 
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0; 
}; 

class ButtonComponent : public sf::RectangleShape, public Component { 
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states); 
}; 
+0

“是否有某种方法重构两个类或调用'sf :: RectangleShapes'绘制函数的方法?”什么阻碍你实际调用'sf :: RectangleShapes :: draw()'? –

+0

它是一个私人函数... – Matthew

+0

它是私有的,因为它应该只由渲染目标调用。如果你想绘制一个drawable,你必须调用相反的一个:'myRenderTarget.draw(myDrawable);'这会在内部调用drawable的draw()成员。 – Mario

回答

-2

的解决办法是让你的孩子类只继承sf::Drawable并有一个sf::RectangleShape你的子类的内部。类似于下面的东西

class Component : public sf::Drawable { 
public: 
    Component(); 
    virtual ~Component(); 
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0; 
}; 

class ButtonComponent : public Component { 
public: 
    ButtonComponent(const sf::Vector2f &size = sf::Vector2f(0, 0)); 

    void draw(sf::RenderTarget& target, sf::RenderStates states) const; 

    void setSize(const sf::Vector2f& size); 
    const sf::Vector2f& getSize() const; 
    virtual unsigned int getPointCount() const; 
    virtual sf::Vector2f getPoint(unsigned int index) const; 
    void setTexture(const sf::Texture* texture, bool resetRect = false); 
    void setTextureRect(const sf::IntRect &rect); 
    void setFillColor(const sf::Color &color); 
    void setOutlineColor(const sf::Color &color); 
    void setOutlineThickness(float thickness); 
    const sf::Texture* getTexture() const; 
    const sf::IntRect& getTextureRext() const; 
    const sf::Color& getFillColor() const; 
    const sf::Color& getOutlineColor() const; 
    float getOutlineThickness() const; 
    sf::FloatRect getLocalBounds() const; 
    sf::FloatRect getGlobalBounds() const; 
    void setPosition(float x, float y); 
    void setPosition(const sf::Vector2f &position); 
    void setRotation(float angle); 
    void setScale(float factorX, float factorY); 
    void setScale(const sf::Vector2f& factors); 
    void setOrigin(float x, float y); 
    void setOrigin(const sf::Vector2f& origin); 
    const sf::Vector2f& getPosition() const; 
    float getRotation() const; 
    const sf::Vector2f& getScale() const; 
    const sf::Vector2f& getOrigin() const; 
    void move(float offsetX, float offsetY); 
    void move(const sf::Vector2f& offset); 
    void rotate(float angle); 
    void scale(float factorX, float factorY); 
    void scale(const sf::Vector2f& factor); 
    const sf::Transform& getTransform() const; 
    const sf::Transform& getInverseTransform() const; 
private: 
    sf::RectangleShape _btnShape; 
};