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:
Here is the screenshot when I don't subtract Glyph->YOffset from Cursor.y:
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);