raylib-cpp icon indicating copy to clipboard operation
raylib-cpp copied to clipboard

Get raylib::Texture from raylib::RenderTexture

Open Ataraxia-Mechanica opened this issue 2 years ago • 4 comments

In raylib::RenderTexture, the GetTexture() method returns a Texture instead of the wrapped raylib::Texture. This is kinda inconvenient since instead of render_texture.GetTexture().Draw(from, to), you have to use

DrawTexturePro(render_texture.GetTexture(),
               from,
               to,
               raylib::Vector2(0, 0),
               0.0f,
               Color{255, 255, 255, 255});

Ataraxia-Mechanica avatar Aug 30 '22 03:08 Ataraxia-Mechanica

This may be tricky, because if we have GetTexture() return a raylib::Texture, it will Unload the underlaying Texture when it completes....

{
  raylib::Texture texture = renderTexture.GetTexture();
  texture.Draw(100, 100);
}
// Error: The renderTexture's texture will now be destroyed.

Been considering implementing a TexturePointer class or something, which inherits from Texture, but overrides Unload() to not actually Unload the Texture on deconstruction. Then in cases where we retrieve the Texture from something like this that doesn't want to Unload, it would be fine.

{
  raylib::TexturePointer texture = renderTexture.GetTexturePointer();
  texture.Draw(100, 100);
}
// Fine, because TexturePointer does not Unload on deconstruction.

Then, you'd be able to do this no problem, as the Texture wouldn't destruct itself.

render_texture.GetTexturePointer().Draw(from, to);

RobLoach avatar Aug 30 '22 04:08 RobLoach

We could also have GetTexture returns the TexturePointer... Hmmm.

RobLoach avatar Aug 30 '22 04:08 RobLoach

Or I wonder if it would be a good idea to add Draw(...) methods to RenderTexture itself?

Ataraxia-Mechanica avatar Aug 30 '22 05:08 Ataraxia-Mechanica

That's defintiely an easy option! :+1:

RobLoach avatar Aug 30 '22 06:08 RobLoach