Loading a font using monogame with VS2012/13

10,041

Solution 1

Okay, here's a quick run-down of what you need to know:

  1. You've probably already found tutorials about how to use sprite fonts in XNA
  2. What you seem to be missing is XNA Game Studio, once you've installed that you'll be able to select the options in Visual Studio.
  3. Note that what you're actually after is a 'Content Project'. This will be used to create the spritefont files and compile them into XNB files for use in MonoGame. You'll need also use a 'fake' XNA game project to do this.
  4. Once you have your XNB files compiled you need to add them to the MonoGame project Content folder and set them to Content / Copy if newer in the properties window.

After that your code should work. Here's an example project with the bare minimum described above: https://dl.dropboxusercontent.com/u/82020056/MonoGameSpriteFonts.zip

Alternately, there's also an option to skip the XNA Content project altogether and use MonoGame with raw asset files. This is how I do it in my games and you can find a tutorial on my blog.

EDIT: I think this is important enough to include in the answer; the MonoGame team are currently working on a solution so that XNA Game Studio will not be required. It looks like fonts are already covered.

https://github.com/mono/MonoGame/wiki/Content-Build-Pipeline

A port of XNA 4.0's Content Build Pipeline is currently in development. This will allow assets to be compiled into optimized binary formats for all platforms supported by MonoGame without requiring XNA Game Studio 4.0 to be installed on a Windows PC. The asset compiling will be done when the project is built, and supported in both Visual Studio (2010 and 2012) and MonoDevelop (Windows, Mac OS X and Linux).

Solution 2

If you just want to draw a spritefont you don not need VS2010 or XNA studio.

First create a .spritefont xml by hand. You can use the example here: http://msdn.microsoft.com/en-us/library/bb447759.aspx

Then use XNA content compiler to compile it to an .xnb: http://xnacontentcompiler.codeplex.com/

Here is a good tutorial: http://www.youtube.com/watch?v=BwtQn02oy6A but note, you do not need XNA studio. It is quite easy to edit the .spritefont xml by hand.

Do not forget to set the content directory: Content.RootDirectory = "Content"; And the .xnb properties should be set to 'Content' and 'Copy Always'.(at least that was necessary for my Windows 8 MonoGame project)

Share:
10,041
Paul Michaels
Author by

Paul Michaels

I've been a programmer for most of my life. I have an interest in games, mobile and tablet development, along with message queuing, and pretty much anything that provides an elegant solution to a problem, technical or otherwise. I like learning new technology and finding new ways to use the old. I blog about my experiences here. You can read about me, or contact me on Linked in here.

Updated on June 05, 2022

Comments

  • Paul Michaels
    Paul Michaels almost 2 years

    I've been trying this for a while, and I believe that I'm doing everything correctly (as far as the code is concerned):

    SpriteFont font = Content.Load<SpriteFont>("Arial");
    

    However, I then read that you need to compile the font using a tool, which I tried, but they all convert from an extension *.spritefont. This doesn't appear to be on my machine, although after some research, I did manage to get hold of a couple of TTF files.

    Some further research implied that if I select "New file" from within VS I should be able to select to create a new spritefont. This doesn't appear to be the case, as it isn't in the list for either VS2012 or 13.

    I haven't had cause to work with fonts very much, so I'm probably making an obvious mistake. Any ideas what?

  • Paul Michaels
    Paul Michaels over 10 years
    I did come across XNA Game Studio, but it seems to require VS2010, so I assumed this was out of date. Do I really need to install VS2010?
  • Cole Campbell
    Cole Campbell over 10 years
    If you want to use the XNA Content Pipeline, then yes, you currently need Visual Studio 2010. XNA does not officially support newer versions of Visual Studio, and never will, seeing as Microsoft has dropped support for the Framework.
  • craftworkgames
    craftworkgames over 10 years
    I'd just like to add that the MonoGame team are working on replacing the Content pipeline so it shouldn't always be this way. github.com/mono/MonoGame/wiki/Content-Build-Pipeline
  • Minh Nguyen
    Minh Nguyen over 8 years
    Do we need to compile testfont to XNB ?
  • Pyroglyph
    Pyroglyph over 7 years
    Doesn't seem to work here. The compiler throws an error as if it doesn't have access to the Content folder (which it clearly does because then it compiles the spritefont into an .xnb like it should, but it still doesn't draw in-game). The DrawString methods goes through without any errors but no string is drawn. Weird. Anyone else got this?