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

Author Topic: Strange behaviour with setTextureRect flip  (Read 3903 times)

0 Members and 1 Guest are viewing this topic.

hagel

  • Newbie
  • *
  • Posts: 23
    • View Profile
Strange behaviour with setTextureRect flip
« on: June 11, 2016, 07:02:55 pm »
I found a strange bug while working on my game today.
I had to flip my sprites for different directions. I used to use setScale with (-1,1) and it worked great, but I had to manage positions etc. I then found out about negative width on the setTextureRect function and started using it.

Here's the problem (SFML 2.3).
When drawing a flipped sprite ( SPR.setTextureRect(sf::IntRect(width, 0, -width, height); ) and setting this sprite's position to a half pixel (0.5f, 1.5f ... X.5f) does not use the correct rectangle. It actually offsets it one pixel to the right, but ONLY at half pixel positions. I assume this is some kind of rounding error.

But why would the sprite's position have any impact on the texture rectangle?
I temporarily fixed it by casting my sprite's x position to int. You can't draw to half pixels anyway.

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Strange behaviour with setTextureRect flip
« Reply #1 on: June 12, 2016, 04:33:42 pm »
Do you call sf::Texture::setSmooth

hagel

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Strange behaviour with setTextureRect flip
« Reply #2 on: June 12, 2016, 05:52:30 pm »
Yeah, it's set to false.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Strange behaviour with setTextureRect flip
« Reply #3 on: June 12, 2016, 05:56:14 pm »
If you want pixel accuracy, then don't draw on half-pixel positions. When something should be placed between pixels, OpenGL's rasterizer has to decide what it wants to show and chances are it will pick something that doesn't fit what you want.
I don't fully know how the rasterizer works, but since you have one big texture and try to cut out a part, it's not too far fetched, that when OpenGL decides what to use, it might think of the big texture as being drawn and then just picks the part you want. Which in combination with the half-pixel can lead to that the displayed texture is actually one pixel off.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Strange behaviour with setTextureRect flip
« Reply #4 on: June 14, 2016, 04:21:47 am »
SPR.setTextureRect(sf::IntRect(width, 0, -width, height);
Unsure of if this is just a typo but why do you use width for the horizontal position and zero for the vertical position?
It might just be a typo since there's also a bracket missing.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Strange behaviour with setTextureRect flip
« Reply #5 on: June 14, 2016, 08:00:43 am »
Quote
Unsure of if this is just a typo but why do you use width for the horizontal position and zero for the vertical position?
That's typically how you flip a rectangle on X axis. Left becomes right, and right becomes left.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Strange behaviour with setTextureRect flip
« Reply #6 on: June 14, 2016, 09:38:53 pm »
My mistake. The values are indeed the expected ones.
I read it as if the height was negative too.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything