Search code examples
c++openglsdlsdl-ttf

SDL_Surface Transparency Issues


I have been working with sdl ttf, Now, I'm creating a text renderer function for multiline text rendering.

I am creating a surface for each line, blitting in in a "global" surface, and then converting that "global" surface to a texture.

Actually My problem is the following: If I let the global surface transparent, the blitting does not works (It shows an empty surface), If I set that SDL_FillRect, and then do the SDL_SetColorKey, does not works.

I tried to do that with a per-pixel method, it works, but it's not the result expected.

What I need actually is: Blit surfaces (Text rendered surfaces with TTF_RenderText_Blended), blit them inside a SDL_Surface with a transparent background, that's all.

SDL_Surface * surfed_texture = SDL_CreateRGBSurface(SDL_SWSURFACE,surface_width,surface_height,32,
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);

Uint32 tormv = SDL_MapRGB(surfed_texture->format,255,0,255);

int possiblecolors[] = { 255,0,255,255,255,255 };

int in_pc = 0;

if(color.r == possiblecolors[0] && color.g == possiblecolors[1] && color.b == possiblecolors[2]){
    in_pc=3;
}

/*SDL_FillRect(surfed_texture,NULL,SDL_MapRGB(surfed_texture->format,possiblecolors[in_pc],possiblecolors[in_pc+1],possiblecolors[in_pc+2]));
SDL_SetColorKey(surfed_texture, SDL_SRCCOLORKEY, SDL_MapRGB(surfed_texture->format,possiblecolors[in_pc],possiblecolors[in_pc+1],possiblecolors[in_pc+2]));
*/
SDL_Surface * temporal = NULL;
for(int i=0;i<(int)buff_split.size();i++){
    const char* c_text = buff_split.at(i).c_str();

    temporal = TTF_RenderText_Blended(font,c_text,color);

    int w_fo;
    int h_fo;
    TTF_SizeText(font,c_text,&w_fo,&h_fo);

    SDL_Rect rct;
    rct.y=i*lineSkip;

    if(txt_align==ALIGN_LEFT){
        rct.x=0;
    }else if(txt_align==ALIGN_CENTER){
        rct.x = surface_width/2 - w_fo/2;
    }else if(txt_align==ALIGN_RIGHT){
        rct.x = surface_width - w_fo;
    }

    rct.w=0;
    rct.h=0;

    // Blit surface

    SDL_BlitSurface(temporal,NULL,surfed_texture,&rct);

    SDL_FreeSurface(temporal);

}

Blitting into "surfed_texture" with transparent background = Does not shows the blitted surface. Per-Pixel method: Does not removes all the background.

And the SDL_SetColorKey is not working! (I already tried SDL_DisplayFormatAlpha and stills not working).

Some help?


Solution

  • The solution is the following:

    Use the SDL_SetAlpha NOT in the "global" surface, apply a SDL_SetAlpha to each "temporal" surface with no flags, and with a value of 0:

    SDL_SetAlpha(temporal,NULL,0);
    

    Now it's working fine!