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

Author Topic: Peculiar issue with transforming a sprite. (Solved.)  (Read 2884 times)

0 Members and 1 Guest are viewing this topic.

Assassin0795

  • Newbie
  • *
  • Posts: 19
    • View Profile
Peculiar issue with transforming a sprite. (Solved.)
« 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.
« Last Edit: January 12, 2014, 09:31:34 pm by Assassin0795 »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Peculiar issue with transforming a sprite.
« Reply #1 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.
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Re: Peculiar issue with transforming a sprite.
« Reply #2 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 docs show what you need to do.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Assassin0795

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Peculiar issue with transforming a sprite.
« Reply #3 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?
« Last Edit: January 12, 2014, 09:00:10 pm by Assassin0795 »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Peculiar issue with transforming a sprite.
« Reply #4 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.
Back to C++ gamedev with SFML in May 2023

Assassin0795

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Peculiar issue with transforming a sprite.
« Reply #5 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.