font improvments

master
Eduardo Bart 13 years ago
parent 9464f99c90
commit 9bd9f4ba53

@ -34,7 +34,9 @@
Font::Font() :
m_lineHeight(14),
m_cursorSize(14),
m_color(Color::white)
m_color(Color::white),
m_firstGlyph(32),
m_numHorizontalGlyphs(16)
{
bzero(m_glyphWidths, sizeof(m_glyphWidths));
}
@ -62,7 +64,6 @@ bool Font::load(const std::string& file)
YAML::Node doc;
parser.GetNextDocument(doc);
doc["name"] >> m_name;
doc["line height"] >> m_lineHeight;
doc["cursor size"] >> m_cursorSize;
doc["color"] >> m_color;
@ -70,7 +71,7 @@ bool Font::load(const std::string& file)
doc["image"] >> textureName;
doc["image glyph size"] >> m_glyphSize;
const YAML::Node& widthsNode = doc["widths"];
const YAML::Node& widthsNode = doc["glyph widths"];
for(auto it = widthsNode.begin(); it != widthsNode.end(); ++it) {
int id, width;
it.first() >> id;
@ -84,7 +85,6 @@ bool Font::load(const std::string& file)
m_texture = g_textures.get("fonts/" + textureName);
m_numHorizontalGlyphs = m_texture->getSize().width() / m_glyphSize.width();
m_numVerticalGlyphs = m_texture->getSize().height() / m_glyphSize.height();
if(!m_texture) {
error("Failed to load image for font \"%s\"", file.c_str());
@ -128,7 +128,7 @@ void Font::renderText(const Point& pos, const std::string& text)
break;
}
// normal glyph
else if(c >= 32 && c <= 255) {
else if(c >= m_firstGlyph) {
currentPos.x += renderGlyph(currentPos, c);
}
}
@ -145,7 +145,7 @@ int Font::renderGlyph(const Point& pos, int glyph)
// calculate glyph coords on texture font
const Size& textureSize = m_texture->getSize();
int glyphTexCoordX = ((glyph - m_firstGlyph) % m_numHorizontalGlyphs) * m_glyphSize.width();
int glyphTexCoordY = ((glyph - m_firstGlyph) / m_numVerticalGlyphs) * m_glyphSize.height();
int glyphTexCoordY = ((glyph - m_firstGlyph) / m_numHorizontalGlyphs) * m_glyphSize.height();
float textureRight = (float)(glyphTexCoordX + glyphWidth) / textureSize.width();
float textureBottom = (float)(glyphTexCoordY + m_glyphSize.height()) / textureSize.height();
float textureTop = (float)(glyphTexCoordY) / textureSize.height();

@ -60,12 +60,9 @@ public:
*/
/// Render a text
const std::string& getName() const { return m_name; }
int renderGlyph(const Point& pos, int glyph);
private:
std::string m_name;
int m_lineHeight;
int m_cursorSize;
Color m_color;
@ -75,7 +72,6 @@ private:
int m_firstGlyph;
int m_glyphWidths[256];
int m_numHorizontalGlyphs;
int m_numVerticalGlyphs;
};
#endif // FONT_H

@ -33,9 +33,11 @@ void Fonts::init()
std::list<std::string> files = g_resources.getDirectoryFiles("fonts");
foreach(const std::string& file, files) {
if(boost::ends_with(file, ".yml")) {
std::string name = file;
boost::erase_first(name, ".yml");
std::shared_ptr<Font> font(new Font);
font->load("fonts/" + file);
m_fonts[font->getName()] = font;
m_fonts[name] = font;
}
}
}

@ -68,9 +68,13 @@ void MenuState::render()
texCoords.moveBottomRight(texSize.toPoint());
g_graphics.drawTexturedRect(Rect(0, 0, screenSize), m_background.get(), texCoords);
Font *font = g_fonts.get("sans14");
Font *font = g_fonts.get("sans-11px-antialised");
if(font)
font->renderText(Point(10,10), "hello\nworld!");
font->renderText(Point(10,10), "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ac orci id quam condimentum semper.");
font = g_fonts.get("tibia-10px-rounded");
if(font)
font->renderText(Point(10,30), "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ac orci id quam condimentum semper.");
}
void MenuState::update(int ticks, int elapsedTicks)

Loading…
Cancel
Save