-
Notifications
You must be signed in to change notification settings - Fork 2
Bitmapped Fonts
Charles Ambrye edited this page Feb 11, 2022
·
1 revision
== Bitmapped Fonts == The OS supports writing bitmapped fonts to a linear framebuffer. The bitmapped font can be created using BMFont (or any other font texture atlas generation tool). The included open source font was exported with constant height, 16 pixels, png.
The png file is converted to a flat binary bitmap file (32bpp). This results in a .bin file. Here is example code for converting a .png to a .bin:
var image = (Bitmap)Bitmap.FromFile(@"Incons16.png");
using (BinaryWriter output = new BinaryWriter(File.Open(@"Incons16.bin", FileMode.Create)))
{
for (int y = 0; y < image.Height; y++)
{
for (int x = 0; x < image.Width; x++)
{
var pixel = image.GetPixel(x, y);
uint r = pixel.R;
uint g = pixel.G;
uint b = pixel.B;
uint color = (r << 16) | (g << 8) | b;
output.Write(pixel.R);
}
}
}
The .fnt generated by BMFont is processed at filesystem generation time by BMFont.cs to be a more concise binary format. Both of these files can be loaded from within the OS to create a BitmapFont
object.
byte[] fontData = ReadFile(charData);
byte[] textureData = ReadFile(texture);
var ptr = Memory.Utilities.ObjectToPtr(fontData) + 8; // skip over byte array length/size and use values stored by diskmaker
return new BitmapFont(Memory.Utilities.PtrToObject<BitmapFont.Character[]>(ptr), textureData, 256, 128);
This code can then be used to write to the linear framebuffer.
font.DrawString("Hello from C#!", 20, 20, bga.FrameBufferAddress, 1280, 720);