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!

  • \$\begingroup\$ Griffin, I answered this when you asked on SO. The answer is the same as Josh's... \$\endgroup\$ – Zack The Human Jul 9 '11 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 '11 at 6:53

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.

  • \$\begingroup\$ +1 Unfortunately for the OP this is the correct answer. It just isn't possible without writing custom classes or modifying the SFML source. \$\endgroup\$ – Zack The Human Jul 10 '11 at 20:47

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.setTextureRect(sf::IntRect( x, y, width, height ));

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.