SpriteSheet in v1.0

Started by Nafffen, 04 March 2023, 09:59:42

Nafffen

I wanted to use SpriteSheet class,
I found that there was a class here:
https://tgui.eu/documentation/v0.6/classtgui_1_1SpriteSheet.html
But I can't find a similarity in v1.0.
Therefore, I wanted to implement it myself with a custom widget.
To set the visible part of the sprite I use this function:
m_sprite.setVisibleRect(tgui::FloatRect(m_indexSpriteSheet % m_tileSizeSheet.x * m_sizeOneSprite.x, m_indexSpriteSheet / m_tileSizeSheet.x * m_sizeOneSprite.y, m_sizeOneSprite.x, m_sizeOneSprite.y));However, I can't find in the doc if the value for FloatRect object are in pixel or in range [0,1] ?
In both case, I can't get the sprite to have expected behavior although values seem correct with cout
I don't understand, does it refer to texture size or sprite size ?
For example with a sprite [100,100] and a texture [2000,2000]. If a want to draw the part [0,0,50,50] of texture on sprite, how do I do ?
Thank you

texus

#1
setVisibleRect isn't what you need. It was added to the sprite just for being able to partially display the loading bar image, it effectively clips part of your sprite (so the size of the sprite would still be the entire loading bar while only part of the bar is shown).

Sprites and textures do work a bit differently in TGUI than in other libraries. I think originally the current Sprite and Texture classes were even a single class.
A sprite always shows the entire texture.
A texture on the other hand may contain only part of an image.

What you need to do is load multiple Texture objects, with the same filename, but with different partRect parameters.
That may sound very inefficient, but it works because TGUI will internally reuse the texture when loading the same filename multiple times. So you would end up with only one internal sf::Texture (containing the full 2000x2000 image), and multiple tgui::Texture objects that refer to (part of) the same resource.

All rectangles used by sprites and textures use pixel ranges.
If your image is 2000x2000, the PartRect of your Texture is (0,0,50,50) and the sprite size is 100x100 then the top 50x50 part of your image will be 2x scaled and shown as 100x100 on your screen.

Nafffen

Thank you I managed to have what I wanted with your advices