Skip to content

Commit 2b1b937

Browse files
committed
Feat: Add GenerateAtlas method to BitmapFontAtlas for custom glyph rendering
1 parent 37f1d99 commit 2b1b937

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

ShapeEngine/Text/BitmapFontAtlas.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,44 @@ public void GenerateAtlas(ColorRgba glyphColor, ColorRgba backgroundColor, bool
9191
if(writeDebugInfo) Console.WriteLine("Atlas generation completed.");
9292
}
9393

94+
/// <summary>
95+
/// Generates the bitmap font atlas using a custom cell drawing function.
96+
/// Fills the atlas with the specified background color and uses the provided drawCell action to render each glyph.
97+
/// Optionally outputs debug information during generation.
98+
/// </summary>
99+
/// <param name="backgroundColor">The background color to fill the atlas texture.</param>
100+
/// <param name="drawCell">
101+
/// The action to draw each glyph cell. Parameters: Rect (cell area), char (glyph), int (glyph width), int (glyph height).
102+
/// </param>
103+
/// <param name="writeDebugInfo">If true, writes debug information to the console during generation.</param>
104+
public void GenerateAtlas(ColorRgba backgroundColor, Action<Rect, char, int, int> drawCell, bool writeDebugInfo = false)
105+
{
106+
if (IsGenerated) throw new InvalidOperationException("Atlas already generated.");
107+
108+
// Create a blank atlas texture
109+
atlasTexture = Raylib.LoadRenderTexture(atlasWidth, atlasHeight);
110+
111+
if(writeDebugInfo) Console.WriteLine($"Atlas generation started with atlas size: {atlasWidth}x{atlasHeight}");
112+
Raylib.BeginTextureMode(atlasTexture);
113+
Raylib.DrawRectangle(0, 0, atlasWidth, atlasHeight, backgroundColor.ToRayColor());
114+
int i = 0;
115+
foreach (var c in supportedChars)
116+
{
117+
var glyphRect = new Rect(
118+
new Vector2(Padding + i * (glyphWidth + Padding), Padding),
119+
new Size(glyphWidth, glyphHeight),
120+
AnchorPoint.TopLeft
121+
);
122+
glyphUvRects[c] = glyphRect;
123+
font.Draw(c, glyphRect, drawCell);
124+
if(writeDebugInfo) Console.WriteLine($"Glyph '{c}' with custom {glyphRect}");
125+
i++;
126+
}
127+
Raylib.EndTextureMode();
128+
IsGenerated = true;
129+
Raylib.SetTextureFilter(atlasTexture.Texture, TextureFilter.Point);
130+
if(writeDebugInfo) Console.WriteLine("Atlas generation completed.");
131+
}
94132

95133
/// <summary>
96134
/// Draws the specified text using the atlas, fitting each character into a grid within the given rectangle.

0 commit comments

Comments
 (0)