Advertisement

BMFont yoffset issue in my code

Started by July 02, 2018 06:22 AM
2 comments, last by To_Err_Is_Human 6 years, 4 months ago

Hi

I'm replacing FreeType with BMFont in my game engine. And characters yoffsets are somehow rendered wrong. I have already verified that my BMFont xml parsing code is correct.

I think the issue might be related to how I render quads in my engine. In my engine quad's origin is the lower left corner. I provide Dest rectangle to my PushSprite() function where Dest.x and Dest.y are the origin and Dest.width and Dest.height are the width and height of the quad.


void PushSprite(std::vector<vert_P1C1UV1> *Vertices, rect Dest, vec4 UV, vec4 Color, r32 Rotation, vec2 Origin, r32 Layer)

My text rendering code:


void PushText(std::vector<vert_P1C1UV1> *Vertices, rect Dest, vec4 Color, r32 Layer, font_t *Font, char const *Format, ...)
{
    char Text[8192];

    va_list ArgList;
    va_start(ArgList, Format);
    vsnprintf(Text, 8192, Format, ArgList);
    va_end(ArgList);

    vec2 Cursor = vec2(Dest.x, Dest.y - Font->LineHeight);

    char *CharCurs = Text;
    
    while(*CharCurs != '\0')
    {
        if((u32)*CharCurs == 10) /* new line */
        {
            Cursor.x = Dest.x;
            Cursor.y = Cursor.y - Font->LineHeight;
            ++CharCurs;
            continue;
        }

        glyph_t *Glyph = Font->Glyphs + ((u32)*CharCurs - 32); // << Temporary, Double checked values for all the glyphs in the text. Issue is not here.

        vec4 TexCoord = vec4(Glyph->Rect.x / Font->ScaleW, 1.0f - (Glyph->Rect.y + Glyph->Rect.height) / Font->ScaleH,
                            (Glyph->Rect.x + Glyph->Rect.width) / Font->ScaleW, 1.0f - Glyph->Rect.y / Font->ScaleH);
		
  		// Rect(x, y, width, height) (x,y lower left corner.)
        rect Dest = Rect(Cursor.x + Glyph->XOffset, Cursor.y - Glyph->YOffset, Glyph->Rect.width, Glyph->Rect.height); // << Already verifed XOffset and YOffset with XML file, (This might be the line where the issue is)
        
        PushSprite(Vertices, Dest, TexCoord, Color, 0.0f, vec2(0.0f), Layer);
        
        ++CharCurs;
        Cursor.x = Cursor.x + Glyph->XAdvance;
    }
}

Here is the screenshot when I subtract Glyph->YOffset from Cursor.y:

cursy_m_yoffset.PNG.cf1c76ea9da1aac94e4d9d8c432a1e64.PNG

Here is the screenshot when I don't subtract Glyph->YOffset from Cursor.y:

cursy.PNG.abba54d33e9d3356e846726e62dd219c.PNG

Font: Georia 42px

Any help would be appreciated. Thanks. 

 

UPDATE: Issue fixed.


rect Dest = Rect(Cursor.x + Glyph->XOffset, Cursor.y - Glyph->YOffset - Glyph->Rect.height, Glyph->Rect.width, Glyph->Rect.height);

 

I think it would help if you look at some existing code, and compare your solution.

You can have a look at my implementation here: http://svn.code.sf.net/p/actb/code/trunk/source/gfx/acgfx_font.cpp . The rendering of the characters happens in 


void CFont::InternalWrite(float x, float y, float z, const char *text, int count, float spacing) 

Make sure you consider the orientation of your coordinate system. In my implementation the coordinate system is left-hand oriented, i.e. (0,0) in the lower left corner of the screen, and Y grows upwards.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
2 hours ago, WitchLord said:

Make sure you consider the orientation of your coordinate system. In my implementation the coordinate system is left-hand oriented, i.e. (0,0) in the lower left corner of the screen, and Y grows upwards.

Thanks.

This topic is closed to new replies.

Advertisement