Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: "Automatic" line breaking  (Read 3276 times)

0 Members and 1 Guest are viewing this topic.

ineed.help

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
"Automatic" line breaking
« 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: "Automatic" line breaking
« Reply #1 on: June 02, 2014, 03:46:48 pm »
Please follow the rules.

"Doesn't work, <code>, help" is not a valid help request. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Syntactic Fructose

  • Jr. Member
  • **
  • Posts: 80
  • Overflowing stacks and eating snacks
    • View Profile
Re: "Automatic" line breaking
« Reply #2 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: "Automatic" line breaking
« Reply #3 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.
Laurent Gomila - SFML developer

ineed.help

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: "Automatic" line breaking
« Reply #4 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.
« Last Edit: June 02, 2014, 06:38:24 pm by ineed.help »