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

Using the Font Class Sample

Last post 5/6/2008 11:20 AM by Shawn Hargreaves. 18 replies.
  • 4/3/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 

  • 4/3/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");

  • 4/3/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.
  • 4/3/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.....
  • 4/3/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
  • 4/3/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
  • 4/3/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
  • 4/3/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. 

  • 4/3/2007 6:46 PM In reply to

    Re: Using the Font Class Sample

    Answer
    Reply Quote

    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!

  • 4/3/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. 

  • 4/3/2007 8:02 PM In reply to

    Re: Using the Font Class Sample

    Answer
    Reply Quote
    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.
  • 4/3/2007 8:23 PM In reply to

    Re: Using the Font Class Sample

    Answer
    Reply Quote

    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. 

  • 4/21/2007 5:59 PM In reply to

    Re: Using the Font Class Sample

    I want to thank all posters for their input!
    I was trying to get teh fonts on a 360 project as cleanly as possible.
    I suceeded and wanted to give a simple tuorial for those who shall follow.
    (This is just for adding fonts to the 360 templated project.)

    1) Download the FontSample.zip stuff here and extract.
    2) Open the FontPipline C# project file, change from Debug to Release mode and Build Solution.
    3) Open the FontRendererXbox C# project file, change from Debug to Release mode and Build Solution.

    4) Copy FontRenderer.dll from FontSample\FontRenderer\bin\Xbox 360\Release to an appropriate location in your 360 project.
    5) Copy FontPipeline.dll from FontSample\FontPipeline\bin\x86\Release to an appropriate location in your 360 project.
    6) Copy the Content Folder and the 2 files it contains from FontSample\FontSample to an appropriate location in your 360 project.
    For the purpose of this example I copied both dll's and the content folder to the main dir of the project where Program.cs and Game1.cs are located

    7) Rt click on your on your Project Name in the solution explorer and select properties.
    Go to contentpipeline and add FontPipline.dll
    8) Rt click on your on your Project Name in the solution explorer and select Add Reference.
    Browse and add FontRenderer.dll
    9) Create a new folder in your project called "Content" (it doesnt overwrite the folder already copied)
    From the content Folder add existng items:
    -Goto Content Folder, select ContentPipelineFiles and add BitmapFont
    Repeat these steps again, when navigated to the Content folder select Data Files and add TrueTypeFont

    10) Select properties of BitMapFont and change Content Processor to Bitmap Font Processor
    11) Select Properties of TrueTypeFont and change Content Importer to XML Content XNA Framework
    also change Content Processor to TrueTypeFontProcessor

    That should handle all the setup now for the code:

    Add:
    using FontRenderer;

    In you class definition (Game1) add:
    Font bitmapFont;
    Font trueTypeFont;
    SpriteBatch spriteBatch;

    In your LoadGraphicsContent method add:
    bitmapFont = content.Load<Font>(@"Content/BitmapFont");
    trueTypeFont = content.Load<Font>(@"Content/TrueTypeFont");
    spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

    And finally some sample code to actually draw some sample you can refrence, in your Draw method add:
    spriteBatch.Begin();
    graphics.GraphicsDevice.Clear(Color.SlateGray);
    trueTypeFont.Draw("Hello, World!", new Vector2(64, 12), 2, Color.Red, spriteBatch);
    trueTypeFont.Draw("This was converted from a TrueType font", new Vector2(64, 76), 1, Color.Black, spriteBatch);
    bitmapFont.Draw("But this font was drawn by hand as a bitmap", new Vector2(64, 124), 1, Color.Black, spriteBatch);
    trueTypeFont.Draw("This text has a drop shadow", new Vector2(66, 142), 1, Color.Black, spriteBatch);
    trueTypeFont.Draw("This text has a drop shadow", new Vector2(64, 140), 1, Color.White, spriteBatch);
    bitmapFont.Draw("#'s have color gradients " + "drawn into the font: 012345", new Vector2(64, 185), 1, Color.White, spriteBatch);
    spriteBatch.End();
    DrawFontTexture(trueTypeFont, "Truetype", new Vector2(64, 235));
    DrawFontTexture(bitmapFont, "Bitmap", new Vector2(320, 235));

    Lastly you'll need to add this method for those last 2 lines from the draw method:
    private void DrawFontTexture(Font font, string name, Vector2 position)
    {
        spriteBatch.Begin(SpriteBlendMode.None);
        spriteBatch.Draw(font.Texture, position, new Color(64, 64, 64));
        spriteBatch.End();

        spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
        spriteBatch.Draw(font.Texture, position, new Color(128, 128, 128));
               
        bitmapFont.Draw(name + " font texture:", position - new Vector2(0, 20), 1, Color.Black, spriteBatch);          
        spriteBatch.End();
    }



    That should do it, again this is for Xbox specific templated project, but minor alterations prolly work for Windows version too.
    Thx to all with the info to get me going, hopefulle others can follow my guide successfully!
  • 4/26/2007 10:46 PM In reply to

    Re: Using the Font Class Sample

    I just thought I'd say thanks for the help with this....
  • 4/27/2007 1:20 PM In reply to

    Re: Using the Font Class Sample

    Wouldnt you know, t days of hacking to come up with working fonts and then posting the howto MS releases th refresh w/ fonts included DOH.
  • 5/6/2008 8:29 AM In reply to

    Re: Using the Font Class Sample

    I searched near everywhere where I could find the "FontSample.zip" stuff, and i couldn't find it !

    The link you posted here is not working anymore.
    Do you know where I could find it please ?
  • 5/6/2008 9:34 AM In reply to

    Re: Using the Font Class Sample

    Hi all,

     

    for use your font, you must create a new dir in your Content named Fonts.

    add your name.spritefont

    and in code write :

    public static SpriteFont FontMainMenu;

    FontMainMenu = Content.Load<SpriteFont>("Fonts\\Arial");

    draw string with :

    spritebatch.DrawString(FontMainMenu ,"Text", new Vector2(100,100), Color.Blue);

  • 5/6/2008 10:32 AM In reply to

    Re: Using the Font Class Sample

    kyoshiro_FR:
    I searched near everywhere where I could find the "FontSample.zip" stuff, and i couldn't find it !

    The link you posted here is not working anymore.
    Do you know where I could find it please ?

    Yes it appears to have gone... I have contacted MS to see what is happening. Hopefully we can get it rehosted somewhere soon.

    Playtest Kissy Poo - a game for 4 year olds on Xbox and windows
    The ZBuffer
    News and information for XNA
      Follow The Zman on twitter, Email me
        Please read the forum FAQs - Bug/Feature reporting
          Don't forget to mark good answers and good playtest feedback when you see it!!!
  • 5/6/2008 11:20 AM In reply to

    Re: Using the Font Class Sample

    Hey Kyoshiro, I'm curious why you want this sample?

    We took it down because it didn't seem useful now that we have the same (or actually rather better, with a lot of bugs fixed :-) font support built directly into the framework.

    If there are things you need that are missing from our built-in font implementation, I would be very interested to understand more about what those are.

    XNA Framework Developer - blog - homepage
Page 1 of 1 (19 items) Previous Next