[SDL_ttf] Rendering on transparent background does not work

Hi,

despite finding several examples out there, i am not able to render a font with transparent background with SDL_ttf and SDL 1.

Even if i break it down to a simple:

Code:

SDL_Color col;
col.r = 255;
col.g = 255;
col.b = 255();
SDL_Surface *s = TTF_RenderText_Blended(sdlFont, text, col);
SDL_SaveBMP(s, "blended.bmp");

The saved bmp has a white font on solid black background.

I actually try to render it into an opengl texture, here is the full code:

Code:

GLuint SGE::Font::render(const char *text, int &width, int &height, int &texSize)
{
if (strcmp(text, “”) == 0) {
return 0;
}
SDL_Color col;
col.r = color.r();
col.g = color.g();
col.b = color.b();
SDL_Surface *s = TTF_RenderText_Blended(sdlFont, text, col);
SDL_SaveBMP(s, “blended.bmp”);
GLuint texture = 0;
SGE::Loader::sdlSurfaceToTexture(s, texture, texSize, width, height);
return texture;
}

void SGE::Loader::sdlSurfaceToTexture(SDL_Surface s, GLuint &texture, int &p, int &width, int &height)
{
width = s->w;
height = s->h;
p = pow(2, ceil( log(MAX(s->w, s->h))/log(2) ) );
SDL_Surface
ns = SDL_CreateRGBSurface(SDL_SWSURFACE, p, p, 32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x000000ff, 0x0000ff00, 0x00ff0000,0xff000000
#else
0xff000000, 0x00ff0000, 0x0000ff00,0x000000ff
#endif
);
// SDL_SetSurfaceBlendMode(s, SDL_BLENDMODE_NONE);
SDL_BlitSurface(s, NULL, ns, NULL);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ns->w, ns->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, ns->pixels);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4) ;
SDL_FreeSurface(s);
SDL_FreeSurface(ns);
}

What am i doing wrong?

BTW: I am actually porting this back from SDL2 to SDL1, the commented line SDL_SetSurfaceBlendMode(s, SDL_BLENDMODE_NONE); was the one that made the trick happen on SDL2 IIRC, but it seems to be not available in SDL1 and i already tried several SDL_SetAlpha versions. Anyway, it seems that original SDL_Surface is already with black background.

Bmp does not know about transparency… Save to a pngOn Aug 23, 2013 12:38 PM, “Heiko” <hi at 93i.de> wrote:

**
Hi,

despite finding several examples out there, i am not able to render a font
with transparent background with SDL_ttf and SDL 1.

Even if i break it down to a simple:

Code:

SDL_Color col;
col.r = 255;
col.g = 255;
col.b = 255();
SDL_Surface *s = TTF_RenderText_Blended(sdlFont, text, col);
SDL_SaveBMP(s, "blended.bmp");

The saved bmp has a white font on solid black background.

I actually try to render it into an opengl texture, here is the full code:

Code:

GLuint SGE::Font::render(const char *text, int &width, int &height, int
&texSize)
{
if (strcmp(text, “”) == 0) {
return 0;
}
SDL_Color col;
col.r = color.r();
col.g = color.g();
col.b = color.b();
SDL_Surface *s = TTF_RenderText_Blended(sdlFont, text, col);
SDL_SaveBMP(s, “blended.bmp”);
GLuint texture = 0;
SGE::Loader::sdlSurfaceToTexture(s, texture, texSize, width, height);
return texture;
}

void SGE::Loader::sdlSurfaceToTexture(SDL_Surface s, GLuint &texture, int
&p, int &width, int &height)
{
width = s->w;
height = s->h;
p = pow(2, ceil( log(MAX(s->w, s->h))/log(2) ) );
SDL_Surface
ns = SDL_CreateRGBSurface(SDL_SWSURFACE, p, p, 32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x000000ff, 0x0000ff00, 0x00ff0000,0xff000000
#else
0xff000000, 0x00ff0000, 0x0000ff00,0x000000ff
#endif
);
// SDL_SetSurfaceBlendMode(s, SDL_BLENDMODE_NONE);
SDL_BlitSurface(s, NULL, ns, NULL);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ns->w, ns->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, ns->pixels);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4) ;
SDL_FreeSurface(s);
SDL_FreeSurface(ns);
}

What am i doing wrong?

BTW: I am actually porting this back from SDL2 to SDL1, the commented line
SDL_SetSurfaceBlendMode(s, SDL_BLENDMODE_NONE); was the one that made the
trick happen on SDL2 IIRC, but it seems to be not available in SDL1 and i
already tried several SDL_SetAlpha versions. Anyway, it seems that original
SDL_Surface is already with black background.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanx, the bmp sent me in to the wrong direction, the image was transparent.

In case somebody has the same problem: i need to use:

Code:

SDL_SetAlpha(s, s->flags^SDL_SRCALPHA, SDL_ALPHA_OPAQUE);

on SDL1 instead of the SDL2 variant

Code:

SDL_SetSurfaceBlendMode(s, SDL_BLENDMODE_NONE);

right before the blit into the square texture.

I also had to move my glEnable(GL_BLEND); which i had at application start into the part where i am actually rendering the texture

To be honest, i do not understand why i have to disable SDL_SRCALPHA, i thougth i have to enable it for keeping the source image transparency, but well, it works.------------------------
sge2d (http://www.93i.de/products/software/sge2d) An open source sdl based game framework for c/c++ (sge++ for c++ available soon)