1
\$\begingroup\$

This question is probably a duplicate but I'm still asking it since it's all yesterday that I'm trying to get this done without any success. I followed many solutions and tried even more libraries which were either outdated or they simply didn't work. Right now the most reasonable method I found is

  • Open the Content Pipeline;
  • Create a new Content if there isn't any;
  • Right click on Content -> Add -> New item -> SpriteFont Description -> Give it a name;
  • Click Build;
  • Go to the output directory, grab the XNB file that was created and put it in some folder of the project, under Content;
  • Change the properties to Content and Copy if newer;

And I use it like this:

private SpriteFont font;

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

spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, Global.Camera.TranslationMatrix);

spriteBatch.DrawString(font, "Some Text", new Vector2(100, 100), Color.White);

spriteBatch.End();

I don't have any errors, the text is simply not shown. I think I could be close to a solution since one and only one single time the text appeared, but as the fool I am I happily closed the tabs of countless tutorials and made some changes which led the text to never show again.

\$\endgroup\$
  • \$\begingroup\$ Seems fine to me. Are you drawing anything else that could potentially draw over top the font? Are you sure position 100,100 is visible on screen ? \$\endgroup\$ – jgallant Mar 10 '16 at 13:09
  • \$\begingroup\$ Ok your comment led me to try and twist something and I found something: the text is drawn but with a fontsize of something like 5 so it's nearly unnoticeable but mostly I may have misunderstood what drawing font is for since it stays anchored on the ground. What I wanted was instead something HUD-like. Anyway thank you to make me recognize my error, if you know how to change fontsize and other properties like the font itself (I don't like Arial this much) I would be extremely happy. \$\endgroup\$ – Dispensable Joe Mar 10 '16 at 13:27
  • \$\begingroup\$ To change font size, simply drag the .spritefont file into visual studio's code editor and edit the properties there. It's a mere .xml file and has comments that will guide you on how to change the font and the size of the characters and even their styling. \$\endgroup\$ – PowerUser Apr 5 '16 at 19:06
-1
\$\begingroup\$

What I wanted was instead something HUD-like.

To make it always appear on top, simply draw it last.

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
spriteBatch.Draw(someTexture,...);
spriteBatch.DrawString(...); //Will overwrite someTexture
spriteBatch.End();

Alternatively, there are overloads for Draw(...), DrawString(...), etc. that allow you to specify the layer depth used by spritebatch to sort sprites.

spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.DrawString(,,,,,,,,,0); //Will overwrite someTexture
spriteBatch.Draw(someTexture,,,,,,,,100);
spriteBatch.End();

But you can also use multiple batches:

spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.DrawString(,,,,,,,,0);
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(someTexture,,,,,,,,100); //Different batch; will overwrite text
spriteBatch.End();

if you know how to change fontsize and other properties like the font itself (I don't like Arial this much) I would be extremely happy.

  • Right click on Content -> Add -> New item -> SpriteFont Description -> Give it a name;

This creates a .spritefont file which contains very simple XML pointing to a particular [font]-[size] which is then used to build the XNB spritesheet. You can edit the .spritefont file with Notepad.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.