Ask a questionAsk a question
 

AnswerDisplaying Fonts in XNA?!

  • Saturday, December 16, 2006 9:57 PMR.Kneyber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi everyone,

    Just got started on this but already running into a heap of trouble... How do I display fonts?

    I already got the bitmapfont.cs from Gary Kacmarcik on http://blogs.msdn.com/garykac/archive/2006/08/30/728521.aspx

    but the class won't build due to all sorts of weird errors. Am I doing something wrong or is it simply because the class was written voor Beta 1 or 2?

    How do you display fonts using XNA? And why is it not easily accessible in the first place? Seems like an enormous oversight to me.

    Thanks in advance.

Answers

  • Sunday, December 17, 2006 7:59 AMneogir Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    If you have found Gary's blog already, why do you still use the incompatible Beta1 version? Check this one:
    http://blogs.msdn.com/garykac/archive/2006/11.aspx
    The final (up to now) are BitmapFont ver.061118 with BMFontGen ver.1119. You'd save the helpfile BMFontGen.html from first release though.

All Replies

  • Saturday, December 16, 2006 10:13 PMJim PerryMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It's not an oversight, it's a matter of how much they could do given the rapid development and release. Font support is on the list of things for a future release. Check here for some help with font rendering.
  • Sunday, December 17, 2006 7:41 AMR.Kneyber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Ok thanks, but when I tried to build this project it gave me numerous errors.

    I had to change the old references to Microsoft.Xna FrameWork to the new ones.

    Then I ran into a new problem.

    The program worked but after trying to render the texture it gave me this error

    Texture resources can only be created with the following ResourceUsage options: AutoGenerateMipMap, Dynamic, ResolveTarget.
    Parameter name: usage

    at this line:

    Texture2D t = new Texture2D(graphicsDevice, iSize, iSize, 1, ResourceUsage.SoftwareProcessing, SurfaceFormat.Color, ResourceManagementMode.Automatic);

    Anyone know what to do?

     

  • Sunday, December 17, 2006 7:59 AMneogir Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    If you have found Gary's blog already, why do you still use the incompatible Beta1 version? Check this one:
    http://blogs.msdn.com/garykac/archive/2006/11.aspx
    The final (up to now) are BitmapFont ver.061118 with BMFontGen ver.1119. You'd save the helpfile BMFontGen.html from first release though.
  • Sunday, December 17, 2006 8:58 AMAaron Leiby Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    As an exercise, I converted Gary's BitmapFont stuff over to using the ContentPipeline.

    http://seattle.servegame.org/XNAExtras.zip

  • Sunday, December 17, 2006 11:39 AMminahito Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you Aaron!

    That's just great.

    I can load BitmapFont files very very easily in XNA 1.0 as well as texture files. I think many developers should know your XNAExtras.
  • Sunday, December 17, 2006 12:08 PMslugonamission Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I found a nice way of displaying text here

    VB.NET XNA Extra Tutorial - Display Text on Screen - www.alanphipps.com

    I ported it over to C#, and here is the result

    public class TextDrawer
        {
            public static Texture2D CreateTextTexture(string text, Font textFont, Brush colour, Game game, GraphicsDevice device)
            {
                //Measure the font dimensions
                Graphics myDrawing = Graphics.FromHwnd(game.Window.Handle);

                SizeF stringSize = myDrawing.MeasureString(text, textFont);
                Bitmap theBMP = new Bitmap((int)stringSize.Width, (int)stringSize.Height);
                myDrawing.Dispose();
                myDrawing = null;

                myDrawing = Graphics.FromImage(theBMP);
                myDrawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                //Now write the text on
                myDrawing.DrawString(text, textFont, colour, new PointF(0, 0));

                //Create a memory stream and dump the drawing into it
                MemoryStream strm = new MemoryStream();
                theBMP.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
                //Reset the stream position back to 0
                strm.Position = 0;
                Texture2D theTex = Texture2D.FromFile(device, strm);

                //Clean up
                strm.Close();
                strm.Dispose();
                strm = null;

                myDrawing.Dispose();
                myDrawing = null;

                theBMP.Dispose();
                theBMP = null;

                return theTex;
            }
        }


    And just call it like this in LoadGraphicsContent()
    sprite = TextDrawer.CreateTextTexture("Hello World", new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Blue, this, graphics.GraphicsDevice);
  • Sunday, December 17, 2006 12:31 PMneogir Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     slugonamission wrote:
    I found a nice way of displaying text here

    This method can be used for Windows target only.  Personaly for me, it is of interest as an  example of possible implementation. I'll never use it due  to performance sake. 

  • Sunday, December 17, 2006 3:20 PMR.Kneyber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I was using the Beta 1 :(

    I did look for an updated version, but I couldn't find one.

    Thanks for the tip man! Awesome help!