Game Development Reference
In-Depth Information
This code relies on a _maxWidth member. If _maxWidth equals -1 then no
wrapping is done. Otherwise, the text is wrapped for the number of pixels spe-
cified in the _maxWidth value. Here's an extra constructor that will take in a
maximum width.
int _maxWidth = -1;
public Text(string text, Font font) : this(text, font, -1) { }
public Text(string text, Font font, int maxWidth)
{
_text = text;
_font = font;
_maxWidth = maxWidth;
CreateText(0, 0, _maxWidth);
}
Figure 7.6 shows text being wrapped using the new maxWidth parameter. It's
generated with the following piece of code.
Text longText = new Text("The quick brown fox jumps over the lazy dog",
_font, 400);
_renderer.DrawText(longText);
This word wrapping code doesn't take account of new line characters or tab
characters, but it wouldn't be hard to extend it.
Figure 7.6
Wrapping text.
 
Search WWH ::




Custom Search