[FIXED] SDL_SetRenderTarget() glitch

Hello everybody! Tried to solve my issue by myself and I just don’t get it=( So the scheme is (in the loop):

  1. SDL_RenderClear(myRender); // clear all

  2. SDL_RenderCopyEx(myRender, whiteLightTexture, &srcRect, &destRect, 0, 0, SDL_FLIP_NONE); // draw light, it’s just a white texture, set as SDL_BLENDMODE_ADD

  3. SDL_RenderCopyEx(myRender, background, &srcRect, &destRect, 0, 0, SDL_FLIP_NONE); // drawing background (just black square), set as SDL_BLENDMODE_BLEND; IMPORTANT, remember this step please

  4. SDL_SetRenderTarget(ptrRender, m_pTargetTexture); // where ‘m_pTargetTexture’ set as SDL_BLENDMODE_MOD; I will use it as a target for all things in the sceen except the light; bcs light is _ADD and target texture is _MOD the re will be light effect in the game

  5. SDL_RenderCopyEx(myRender, all, &srcRect, &destRect, 0, 0, SDL_FLIP_NONE); // lets draw map layers, player ad everything else

SDL_SetRenderTarget(myRender, 0);
SDL_RenderCopyEx(ptrRender, m_pTargetTexture, 0, 0, 0, 0, SDL_FLIP_NONE); // draw our render target on the whole window

  1. SDL_RenderPresent(myRender);

So, here is the problem. If you exclude step 3 and just draw ‘m_pTargetTexture’ without background texture you will see ‘ghost’ effect of the previously rendered textures ANY time you move any image. Easy fix - draw background before target texture. But no.

Somewhere between 5 and 6 I’m drawing bunch of textures (I’m trying to draw shadows) to another target texture (lets call it ‘smallTarget’) and THEN I draw this smallTarget to the ‘m_pTargetTexture’. Simply, I can’t draw black rectangle under the ‘smallTarget’, bcs then we will see beautiful map layers, then ugly placeholder black rectangle, then ‘smallTarget’ with my shadows (but if you do like this there will be no ghost effect at all, hurray). How can I clear target texture and avoid ghost effect? Of course without drawing background texture before it. And if you draw background and make it transparent - ‘ghost’ effect will return.

I hope description is clear. Please help!

SDL 2.0.6

After step 4, if you call SDL_RenderClear(myRender); again (but this time clearing the m_pTargetTexture, since it’s the current render target), does it solve the issue?

1 Like

Naith, thank you! It works indeed. You da best. SDL da best.

P.S. so simple and obvious, I’m confused I didn’t try it earlier.