XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

How do I use a SpriteFont?

Last post 9/6/2009 8:52 PM by Kyle Jacobs. 5 replies.
  • 9/5/2009 7:52 PM

    How do I use a SpriteFont?

    The documentation on msdn only produces errors.

    The demo has 5 errors and won't run (it lacks a ton of things like proper references and the entire content) and documentation on the SpriteFont class is lacking crucial information and the example produces errors as well.

    Apparently at least one <CharacterRegion> with and <Start> and <End> is needed, but what values do they need if I have no unicode characters and just want to display simple text?

    I copied the example XML from the msdn documentation and substituted "Courier New" with "Comic Sans":
    1 <?xml version="1.0" encoding="utf-8"?> 
    2  
    3 <XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics"
    4   <Asset Type="Graphics:FontDescription"
    5     <FontName>Comic Sans</FontName> 
    6     <Size>18</Size> 
    7     <Spacing>0</Spacing> 
    8     <UseKerning>true</UseKerning> 
    9     <Style>Regular</Style> 
    10     <CharacterRegions> 
    11       <CharacterRegion> 
    12         <Start>32</Start> 
    13         <End>127</End> 
    14       </CharacterRegion> 
    15     </CharacterRegions> 
    16   </Asset> 
    17 </XnaContent> 

    This gets me the following error for lines 12 and 13 (start and end):
    There was an error while deserializing intermediate XML. String must be exactly one character long.   

    If I change <Start> to 1 and <End> to 9, I get the following error:
    Unsupported type. Cannot find a ContentTypeWriter implementation for Microsoft.Xna.Framework.Content.Pipeline.Graphics.FontDescription.

    The code I use for displaying it in the Draw method:
    1 string text = "Error"
    2 SpriteFont sf = Content.Load<SpriteFont>("Font"); //Content\Font.xml 
    3 Vector2 origin = sf.MeasureString(text); 
    4 foreground.DrawString(sf, text, pos, Color.Black, 
    5 0, origin, 1.0f, SpriteEffects.None, 0.5f); 

    Really, all I want is do display some text, why is it so difficult?
    I'd really appreciate if someone could tell me a way that really works.

    Thanks.
  • 9/5/2009 8:09 PM In reply to

    Re: How do I use a SpriteFont?

    Answer
    Reply Quote
    You need to specify them in hex:

          <CharacterRegion> 
            <Start>&#32;</Start> 
            <End>&#126;</End> 
          </CharacterRegion> 
     

    Note the &# characters.

    Yeah, not real intuitive.
    Plan B Episode 1 in Peer Review!

    Kyle Jacobs - Plan B Game Studios Inc.
  • 9/6/2009 10:02 AM In reply to

    Re: How do I use a SpriteFont?

    I still get this error:
    Unsupported type. Cannot find a ContentTypeWriter implementation for Microsoft.Xna.Framework.Content.Pipeline.Graphics.FontDescription.   
    (has no line number, just the xml file)


    I think I should also note that I'm using XNA 3.0, but in the "Add New Item" dialog when right clicking on the game project, there is no font.
    I only have these new items to select from:
    • Game Content
    • Content Importer
    • Content Processor
    • Content Type Reader
    • Content Type Writer

  • 9/6/2009 4:18 PM In reply to

    Re: How do I use a SpriteFont?

    Answer
    Reply Quote
    You need to add content files to the nested Content subproject, not to the main game project.
    XNA Framework Developer - blog - homepage
  • 9/6/2009 8:24 PM In reply to

    Re: How do I use a SpriteFont?

    I did. It's right between the two images I can display without trouble.
    Also, that obviously doesn't help at all when the project won't compile due to an error.
  • 9/6/2009 8:52 PM In reply to

    Re: How do I use a SpriteFont?

    Wow, that is totally unexpected. If the file has the .spritefont extension, is located in your content subproject, check the properties of the .spritefont file by right-clicking the file and selectecting "Properties".

    Make sure the Content Importer is set to Sprite Font Description, and that the Content Processor is also set to Sprite Font description.

    If all of this is true, your installation of XNA framework may be broken. Try reinstalling the XNA framework?
    Plan B Episode 1 in Peer Review!

    Kyle Jacobs - Plan B Game Studios Inc.
Page 1 of 1 (6 items) Previous Next