The Alpha channel of textures is not take into account

Hey,

I am dicovering the SDL and I have a problem. Actually I’m trying to create class to draw shapes on the window.
Actually i’m making the circle but have a problem. The circle it’s draw but the alpha isn’t take into account.The pixel outside the circle are displayed even if the alpha is set to 0.
There is a lot of topic about this subjet on the net, but none helped me :frowning:

You have here the code who create the texture with the circle inside :

void rbb_circle::create_texture(window::gm_window *window)
    {
        SDL_Surface *surf = SDL_CreateRGBSurface(0, this->rect.w, this->rect.h, 32, 0, 0, 0, 0);
        SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND);
        
        SDL_LockSurface(surf);
        SDL_PixelFormat *format = surf->format;

        for (int y = 0; y < this->rect.h; y++) {
            for (int x = 0; x < this->rect.w; x++) {
                int dx = x - this->rect.w / 2;
                int dy = y - this->rect.h / 2;
                if (dx * dx + dy * dy <= (this->rect.w / 2) * (this->rect.w / 2)) {
                    ((Uint32*) surf->pixels)[y * this->rect.w + x] = SDL_MapRGBA(format, this->color.r, this->color.g, this->color.b, 255);
                } else {
                    ((Uint32*) surf->pixels)[y * this->rect.w + x] = SDL_MapRGBA(format, 140, 10, 150, 0);
                }
            }
        }
        SDL_UnlockSurface(surf);

        this->texture = SDL_CreateTextureFromSurface(window->get_renderer(), surf);

        SDL_FreeSurface(surf);
    }

I think there is no problem in this function.

To draw the circle inside my window I do like that :

void rbb_circle::draw(window::gm_window *window)
    {
        SDL_RenderCopy(window->get_renderer(), this->texture, NULL, &this->rect);
    }

And I create my renderer and window like that :

gm_window::gm_window()
    {
        this->window = SDL_CreateWindow(WINDOW_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SIZE_X, SIZE_Y, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
        this->renderer = SDL_CreateRenderer(this->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);

        SDL_SetRenderDrawBlendMode(this->renderer, SDL_BLENDMODE_BLEND);

        this->event = new event::gm_event;
        is_open = true;
        
        SDL_SetRenderDrawColor(this->renderer, BG_R, BG_G, BG_B, BG_A);
    }

I really don’t understand why my alpha channel is not take into account, that weird. You also have a screenshoot of the result.

Thank you for your answer :slight_smile:

Does creating your surface with masks 0,0,0,0 give you a surface with an alpha channel?

Try like SDL_CreateRGBSurfaceWithFormat with the format RGBA.

2 Likes

In the documentation of SDL_CreateRGBSurface() it says: “However, using zero for the Amask results in an Amask of 0” so you are creating a surface with no alpha channel.

Also, you are not anti-aliasing your circle so you will get jaggies on the edge.