2

Hey i'm working with SFML right now, and upon finishing the tutorials i still do not know how to give a shape a texture or image, and not just a solid color/outline.

The only thing i know can take an image is a sprite, but thats WAY to simple, as it only allows you to render rectangular images in a rectangluar way!

What are the tecniques for rendering images onto shapes, and ONLY inside the shape? It would be great if some of you could provide some resources or SFML-specific stuff!

4

2 回答 2

1

2d:

As far as I know, with SFML 1.6 it isn't possible to use a separate image as a mask. You can, however, use the alpha channel of an image to draw a "shape" from it. This is supported by default; just give your image an alpha channel.

This post on the SFML forms verifies that using a separate image as a mask is not supported.

The author of SFML, Laurent Gomila, has posted some code that will allow you to mask using shapes, sprites, and strings. That can be found here: Masking using Sprites, Shapes or Strings. To get this working, however, you need to modify and recompile some parts of SFML.

3d:

With regards to using an image as a texture and mapping it to an object in 3d space, I don't think SFML has too much to help you with this besides setting up a window for you. There is a project on Google Code called sf3d that could maybe get you in the right direction, though.

于 2011-07-09T01:35:03.393 回答
1

SFML has been updated since this question was originally answered and you can now add textures to shapes easily. The shape class has the setTexture() and setTextureRect() methods. setTexture() takes a pointer to an sf::Texture. See the documentation.

Sf::Texture texture;
if (!texture.loadFromFile("mytexture.png"))
{
    std::cerr << "failed to load";
}
sf::RectangleShape myRect{ sf::Vector2f(width, height) };
myRect.setTexture(&texture);
myRect.setTextureRect(sf::IntRect( x, y, width, height ));
于 2018-12-23T04:04:51.947 回答