SFML community forums

Help => Window => Topic started by: LakySimi1 on July 29, 2024, 10:09:02 pm

Title: How to properly draw multiple RenderTexture ?
Post by: LakySimi1 on July 29, 2024, 10:09:02 pm
Hi Everyone,

i'm trying to have two separate RenderTexture, one is the game and the other is a simple grid which, as it is coded, is very slow.

Therefore i want to draw on a separate RenderTexture just one time, and then show it on screen when needed, avoiding any redundant calculation.

I've managed to draw both RenderTexture but when i clear the first one it does not render the second one anymore, here the related code:

(click to show/hide)

Thank you very much in advance!
Title: Re: How to properly draw multiple RenderTexture ?
Post by: eXpl0it3r on July 29, 2024, 11:57:57 pm
Think of render textures like offscreen window surfaces.

When you clear, draw, display on it, the content remains permanent.
When you call clear again, everything is overwritten with the provided color.

From your code, I don't fully understand what you're trying to achieve, since you never actually draw anything to the render texture.
Personally, I'd not interleave the window rendering and render texture rendering.
Draw everything you need to the render texture, then use that texture to render to the screen.
Title: Re: How to properly draw multiple RenderTexture ?
Post by: LakySimi1 on July 30, 2024, 07:50:22 pm
I have updated the code in the OP.

Exactly, i want different offscreen surfaces where to draw (in my case the RenderTexture_Two is a simple grid that i want to overlap to the RenderTexture_One, to check the allignment and position of things..) and then combine them on screen.

The fact is that the grid is always the same therefore there is no benefit in redrawing it from scratch every frame, i prerender it on the RenderTexture_Two and than use it when needed.

I can simply use a .png with transparency to draw any overlapping image, without any calculation since is an image... but handle it directly in-code, since i think of a simple game, can make things more manageble than have to pass from Gimp.
Title: Re: How to properly draw multiple RenderTexture ?
Post by: eXpl0it3r on July 30, 2024, 09:37:18 pm
How, it seems like you misunderstand the use of display() on the render texture.

It's not about "displaying" it on the window or anything like that, but it has the same use as on the window, to do the final "render pass" on the window/off-screen surface.
In the same way as you have a single clear-draw-display sequence for the window, you should have a single clear-draw-display sequence per render texture.
So for RenderTexture_Two you just call clear, then draw your grid, followed immediately by a call to display and you'll never have to call display on it again.
Title: Re: How to properly draw multiple RenderTexture ?
Post by: Hapax on August 11, 2024, 04:38:57 pm
Don't forget to clear your render textures with a colour that is transparent if you want a transparent background as the default colour is an opaque black.

e.g.
renderTexture.clear(sf::Color::Transparent);

This would allow your render textures to be transparent "layers" but don't forget that either the window or the lowest layer should be cleared with the solid colour of choice (and should also fill the entire window if it is a layer).