We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3491

Summary: TTF_RenderText_Blended ignores alpha channel
Product: SDL_ttf Reporter: xyzdragon
Component: miscAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: xyzdragon
Version: 2.0.14   
Hardware: x86_64   
OS: Linux   
Attachments: Comparison what I thought vs. what I get

Description xyzdragon 2016-11-14 11:28:00 UTC
Created attachment 2624 [details]
Comparison what I thought vs. what I get

I have 'libsdl2-ttf-dev:amd64  2.0.14+dfsg1-1' which was not selectable in the bug report header.

The last argument "TTF_RenderText_Blended" takes is "SDL_Color fg". SDL_Color in turn has the members r,g,b,a. But member a is completely ignored with no mention in the documentation here https://www.libsdl.org/projects/docs/SDL_ttf/SDL_ttf_44.html

Either the alpha channel should be heeded or it should be explicitly mentioned that it isn't in the documentation, because the SDL_Color struct implies that fg.a will be processed. I hoped instead of the alpha channel of the rendering being in 0..255 that it would be in 0..fg.a, i.e. every alpha value would be multiplied by fg.a/255.

How I tested:

    /*
    g++ -g -std=c++11 -Wall -Wextra -Wshadow sdlttf-alpha-test.cpp $(sdl2-config --cflags --libs) -l SDL2_ttf && ./a.out
    */

    #include <SDL.h>
    #include <SDL_ttf.h>

    static bool useWorkaround = false;

    int main( void )
    {
        SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
        TTF_Init();

        int const screenWidth  = 400;
        int const screenHeight = 50;
        SDL_Window * pWindow = SDL_CreateWindow( "SDL2_TTF Alpha Test",
            SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screenWidth, screenHeight, SDL_WINDOW_SHOWN );
        SDL_Renderer * pRenderer = SDL_CreateRenderer( pWindow, -1, SDL_RENDERER_ACCELERATED );

        SDL_SetRenderDrawColor( pRenderer, 40,40,40,255 );
        SDL_RenderClear( pRenderer );

        /* test TTF_RenderText_Blended alpha */
       TTF_Font * font = TTF_OpenFont( "Hack-Regular.ttf", 12 );
        for ( int nMax=32u, i = 0u; i <= nMax; ++i )
        {
            Uint8 const alpha = 255 * i / nMax;
            SDL_Color faceColor{ 255,0,0, useWorkaround ? Uint8(255) : alpha };
            SDL_Surface * const sString = TTF_RenderText_Blended( font, "+", faceColor );
            SDL_Texture * tString = SDL_CreateTextureFromSurface( pRenderer, sString );
            SDL_Rect dstRect{ screenWidth/2 - nMax*10/2 + i*10, screenHeight/2 - sString->h / 2, sString->w, sString->h };
            if ( useWorkaround )
                SDL_SetTextureAlphaMod( tString, alpha );
            SDL_RenderCopy( pRenderer, tString, NULL, &dstRect );
        }
        TTF_CloseFont( font );

        SDL_RenderPresent( pRenderer );
        SDL_Delay( 2000 );
        SDL_DestroyRenderer( pRenderer );
        SDL_DestroyWindow( pWindow );
        SDL_Quit();

        return 0;
    }


P.S.: In the linked documentation there is a typo: "sntialiased" instead of "antialiased".
Comment 1 Sam Lantinga 2017-09-10 07:23:40 UTC
There's no reason why the text rendering can't respect the alpha component of the text color.

Implemented, thanks!
https://hg.libsdl.org/SDL_ttf/rev/20fdc27113cf