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

Author Topic: SFML2.0 - Sprite flipping does not work  (Read 2829 times)

0 Members and 1 Guest are viewing this topic.

Chi

  • Newbie
  • *
  • Posts: 25
    • View Profile
SFML2.0 - Sprite flipping does not work
« on: March 06, 2013, 09:45:41 pm »
Hello all,

i read in the post from Laurent that its possible to flip a sprite using textureRec with negative values.

with this my sprite is showing:

pSprite.setTextureRect(sf::IntRect(9,7,21,38));

after Laurents post i think i can use
pSprite.setTextureRect(sf::IntRect(9,7,-21,-38));

for flipping my sprite, but my sprite is disappear and not flipped.
Did i understand sth. wrong?

best regards

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML2.0 - Sprite flipping does not work
« Reply #1 on: March 06, 2013, 09:50:42 pm »
You mustn't blindly apply the negative width/height, you have to think about what result you get. If left = 9 and width = -21, what's the right coordinate? It's 9 - 21 = -12. So to keep the same rectangle, you must set the left coordinate to be the right. In this case, "left" should be 30 so that the other coordinate (the actual left) is still 9 when you add the width (-21).

In other words, when you invert width and height you must consider that the members "left" and "top" of the rectangle are actually the right and the bottom.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML2.0 - Sprite flipping does not work
« Reply #2 on: March 06, 2013, 09:52:03 pm »
Well you missed his point entirely. ;)

// flip X
sprite.setTextureRect(sf::IntRect(width, 0, -width, height));

So your code should be:

Flipped along the x axis:
pSprite.setTextureRect(sf::IntRect(21+9,7,-21,38));

Flipped along the y axis:
pSprite.setTextureRect(sf::IntRect(9,38+7,21,-38));

Both:
pSprite.setTextureRect(sf::IntRect(21+9,38+7,-21,-38));
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Chi

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: SFML2.0 - Sprite flipping does not work
« Reply #3 on: March 06, 2013, 10:09:17 pm »
Hello Laurent and eXpl0it3r,

first, thanks for your help.

i trying to understand. If i flipp my Sprite, i must use the x/y coord. from the "opposite" site and then negative values for hight and width.So the x/y coord. is 30/45,  i have check this out by a graphic tool. If using now

pSprite.setTextureRect(sf::IntRect(30,45,-21,-38));

my sprite is still disappear as before.
Did i still understand sth. wrong? - sorry my english is not the best  :D . . .

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML2.0 - Sprite flipping does not work
« Reply #4 on: March 06, 2013, 10:21:06 pm »
Seems to be working smoothly here. ;)

#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 600), "Test");

    sf::Texture tex1;
    tex1.loadFromFile("test.png");

    sf::Sprite spr;
    spr.setTexture(tex1);
    spr.setTextureRect(sf::IntRect(9,7,21,38));

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
                        spr.setTextureRect(sf::IntRect(9,7,21,38));
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
                        spr.setTextureRect(sf::IntRect(21+9,38+7,-21,-38));

        window.clear(sf::Color::Blue);
        window.draw(spr);
        window.display();
    }
}
 

[attachment deleted by admin]
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Chi

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: SFML2.0 - Sprite flipping does not work
« Reply #5 on: March 06, 2013, 10:31:06 pm »
Hello eXpl0it3r,

iam using your code and your test.png. I get exactly the same problem here as in my code and my png ... the sprite disappears. What the hell :( . . .

iam stumped.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML2.0 - Sprite flipping does not work
« Reply #6 on: March 06, 2013, 10:35:56 pm »
Which revision of SFML 2 are you using?
Laurent Gomila - SFML developer

Chi

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: SFML2.0 - Sprite flipping does not work
« Reply #7 on: March 06, 2013, 10:41:50 pm »
Hey Laurent,

i download the lib from the main site:

C++ | version 2.0 RC -> Windows 32 bits - Visual C++ 2010 (11.4 MB)

Chi

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: SFML2.0 - Sprite flipping does not work
« Reply #8 on: March 06, 2013, 11:16:42 pm »
Hey Laurent,

its me again. Ive download now the nightly build (latest 2013-02-19  SFML 2.0) from eXpl0it3r's Signature, and with this everything works fine.

i hope this help you (?) - and thanks all for the help.

best regards

Chi
« Last Edit: March 06, 2013, 11:20:56 pm by Chi »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML2.0 - Sprite flipping does not work
« Reply #9 on: March 06, 2013, 11:29:33 pm »
and with this everything works fine.
I'm glad it works again!

i hope this help you (?) - and thanks all for the help.
Since my Nightly Builds are newer than the RC release, it was a problem that seems to have been already fixed. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/