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

Problem loading SpriteFont in a WinForm project

Last post 08/06/2009 22:15 by dsoltyka. 6 replies.
  • 08/06/2009 19:42

    Problem loading SpriteFont in a WinForm project

    Hello everyone.  I'm attempting to load a spritefont using the content pipeline in a WinForm project, but I've hit some snags.

    My first attempt seemed to be the most logical.  I would simply build the spritefont at runtime using the content pipeline as I am doing with some other assets. 

    string
     buildError; 
    spriteFont = LoadContent<SpriteFont>(currentDirectory + "/font.spritefont"nullnullout buildError); 

    I'm using the methods from the WinForms examples, and my LoadContent method works fine, from basic Texture2D files to my own custom content using custom reader/writers.  However, this spritefont will not build. 

    buildError contains the following:
    "Unsupported type.  Cannot find a ContentTypeWriter implementation for Microsoft.Xna.Framework.Content.Pipeline.Graphics.FontDescription"

    I would have preferred this implementation, but I tried for a work around.

    My second attempt was basically a way to try and work around the first problem.  Instead of building the spritefont from my winform project, I would just build it from another project an load it using the content manager directly using the following code:

    contentManager.Unload(); 
    object content; 
    content = contentManager.Load<SpriteFont>(currentDirectory + "/font"); 
    spriteFont = (SpriteFont)content; 
     
     

    currentDirectory is simply the path to the executable.

    When I call spriteBatch.DrawString  with this font loaded, I get the following exception thrown:

    "Cannot access a disposed object.  Object name: 'Texture2D' "

    I'm not really sure where the object is being disposed, if anywhere, as it is simply being stored in a member field in the Form.

     I've also tried this without using a base object and simply doing:

    spriteFont = contentManager.Load<SpriteFont>(currentDirectory + "/font");

    No change.

    Anyone know how to preferably get the first version working?  The second would be fine also.
     

  • 08/06/2009 19:58 In reply to

    Re: Problem loading SpriteFont in a WinForm project

    For the second version, you are calling contentManager.Unload(), thus disposing any assets loaded with the manager previously. If you stop doing that the second version should work.
    www.dadoogames.com
    Curling 2010 - in playtest soon, this month or next, this year for sure (maybe)
  • 08/06/2009 20:45 In reply to

    Re: Problem loading SpriteFont in a WinForm project

    dadoo Games:
    For the second version, you are calling contentManager.Unload(), thus disposing any assets loaded with the manager previously. If you stop doing that the second version should work.


    I thought that originally but it isn't the case.  The second version uses an excerpt from a functional build method, which also unloads the content.  Reason being, i assume, is that the old content needs to be purged from the manager before it can load new content.  Whether that is or isn't true, it doesn't matter, as it doesn't matter if I unload or not.
  • 08/06/2009 20:54 In reply to

    Re: Problem loading SpriteFont in a WinForm project

    dsoltyka:
    buildError contains the following:
    "Unsupported type.  Cannot find a ContentTypeWriter implementation for Microsoft.Xna.Framework.Content.Pipeline.Graphics.FontDescription"


    This is because you haven't told the pipeline what processor it should use, so it defaulted to no processor, and is trying to serialize the input FontDescription data into an .xnb file without having converted it to a SpriteFontContent first. You need to tell it to use the FontDescriptionProcessor.
    XNA Framework Developer - blog - homepage
  • 08/06/2009 21:44 In reply to

    Re: Problem loading SpriteFont in a WinForm project

    Shawn Hargreaves:
    dsoltyka:
    buildError contains the following:
    "Unsupported type.  Cannot find a ContentTypeWriter implementation for Microsoft.Xna.Framework.Content.Pipeline.Graphics.FontDescription"


    This is because you haven't told the pipeline what processor it should use, so it defaulted to no processor, and is trying to serialize the input FontDescription data into an .xnb file without having converted it to a SpriteFontContent first. You need to tell it to use the FontDescriptionProcessor.



    Once again, you are my hero Shawn.  Thank you.  I had a feeling it was going to be a customer processor or importer this time, I just had no idea where to go with that idea.

    *EDIT*

    Spoke too soon.  It compiled and build the spritefont using the content pipeline, but I'm still getting the disposed object exception.  I'll post my methods here  in one moment, maybe something is screwy.
  • 08/06/2009 21:54 In reply to

    Re: Problem loading SpriteFont in a WinForm project

    Object disposed means that you must be either unloading the ContentManager that you loaded the SpriteFont with, or just leaking that ContentManager so it gets garbage collected, or calling Dispose on the SpriteFont object, at some point before you come to render it.
    XNA Framework Developer - blog - homepage
  • 08/06/2009 22:15 In reply to

    Re: Problem loading SpriteFont in a WinForm project

    Shawn Hargreaves:
    Object disposed means that you must be either unloading the ContentManager that you loaded the SpriteFont with, or just leaking that ContentManager so it gets garbage collected, or calling Dispose on the SpriteFont object, at some point before you come to render it.


    Ahhhhh, so dadoo's idea was in fact correct.  My load method basically had set a single content name and unloaded the manager on every content load, which wasn't causing problems with any other content....I would imagine because the GPU needs to contantly request drawable content.

    In any case, I fixed up the loadcontent method to just use a random temporary content name when loading new content and to not unload on every call.  Works like a charm, thanks both of you.
Page 1 of 1 (7 items) Previous Next