Word wrapping is pretty easy, here's a pseudo code implementation of how I usually do it:
Function DrawWrappedText( String TextToDisplay, Int StartX, Int StartY, Int WrapAtWidth )
{
Int lineStartIndex, lineEndIndex, lineWidth
Int drawY
String TextLine
lineStartIndex = 0
drawY = StartY
While( lineStartIndex < TextToDisplay.Length )
{
lineEndIndex = lineStartIndex
lineWidth = 0
https:// Find the first character that takes us beyond our limit (or to the end of the text)
While( lineWidth < WrapAtWidth || lineEndIndex == TextToDisplay.Length - 1 )
{
lineWidth += GetCharacterWidth( TextToDisplay[lineEndIndex] )
lineEndIndex++
}
https:// start tracking backwards looking for the first word break character (but only if it causes a wrap)
While( TextToDisplay[lineEndIndex] Not In ( ' ', '.', ',', '!', '?', ')', '-' ) && lineWidth >= WrapAtWidth )
{
lineEndIndex--
}
https:// Get and draw the substring
TextLine = SubString( TextToDisplay[lineStartIndex], lineEndIndex - lineStartIndex )
DrawText( TextLine, StartX, drawY )
https:// Move down a line (assuming all characters are same render height)
drawY += GetCharacterHeight( TextToDisplay[lineStartIndex] )
https:// Start from the next character after the break
lineStartIndex = lineEndIndex + 1
}
}
Hope that helps