3
\$\begingroup\$

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 -- 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 techniques 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!

\$\endgroup\$
2
  • \$\begingroup\$ Griffin, I answered this when you asked on SO. The answer is the same as Josh's... \$\endgroup\$ Jul 9, 2011 at 4:44
  • \$\begingroup\$ i put the answer on both sites right away, hoping i'd get an answer on 1 \$\endgroup\$
    – Griffin
    Jul 9, 2011 at 6:53

2 Answers 2

3
\$\begingroup\$

I don't think you can do this directly using SFML. It looks like the shape class will not support texture mapping; allegedly it is a planned feature for the next version (discussion).

Depending on the kind of visual effect you are going for -- I didn't find your description entirely clear -- you could do something like using a shape to mask a sprite, as described in this post (scroll down a ways, there's an image with the example result), or you could use OpenGL directly to render a series of texture-mapped triangles with the image mapped appropriately using texture coordinates.

Note that while sprites are rectangular in their nature, you can achieve the visual result of a non-rectangular image by including transparency (typically via the alpha channel of the image), and that may be all you need to achieve your desired visual result.

\$\endgroup\$
0
3
\$\begingroup\$

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 ));
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .