SFML community forums

Help => General => Topic started by: ineed.help on June 02, 2014, 02:35:08 pm

Title: "Automatic" line breaking
Post by: ineed.help on June 02, 2014, 02:35:08 pm
Hello. Apparently, making an automatic line break for a string isn't as easy as I had thought.

I tried using this code:
int main()
{
        sf::RenderWindow wnd(sf::VideoMode(640, 480, 32), "Video Game");
       
        int sizeOfLine = 0;
        int xPosition = 0; int yPosition = 0;
        char previousChar, currentChar;
        int fontSize = 24;

        std::vector<sf::Sprite> characters;
        sf::Sprite temporarySprite;
        sf::Font font;
        sf::Glyph glyph;
        font.loadFromFile("font.ttf");

        std::string msg("Hello! This is a text message. Because this message is so long, there has to be a line break somewhere.");

        for (int i = 0; i < msg.length(); i++)
        {
                        currentChar = msg[i];
                        glyph = font.getGlyph(currentChar, fontSize, false);

                        temporarySprite.setTexture(font.getTexture(fontSize));
                        temporarySprite.setTextureRect(glyph.textureRect);

                        if (i >= 1) { xPosition += font.getKerning(previousChar, currentChar, fontSize); }

                        sizeOfLine += temporarySprite.getTextureRect().width;

                        // THIS IS THE PART THAT REALLY MATTERS
                        if (sizeOfLine + fontSize >= wnd.getSize().x)
                        {
                                yPosition += font.getLineSpacing(fontSize);
                                xPosition = 0;
                                sizeOfLine = 0;
                        }
                       
                        temporarySprite.setPosition(xPosition, yPosition + glyph.bounds.top + font.getGlyph('A', fontSize, false).bounds.height);
                       
                        characters.push_back(temporarySprite);

                        xPosition += glyph.advance;

                        previousChar = currentChar;
        }
}

But it doesn't work as well as I had hoped. What can I do?
Title: Re: "Automatic" line breaking
Post by: eXpl0it3r on June 02, 2014, 03:46:48 pm
Please follow the rules (http://en.sfml-dev.org/forums/index.php?topic=5559.0).

"Doesn't work, <code>, help" is not a valid help request. ;)
Title: Re: "Automatic" line breaking
Post by: Syntactic Fructose on June 02, 2014, 04:12:51 pm
As exploit mentioned, we're not to sure what you're specifically asking for. Try being more clear and providing us with some output so we know what exactly is wrong.
Title: Re: "Automatic" line breaking
Post by: Laurent on June 02, 2014, 04:19:18 pm
You don't have to rewrite sf::Text. Just compute each character's position (text.findCharacterPos) and insert a line break ('\n') in the text's string everytime the x of the returned position is higher than your limit.
Title: Re: "Automatic" line breaking
Post by: ineed.help on June 02, 2014, 05:22:21 pm
Sorry, I was away; I know I don't have to rewrite sf::Text, but doing so is necessary for the other part of what I'm working on.
if (xPos + tempSprite.getTextureRect().width >= windowWidth) { ... }
This seems to work for me, so I'll stick with it. Thank you.