Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]Make sf::Sprite NOT hold a reference to sf::RenderTexture::getTexture()?  (Read 2746 times)

0 Members and 1 Guest are viewing this topic.

7krs

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
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?
« Last Edit: October 20, 2012, 09:27:31 pm by 7krs »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Make sf::Sprite NOT hold a reference to sf::RenderTexture::getTexture()?
« Reply #1 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.
Laurent Gomila - SFML developer

7krs

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: Make sf::Sprite NOT hold a reference to sf::RenderTexture::getTexture()?
« Reply #2 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);
 
« Last Edit: October 20, 2012, 09:02:27 pm by 7krs »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Make sf::Sprite NOT hold a reference to sf::RenderTexture::getTexture()?
« Reply #3 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

7krs

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: Make sf::Sprite NOT hold a reference to sf::RenderTexture::getTexture()?
« Reply #4 on: October 20, 2012, 09:27:13 pm »
Alright, all my questions have been answered, thanks a lot!

 

anything