SFML community forums

Help => General => Topic started by: bobbyport on June 29, 2009, 02:32:30 am

Title: Getting a sprite to draw a shape
Post by: bobbyport on June 29, 2009, 02:32:30 am
This is what I've got right now: http://codepad.org/udmkNHX9

I would like the node object to be responsible for drawing its sprite image as well as its origin (using a circle).

I could easily have it draw multiple sprites (one for its actual image the other for its origin) but I would prefer using a shape.


When I try getting the node class to inherit from sf::Sprite and sf::Shape I receive the following error:
Quote

error C2594: 'argument' : ambiguous conversions from 'Node' to 'const sf::Drawable &'
Title: Getting a sprite to draw a shape
Post by: Laurent on June 29, 2009, 07:53:28 am
sf::Sprite and sf::Shape both inherit from sf::Drawable, that's why the compiler is confused.

You shouldn't inherit from them. Your class is not a sprite or a shape, it is implemented using a sprite and a shape. Public inheritance in C++ is a strong design choice, it should really be used carefully.

But what you can do is inherit from sf::Drawable and override the Render function to draw your sprite and shape.
Title: Getting a sprite to draw a shape
Post by: bobbyport on June 29, 2009, 11:39:56 pm
Could you give me an example?

The sf::Drawable does not have a Render method.
Title: Getting a sprite to draw a shape
Post by: Laurent on June 30, 2009, 12:05:36 am
It does.

Code: [Select]
class Node : public sf::Drawable
{
    virtual void Render(RenderTarget& Target) const
    {
        Target.Draw(mySprite);
        Target.Draw(myShape);
    }

    sf::Sprite mySprite;
    sf::Shape myShape;
};

Node node;
window.Draw(node);