15
\$\begingroup\$

I just want to render some text to the screen using:

  • monogame 3.0
  • MS Visual Studio 2010 C# Express

In XNA, you were able to add fonts to the content pipeline quite easily. But this doesn't seem to be the case in monogame. Loading TTF Files using Content<SpriteFont>.Load() doesn't work. Is there any way to generate or download *.spritefont files or *.xnb files containing the font data (without resorting to install XNA)?

\$\endgroup\$
1
  • \$\begingroup\$ I also had a lot of problems figuring this out, this tutorial helped me out a whole lot. youtube.com/watch?v=BwtQn02oy6A \$\endgroup\$
    – user29722
    Apr 19, 2013 at 8:11

8 Answers 8

6
\$\begingroup\$

An alternative is to use pre-generated bitmap fonts instead. Here's a tutorial I wrote a while back on using the BMFont tool to get text into your game using the MonoGame.Extended library.

http://dylanwilson.net/bmfont-rendering-with-monogame-extended

\$\endgroup\$
6
  • \$\begingroup\$ Although I'm not 100% satisfied with this solution, I guess I'll go with bitmap fonts for now and hope for the best in the near future. \$\endgroup\$
    – Khôi
    Dec 25, 2012 at 11:37
  • \$\begingroup\$ More recently I added BitmapFont's to the MonoGame.Extended library dylanwilson.net/bmfont-rendering-with-monogame-extended \$\endgroup\$ Jul 28, 2015 at 12:39
  • \$\begingroup\$ @J2V I updated the answer to point at a working link. It's not exactly the same as the old one but it still answers the question I believe. \$\endgroup\$ Oct 1, 2015 at 7:55
  • \$\begingroup\$ @craftworkgames Thanks! I'm going to remove my previous comment. \$\endgroup\$ Oct 1, 2015 at 7:59
  • \$\begingroup\$ The link is broken now. \$\endgroup\$
    – rbwhitaker
    Mar 1, 2021 at 3:09
3
\$\begingroup\$

I ran into the same problem as you. Thankfully, I had an existing XNA project that already used a .spritefont file, and generated the XNB file. I just compiled it, and copied the XNB into my MonoGame project, and I can use it without any problems.

(Disclaimer: I tried building the XNA content pipeline for MonoGame 3.0 beta as per instructions, and after a few hours, I gave up; it wouldn't include the project properly in Visual Studio.)

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for tip. Good to know that you can work around it like this. \$\endgroup\$
    – Khôi
    Dec 25, 2012 at 11:33
  • \$\begingroup\$ @Khôi it's not perfect, but it can at least get you somewhere while you figure out how to integrate the content pipeline project. \$\endgroup\$
    – ashes999
    Dec 25, 2012 at 15:11
2
\$\begingroup\$

At least in August this year generating spritefonts was not yet supported by MonoGame, however the content pipeline was the main focus of attention the last few months so I wouldn't be surprised if it does work currently. Of course you need to know the syntax of a .spritefont file which needs to be added to your content folder. For that see the bottom of this post.

As far as I can tell, even as far back as August, the loading of already compiled spritefonts from XNB files is supported. Since you don't want to install XNA to generate these XNB files you might want to check the XNA Content Compiler Project on codeplex.

Here is an example .spritefont file so you can see if it's currently supported

<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
  <Asset Type="Graphics:FontDescription">

    <!--
    Modify this string to change the font that will be imported.
    -->
    <FontName>Arial</FontName>

    <!--
    Size is a float value, measured in points. Modify this value to change
    the size of the font.
    -->
    <Size>10</Size>

    <!--
    Spacing is a float value, measured in pixels. Modify this value to change
    the amount of spacing in between characters.
    -->
    <Spacing>0</Spacing>

    <!--
    UseKerning controls the layout of the font. If this value is true, kerning information
    will be used when placing characters.
    -->
    <UseKerning>true</UseKerning>

    <!--
    Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
    and "Bold, Italic", and are case sensitive.
    -->
    <Style>Regular</Style>

    <!--
    If you uncomment this line, the default character will be substituted if you draw
    or measure text that contains characters which were not included in the font.
    -->
    <!-- <DefaultCharacter>*</DefaultCharacter> -->

    <!--
    CharacterRegions control what letters are available in the font. Every
    character from Start to End will be built and made available for drawing. The
    default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
    character set. The characters are ordered according to the Unicode standard.
    See the documentation for more information.
    -->
    <CharacterRegions>
      <CharacterRegion>
        <Start>&#32;</Start>
        <End>&#126;</End>
      </CharacterRegion>
    </CharacterRegions>
  </Asset>
</XnaContent>
\$\endgroup\$
4
  • \$\begingroup\$ As of now (25.12.2012 - merry xmas!), spritefonts are not supported by monogame 3.0 yet. I just tried it out according to your instructions. \$\endgroup\$
    – Khôi
    Dec 25, 2012 at 11:27
  • \$\begingroup\$ The XNA Content Compiler won't run for me either. Since it seems you need to have XNA installed to run the content compiler. The XNA Content Compiler builds a dummy project using XNA in the background to compile .xnb files. \$\endgroup\$
    – Khôi
    Dec 25, 2012 at 11:31
  • \$\begingroup\$ @Khôi Merry Christmas to you too :). Strange that the project requires XNA installed, makes it quite useless for most scenarios! \$\endgroup\$
    – Roy T.
    Dec 25, 2012 at 11:50
  • \$\begingroup\$ It's not strange, nor useless. MonoGame needs its own content pipline to support other platforms, like Android. This is a temporary xrutch--piggyback off the XNA content pipeline. \$\endgroup\$
    – ashes999
    Dec 25, 2012 at 15:12
