Game Development Reference
In-Depth Information
get { return _dimensions.Y; }
}
}
The dimension member needs to be updated every time the text is changed. There is
onlyoneplaceinthecodewherethetextgetschanged:the CreateText method.
private void CreateText(double x, double y)
{
_bitmapText.Clear();
double currentX = x;
double currentY = y;
foreach (char c in _text)
{
CharacterSprite sprite = _font.CreateSprite(c);
float xOffset = ((float)sprite.Data.XOffset) / 2;
float yOffset = ((float)sprite.Data.YOffset) / 2;
sprite.Sprite.SetPosition(currentX + xOffset, currentY - yOffset);
currentX += sprite.Data.XAdvance;
_bitmapText.Add(sprite);
}
_dimensions = _font.MeasureFont(_text);
SetColor();
}
One more line is added at the end to measure the size of the string. To confirm
this works, try centering the fps text, or render the fps text twice in a row, or
twice in a column.
The final functionality to be added to the text class is the ability to set a max
width. This will cause very long sentences to be wrapped onto a new line. This is
very useful when attempting to keep text in a textbox or ensuring text doesn't go
off the edge of the screen. Try to work through what the algorithmwould need to
do to format text with a max width.
Split the text into words.
Get the next word in the text.
Measure the length of the word.
If the current length is greater than the max width, start a new line.
 
Search WWH ::




Custom Search