So I got tileset like this: Tileset
How do I load only one tile from it in SFML?
Load the image into a texture (either sf::Image
if using SFML 1.6, or sf::Texture
if using SFML 2.0), and then set the sub-rect for the sprite. Something like this (using SFML 2.0):
sf::Texture texture;
texture.loadFromFile("someTexture.png"); // just load the image into a texture
sf::IntRect subRect;
subRect.left = 100; // of course, you'll have to fill it in with the right values...
subRect.top = 175;
subRect.width = 80;
subrect.height = 90;
sf::Sprite sprite(texture, subRect);
// If you ever need to change the sub-rect, use this:
sprite.setTextureRect(someOtherSubRect);
For SFML 1.6, it's more like this:
sf::Image image;
image.LoadFromFile("someTexture.png"); // just load the image into a texture
sf::IntRect subRect;
subRect.Left = 100; // of course, you'll have to fill it in with the right values...
subRect.Top = 175;
subRect.Right = 180;
subrect.Bottom = 265;
sf::Sprite sprite(image);
sprite.SetSubRect(subRect);
Note that you may want to disable smoothing for the image/texture, depending on how you're using your sprites. If you don't disable smoothing, the edges may bleed (like texture.setSmooth(false)
or image.SetSmooth(false)
).