tibia-client/src/framework/font.cpp

151 lines
4.7 KiB
C++
Raw Normal View History

/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2011-04-06 21:46:58 +02:00
#include "font.h"
2011-04-07 02:58:36 +02:00
#include "resources.h"
#include "textures.h"
#include "graphics.h"
Font::Font() :
m_lineHeight(14),
m_cursorSize(14),
2011-04-07 09:30:32 +02:00
m_color(0xFFFFFFFF)
2011-04-07 02:58:36 +02:00
{
}
2011-04-06 21:46:58 +02:00
bool Font::load(const std::string& file)
{
2011-04-07 02:58:36 +02:00
if(!g_resources.fileExists(file)) {
error("Font file %s does not exists", file.c_str());
return false;
}
std::string fileContents = g_resources.loadTextFile(file);
if(!fileContents.size()) {
error("Empty font file \"%s", file.c_str());
return false;
}
std::istringstream fin(fileContents);
std::string textureName;
2011-04-07 09:30:32 +02:00
int numHorizontalGlyphs;
int firstGlyph;
Size glyphSize;
Size textureSize;
2011-04-07 02:58:36 +02:00
try {
YAML::Parser parser(fin);
YAML::Node doc;
parser.GetNextDocument(doc);
doc["line height"] >> m_lineHeight;
doc["cursor size"] >> m_cursorSize;
doc["color"] >> m_color;
2011-04-07 09:30:32 +02:00
doc["first glyph"] >> firstGlyph;
doc["image glyph size"] >> glyphSize;
2011-04-07 02:58:36 +02:00
doc["image"] >> textureName;
2011-04-07 09:30:32 +02:00
m_texture = g_textures.get("fonts/" + textureName);
if(!m_texture) {
error("Failed to load image for font \"%s\"", file.c_str());
return false;
}
textureSize = m_texture->getSize();
numHorizontalGlyphs = textureSize.width() / glyphSize.width();
2011-04-07 02:58:36 +02:00
2011-04-07 05:13:57 +02:00
const YAML::Node& widthsNode = doc["glyph widths"];
2011-04-07 02:58:36 +02:00
for(auto it = widthsNode.begin(); it != widthsNode.end(); ++it) {
2011-04-07 09:30:32 +02:00
int glyph, glyphWidth;
it.first() >> glyph;
it.second() >> glyphWidth;
// calculate glyph texture coords
m_glyphsTextureCoords[glyph].setRect(((glyph - firstGlyph) % numHorizontalGlyphs) * glyphSize.width(),
((glyph - firstGlyph) / numHorizontalGlyphs) * glyphSize.height(),
glyphWidth,
glyphSize.height());
// store glyph size
m_glyphsSize[glyph].setHeight(glyphSize.height());
m_glyphsSize[glyph].setWidth(glyphWidth);
2011-04-07 02:58:36 +02:00
}
} catch (YAML::ParserException& e) {
error("Malformed font file \"%s\"", file.c_str());
return false;
}
return true;
}
void Font::renderText(const Point& pos, const std::string& text)
{
2011-04-07 09:30:32 +02:00
// begin texture rendering
g_graphics.setColor(m_color);
g_graphics._beginTextureRender(m_texture.get());
2011-04-07 02:58:36 +02:00
Point currentPos = pos;
const Size& screenSize = g_graphics.getScreenSize();
int textLenght = text.length();
for(int i = 0; i < textLenght; ++i) {
int c = (int)text[i];
2011-04-07 09:30:32 +02:00
// break rendering if the Y pos is below the screen
2011-04-07 02:58:36 +02:00
if(currentPos.y >= screenSize.height())
break;
// new line
if(c == '\n') {
currentPos.y += m_lineHeight;
currentPos.x = pos.x;
}
2011-04-07 09:30:32 +02:00
// render glyph
else {
// render online if is visible on screen
if(currentPos.x < screenSize.width())
currentPos.x += renderGlyph(currentPos, c);
2011-04-07 02:58:36 +02:00
}
}
2011-04-07 09:30:32 +02:00
// end texture redering
g_graphics._endTextureRender();
g_graphics.resetColor();
2011-04-07 02:58:36 +02:00
}
int Font::renderGlyph(const Point& pos, int glyph)
{
2011-04-07 09:30:32 +02:00
// don't render invalid glyphs
if(glyph < 32)
return 0;
2011-04-07 02:58:36 +02:00
// render glyph
2011-04-07 09:30:32 +02:00
Rect screenCoords(pos, m_glyphsSize[glyph]);
g_graphics._drawTexturedRect(screenCoords, m_glyphsTextureCoords[glyph], m_texture->getSize());
2011-04-07 02:58:36 +02:00
2011-04-07 09:30:32 +02:00
return m_glyphsSize[glyph].width();
2011-04-06 21:46:58 +02:00
}