Ask a questionAsk a question
 

AnswerHow do you draw text?

  • Tuesday, December 12, 2006 4:51 AMBapa Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Tuesday, December 12, 2006 6:52 AMMark Anderson - DVU Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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

  • Tuesday, December 12, 2006 5:17 AMJim PerryMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Why not just use Console.WriteLine or System.Debug?
  • Tuesday, December 12, 2006 6:52 AMMark Anderson - DVU Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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.

  • Tuesday, December 12, 2006 12:05 PMJubber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Tuesday, December 12, 2006 3:14 PMJubber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

  • Tuesday, December 12, 2006 6:54 PMWilderLand Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Thursday, December 14, 2006 6:10 AMBapa Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks a lot, fellas. I already uses BitmapFont the first time one of you fellas posted a link.

    It works great. I use Comic :)

  • Sunday, December 31, 2006 9:10 PMBECK Games Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Sunday, December 31, 2006 9:14 PMBapa Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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 :|

  • Monday, January 01, 2007 8:51 PMBECK Games Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Monday, January 01, 2007 8:55 PMJim PerryMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    If you're talking about mixing 2D and 3D see here.
  • Tuesday, January 02, 2007 5:50 AMBECK Games Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks alot Jim.. that was just what I was looking for...  works great!

    - Wes
  • Tuesday, January 02, 2007 3:36 PMlordJapheth Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Tuesday, January 02, 2007 3:45 PMJim PerryMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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?
  • Tuesday, January 02, 2007 9:03 PMlordJapheth Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Tuesday, January 02, 2007 9:36 PMAdamusTheGreat Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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).
  • Tuesday, January 02, 2007 10:30 PMlordJapheth Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you, that did the trick. The property was called "Copy to Output Directory" and I set it to "Copy always"