所以我得到了这样的tileet:Tileset
如何在SFML中仅从其中加载一个图块?
最佳答案
将图像加载到纹理中(如果使用SFML 1.6,则为sf::Image
;如果使用SFML 2.0,则为sf::Texture
),然后为该图片设置子矩形。像这样(使用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);
对于SFML 1.6,它更像是这样:
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);
请注意,您可能要禁用图像/纹理的平滑处理,具体取决于您使用精灵的方式。如果不禁用平滑,则边缘可能会出血(例如
texture.setSmooth(false)
或image.SetSmooth(false)
)。