Paul Michaels
Paul Michaels

Reputation: 16695

Loading a font using monogame with VS2012/13

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?

Upvotes: 3

Views: 10352

Answers (3)

Michael Parker
Michael Parker

Reputation: 8400

I don't think you need XNA any more.

Just double click on Content.mgcb to open the content editor.

Right click on Content -> Add -> New item -> Spritefont description, give it a name, e.g. "testfont"

Load your spritefont in game:

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

Use it!

spriteBatch.DrawString(font, "hello", new Vector2(10, 10), Color.AliceBlue);

Upvotes: 0

pauldendulk
pauldendulk

Reputation: 1598

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)

Upvotes: 4

craftworkgames
craftworkgames

Reputation: 9957

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).

Upvotes: 10

Related Questions