1
Graphics / 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.
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.