Search code examples
c++texturessfml

How can I make this function return a sf::Texture? SFML2.0


void TileSheetManager::setTileSheet(const string textureName)
{
    texture.loadFromFile(textureName);
}

sf::Sprite TileSheetManager::getTile(int left, int top, int width, int height)
{

    sf::IntRect subRect;
    subRect.left = left * 32;
    subRect.top = top * 32;
    subRect.width = width;
    subRect.height = height;

    sf::Sprite sprite(texture, subRect);

    return sprite;
}

I need getTile() to return a sf::Texture yet I have no idea how I could do it.
I'm using SFML2.0 by the way.


Solution

  • According to the documentation here and here you should be able to

    sf::Image fullImage = texture.copyToImage();
    sf::Image croppedImage(width, height);
    croppedImage.copy(fullImage, 0, 0, subRect);
    sf::Texture returnTexture();
    returnTexture.LoadFromImage(croppedImage);