Merganen
Merganen

Reputation: 97

Using an Image as for Font in XNA (SpriteFont? Or something else?)

So I've gotten to the portion of a 2D XNA-based top-down hack-and-slash project I'm working on where I need to draw text onto the screen. What I want to use is a SpriteSheet-esque image I found with the characters and symbols I'm going to need for this project.

Now, I've done a little bit of reading on this before asking, and the only two ways of putting a "Font" or something onto the screen is by using Fonts already installed on the computer, or, according to this, I have to create a Custom Content Processor?

My question is, if I have an image (say, in PNG format), that has all the letters/characters I want on it, how do I use those letters in my 2D game? And it doesn't necessarily have to be an XNA-based solution.

Thanks in advance.

Upvotes: 2

Views: 2099

Answers (1)

You draw it exactly how you would draw any other sprite from a sprite sheet.

You might already know that the SpriteBatch.Draw method has some overloads that can take a Rectangle that represents the source from your sprite-sheet.

you can keep track of your relation from chars to sprite rectangle with a dictionary

Dictionary<char, Rectangle> fontDict = new Dictionary<char,Rectangle>();
fontDict.Add('A', new Rectangle(/*params representing source of A*/);

and you can your word with a for or foreach loop

for(int i = 0; i<str.Count; i++)
{
    Rectangle spriterect = fontDict[str[i]];

    SpriteBach.Draw(/*params*/);
}

Keep in mind that you also have to manage the spacing of the letters out on your own, but it's possible to do on your own.

It does however Look like the ContentManager supports creating SpriteFonts based off of image formats such as jpg and png, so I'd say you might just be better off exploring that


further googling yields the FontTextureProcessor Class, which might be helpful.

Upvotes: 2

Related Questions