将图像加载到纹理中(sf::Image如果使用SFML 1.6,或者sf::Texture使用SFML 2.0),然后为sprite设置子矩形.像这样(使用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);
Run Code Online (Sandbox Code Playgroud)
对于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);
Run Code Online (Sandbox Code Playgroud)
请注意,您可能希望禁用图像/纹理的平滑处理,具体取决于您使用精灵的方式.如果不禁用平滑,边缘可能会出血(如texture.setSmooth(false)或image.SetSmooth(false)).
| 归档时间: |
|
| 查看次数: |
4834 次 |
| 最近记录: |