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

Author Topic: SFML 2.0 flip question [SOLVED]  (Read 4680 times)

0 Members and 2 Guests are viewing this topic.

sandorlev

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
SFML 2.0 flip question [SOLVED]
« on: February 24, 2013, 10:16:47 pm »
Hey guys!

I found a forum post explaining how to flip a sprite in SFML 2.0, but it doesn't seem to be working well in my game and I can't find where I went wrong. The problem is, it only flips the sprite once and it won't ever flip it back to original. Here is the code:

// The flip function works well
void Flip(sf::Sprite& sprite)
{
        sprite.setTextureRect(
                sf::IntRect(
                        sprite.getLocalBounds().width,
                        0,
                        -sprite.getLocalBounds().width,
                        sprite.getLocalBounds().height
                )
        );
}

while (window.isOpen())
{
        vector.x = 0;
        vector.y = 0;

        // Input
        while (window.pollEvent(event))
        {
                // ...
        }

        // Same thing with the right key, you get the idea
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                vector.x -= 7;

                if (direction != LEFT)
                {
                        direction = LEFT;
                        Flip(sprite);
                }
        }

        // ...

        sprite.setTexture(
                spriteAnimTextures[spriteAnim.GetCurrentFrame()]
        );
}

Now, as I said, after the first flip to the right, it stops working. What might be the error here?
« Last Edit: February 25, 2013, 12:43:28 am by sandorlev »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: SFML 2.0 flip question
« Reply #1 on: February 24, 2013, 10:43:37 pm »
Use getTextureRect() instead of getLocalBounds(), width and height are always positive in bounds so -width is always negative.
Back to C++ gamedev with SFML in May 2023

sandorlev

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: SFML 2.0 flip question
« Reply #2 on: February 25, 2013, 12:29:01 am »
Use getTextureRect() instead of getLocalBounds(), width and height are always positive in bounds so -width is always negative.

Now the sprite turns invisible instead of simply not turning back when I flip the second time.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: SFML 2.0 flip question
« Reply #3 on: February 25, 2013, 12:34:13 am »
Your new left should be "left + width", or something like that.

sandorlev

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: SFML 2.0 flip question
« Reply #4 on: February 25, 2013, 12:43:16 am »
Your new left should be "left + width", or something like that.

Thanks, I have no idea why I didn't realize that!

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: SFML 2.0 flip question [SOLVED]
« Reply #5 on: February 28, 2013, 10:08:34 pm »
Related question: is it faster to use  flip function or to have a flipped version of all animations in the same texture?
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML 2.0 flip question [SOLVED]
« Reply #6 on: February 28, 2013, 10:30:24 pm »
Questions related to performance are mostly always strongly tight to the use case...

The texture has to get drawn anyways if it's not drawn flipped or not shouldn't cost you anything.
If you have spritesheets, the movement of the texture rect also shouldn't cost you anything.
Thus if you really, really, really, really have to know, write a benchmark and messure it on your own, on different machines with different compilers and different OS. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: SFML 2.0 flip question [SOLVED]
« Reply #7 on: February 28, 2013, 10:35:44 pm »
I have spritesheets. I guess it's memory/speed problem again. For example if I have walkingRight animation, do I just flip every frame during run time when I want to use it for walkingLeft animation? That way I can use a smaller spritesheet. But on the other hand flipping operation will be slower than setTextureRect operation, I guess?
« Last Edit: February 28, 2013, 10:39:55 pm by moarthenfeeling »
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML 2.0 flip question [SOLVED]
« Reply #8 on: February 28, 2013, 10:40:59 pm »
I've no idea how you flip the image, but if I had to do it, I'd call setTextureRect in both cases, so...

Thus if you really, really, really, really have to know, write a benchmark and messure it on your own, on different machines with different compilers and different OS. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: SFML 2.0 flip question [SOLVED]
« Reply #9 on: February 28, 2013, 10:45:50 pm »
Yeah, I guess I'm worrying too much sometimes... Thanks  ;)
I'll try some tests if this becomes an issue in the future.
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

 

anything