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

Author Topic: [SOLVED]Problem with transformation in own drawable  (Read 1471 times)

0 Members and 1 Guest are viewing this topic.

DarkYoung

  • Newbie
  • *
  • Posts: 8
    • View Profile
[SOLVED]Problem with transformation in own drawable
« on: July 25, 2013, 09:35:46 pm »
Hello,

i'm trying to write my own animation class. My class derive from sf::Drawable and sf::Transformable. Problem is, when i use "setPosition" for example "setPosition(32,48)" animation is rendered at 64,96. There's some code:

void ANIMATION_BASE::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    if(m_state!="") {
        std::map<const sf::String, ANIMATION_SET>::const_iterator it;
        it=m_animationsSet.find(m_state);
        sf::Vector2f pos;
        pos=getPosition();

        sf::VertexArray vertex_array(sf::Quads, 4);
        vertex_array[0].position=pos;
        vertex_array[1].position=pos+sf::Vector2f(it->second.frameSize.x, 0);
        vertex_array[2].position=pos+sf::Vector2f(it->second.frameSize.x, it->second.frameSize.y);
        vertex_array[3].position=pos+sf::Vector2f(0, it->second.frameSize.y);

        sf::FloatRect frame;
        if(it->second.orientation) {
            frame.left=it->second.textureRect.left+(m_frame*(it->second.frameSize.x));
            frame.top=it->second.textureRect.top;
        } else {
            frame.left=it->second.textureRect.left;
            frame.top=it->second.textureRect.top+(m_frame*(it->second.frameSize.y));
        }
        frame.width=it->second.frameSize.x;
        frame.height=it->second.frameSize.y;


        vertex_array[0].texCoords=sf::Vector2f(frame.left, frame.top);
        vertex_array[1].texCoords=sf::Vector2f(frame.left+frame.width, frame.top);
        vertex_array[2].texCoords=sf::Vector2f(frame.left+frame.width, frame.top+frame.height);
        vertex_array[3].texCoords=sf::Vector2f(frame.left, frame.top+frame.height);

        states.transform*=getTransform();
        states.texture=m_texture;
        target.draw(vertex_array,states);
    }
}

Everything is ok when i delete this line:
states.transform*=getTransform();

But if i do this, i cant rotate, scale etc.

It's my first time with Vertex so maybe i do something wrong  :-[

EDIT:
Finally i found whats wrong. Problem is here:

pos=getPosition();

I was thinking that i need to set vertices in global coords. And now i just realize that i need to set everything locally and then magically everything is rendered in right place when i do "window.draw(animation); ". This Library is awesome!

EDIT2:
Topic can be closed ;)

EDIT3:
Title changed
« Last Edit: July 26, 2013, 08:55:59 am by DarkYoung »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with transformation in own drawable
« Reply #1 on: July 25, 2013, 10:35:43 pm »
Could you please edit your post to add something at the beginning and/or in the title, so that people know you solved your problem before reading your post entirely ;)
Laurent Gomila - SFML developer