How to make a color pixel transparent SDL2 (Question)

hello everyone , I have a question I’m trying to set a transparent color to a texture , I already read the tutorial, texture manipulation it show me how to create a texture for streaming on pixel and it’s ok(working changing pixel color for other ) but when I change whatever pixel with transparent color that must r=0xff, g=0xff,b=0xff,a=0 it stay white and it show in the screen as is. do you recomend other way or how can I accomplish that? please and thank you in advance.

Did you remember to call SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND)?

1 Like

no, I did not forget.

next this my code for the function.

void Graphics::remove_color(Texture *texture, Color color){
       int pitch       = 0;
        Uint32 format = SDL_GetWindowPixelFormat( global_window );
        void *data;
        SDL_PixelFormat* mappingFormat = SDL_AllocFormat( format );

        SDL_SetTextureBlendMode((SDL_Texture *)texture->texture, SDL_BLENDMODE_BLEND);
        SDL_LockTexture((SDL_Texture *)texture->texture, NULL, &data, &pitch);
        Uint32* pixels = (Uint32 *)data;
        int pixelCount = ( pitch / 4 ) * texture->h;
        Uint32 color_to_remove = SDL_MapRGB( mappingFormat, color.r,color.g, color.b );
        Uint32 transparent = SDL_MapRGBA( mappingFormat, 0xFF, 0xFF, 0xFF, 0x00 );
        for(int i =0; i<pixelCount;++i){
        SDL_UnlockTexture((SDL_Texture *)texture->texture);
        SDL_FreeFormat( mappingFormat );
        data =NULL;
        pixels = NULL;

with this function nothing change ,cheers.

The pixel format of your texture should have an alpha component, for instance you can use SDL_PIXELFORMAT_ARGB8888

1 Like

Can I just say thank you so much! I was tearing my hair out trying to get the alpha to show properly and have the performance of a converted surface.

surface = SDL_ConvertSurfaceFormat(loadedSurface, SDL_PIXELFORMAT_ARGB8888, 0); is exactly what I needed.