Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm playing around with MonoGame, and I've noticed the SpriteFont class draws static text very inefficiently. Each time the text is drawn the spacing is recalculated. This isn't a big deal on my quad core PC, but on mobile applications it might be a problem.

Before I go and program some text which caches the arrangement of its letters in an array and then feeds that array to the SpriteBatch, I would like to make sure there isn't something available to do this already, either in MonoGame itself or a class someone has implemented and made available for general use.

share|improve this question
"but on mobile applications it might be a problem" Is it actually a problem yet? Don't spend too much time on pre-optimizing something that might not need optimizing at all. – Marton Nov 25 '12 at 9:38
Just to second Marton here: Even though it could be faster (at the expense of memory, mind you!), it's probably not worth optimising because, if you're drawing enough text for it to matter, your text will be unreadable! – Andrew Russell Nov 25 '12 at 14:20
It was an issue with slower phones on Android using Java/LibGDX when only printing a single line. LibGDX has a BitmapFontCache class for this very reason. You're right though, as I'm just making a game for fun and don't even know if I'll be running it on Android, I'm guilty of premature optimization here. Still, if someone has already made such a class, I may as well use it. – Casey Nov 25 '12 at 15:17
up vote 1 down vote accepted

you can convert the spritefont into a Texture2D (RenderTarget inherits from Texture2d i think)

as described in this article

Vector2 size = font.MeasureString(text) + new Vector2(0.5f);
int width = (int)size.X;
int height = (int)size.Y;

RenderTarget2D target = new RenderTarget2D(device, width, height);

SpriteBatch spriteBatch = new SpriteBatch(device);
spriteBatch.DrawString(font, text, Vector2.Zero, Color.White);

device.SetRenderTarget(null);   // unset the render target

// read back the pixels from the render target
Color[] data = new Color[width * height];

if required it may be possible to create your text this way

share|improve this answer

One option is to use bitmap font rendering instead.

share|improve this answer
This is essentially the solution that already exists. The draw method is recalculating the spacing of the text 60 times a second. – Casey Nov 26 '12 at 0:18
Ah I see, sorry I didn't fully understand the performance bottleneck. Personally I'd just optimise it later once you know how much static vs dynamic text you're actually going to need. – craftworkgames Nov 26 '12 at 2:43
you can write your own text drawing system, its pretty easy. But, this should not really be an issue since the calculations for drawing text are pretty simple – CobaltHex Nov 26 '12 at 3:10
Actually the SpriteFont will be interpreted on the pipeline stage and will be converted to a bitmap font. – Gustavo Maciel Aug 12 '14 at 14:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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