Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blending textures in SDL2

Tags:

sdl-2

I have 2 textures, both of them are created with SDL_TEXTUREACCESS_STREAMING flag. They have blend mode set to SDL_BLENDMODE_BLEND. I'm updating both of them in each frame from 2 different surfaces. One of them has mostly alpha values equal to 0 (mostly transparent). When I put one of the textures on the top of another, the blending doesn't happen.

For example - one surface is going to have RGBA values = 255, 255, 255, 0 for all pixels. When I update a texture with it, and put the same texture on the top of another image I get only black pixels.

How do I accomplish blending on the 2 textures in such a way that pixels with 0 alpha will be treated as transparent instead of being turned to black pixels? What is happening? Are Textures ignore alpha values?

like image 988
Moon4u Avatar asked Jan 30 '26 02:01

Moon4u


1 Answers

This has the formulae for blending modes:

SDL.org: SDL_BlendMode

and this has visual examples of each of the ways of combining two textures via blending (for openGL, but the results should be the same for the few modes SDL implements):

StackExchange GameDev: OpenGL Blending GUI Textures: Answer 1

SDL_BLENDMODE_BLEND has a (1-alpha) term in it, which, if alpha is near 0, will be near 1, which could be causing opacity where you're expecting transparency. It depends on which you're using as src and which as dest, also.

(nb: I'd just post this as a comment, but I don't have the reputation yet.)

like image 149
Blair Houghton Avatar answered Feb 02 '26 09:02

Blair Houghton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!