How to not clear all texture in render

When i use gRenderer to save texture, how to not clear the texture background when i use SDL_RenderClear(gRenderer);

image

I don’t think there is a way to only clear some things. Normally you would just call SDL_RenderClear, then draw the background and everything else, every frame.

Note that this does not mean you need to call IMG_LoadTexture every frame. Just load the texture once and keep using it for as long as you need. Don’t call SDL_DestroyTexture until you are finished using the texture.

From your description, my answer would be to just not use SDL_RenderClear() if it’s not needed. I feel like it gets overused to begin with. If your image is filling the entire texture/renderer then just redraw the background image instead.

SDL_RenderClear() is meant as a way to bash the pixels in the destination renderer, it doesn’t just draw over the destination’s previous state. This is a way to force pixels on a texture to be transparent, but not the way to add a semi-transparent color overlay on top of an image.
If you are trying to use it as a way to draw a semi-transparent layer on top of your image, then use SDL_SetRenderDrawColor() with alpha set to a low value and SDL_FillRect instead.

I feel like there’s more to your problem that we’re not hearing.
Can we get a more complete description of what you are trying to do, and what the problem is?

2 Likes

It’s not surprising that it’s “over used” when the docs for SDL_RenderPresent states:

You are strongly encouraged to call SDL_RenderClear() to initialize the backbuffer before starting each new frame’s drawing, even if you plan to overwrite every pixel.

Someone asked if this was really necessary in another topic, and I’m not sure what to make of it, but the final words by slouken was:

At the moment clearing the buffer isn’t necessary if you’re going to completely fill the frame, however it’s common practice and recommended by Apple on their platforms.

2 Likes

As I understand it, that applies to the first 2 times that you call SDL_RenderPresent(). If you only use it once before your game loop it doesn’t fill the backbuffer and you end up with a fast-flickering background screen. The renderer is doing double buffering behind the scenes.
(I do now see that they recommend using it every frame. I don’t see a benefit, maybe there’s a potential issue on some other OS?)

… And if you’re going to draw a full background image every frame, that does virtually the same thing.

In some instances, depending on the blend mode being used this might mess up the render if the destination is full of junk data on initialization, so you would want that clear for a safety net…

You have me in a quandary, because while it doesn’t hurt much to use it, I do get a change in fps when I’m testing for max fps and skip SDL_RenderClear().

Edit:
@Peter: thank you for that link and this current discussion. In the link it boils down to being an OS and platform rendering performance issue. On average there is not much difference between skipping it or using it, and on phones there is a potential that the renderer might run slower if you skip the clear. So it is best practice to use SDL_RenderClear even with backgrounds that fully fill the screen.

1 Like

That’s what I do in my apps.

1 Like