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

Author Topic: sf::Sprite.setTextureRect possible bug?  (Read 7994 times)

0 Members and 2 Guests are viewing this topic.

Symphonym

  • Newbie
  • *
  • Posts: 32
    • View Profile
sf::Sprite.setTextureRect possible bug?
« on: August 28, 2012, 08:08:28 pm »
So I decided to move my tile textures onto a spritesheet instead of having separate images. So I loaded the spritesheet texture to the sprite of my tile, set the textureRect of that sprite and this bug occurs.

I believe that the TextureRect is leaking, and here's why:

As I'm moving my character vertically, these spaces between tiles occasionally flickers (Took me ages to actually get one of these flickers on picture). For the record, this did NOT happen when I had separate textures. And I believe that the reason to why the lines are black/yellow is because my spritesheet looks like this:

The tiles below the ground tiles are just there so I can distinguish the tiles easier, and no, I haven't finished the tile textures, that's why the same tile is looped all over the tiles on the spritesheet. Anyway, as you can see the edges around the tiles below the "ground" tiles are indeed yellow and black, which the flickering lines in the game are.

Is this a bug or am I doing something wrong? Should I create separate textures from the spritesheet and store them in a vector or such?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Sprite.setTextureRect possible bug?
« Reply #1 on: August 28, 2012, 08:38:38 pm »
It's not a bug, this is the intended behaviour. It happens when your sprite is at decimal coordinates.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Sprite.setTextureRect possible bug?
« Reply #2 on: August 28, 2012, 08:44:42 pm »
It might be an artifact resulting from OpenGL rasterization, like here. Does the problem disappear if you round the coordinates of sprite position and origin before drawing?

Laurent, it might be intended if you know the underlying mechanisms, but not from user perspective. One doesn't assume that pixels outside the texture rect are rendered. Is the behavior already mentioned somewhere in the documentation/tutorials or do you think it occurs too rarely?
« Last Edit: August 28, 2012, 08:50:33 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Sprite.setTextureRect possible bug?
« Reply #3 on: August 28, 2012, 08:54:01 pm »
Quote
Is the behavior already mentioned somewhere in the documentation/tutorials or do you think it occurs too rarely?
It is mentioned in the tutorials that don't exist yet ;D
Laurent Gomila - SFML developer

Symphonym

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: sf::Sprite.setTextureRect possible bug?
« Reply #4 on: August 28, 2012, 08:55:42 pm »
Alright, if it's not a bug. What would be the optimal way of making a workaround? In other words, having a spritesheet, - the flickering

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Sprite.setTextureRect possible bug?
« Reply #5 on: August 28, 2012, 10:24:12 pm »
Try to align the sprite's position and origin to integral coordinates. Otherwise, leave a free pixel between the texture rects (the pixel has alpha value 0 or is masked with a color).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Symphonym

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: sf::Sprite.setTextureRect possible bug?
« Reply #6 on: August 29, 2012, 07:04:25 pm »
Try to align the sprite's position and origin to integral coordinates. Otherwise, leave a free pixel between the texture rects (the pixel has alpha value 0 or is masked with a color).

As I'm using very small pixel graphics I'm using a sf::View to scale things up. This also makes pixels much "bigger", so rounding off position to integers makes to sprite jump between different points etc, which I don't like at all. And leaving a free pixel on all sides of each tile (on the spritesheet) with the alpha value of 0 only gives the "lines" another color, this time the background color of the SFML window.

At the moment it feels like the only choice is to have sepearate images, but that will get very messy and just unorganized. Is there any other way to use spritesheets? :/

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: sf::Sprite.setTextureRect possible bug?
« Reply #7 on: August 29, 2012, 07:11:52 pm »
Don't scale the view then... It's anways not the best way to get such an effect. If (for some reason) you don't want to scale the original images to the needed size, then scale the sprite to the size you want. So you have a better precision while keeping the sprites scaled. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Symphonym

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: sf::Sprite.setTextureRect possible bug?
« Reply #8 on: August 29, 2012, 07:35:09 pm »
Don't scale the view then... It's anways not the best way to get such an effect. If (for some reason) you don't want to scale the original images to the needed size, then scale the sprite to the size you want. So you have a better precision while keeping the sprites scaled. ;)

But still.. I don't really like having a small thing like sprite scaling deciding whether or not I use a spritesheet :s

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Sprite.setTextureRect possible bug?
« Reply #9 on: August 29, 2012, 10:00:06 pm »
And leaving a free pixel on all sides of each tile (on the spritesheet) with the alpha value of 0 only gives the "lines" another color, this time the background color of the SFML window.
Do you mean they get the window clear color, even when something has already been rendered before? That's strange, normally pixels with alpha value 0 shouldn't be rendered. Do you use a blend mode other than sf::BlendAlpha?

What happens if you use a color mask with sf::Image::createMaskFromColor()? You need to load the sf::Texture indirectly via sf::Image.
« Last Edit: August 29, 2012, 10:01:56 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: