Skip to content

Commit

Permalink
fix: extend BitmapFont with IDisposable to unload module fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
agaertner committed Sep 19, 2023
1 parent f395c48 commit c9114ac
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
42 changes: 42 additions & 0 deletions Blish HUD/GameServices/Content/BitmapFont.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.BitmapFonts;
using System;
using System.Collections.Generic;

namespace Blish_HUD.Content {
/// <summary>
/// Extends <see cref="MonoGame.Extended.BitmapFonts.BitmapFont"/> to allow disposing of its glyph lookup texture.
/// </summary>
public class BitmapFont : MonoGame.Extended.BitmapFonts.BitmapFont, IDisposable {

private readonly Texture2D _texture;

/// <summary>
/// Creates a <see cref="BitmapFont"/> with the provided identifier name, glyph regions, line height, and texture to draw letters from.
/// </summary>
/// <param name="name">Name to identify the font with.</param>
/// <param name="regions">Regions of the glyphs on the <c>texture</c>.</param>
/// <param name="lineHeight">Line height of the font.</param>
/// <param name="texture">Lookup texture to draw letters from.</param>
public BitmapFont(string name, IEnumerable<BitmapFontRegion> regions, int lineHeight, Texture2D texture) : base(name, regions, lineHeight) {
_texture = texture;
}

/// <summary>
/// Creates a <see cref="BitmapFont"/> with the provided identifier name, glyph regions and line height.
/// </summary>
/// <param name="name">Name to identify the font with.</param>
/// <param name="regions">Regions of the glyphs on the <c>texture</c>.</param>
/// <param name="lineHeight">Line height of the font.</param>
public BitmapFont(string name, IReadOnlyList<BitmapFontRegion> regions, int lineHeight) : base(name, regions, lineHeight) {
_texture = regions[0].TextureRegion.Texture;
}

/// <summary>
/// Disposes the lookup texture of this <see cref="BitmapFont"/> to free memory. Renders this <see cref="BitmapFont"/> unusable.
/// </summary>
public void Dispose() {
_texture?.Dispose();
}
}
}
4 changes: 2 additions & 2 deletions Blish HUD/GameServices/Modules/Managers/ContentsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ public SpriteFont GetSpriteFont(string fontPath, int fontSize, int textureSize =
}

/// <summary>
/// Loads a <see cref="BitmapFont"/> from a TrueTypeFont (*.ttf) file.
/// Loads a <see cref="Content.BitmapFont"/> from a TrueTypeFont (*.ttf) file.
/// </summary>
/// <param name="fontPath">The path to the TTF font file.</param>
/// <param name="fontSize">Size of the font.</param>
/// <param name="lineHeight">Sets the line height. By default, <see cref="SpriteFont.LineSpacing"/> will be used.</param>
/// <param name="textureSize">Size of the <see cref="SpriteFont.Texture"/>.<br/>A greater <c>fontSize</c> results in bigger glyphs which may require more texture space.</param>
public BitmapFont GetBitmapFont(string fontPath, int fontSize, int lineHeight = 0, int textureSize = 1392) {
public Content.BitmapFont GetBitmapFont(string fontPath, int fontSize, int lineHeight = 0, int textureSize = 1392) {
return GetSpriteFont(fontPath, fontSize, textureSize)?.ToBitmapFont(lineHeight);
}

Expand Down
10 changes: 5 additions & 5 deletions Blish HUD/_Extensions/SpriteFontExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace Blish_HUD {
public static class SpriteFontExtensions {

/// <summary>
/// Converts a <see cref="SpriteFont"/> to a <see cref="BitmapFont"/>.
/// Converts a <see cref="SpriteFont"/> to a <see cref="Content.BitmapFont"/>.
/// </summary>
/// <param name="font">The <see cref="SpriteFont"/> to convert.</param>
/// <param name="lineHeight">Line height for the <see cref="BitmapFont"/>. By default, <see cref="SpriteFont.LineSpacing"/> will be used.</param>
/// <returns>A <see cref="BitmapFont"/> as result of the conversion.</returns>
public static BitmapFont ToBitmapFont(this SpriteFont font, int lineHeight = 0) {
/// <param name="lineHeight">Line height for the <see cref="Content.BitmapFont"/>. By default, <see cref="SpriteFont.LineSpacing"/> will be used.</param>
/// <returns>A <see cref="Content.BitmapFont"/> as result of the conversion.</returns>
public static Content.BitmapFont ToBitmapFont(this SpriteFont font, int lineHeight = 0) {
if (lineHeight < 0) {
throw new ArgumentException("Line height cannot be negative.", nameof(lineHeight));
}
Expand All @@ -38,7 +38,7 @@ public static BitmapFont ToBitmapFont(this SpriteFont font, int lineHeight = 0)
regions.Add(region);
}

return new BitmapFont($"{typeof(BitmapFont)}_{Guid.NewGuid():n}", regions, lineHeight > 0 ? lineHeight : font.LineSpacing);
return new Content.BitmapFont($"{typeof(Content.BitmapFont)}_{Guid.NewGuid():n}", regions, lineHeight > 0 ? lineHeight : font.LineSpacing, font.Texture);
}

}
Expand Down

0 comments on commit c9114ac

Please sign in to comment.