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

Author Topic: animation class bad coord drawing  (Read 1529 times)

0 Members and 1 Guest are viewing this topic.

djarkan

  • Newbie
  • *
  • Posts: 16
    • View Profile
animation class bad coord drawing
« 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
« Last Edit: May 12, 2023, 02:13:14 pm by djarkan »

djarkan

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: animation class bad coord drawing
« Reply #1 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: animation class bad coord drawing
« Reply #2 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

djarkan

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: animation class bad coord drawing
« Reply #3 on: May 13, 2023, 04:21:20 pm »
thx

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

working good