2
\$\begingroup\$

You can now do this without installing anything extra. In a brand new MonoGame project you should have a Content.mgcb file. That should bring up the MonoGame Pipeline app. You can right click on the Content node in the tree and add a new item. Select `SpriteFont Description'. That will get compiled correctly and you should be able to load it in your code.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you! I've been looking for hours and even tried installing VS2010 & XNA on WIndows 10 (disaster!). For anyone else finding this, the next step would be to open the .spritefont file (you just created) in a text editor and modify it from the default 'arial' to the font you want. \$\endgroup\$ Jan 14, 2016 at 22:42
  • \$\begingroup\$ I don't understand why more tutorials don't give you these steps! I didn't find any after looking all day. It's such an easy thing to overlook that the Pipeline does for you. \$\endgroup\$ Jan 14, 2016 at 22:43
1
\$\begingroup\$

Check this out guys, I've been compiling my *.spritefont files to .xnb for use in MonoGame, for me, it's the only way it worked.

Check it out, might help you too. http://xnacontentcompiler.codeplex.com/

\$\endgroup\$
1
\$\begingroup\$

The best alternative is to pre-generate the font xnb via XNA first then just use them in your Monogame for Windows project or you can do the old trick of getting the Window.Handle and the screen coordinates ScreenToClient then use the GDI to draw your text with the System.Drawing.dll.

\$\endgroup\$
0
0
\$\begingroup\$

I have been working on this for several days now, trying to fix this and eventually i got i to work. What i am working on is Windows8, VS2012, MonoGame. i should add that this is as "monoGame Windows OpenGL Pjoject"

Let me take you through the steps i have taken. 1. I installed monGame for VS2012 2. I installed WindowsPhone SDK8 3. I installed XNA 4.0 for my VS2010

I dont know if all these steps are nessecery....

Sence you cant generate a .spritefont in vs2012 I generated a spriteFont file in my vs2010 and then added i to my vs2012 project....BUT it did not work. And this morning I saw that the URL for that .spritefont file was wrong. When i added it through solution explorer..content-->add exsisting item...then the url was wrong.. "C:\Users\admin\Documents\Visual Studio 2012\Projects\MyGame\MyGame\Content" this is not where vs2012 where looking for my *.spritefot file it was here..--> "C:\Users\admin\Documents\Visual Studio 2012\Projects\MyGame\MyGame\bin\WindowsGL\Debug\Content" I added i manully, and WOW it worked. i paste some code here so you better can see what i have done.. inside my game class i put these variables

    SpriteFont myFont;
    int score;

in my loadcontent method autogenerated by VS was

    myFont = Content.Load<SpriteFont>("test");

in my draw method autogenerated by VS was

         spriteBatch.Begin();
     spriteBatch.DrawString(myFont, "Score: " +score, new Vector2(20,10),Color.Red);
        spriteBatch.End();

and then manully added my spritefont file in the right folder

The strange thing is that i have added pictures with this way and the have ended up in the right folder but not the *.spritefont, you have to manully add it in the later folder to get i to work..

I hope some of this makes sense to someone because this has given me the biggest headache these last days. I may have forgotten some steps and all this may be useless, but I hope that I may ease the headaches for some out there

And i hope that monoGame will be updated to fix these issues...

\$\endgroup\$
0
\$\begingroup\$

I've used XNB Builder to manually convert them into .xnb files. Simply run the app, choose your files and it will convert them into XNB files that are ready to use with MonoGame.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .