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

Author Topic: How to properly draw multiple RenderTexture ?  (Read 4370 times)

0 Members and 1 Guest are viewing this topic.

LakySimi1

  • Newbie
  • *
  • Posts: 35
    • View Profile
How to properly draw multiple RenderTexture ?
« 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!
« Last Edit: July 30, 2024, 07:43:18 pm by LakySimi1 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How to properly draw multiple RenderTexture ?
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

LakySimi1

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: How to properly draw multiple RenderTexture ?
« Reply #2 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How to properly draw multiple RenderTexture ?
« Reply #3 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to properly draw multiple RenderTexture ?
« Reply #4 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).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*