I'm a VB.NET programmer, so this is probably not close to what you are looking after. However, just in case no one comes along and posts fully functional code, maybe this can give you an idea of what you may be able to do...I used an online converter for this BTW. This has been generically modified as best as I could while trying to remain functional, and with C++ there is probably a much more efficient method of achieving the results you are looking for.
Regardless, here I have a list of strings, the length of the string, and what I want to set to split the words with so it doesn't cut them off at the end:
private List
<string> _allTextLines
{ get; set; }const int _wordWrapLength
= 50;private readonly char[] _splitChars
= new char[] { ' ',
'-',
ControlChars
.Tab}; You entire text string is going to pass here to populate the list with strings of words:
public void AddText
(string msg
){ _allTextLines
= new List
<string>(); foreach (string str
in WordWrap
(msg, _wordWrapLength
)) { _allTextLines
.Add(str
); }} Here is where it is wrapping the lines to be put in sections so the lines aren't split in the middle of a word, then returning it to the method above:
public List
<string> WordWrap
(string str,
int width
){ string[] words
= Explode
(str, _splitChars
); int curLineLength
= 0; StringBuilder strBuilder
= new StringBuilder
(); int i
= 0; List
<string> rtnString
= new List
<string>(); while (i
< words
.Length) { string word
= words
(i
); // If adding the new word to the current line would be too long, // then put it on a new line (and split it up if it's too long). if (curLineLength
+ word
.Length > width
) { // Only move down to a new line if we have text on the current line. // Avoids situation where wrapped whitespace causes emptylines in text. if (curLineLength
> 0) { strBuilder
.Append("|"); curLineLength
= 0; } // If the current word is too long to fit on a line even on it's own then // split the word up. while (word
.Length > width
) { strBuilder
.Append(word
.Substring(0, width
- 1) + "-"); word
= word
.Substring(width
- 1); strBuilder
.Append("|"); } // Remove leading whitespace from the word so the new line starts flush to the left. word
= word
.TrimStart(); } strBuilder
.Append(word
); curLineLength
+= word
.Length; i
+= 1; } string[] lines
= strBuilder
.ToString.Split("|"); foreach (string line
in lines
) { rtnString
.Add(line
.Replace("|",
"")); // & vbNewLine) } return rtnString
;} And here is where it is splitting the words at a space or hyphen, returning it to WordWrap:
public string[] Explode
(string str,
char[] splitChars
){ string[] functionReturnValue
= null; List
<string> parts
= new List
<string>(); int startIndex
= 0; functionReturnValue
= null; while (true) { int index
= str
.IndexOfAny(splitChars, startIndex
); if (index
== -1) { parts
.Add(str
.Substring(startIndex
)); return parts
.ToArray(); } string word
= str
.Substring(startIndex, index
- startIndex
); char nextChar
= str
.Substring(index,
1)(0); // Dashes and the likes should stick to the word occuring before it. Whitespace doesn't have to. if (char.IsWhiteSpace(nextChar
)) { parts
.Add(word
); parts
.Add(nextChar
.ToString()); } else { parts
.Add(word
+ nextChar
); } startIndex
= index
+ 1; } return functionReturnValue
;} I'm not going to post what you need to do to draw the text in your list of strings, because the code I have doing that is not yet ready to be presented because of how I am drawing the strings in a pseudo-control multiline textbox. However, you would do something like this pseudo code:
AddText(a_whole_lot_of_text)
For each textLine As String in _allTextLines
RenderWindow.Draw(textLine)
//offset Y axis here
Next //textLine
I'm 99% sure there is a way to get the width of the font so you can set the limit of the _wordWrapLength, but I have been too busy with other things to look into that. If you happen across it feel free to share. I hope this helps in some way