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

Using the Font Class Sample

Last post 05-06-2008 11:20 AM by Shawn Hargreaves. 18 replies.
  • 04-03-2007 3:59 AM

    Using the Font Class Sample

    Hey guys,

     

    I'm new to C# and XNA, but I have some knowledge of C++. I was making a pong game and I got to the point when the ball collides with the paddles and the walls, but now I want to add a score chart.

     

    To do that I tried to integrate the sample font class but i'm not sure how. I added the TrueTypeFont.xml, font.cs and fontReader.cs file to my project and I included the FontReader namespace in my game1.cs file, (I created a standard windows game project template).

     

    I created a spriteBatch called textBatch and an instance of the font class called type and then in the load content method I added:

     

                   type = content.Load<Font>("Content\\TrueTypeFont");

                   textBatch = new SpriteBatch(graphics.GraphicsDevice);

     

    and in the draw section I added:

     

                textBatch.Begin();
                type.Draw("Hello, World!", new Vector2(64, 12), 2, Color.Red, textBatch);
                textBatch.End();

     

    The project compiles just fine, but when I run it it hangs (i'm using vista so it says the checking for solutions thing). Usually that means that its trying to access a file but can't find it. Any ideas what i'm missing?

     

     

    Thanks 

  • 04-03-2007 4:29 AM In reply to

    Re: Using the Font Class Sample

    c26354:

                   type = content.Load<Font>("Content\\TrueTypeFont");

    try

    type = content.Load<Font>("Content\TrueTypeFont");

  • 04-03-2007 5:22 AM In reply to

    Re: Using the Font Class Sample

    thanks for the reply but that not the problem. I already have some sprites loaded and their textures are in the content folder. To reference them you have to use \\ because \ is a special character. The path of the TrueTypeFont is not the problem.
  • 04-03-2007 10:14 AM In reply to

    Re: Using the Font Class Sample

    You can use the @ character to create litteral strings that don't require the double slash so

     Content.load(@"content\truetypefont") will work just fine.

     
    Now on to your problem.

     The font sample uses the following xml format in order to tell it's content pipeline what to do

     

    - <XnaContent>
    - <Asset Type="FontPipeline.FontDescription">
      <Name>Comic Sans MS</Name>
      <Size>16</Size>
      <Style>Bold</Style>
      <Spacing>2</Spacing>
    - <Regions>
    - <Item>
      <Start></Start>
      <End></End>
      </Item>
      </Regions>
      </Asset>
      </XnaContent>
     
    If you don't have the described font on your system, then the conversion will fail. If you move the font sample into your own program, then you must remember to move the font pipeline and renderer too. 

     

    For all things there is a first time , a best time and a last.....
  • 04-03-2007 11:15 AM In reply to

    Re: Using the Font Class Sample

    c26354, you're not going crazy, cause I had the same problem on Sunday night.  I haven't had a chance to find out why exactly since I've been at work since then, but I loaded both the fontrenderer and the fontpipeline into my program and put them in the using statement.  Then I added both the TrueTypeFont and the BitMapFont in and tried both but got the same error. I know this isn't exactly "helpful" but at least this isn't an uncommon problem.
    http://www.freewebs.com/campelmxna/ - C# and XNA tutorials
    The only stupid mistake is the one you make twice
  • 04-03-2007 12:30 PM In reply to

    Re: Using the Font Class Sample

    Most likely you need to update the GetRuntimeReader method (in FontWriter.cs) to match the name of your game assembly. The second "FontRenderer" entry (after the type name, which is "FontRenderer.FontReader") is the name of the assembly that the type will be loaded into. If you added the runtime font files to your own project rather than just referencing the assembly built by the sample, these types will now be in a different assembly, so you must update FontWriter.cs to match their new location.
    XNA Framework Developer - blog - homepage
  • 04-03-2007 1:39 PM In reply to

    Re: Using the Font Class Sample

    I'll have to try that this weekend (I'm at work right now) but I'll try that first.  Like I said I hit this error right before bed so I only tried a few things.
    http://www.freewebs.com/campelmxna/ - C# and XNA tutorials
    The only stupid mistake is the one you make twice
  • 04-03-2007 5:19 PM In reply to

    Re: Using the Font Class Sample

    Thanks for the replies,

     

    Ok, I tried using the reference paths, I added a reference for the main folder that holds font.cs and I also added references to the three sub folders but when I type:

     

    using FontRenderer;

     

    it can't find the namespace thats in the font.cs file. Could someone please tell me how to reference other assemblies? And which files I need to add to my project.

     

    Thanks. 

  • 04-03-2007 6:46 PM In reply to

    Re: Using the Font Class Sample

    Answer

    haha, I just finished up my pong game and I ran into same problem (I hope the XNA team does work on making fonts better).

     If you load up the Sample Font's project and then select the \Content\TrueTypeFont.xml file and look at it's properties, you'll see the Content Importer and Content Processor's set.    In your project, you'll need to do the following:

    1. Right click on your project and select properties.
    2. Select the Content Pipeline
    3. Select Add
    4. Browse to your FontPipeline\bin\debug folder (or wherever you have the fontpipeline compiled)
    5. Select Open on the FontPipeline.dll.

    Then on the properties of the TrueTypeFont.xml file in your project

    1. Set the XNA Framework Content to True.
    2. Set the Content Importer to XML Content - XNA Framework.
    3. Set the Content Processor to TrueTypeFontProcessor.

    I did this by memory so just let me know if that doesn't do the trick and I'll dig through my project.

    HTHs!

  • 04-03-2007 7:52 PM In reply to

    Re: Using the Font Class Sample

    ok, I tried that and it still doesn't work.

     

    This is what I have so far:

    1) At the top of my files I put using FontRenderer, and I added the font.cs and fontReader.cs to my project by going to add existing files.

     

    2)I added the XML file by also going to add existing and I put it in a folder called content. I changed the properties like you said.

     

    3) I  added FontPipeline to the content pipeline like you said.

     

    4) Now for the actual code:

    I have the following outside of any methods but inside the game class:

            SpriteBatch textBatch;
                   
            Font type;

     

    The inside the load content method I have:

                   type = content.Load<Font>("Content\\TrueTypeFont");

                   textBatch = new SpriteBatch(graphics.GraphicsDevice);

     

    And inside the draw method I have:

                textBatch.Begin();
                type.Draw("Hello, World!", new Vector2(64, 12), 2, Color.Red, textBatch);
                textBatch.End();

     

    The project compiles but it hangs when I run it. So I set a break point at the  

    type = content.Load<Font>("Content\\TrueTypeFont");

     

    line and then the debugger give me the following error:

    Error loading "Content\TrueTypeFont". Cannot find ContentTypeReader FontRenderer.FontReader, FontRenderer, Version=1.0.0.0, Culture=neutral.

     

    I'm pretty sure something is wrong with the way I added the files to my project but i'm not sure what.

     

    Thanks for the help. 

  • 04-03-2007 8:02 PM In reply to

    Re: Using the Font Class Sample

    Answer
    In your game project you need to add a reference (right-click your project and select Add Reference) to the FontRendererWindows project (for Windows) or the FontRendererXbox (for XBox).  In my case, I just added the FontRendererWindows project to my Windows Solution and added a project reference (I did the same for my XBox) solution.  I'm sure a file reference will work too.
  • 04-03-2007 8:23 PM In reply to

    Re: Using the Font Class Sample

    Answer

    Thank you!!

     

    I've been racking my brain for so long on this one and I finally got it. First you have to add the FontRenderer project and then you have to add a reference to it. I finally got hello world to print:) 

     

    Thanks again for your help. 

  • 04-21-2007 5:59 PM In reply to