Hi, I’m trying to render text in SDL2, so I came up with the idea of rendering every glyph that I will use in white on a Texture, then set the color mod and RenderCopy the glyphs on the screen for rendering any text in any color. But I have a problem, Somehow, I can see the white glyphs on white background, while it’s not the case on other apps such as paint or word.
This is my code (I removed the parts which weren’t related to the problem)
void load(SDL_Renderer *renderer, std::string filepath, int size)
{
const int m_size = 95;
const char* m_chars = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
std::vector<SDL_Rect> m_rects = {};
SDL_Texture m_texture;
TTF_Font *font = TTF_OpenFont(filepath.c_str(), size);
SDL_Rect rect = { 0, 0, 0, 0 };
std::vector<SDL_Surface*> glyphs;
for (int i = 0; i < m_size; i++)
{
char ch = m_chars[i];
SDL_Surface *glyph = TTF_RenderGlyph_Blended(font, ch, { 255, 255, 255 });
glyphs.push_back(glyph);
rect.w = glyph->w;
rect.h = glyph->h;
m_rects.push_back(rect);
rect.x += rect.w;
}
int width = 0, height = 0;
for (int i = 0; i < m_size; i++)
{
width += (int)rect.w;
height = std::max(height, (int)rect.h);
}
SDL_Surface *atlas = SDL_CreateRGBSurface(0, width, height, 32,
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
#else
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
#endif
);
for (int i = 0; i < m_size; i++)
{
SDL_Rect dst = { (int)m_rects[i].x, (int)m_rects[i].y };
SDL_BlitSurface(glyphs[i], NULL, atlas, &dst);
SDL_FreeSurface(glyphs[i]);
}
m_texture = SDL_CreateTextureFromSurface(renderer, atlas);
SDL_FreeSurface(atlas);
TTF_CloseFont(font);
}
But when I render m_texture on a window with white background, I’m not supposed to see the texture, it should seem invisible, but I’m seeing:
Whats the problem? what am I doing wrong?