Draw to texture without changing target alpha?


I wrote a game a while back, and one thing in the game is that blood-spatter gets drawn on the level during gameplay. I achieve this by using a patch I proposed a few years ago, where I suggested a new blend mode (Propose new blend mode, patch included) … This didn’t get much traction from the SDL developers.

The blend mode worked pretty much like SDL_BLENDMODE_BLEND, but it kept the alpha channel of the rendering target unchanged.

I’m wondering if anyone has any ideas of how one would implement this while using the SDL renderer with an unmodified version of SDL2 (as in current trunk, or 2.0.6 when it’s released), as I would prefer to not rewrite my game to use OpenGL for this reason alone.

/Trygve Vea

I managed to solve this problem using the following workflow:

  1. Create a ‘backup’ of the target texture, which is created by rendering a copy of the target texture using SDL_SetColorMod(0, 0, 0);
  2. Apply blood-spatter to target texture with SDL_BLENDMODE_BLEND (This may modify the alpha channel of the target texture)
  3. Apply target texture to the backup with SDL_BLENDMODE_ADD (This leaves the alpha channel of the backup texture untouched)
  4. Copy the entire backup texture using SDL_BLENDMODE_NONE, and perform stem 1

A few more steps than with a simple blend function, but it gives acceptable results, so I’m happy about it =)

If performance is ever a problem, you’ll definitely want to minimize the intermediate copies.

If you are using the OpenGL backend for SDL_Renderer, then you should be able to mess with the blend mode yourself before rendering (using GL calls) and then set it back to how it should be.

Wow, that thread you linked to brings me back…