Freetype2 rendering to texture

Started by
-1 comments, last by Clueless 17 years, 9 months ago
Hi, I asked this a while ago in the forum for beginners ... but the problem could not be solved then. Has anybody here written code that renders the ascii-letters of a font to a texture? The problem that I'm running into is that the vertical positioning doesn't work properly. I want the letters in the top left corner, but moved down depending on the bitmap_top value. With the code below they are moved too far down and I have to change a value manually. I need a way to do the vertical positioning inside the quad automatically. Can anybody help?

void EditorFont::initialize() {
    g_error = FT_Init_FreeType(&g_library);
    if (g_error) {
        Instances::get_pGlobal()->get_pCore()->quit();
    } else {
        g_error = FT_New_Face(g_library,"./content/Gargoyles.ttf",0,&g_face);
        if (g_error) {
            Instances::get_pGlobal()->get_pCore()->quit();
        } else {
            m_pFont  = new Font();
            m_pGlyph = new Glyph();

            Uint8* pixels = 0;
            Uint16 width; Uint16 height;
            Uint16 glyphpixels = 0;

            g_error = FT_Set_Char_Size(g_face,32*64,0,0,0);
            if (g_error) {
                Instances::get_pGlobal()->get_pCore()->quit();
            } else {

                for (Uint16 j=0; j<256; j++) {
                g_error = FT_Load_Char(g_face,(Uint8)j,FT_LOAD_RENDER);
                if (g_error) {
                    Instances::get_pGlobal()->get_pCore()->quit();
                } else {
                    width  = 32;
                    height = 32;
                    // setting alpha to 0 everywhere
                    for (Uint32 i=0; i<width*height; i++) {
                        m_pFont->set_pixel(255,255,255,0,
                            i%width, i/width,
                            (Uint8)j);
                    }
                    // setting the gray-values of the bitmap as alpha values
                    for (Uint32 i=0; i<g_face->glyph->bitmap.rows*
                         g_face->glyph->bitmap.width; i++) {
                        m_pFont->set_pixel(255,255,255,
                            g_face->glyph->bitmap.buffer,
                            ((i%g_face->glyph->bitmap.width)+1),
                            ((i/g_face->glyph->bitmap.width))
/* !!!  Here is the value that has to be adjusted manually for different fonts !!! : */
                                + 26
                                - g_face->glyph->bitmap_top,
                            (Uint8)j);
                    }
                }
                glyphpixels = 0;
                }
                m_pFont->export_font("./content/gargoyles.tga");
            }
        }
    }
}


Writing errors since 10/25/2003 2:25:56 AM

This topic is closed to new replies.

Advertisement