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

Author Topic: Inverted Texture Rect explanation  (Read 2613 times)

0 Members and 1 Guest are viewing this topic.

Fugan

  • Newbie
  • *
  • Posts: 5
    • View Profile
Inverted Texture Rect explanation
« on: February 22, 2012, 12:04:22 am »
From what I've read, inverted texture rects should be working, but I can't seem to get the behaviour I want.

For example, assuming a texture is 64x64, w and h are the width and height values, sprite is an initialised sf::sprite, the origin is in the top left default position, and the texture has been properly set (all tested)

Based on what I know about how this all works this code
Code: [Select]
sprite.SetTextureRect(sf::InteRect(w, 0, -w, h) );
should generate a flipped sprite, since I am setting the origin of the sub-rect for the texture to 0, 64 and creating a rectangle that goes backward 64 pixels and down 64 pixels, which should generate a texture that is mirrored on the Y axis.  

What I actually get when I draw the sprite, it that it is translated -w pixels on the x axis from the position.  i.e., if the aforementioned sprite is located at (128,128) the sprite will render as if it was located at (64,128).  The rendered sprite is also not mirrored.

(I have a solution that works, using:
Code: [Select]
sprite.SetTextureRect(sf::InteRect(w, 0, -w, h) );
sprite.SetScale(-1,1);

I like this solution better than setting the origin to (w/2, h/2) since I am a top-left kinda guy.  In this case I understand how the SetScale() function works.  I am looking for an explanation as to why the inverted texture rect behaves the way it does).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Inverted Texture Rect explanation
« Reply #1 on: February 22, 2012, 08:26:50 am »
The sprite's right coordinate is left + width. So you easily understand why it ends up being 64 in your case ;)

Also, the texture must be in repeat mode (SetRepeated(true)).

Quote
From what I've read, inverted texture rects should be working

In fact I never tested it, that was just an idea to investigate. So don't expect it to be perfect, or working at all.
Laurent Gomila - SFML developer

Fugan

  • Newbie
  • *
  • Posts: 5
    • View Profile
Thanks
« Reply #2 on: February 22, 2012, 05:12:42 pm »
I've spent the night looking through the source and I see exactly what you mentioned.  I forgot to mention that I have tried with repeating textures on and off.

Is sf::Sprite likely to change between now and the SFML 2 release?  The only reason I ask is that I am taking advantage of the current behaviour for an inverted texture rect, combined with negative scaling, to "flip" my textures and I'd hate for all that code to break in the next update when I could exercise a little patience instead.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Inverted Texture Rect explanation
« Reply #3 on: February 22, 2012, 07:02:06 pm »
No, sf::Sprite won't change.
Laurent Gomila - SFML developer

 

anything