SFML community forums

Help => Graphics => Topic started by: djarkan on May 12, 2023, 03:24:23 am

Title: animation class bad coord drawing
Post by: djarkan on May 12, 2023, 03:24:23 am
hi

i wrote an animation class.
the animation plays well but not where it should that seems to be drawn at coords * 2

can't find what i'm doing wrong. did cout to see coords in console and they are goods

minimal code representing a sprite

main.cpp
#include <SFML/Graphics/RenderWindow.hpp>
#include "bitmap/animation/animation.hpp"
#include "mysprite.hpp"

int main()
{
    sf::RenderWindow window(sf::VideoMode(600, 600), "tests divers", sf::Style::Close);
    MySprite mysprite;
    mysprite.m_texture.loadFromFile("countdown.png");
    sf::Vector2f coords(275,275);
    mysprite.setPosition(coords);

    sf::Vector2f framecoord = mysprite.getPosition();
    float LeftX = framecoord.x;
    float RightX = framecoord.x + 49;
    float topY = framecoord.y;
    float bottomY = framecoord.y + 49;

    mysprite.m_frame[0].position = sf::Vector2f(LeftX, topY);
    mysprite.m_frame[1].position = sf::Vector2f(RightX, topY);
    mysprite.m_frame[2].position = sf::Vector2f(RightX, bottomY);
    mysprite.m_frame[3].position = sf::Vector2f(LeftX, bottomY);

    mysprite.m_frame[0].texCoords = sf::Vector2f(0, 0);
    mysprite.m_frame[1].texCoords = sf::Vector2f(49, 0);
    mysprite.m_frame[2].texCoords = sf::Vector2f(49, 49);
    mysprite.m_frame[3].texCoords = sf::Vector2f(0, 49);

    while(1) {
        window.clear();
        window.draw(mysprite);
        window.display();
    }
}

animation .cpp
#include "mysprite.hpp"

bool MySprite::loadTexture(std::string& fileName)
{
    return m_texture.loadFromFile(fileName);
}

void MySprite::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    states.transform *= getTransform();
    states.texture = &m_texture;
    target.draw(m_frame, 4, sf::Quads, states);
}
 

animation.hpp
#ifndef MYSPRITE_CPP
#define MYSPRITE_CPP

#include <SFML/Graphics.hpp>

class MySprite : public sf::Drawable , public sf::Transformable {
    public :
        bool                                        loadTexture(std::string& fileName);

        sf::Vertex                                  m_frame[4];
        sf::Texture                                 m_texture;
    private :

        virtual void                                draw(sf::RenderTarget& target, sf::RenderStates states) const;
};

#endif // MYSPRITE_CPP
 

image
https://ibb.co/Ry4TYN6
Title: Re: animation class bad coord drawing
Post by: djarkan on May 12, 2023, 04:07:34 am
must be with the transform because when i remove
states.transform *= getTransform();
from the draw it's ok for coords but of course it's no longer a transformable

how do i fix ?

thx
Title: Re: animation class bad coord drawing
Post by: eXpl0it3r on May 13, 2023, 02:31:27 pm
You are setting the frame (i.e. vertex position) based off the sprite position.
If you want to animate the sprite, then you should set the vertex position based off the texture position/size, which is (0,0) at the top left and goes to (width,height) at the bottom right.

You also might wrap this inside the class and not have member variables exposed publicly.
Title: Re: animation class bad coord drawing
Post by: djarkan on May 13, 2023, 04:21:20 pm
thx

didn't realised that setpostion() and vextex coord are already the same

working good