font improvments

This commit is contained in:
Eduardo Bart 2011-04-07 00:13:57 -03:00
parent 9464f99c90
commit 9bd9f4ba53
4 changed files with 15 additions and 13 deletions

View File

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

View File

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

View File

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

View File

@ -68,9 +68,13 @@ void MenuState::render()
texCoords.moveBottomRight(texSize.toPoint()); texCoords.moveBottomRight(texSize.toPoint());
g_graphics.drawTexturedRect(Rect(0, 0, screenSize), m_background.get(), texCoords); 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) 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) void MenuState::update(int ticks, int elapsedTicks)