The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
How do I copy just the alpha between two textures?
Danni111111


Joined: 30 Aug 2015
Posts: 5
Hi,

I'm trying to put in code such that I can have three textures, A, B and C, and have the RGB from A to C, and copy the alpha from B to C, resulting in texture C having the RGB data from A and the alpha data from B. (Or to put it more simply, I want to be able to copy JUST the alpha layer between textures, or JUST the RGB layers)

In the context of my program, texture A is the color pattern I want to display, and texture B is the shape (the alpha) that I want to display of that color pattern. I would render texture C onto the screen. I'm doing this instead of having a single texture with all layers together as I want to be able to have a large number of combinations of shapes and textures.

I've been told that modifying pixels could work, but would be slow. (Don't know much about it myself)

I'm not sure what to do - I would like to cut out the middle man texture C and render using RGB from A and alpha from B, but I have no idea how I might do that. If anyone has any suggestions, I would be extremely grateful to hear them!
Danni111111


Joined: 30 Aug 2015
Posts: 5
I managed to solve it, luckily it was easier than I thought:

Code:
        TextureC = SDL_CreateTexture(Render, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 64, 64);


        SDL_SetTextureBlendMode(TextureC, SDL_BLENDMODE_BLEND);
        SDL_SetRenderTarget(Render, TextureC);


        SDL_SetTextureBlendMode(TextureB, SDL_BLENDMODE_ADD);

        SDL_SetRenderDrawColor(Render, 0,0,0,0);
        SDL_RenderClear(Render);


        SDL_RenderCopy(Render, TextureA, &InputPosition, NULL);
        SDL_RenderCopy(Render, TextureB, &InputPosition, NULL);

        SDL_SetRenderTarget(Render, NULL);

        SDL_RenderCopy(Render, TextureC, &InputPosition, &OutputPosition);


Using BLENDMODE_ADD works when the first texture applied is the mask texture (has to be a black mask), but it will only work when the target texture (C) has BLENDMODE_BLEND, otherwise the transparent parts appear as black. Not sure why.