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

Author Topic: Texture created by RenderTexture is going out of scope  (Read 2139 times)

0 Members and 1 Guest are viewing this topic.

smurf

  • Newbie
  • *
  • Posts: 36
    • View Profile
Texture created by RenderTexture is going out of scope
« on: February 17, 2021, 06:09:55 pm »
I frequently use RenderTextures to pre-draw complicated textures, then I create a Sprite out of the resulting RenderTexture.Texture for drawing to the screen.

I used to maintain a list of all the RenderTextures for the life of the program, just so they wouldn't go out of scope.

But then I realized I don't need the entire RenderTexture once I'm done my pre-drawing. I only need the texture part. So I changed my resource manager to only store the RenderTexture.Texture, but of course that didn't work. I guess because my reference to the Texture field was no enough to keep the overall RenderTexture object from going away.

So I guess my question is what is the best practice here? Should I go back to storing the entire RenderTexture even though I'm no longer rendering to it? It seems unclean to do this. Maybe the other option is to COPY the Texture field to my list and create the sprite from that copy. That way, I'm only keeping around the exact thing I need. Downside is that copying might be slow?

What do you guys do?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Texture created by RenderTexture is going out of scope
« Reply #1 on: February 17, 2021, 08:40:24 pm »
have you tried to store a simple texture?
sf::Texture tex = my_render_tex.getTexture();
I just don't remember now if that keeps on working when the RenderTexture goes out of scope, but I remember doing something like that.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Texture created by RenderTexture is going out of scope
« Reply #2 on: February 19, 2021, 11:27:50 am »
If you don't just reference the texture of the RT, but actually asign it like Stauricus showed, then the texture data will be copied. It's not the cheapest operation, but should be fine to "finalize" some pre-drawn complex texture renderings.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/