SFML community forums

Help => Graphics => Topic started by: Mithra on February 22, 2019, 05:14:44 pm

Title: Can a RenderTexture be resized?
Post by: Mithra on February 22, 2019, 05:14:44 pm
Hey guys!

I have a dynamic width requirement for a sprite generated from a RenderTexture, but redundant calls to rt.create don't seem to reinitialize it to the new width ( apparently I'm not supposed to do it this way )

Is there a proper way to destroy and recreate a RenderTexture?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Basically I've created a custom font class that makes a single sprite out of a line of text, but only when the text changes.  If I call create redundantly, I see graphical artifacts at the edge of my sprite, which tells me the texture probably didn't resize.  Hopefully there is a better solution than creating the render texture once to the entire width of the window (since I may have dozens of these objects)

This is what I'm doing when the text changes..

Quote
rt.create( width, height);

rt.clear( sf::Color( 0,0,0,0));

DrawText( pszString, rshader, tint);

rt.display();

textureText = rt.getTexture();
      
spriteText.setTexture( textureText);

Any feedback is appreciated, thank you!!
Title: Re: Can a RenderTexture be resized?
Post by: Laurent on February 22, 2019, 05:39:18 pm
Please read the documentation (https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Sprite.php#a3729c88d88ac38c19317c18e87242560) first.

But a much better (more efficient) solution would be to give the RenderTexture a maximum size, then leave it untouched and instead only adjust the sprite's textureRect. Which is a free operation compared to re-creating a RenderTexture. You don't even need to call sprite.setTexture again as long as you don't re-create the RenderTexture.