在SFML中从tileset加载一个tile

时间:2012-04-26 16:58:57

标签: c++ sfml tile

所以我得到了这样的tileset:Tileset

如何在SFML中只加载一个图块?

1 个答案:

答案 0 :(得分:6)

将图像加载到纹理中(如果使用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))。