Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualZylann

Posted 20 December 2012 - 08:06 PM

Hi,
I'm implementing a custom BMFont renderer in C++, but I get weird results with xoffset and yoffset :
Posted Image
The green-red lines are the origin axes,
yellow rects are not offset char rects,
cyan rects are offset char rects.

Characters are not displayed the right way, but I can't find what I'm doing wronng :(
I believe it's not a bug of BMFont, as I used the same font in a Java game with Slick2D and it worked fine.

Here is my drawing code :
[source lang="cpp"]void Font::draw(const std::string text){ unsigned int originX = 0, originY = 0; // Cursor position unsigned int x, y; // Shifted position float tx, ty, tw, th; // Texture sub-rect coordinates char c; // Current read character glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); for(unsigned int i = 0; i < text.size(); i++) { c = text[i]; // Line endings if(c == '\n') { originY += m_settings.lineHeight; originX = 0; continue; } else if(c == '\r') { continue; } // Get character descriptor const CharDescriptor * cd = m_settings.getChar©; if(cd == nullptr) { //std::cout << "E" << (int)c; break; } // Use the glyph atlas texture const sf::Texture &amp; tex = m_textures[cd->page]; tex.bind(); // Get glyph texture sub-rect const sf::Vector2u ts = tex.getSize(); tx = static_cast<float>(cd->x) / static_cast<float>(ts.x); ty = static_cast<float>(cd->y) / static_cast<float>(ts.y); tw = static_cast<float>(cd->width) / static_cast<float>(ts.x); th = static_cast<float>(cd->height) / static_cast<float>(ts.y); // Get drawing coordinates x = originX + cd->xoffset; y = originY + cd->yoffset; // Draw glyph glBegin(GL_QUADS); glTexCoord2f(tx, ty + th); glVertex2i(x, y); glTexCoord2f(tx + tw, ty + th); glVertex2i(x + cd->width, y); glTexCoord2f(tx + tw, ty); glVertex2i(x + cd->width, y + cd->height); glTexCoord2f(tx, ty); glVertex2i(x, y + cd->height); glEnd(); // Advance cursor originX += cd->xadvance; // DEBUG // Some code to draw the rects }}[/source]

The full implementation source code is available here : https://dl.dropbox.com/u/60408088/INFO/bmfont_impl_src_part.zip
But it is not really useable alone because it has a few dependencies to other parts of a bigger project (will be on github in the future).

#2Zylann

Posted 20 December 2012 - 08:05 PM

Hi,
I'm implementing a custom BMFont renderer in C++, but I get weird results with xoffset and yoffset :
Posted Image
The green-red lines are the origin axes,
yellow rects are not offset char rects,
cyan rects are offset char rects.

Characters are not displayed the right way, but I can't find what I'm doing wronng :(
I believe it's not a bug of BMFont, as I used the same font in a Java game with Slick2D and it worked fine.

Here is my drawing code :
[source lang="cpp"]void Font::draw(const std::string text){ unsigned int originX = 0, originY = 0; // Cursor position unsigned int x, y; // Shifted position float tx, ty, tw, th; // Texture sub-rect coordinates char c; // Current read character glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); for(unsigned int i = 0; i < text.size(); i++) { c = text[i]; // Line endings if(c == '\n') { originY += m_settings.lineHeight; originX = 0; continue; } else if(c == '\r') { continue; } // Get character descriptor const CharDescriptor * cd = m_settings.getChar©; if(cd == nullptr) { //std::cout << "E" << (int)c; break; } // Use the glyph atlas texture const sf::Texture &amp; tex = m_textures[cd->page]; tex.bind(); // Get glyph texture sub-rect const sf::Vector2u ts = tex.getSize(); tx = static_cast<float>(cd->x) / static_cast<float>(ts.x); ty = static_cast<float>(cd->y) / static_cast<float>(ts.y); tw = static_cast<float>(cd->width) / static_cast<float>(ts.x); th = static_cast<float>(cd->height) / static_cast<float>(ts.y); // Get drawing coordinates x = originX + cd->xoffset; y = originY + cd->yoffset; // Draw glyph glBegin(GL_QUADS); glTexCoord2f(tx, ty + th); glVertex2i(x, y); glTexCoord2f(tx + tw, ty + th); glVertex2i(x + cd->width, y); glTexCoord2f(tx + tw, ty); glVertex2i(x + cd->width, y + cd->height); glTexCoord2f(tx, ty); glVertex2i(x, y + cd->height); glEnd(); // Advance cursor originX += cd->xadvance; // DEBUG // Some code to draw the rects }}[/source]

The full implementation source code is available here : https://dl.dropbox.com/u/60408088/INFO/bmfont_impl_src_part.zip
But it is not really useable alone because it has a few dependencies to other parts of a bigger project (will be on github in the future).

#1Zylann

Posted 20 December 2012 - 08:03 PM

Hi,
I'm implementing a custom BMFont renderer in C++, but I get weird results with xoffset and yoffset :
Posted Image
The green-red lines are the origin axes,
yellow rects are not offset char rects,
cyan rects are offset char rects.

I believe it's not a bug of BMFont, as I used the same font in a game in Java with Slick2D and it worked fine.

Here is my drawing code :
[source lang="cpp"]void Font::draw(const std::string text){ unsigned int originX = 0, originY = 0; // Cursor position unsigned int x, y; // Shifted position float tx, ty, tw, th; // Texture sub-rect coordinates char c; // Current read character glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); for(unsigned int i = 0; i < text.size(); i++) { c = text[i]; // Line endings if(c == '\n') { originY += m_settings.lineHeight; originX = 0; continue; } else if(c == '\r') { continue; } // Get character descriptor const CharDescriptor * cd = m_settings.getChar©; if(cd == nullptr) { //std::cout << "E" << (int)c; break; } // Use the glyph atlas texture const sf::Texture & tex = m_textures[cd->page]; tex.bind(); // Get glyph texture sub-rect const sf::Vector2u ts = tex.getSize(); tx = static_cast<float>(cd->x) / static_cast<float>(ts.x); ty = static_cast<float>(cd->y) / static_cast<float>(ts.y); tw = static_cast<float>(cd->width) / static_cast<float>(ts.x); th = static_cast<float>(cd->height) / static_cast<float>(ts.y); // Get drawing coordinates x = originX + cd->xoffset; y = originY + cd->yoffset; // Draw glyph glBegin(GL_QUADS); glTexCoord2f(tx, ty + th); glVertex2i(x, y); glTexCoord2f(tx + tw, ty + th); glVertex2i(x + cd->width, y); glTexCoord2f(tx + tw, ty); glVertex2i(x + cd->width, y + cd->height); glTexCoord2f(tx, ty); glVertex2i(x, y + cd->height); glEnd(); // Advance cursor originX += cd->xadvance; // DEBUG // Some code to draw the rects }}[/source]

The full implementation source code is available here : https://dl.dropbox.com/u/60408088/INFO/bmfont_impl_src_part.zip
But it is not really useable alone because it has a few dependencies to other parts of a bigger project (will be on github in the future).

PARTNERS