SFML community forums

Help => Graphics => Topic started by: MaeZer on May 27, 2015, 09:56:34 pm

Title: Question about textures and shapes
Post by: MaeZer on May 27, 2015, 09:56:34 pm
I would like to set the texture of a RectangleShape, but I don't understand how you do it like in the documentation. Can someone tell me how to do it please? I created my texture and rectangleshape but I don't know how you actually set it. My code doesn't seem to work.

sf::Texture rectex;
rectex.loadFromFile("resources/texture");

rectangle.setTexture(rectex);
 
Title: Re: Question about textures and shapes
Post by: Hapax on May 27, 2015, 09:59:22 pm
What doesn't work? What goes wrong?

You should be aware that the texture you're loading here is missing a file extension.
Title: Re: Question about textures and shapes
Post by: MaeZer on May 27, 2015, 10:03:47 pm
Oops I forgot the extension  :P Corrected it

It says after .setTexture "No suitable conversion function from sf::Texture to const sf::Texture* exists"
Title: Re: Question about textures and shapes
Post by: Hapax on May 27, 2015, 10:16:09 pm
Ah, unlike sf::Sprite's setTexture() which takes a reference to an sf::Texture, sf::RectangleShape's setTexture() takes a pointer to an sf::Texture.

Try:
rectangle.setTexture(&rextex);

Does that fix your problem?


It's actually sf::Shape that takes the pointer and sf::RectangleShape is derived from that.
Not sure why Shape takes a pointer while Sprite takes a reference. For consistency, they should be the same (preferably reference) if possible.
Title: Re: Question about textures and shapes
Post by: MaeZer on May 27, 2015, 10:19:59 pm
Aha, it works perfectly now thanks  :)
Title: Re: Question about textures and shapes
Post by: Hapax on May 27, 2015, 10:20:38 pm
You're welcome  :)
Title: Re: Question about textures and shapes
Post by: shadowmouse on May 27, 2015, 10:26:47 pm
Is there any reason why they take it differently, even though the function is used in the same way by the user?
Title: Re: Question about textures and shapes
Post by: eXpl0it3r on May 27, 2015, 10:31:51 pm
Because shapes don't have to have a texture, thus one needs to be able to set it to NULL, while sprites always need a texture.
Title: Re: Question about textures and shapes
Post by: Hapax on May 27, 2015, 10:46:56 pm
That makes sense. It's just confusing that they work differently.