2

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.

Community
  • 1
  • 1
Merganen
  • 67
  • 6

1 Answers1

2

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.

  • Thanks, Sam I am! The very last solution interests me greatly, as I have all of the things that the class would need for it to work properly. However, reading that class says that any black on the sprite sheet would be transparent. As the characters on my SpriteSheet are surrounded by black for their look, can you see anything in that FontTextureProcessor class about allowing black to be visible? Because the second solution you offered is what I'm currently using, and it uses the wrong characters for what I try to write. – Merganen Jul 26 '13 at 22:37