Changed how the bitmap font prepares characters.

Playground was updated to reflect this.
This commit is contained in:
Harrison Deng 2020-07-04 13:35:38 -05:00
parent 0e3f1dfef3
commit 75ef59293f
2 changed files with 28 additions and 13 deletions

View File

@ -67,9 +67,33 @@ namespace SlatedGameToolkit.Framework.Graphics.Text
return textures[index];
}
public unsafe void Draw(MeshBatch batch, float x, float baseLine, string text, Color color) {
/// <summary>
/// Prepares the characters for rendering. All characters to be rendered in one frame should be loaded using this method.
/// This will also ensure that the bitmap font is big enough.
/// Calling this multiple times per frame is not good for performance as multiple texture uploads are occurring.
/// </summary>
/// <param name="characters">An array of characters to load. Won't load if already loaded.</param>
public void PrepareCharacterGroup(params char[] characters) {
int textureChanges = 0;
foreach (char c in characters)
{
if (!glyphTexLocations.ContainsKey((c, scale)) || !textures[glyphTexLocations[(c, scale)]].ContainsChar(c, scale)) {
glyphTexLocations.Remove((c, scale));
if (!textures[drawingTo].Upload(scale, c)) {
drawingTo++;
if (drawingTo >= textures.Length) drawingTo = 0;
FontTexture fontTexture = textures[drawingTo];
fontTexture.Clear();
fontTexture.Upload(scale, c);
textureChanges++;
if (textureChanges > textures.Length) throw new FrameworkUsageException(string.Format("Character group \"{0}\" takes up too much texture space! Consider increasing decreasing font size, or increasing texture lengths, or number of backing textures.", new string(characters)));
}
glyphTexLocations.Add((c, scale), drawingTo);
}
}
}
public unsafe void Draw(MeshBatch batch, float x, float baseLine, string text, Color color) {
float currentPoint = x;
char[] chars = text.ToCharArray();
for (int i = 0; i < chars.Length; i++) {
@ -80,19 +104,9 @@ namespace SlatedGameToolkit.Framework.Graphics.Text
context.GetViewport(out vX, out vY, out viewWidth, out viewHeight);
if (c != ' ') {
//Check if glyph is loaded, if not, load it.
//Check if glyph is loaded, if not, throw exception.
if (!glyphTexLocations.ContainsKey((c, scale)) || !textures[glyphTexLocations[(c, scale)]].ContainsChar(c, scale)) {
glyphTexLocations.Remove((c, scale));
if (!textures[drawingTo].Upload(scale, c)) {
drawingTo++;
if (drawingTo >= textures.Length) drawingTo = 0;
FontTexture fontTexture = textures[drawingTo];
fontTexture.Clear();
fontTexture.Upload(scale, c);
textureChanges++;
if (textureChanges > textures.Length) throw new FrameworkUsageException(string.Format("String \"{0}\" takes up too much texture space! Consider increasing decreasing font size, or increasing texture lengths, or number of backing textures. Attempted to swap {1} times within draw call.", text, textureChanges));
}
glyphTexLocations.Add((c, scale), drawingTo);
throw new FrameworkUsageException(string.Format("Character \'{0}\' was not prepared and is missing!", c));
}
FontTexture texture = textures[glyphTexLocations[(c, scale)]];

View File

@ -80,6 +80,7 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground
font = new BitmapFont("Resources/Playground/earwig_factory_rg.ttf");
font.PixelHeight = 128;
font.PrepareCharacterGroup("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray());
}
public void Render(double delta)