SFML community forums

Help => Graphics => Topic started by: 7krs on October 20, 2012, 04:36:46 pm

Title: [SOLVED]Make sf::Sprite NOT hold a reference to sf::RenderTexture::getTexture()?
Post by: 7krs on October 20, 2012, 04:36:46 pm
Hello there,

I have two threads, one drawing a sprite to the screen at 30 Hz, the other drawing to a RenderTexture at 1 Hz.

The thread drawing to the RenderTexture has a lot of work to do, so whilst it is drawing, the first thread draws an unfinished sprite to the screen. This can be seen as half of what's being drawn, flickers.

I see that the sprite holds a reference to the renderTexture's texture, and I don't want to sf::Mutex::Lock the entire clear()-display() part of the second thread, that'd take too much time. So therefore I want one sprite that is always complete, and gets updated after finishing drawing to the rendertexture, to avoid the flickering.

How do I do this?
Title: Re: Make sf::Sprite NOT hold a reference to sf::RenderTexture::getTexture()?
Post by: Laurent on October 20, 2012, 07:40:51 pm
You need to work with two render-textures, the current one (being filled in the thread) and the finished one (displayed by the sprite). At the end of every cycle, switch them. This is basically double-buffering.
Title: Re: Make sf::Sprite NOT hold a reference to sf::RenderTexture::getTexture()?
Post by: 7krs on October 20, 2012, 09:00:44 pm
Thanks for the info and response.

What would you say is faster:

One sprite, switch the texture thereof, this means the window drawing function does not have to check for a condition, the pointer rendertexture changes during every iteration:

rendertexture->display();

renderSprite.setTexture(renderTexture->getTexture());
 

or

Two sprites, each for a rendertexture, switch which is drawn, window drawing function checks on every iteration (or one could change a pointer to the renderSprite at the end of the rendertexture thread iteration):

if (first)
    window.draw(firstRenderSprite);
else
    window.draw(secondRenderSprite);
 
Title: Re: Make sf::Sprite NOT hold a reference to sf::RenderTexture::getTexture()?
Post by: eXpl0it3r on October 20, 2012, 09:19:43 pm
The one with two sprites will use a tiny bit more memory, where as the other just passes around references.
I don't think that one will have a better performance that the other, so you can use whatever fits better your code design. ;)
Title: Re: Make sf::Sprite NOT hold a reference to sf::RenderTexture::getTexture()?
Post by: 7krs on October 20, 2012, 09:27:13 pm
Alright, all my questions have been answered, thanks a lot!