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

Author Topic: sf::Text wrap?  (Read 12429 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Text wrap?
« Reply #15 on: April 18, 2014, 10:30:56 pm »
If you write \n in the text file, it's not the same as \n inside a string literal in your code. The former are two characters backlash and n, whereas the latter is an escape sequence newline.

Of course, reading \\n from the file won't help. What you have to do is replace the two characters from the file with a single newline character.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

zakkor

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: sf::Text wrap?
« Reply #16 on: April 18, 2014, 11:31:18 pm »
I have absolutely no idea how to use sf::Font, sf::Glyph and sf::Text to do this, lol.
As far as I understand it, I should get the width (in pixels, right?) of the string using sf::Glyph, then check to see if it's bigger than the width of the window, and if it is, insert a newline at the nearest space?
This is really confusing, I don't know where to begin :(

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Text wrap?
« Reply #17 on: April 18, 2014, 11:55:25 pm »
I have absolutely no idea how to use sf::Font, sf::Glyph and sf::Text to do this, lol.
That's why there is a documentation.

Yes, you can sum up the character widths until a certain threshold (in your case at the window border), and the word that exceeds that threshold is moved to the next line. But as Jesper Juhl already mentioned, other people have already had this problem, so it might be worth searching first.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Text wrap?
« Reply #18 on: April 19, 2014, 10:35:10 am »
Calculating a character position is trickier than just accumulating the glyphs' widths. For this reason, there's a sf::Text::getCharacterPos function which gives you what you want. Call it for every position (from 0 to text size), and when result.x becomes greater than your limit, insert a \n. And start again after the line break.
Laurent Gomila - SFML developer

 

anything