1

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

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

4

1 回答 1

6

将图像加载到纹理中(sf::Image如果使用 SFML 1.6,或者sf::Texture如果使用 SFML 2.0),然后为精灵设置子矩形。像这样的东西(使用 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))。

于 2012-04-26T20:48:01.610 回答