[SOLVED] Premultiplied alpha


#1

Hi
Is there any way with latest SDL2.0.8 in run-time create textures with premultiplied alpha and set a blend mode to use premultiplied alpha?


#2

I solved my problem. If someone need there is a code:

//modify surface before create texture
typedef struct ColorRGBA_t {
    Uint8 r;
    Uint8 g;
    Uint8 b;
    Uint8 a;
} ColorRGBA_t;

//...
    if (m_surface->format->BytesPerPixel == 4)
    {
        auto pixels = (Uint8*)m_surface->pixels;
        for (int y = 0; y < m_real_height; ++y)
        {
            for (int x = 0; x < m_real_width; ++x)
            {
                int index = y * m_surface->pitch + x * m_surface->format->BytesPerPixel;
                auto target_pixel = (ColorRGBA_t*)&pixels[index];
                target_pixel->r = (Uint8)(target_pixel->r * target_pixel->a / 255.0);
                target_pixel->g = (Uint8)(target_pixel->g * target_pixel->a / 255.0);
                target_pixel->b = (Uint8)(target_pixel->b * target_pixel->a / 255.0);
            }
        }
    }

//...
//... create texture
//...
// set blend mode for texture
auto target = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
	result = SDL_SetTextureBlendMode(m_texture, target) == 0;