SFML community forums

Help => Graphics => Topic started by: Assassin0795 on January 12, 2014, 07:28:37 pm

Title: Peculiar issue with transforming a sprite. (Solved.)
Post by: Assassin0795 on January 12, 2014, 07:28:37 pm
Hi again,

Sorry for another topic so soon. While trying to test some behavior with sprites, I ended up running into a problem where I couldn't transform the sprite unless I overrode the respective transform methods in my Entity class (which inherits Drawable and Transformable), such as setPosition(float x, float y) or move(float x, float y).

Essentially, this is the layout of what I have that works - and if I want to manipulate the position of the Entity, I just call the respective function (if you'd like me to post the actual code I have, please just let me know):

class Entity : public Drawable, public Transformable {

private:

    sf::Sprite sprite;
    sf::Texture texture;
    sf::VertexArray vertices;

public:

    void setPosition(float x, float y) { sprite.setPosition(x, y); }
    // Other miscellaneous functions.

};

Previously, if I declared an Entity and just called something like Entity.setPosition() or Entity.getSprite().setPosition() (i.e. without overriding any transform methods), the transform wouldn't apply. I was looking through the documentation/tutorials for Transformable, Sprite, and Texture, and didn't see any reason as to why I'm getting this behavior unless I misunderstood the tutorials and sample code.

I greatly appreciate any assistance/advice on the matter.
Title: Re: Peculiar issue with transforming a sprite.
Post by: FRex on January 12, 2014, 08:02:01 pm
You need to multiply your transform with the one in sf::RenderStates in draw() from drawable. Show your Entity draw function.
Title: AW: Re: Peculiar issue with transforming a sprite.
Post by: eXpl0it3r on January 12, 2014, 08:24:57 pm
I was looking through the documentation/tutorials for Transformable, Sprite, and Texture, and didn't see any reason as to why I'm getting this behavior unless I misunderstood the tutorials and sample code.
You didn't look close enough then. The example in the Transformable (http://www.sfml-dev.org/documentation/2.1/classsf_1_1Transformable.php) docs show what you need to do.
Title: Re: Peculiar issue with transforming a sprite.
Post by: Assassin0795 on January 12, 2014, 08:51:20 pm
You need to multiply your transform with the one in sf::RenderStates in draw() from drawable. Show your Entity draw function.

void Entity::draw(sf::RenderTarget &target, sf::RenderStates states) const {
        states.transform *= getTransform();
        states.texture = &texture;
        target.draw(vertices, states);
}

My draw function was the same for both tests, and it was called in a render function where computer is an Entity (if I don't use getSprite(), nothing is rendered to the window):

void Manager::onRender() {
        mainWindow.clear(sf::Color::Black);

        mainWindow.draw(computer.getSprite());

        mainWindow.display();
}

Expl0it3r, I'm assuming you're also referring to what appeared to be my lack of a draw function?
Title: Re: Peculiar issue with transforming a sprite.
Post by: FRex on January 12, 2014, 08:58:01 pm
If computer is entity then its draw is NOT called, sprite's draw is called, if you want to have sprite both transformed by own transform and computers transform, draw it after vertices in draw and draw computer itself or pass a second argument to draw which will be render states with your transform from computer.
Title: Re: Peculiar issue with transforming a sprite.
Post by: Assassin0795 on January 12, 2014, 09:09:45 pm
If computer is entity then its draw is NOT called, sprite's draw is called, if you want to have sprite both transformed by own transform and computers transform, draw it after vertices in draw and draw computer itself or pass a second argument to draw which will be render states with your transform from computer.

This was the issue; thank you again, everyone.