# My own bitmap font: Question about speed

## Recommended Posts

javasirc    199
I need some fonts so i can draw text in my game, but i thaught about this and i was wondering: Would making my own font class that loads a Texture2D in the form of a tileset, where each "tile" is the image of a char, and using the regular spritebatch.Draw() method to display them be very slow performance? PS: I already designed how it would work, but i havnt programmed it yet because im not sure if it will run fast enough in my isometric game. Or do i need to choose a font library and just use that?

##### Share on other sites
dave    2187
This is actually one of the fastest ways to render text because you can batch nearly all of the text into a single draw call, providing it is all written in the font that is on a given tileset. I highly recommend that you choose a solution that doesn't involve much work. For debug text or any other very plain text you can use ID3DXFont. The simplest thing for you to write is one that loads a precompiled font sheet and provides kerning information. You simply load the font sheet image and also a kerning file that is produced by the same tool and coordinate the two.

If you feel like more work, you can load the font pictures, called glyphs, manually using something like FreeType2 (which does alot of the work for you.

Hope that helps,

##### Share on other sites
javasirc    199
Well what i had designed was i take a string like "Hello, my name is Chris!", send it to the font class i make, and the class will process and display each character of the string.

So to display 'H' in the string, i get the tile ID of 'H' using a switch()

GetIDFromCharValue(char cChar)
{
switch(cChar)
{
//returns the ID of the image in the Texture2D Tileset
case 'A': return 0;
case 'B': return 1;
...
case 'H': return 7;
...
}
}

Or by converting the char to ascii then using the ascii code in the switch (i forget how i designed it, the paper i designed it on is somewhere around here)

Then take the rectangle based on the returned ID and use spritebatch.Draw(...) to display the character on screen.

The full code would look something like this:

MyFontClass.DrawString("Hello, my name is Chris!");

public void DrawString(string str) //in the class of course
{
for(int cur=0;cur<str.Length;cur++)
{
spritebatch.Draw(GetRectangleFromSourceID(GetIDFromCharValue((char)str[cur])));
}

Ive already got GetRectangleFromSourceID() programmed, as my tile game uses it...
But thats basicly how i would display text on the screen. Will that run fast enough as a regular font library?

##### Share on other sites
dave    2187

There are plenty of articles out there.

##### Share on other sites
Just a little nitpick, it would be much simpler to write this function

GetIDFromCharValue(char cChar){switch(cChar){//returns the ID of the image in the Texture2D Tilesetcase 'A': return 0;case 'B': return 1;...case 'H': return 7;...}}

As this

// Where is your return value? I'll make it intint GetIDFromCharValue(char cChar){return (int)(cChar - 'A');}

##### Share on other sites
javasirc    199
I dont really understand...

return (int)(cChar - 'A'); returns 65-65=0......

So if i did H, it would be 'H'-'A' or 'H'-'H'... which doesnt do anything for me either way.

The "return 0" i used is simply an ID that represends A. Much like 65 represends A in ASCII. But im not using ascii (although i should and i might change my code to match ASCII later).

Anyway i got the font class working... just dont know how to test the performance and compare it to the speed of other font libraries to see if its actually worth using.

'A' - 'A' = 0

'H' - 'A' = 7

##### Share on other sites
javasirc    199
... I still dont understand how that helps my code.

Anyway im probably going to just convert to ascii "(int)cChar" tomorrow and use the ascii values as IDs so it has more portability if i need it for the regular characters, and keep using the IDs for glyphs like i have now.

##### Share on other sites
zdlr    266
You said you were going to write a function which would take as input an ascii character and return a character ID code. The process you were using to convert this was a big switch statement. This would be redundant since ID could be easily calculated, as was shown.

Using your ascii character as an ID circumvents this need, but make sure you will never require unicode strings to be displayed.