Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch all TTF glyph of a font in a single atlas #7

Open
Gallasko opened this issue Oct 14, 2024 · 0 comments
Open

Batch all TTF glyph of a font in a single atlas #7

Gallasko opened this issue Oct 14, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Gallasko
Copy link
Owner

Right now a new texture is generated for each unique text written using a TTF Font, making the number of texture and drawcall increase really fast when writting a lot of text

OpenGLTexture fontTexture;

TTF_Font * font = TTF_OpenFont(fontPath.c_str(), textSize);

if (not font)
{
    LOG_ERROR(DOM, "Font could not be loaded: " << fontPath << ", error: " << TTF_GetError());

    return fontTexture;
}

SDL_Color color = {255, 255, 255, 255};

SDL_Surface * sFont = TTF_RenderText_Blended(font, text.c_str(), color);

if (not sFont)
{
    LOG_ERROR(DOM, "Font surface could not be generated" << ", error: " << TTF_GetError());

    TTF_CloseFont(font);

    return fontTexture;
}

auto w = nextpoweroftwo(sFont->w);
auto h = nextpoweroftwo(sFont->h);

auto intermediary = SDL_CreateRGBSurface(0, w, h, 32, 
        0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);

SDL_BlitSurface(sFont, 0, intermediary, 0);

GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, intermediary->pixels);

sizeMap[textureName] = TTFSize{w, h};

fontTexture.id = texture;
fontTexture.transparent = false;

SDL_FreeSurface(sFont);
SDL_FreeSurface(intermediary);
TTF_CloseFont(font);

return fontTexture;
@Gallasko Gallasko added the enhancement New feature or request label Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant