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.