Hello!
I have been working for quite a while now on a system to draw a series of sprite as if they were an sf::Text.
However, I recently took notice of a problem: if you look at this picture...
As you can see, there are some rather large gaps between some of the characters, and I don't quite understand why.
There is also a very large difference between the y-coordinates of the lines.
This is a sample of the code I have been using which contains my problem.
int main()
{
sf::RenderWindow window(sf::VideoMode(360, 240, 32), "Title");
sf::Font font;
font.loadFromFile("fonts/font.ttf");
std::vector<sf::Sprite> chars;
sf::Sprite tempSprite;
std::string str = "Hello! This is a string! This string is rather long, so it needs to be divided into multiple lines.";
unsigned short xPos = 0, yPos = 0;
unsigned char prevChar = 'A';
for (unsigned short i = 0; i < str.length(); i++)
{
tempSprite.setTexture(font.getTexture(24));
tempSprite.setTextureRect(font.getGlyph(str[i], 24, false).textureRect);
tempSprite.setColor(sf::Color::Black);
if (xPos + (2 * tempSprite.getTextureRect().width) >= window.getSize().x)
{
xPos = 0;
yPos += font.getLineSpacing(24);
}
else
{ if (i > 1) { xPos += font.getKerning(prevChar, str[i], 24); } }
tempSprite.setPosition(xPos, yPos + font.getGlyph(str[i], 24, false).bounds.top + font.getGlyph('A', 24, false).bounds.height); // should this be 'A' (?)
chars.push_back(tempSprite);
xPos += font.getGlyph(str[i], 24, false).advance;
prevChar = str[i];
}
unsigned short crtCharNo = 0;
while(window.isOpen())
{
window.clear(sf::Color::White);
if (crtCharNo < chars.size() - 1) { crtCharNo++; }
for (unsigned short i = 0; i <= crtCharNo; i++)
{ window.draw(chars[i]); }
window.display();
}
}
What am I doing wrong? I thought "getKerning" moves the characters closer, and "advance" moved them apart, but I am clearly doing something wrong.
(This assumes there is nothing wrong with the font I am using, which is called "Linux Libertine", although I doubt that's the case.)
Hopefully I explained myself in a decent manner. I would greatly appreciate any help.