From 9bd9f4ba53630713c65b29516f8176fcb6cfb221 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Thu, 7 Apr 2011 00:13:57 -0300 Subject: [PATCH] font improvments --- src/framework/font.cpp | 12 ++++++------ src/framework/font.h | 4 ---- src/framework/fonts.cpp | 4 +++- src/menustate.cpp | 8 ++++++-- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/framework/font.cpp b/src/framework/font.cpp index a26abcd5..bca01af5 100644 --- a/src/framework/font.cpp +++ b/src/framework/font.cpp @@ -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(); diff --git a/src/framework/font.h b/src/framework/font.h index 9225a534..81757841 100644 --- a/src/framework/font.h +++ b/src/framework/font.h @@ -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 diff --git a/src/framework/fonts.cpp b/src/framework/fonts.cpp index 214141c8..86c5ebb9 100644 --- a/src/framework/fonts.cpp +++ b/src/framework/fonts.cpp @@ -33,9 +33,11 @@ void Fonts::init() std::list 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(new Font); font->load("fonts/" + file); - m_fonts[font->getName()] = font; + m_fonts[name] = font; } } } diff --git a/src/menustate.cpp b/src/menustate.cpp index a1e65560..8c90eb65 100644 --- a/src/menustate.cpp +++ b/src/menustate.cpp @@ -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)