SFML community forums

General => General discussions => Topic started by: escape on June 15, 2020, 08:42:27 pm

Title: Multiple IntRects in a single Sprite
Post by: escape on June 15, 2020, 08:42:27 pm
I'm trying to do level gen but I want to have a single sprite for the level. I have a sprite sheet and i would like to assign different textures to different "areas" of a sprite.

sprite.setTextureRect(sf::IntRect(0, 0, 32, 32));
sprite.setTextureRect(sf::IntRect(32,32,32,32));
 

The code above I know would replace the texture rect but is there a way that I can have both?
Title: Re: Multiple IntRects in a single Sprite
Post by: Hapax on June 15, 2020, 08:50:36 pm
A sprite is a single area of a texture but there are ways to draw multiple areas at once.

The first - and probably easiest - way is to just have multiple sprites with one for each area of the texture.

The second is more powerful but a bit more complex: vertex array.
The idea here is that each quad can have its own area of a texture. With a vertex array, you can have multiple quads as one object (a single draw call and all transformations apply to all quads together).

Take a look at the vertex array tutorial (https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php) to see how it works.
Specifically, take extra note of the tile map example part (https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php#example-tile-map).
Title: Re: Multiple IntRects in a single Sprite
Post by: G. on June 15, 2020, 08:52:23 pm
Yes, what Hapax said.

However if for some weird reason you want to draw everything with the same sprite, you can set the texture rect, draw, set a different texture rect, draw, etc.