How do you draw text?
Since the XNA documentation is lousy, I'm gonna ask here.
How do you create text in XNA? I want to use it for debugging purposes :(
Answers
The XNA Framework in itself does not supply a way to draw text on the screen. You would have to either download a component to do it for you, like BitMapFont, or make one yourself. Or you could just create a text file with streamwriter, and put all the information you need to output in there.
All Replies
- Why not just use Console.WriteLine or System.Debug?
The XNA Framework in itself does not supply a way to draw text on the screen. You would have to either download a component to do it for you, like BitMapFont, or make one yourself. Or you could just create a text file with streamwriter, and put all the information you need to output in there.
Here you go -
http://jubbernaut.blogspot.com/
I've made a little blog to explain how to very quickly plot a font on screen. Including a font I drew with my own fair hand!
The code has been simplified down to the bare minimum for ease of understanding - but a good font routine will have tons of options for transparency, scaling, kerning etc. And possibly different routines if you intend to plot a lot of text - some without the options to increase speed a little.
I've lost count of the number of font routines I've written, from back when you had to redefine the character set using VDU 23 commands on the BBC micro. C# is the winner so far for easy to write code.
Cheers,
Robin Jubber
Quick note - someone (anonymous) posted a question on the blog above asking "where do the characters come from" - well, in answer to your question Mr. Anonymous - with a call to the function along the following lines -
draw_font( x, y, "here is my text" );
and it will print out the entire line. If you need to include numbers, for instance checking a variable -
draw_font( x, y, "this variable is " + some_variable.ToString() );
Hope that helps
Cheers,
Robin.
I use BitMapFont as well, and just dump the data to the screen so I can see how the data I want to watch in real time changes as I play test the game.
BitMapFont is available here.
Mike Polzin
WilderLand Software
Thanks a lot, fellas. I already uses BitmapFont the first time one of you fellas posted a link.
It works great. I use Comic :)
- Hey Mike... I downloaded your BitMapFont class and it works great.. the only issue is now that i started to use it my 3d models look funky. It looks like the normals are all inverted. any suggestions?
Thanks,
Beck Yeh, bitmap font is great :)
I use it so much I even made a static library and link it to my project every time I need it :D - I would use a DLL, but nobody likes those :|
- yeh its really helpful.. but did anyone else experiance the problem of their 3D models looking messed up? i thought it was the face normals but it could also be the transparency on the model.. basically i can look through the model and see his feet from a top down perspective.
- If you're talking about mixing 2D and 3D see here.
- Thanks alot Jim.. that was just what I was looking for... works great!
- Wes - I tried using BitmapFont myself, but even though I copied all the code correctly from the example into my program, it tells me it cannot find the font. I included the .xml file and the .png file in the project. Could somebody tell me a detailed explanation of everything I need for the font to work correctly? I need to know what I have to include where, but I think the code is fine.
- Where did you put the image and XML files? Are they in a sub-folder in the project? If so, did you include the sub-folder in your path in referencing them? Can you post the relevant code?
- The files are in a folder called Content in the project. They show up in the solution explorer.
BitmapFont m_font;
protected override void Initialize()
{
// TODO: Add your initialization logic here
m_font = new BitmapFont("Content/times.xml");
base.Initialize();
}
The error is thrown in the BitmapFont class after calling the BitmapFont constructor. - Make sure that both the PNG file and the xml file are set to copyoutput = true (don't remember if thats the exact property name).
- Thank you, that did the trick. The property was called "Copy to Output Directory" and I set it to "Copy always"

