Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - neondrop

Pages: [1]
1
I have a class called Actor which is intended to represent both rectangles and circles in my application. The class contains a member sf::Shape *mShape in which either a new sf::CircleShape or sf::RectShape is stored on construction of the object. My first question would be if this is actually the best approach or if I should make two extra classes for rectangles and circles that derive from Actor. I thought about this, but I first wanted to try it with one class since I didn't want to bloat the code with so much classes if I can avoid it, and also its easier to just loop through a vector of one class type. This approach worked fine until I needed to pass the mShape of an Actor to a function that specifically requires a pointer to sf::CircleShape and sf::RectangleShape. Here is some code:
class Actor : public sf::Transformable, public sf::Drawable {
    public:
    sf::Shape* mShape;

    Actor(float radius) {
        mShape = new sf::CircleShape(radius);
        //do stuff
    }

    Actor(float width, float height) {
        mShape = new sf::RectangleShape(sf::Vector2f(width, height));
        //do stuff
    }

    private:
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
        target.draw(*mShape, states);
    }
};

void foo(sf::CircleShape* circle, sf::RectangleShape* rect) {
    //do stuff
}

void moveActors() {
    foo(actors[0].mShape, actors[1].mShape);  
}
 
It doesn't work how I intend it to, which may be obvious to someone who has a deeper understanding of pointers and class inheritance than me. The compiler complains that conversion from sf::Shape* to sf::CircleShape* or sf::RectangleShape* is invalid.So my second question would be I should best go about this. I realize that the problem may lie in my understanding of C++ and pointers, but I hope it's specific enough to SFML that I can ask here.

Pages: [1]
anything