Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (3 posts)

SpriteFont content in run-time

Last post 7/11/2007 7:50 AM by areia. 2 replies.
  • 7/11/2007 7:07 AM

    SpriteFont content in run-time

    Okay another strange topic here. SpriteFont is great for western languages and good for Asian languages if you're going to use the method described here. The previous method works fine if you know exactly which characters your game is going to use. We got a project here though for an online game, that you have no idea on what the user is going to chat about.
    Due to the nature of the game, typing text in Chinese and Japanese is vital. We managed to get the IME support on the input but after that we are stuck on displaying this text on screen. The need for a run-time font rendering method is obvious.

    Creating a SpriteFont with a texture holding all the unicode characters is completely out of question (last time we tried we had to abort after three hours of processing). Then pre-selecting the most common characters is also difficult, since the players might decide to write something we didn't include and we can't afford to limit them this way.

    One solution we though about is using the classes from System.Drawing to finally produce a texture with the required text. Still this method looks very akward and error-prone since we will have to finetune how this text is going to look on the screen, something that SpriteFont already does smoothly.

    So here is the question: Is it possible to create a SpriteFontContent that includes the required characters in run-time? We were thinking about a fixed size texture (a buffer) that would have characters written and taken out in run-time. Still how the whole mix is going to work together? Anybody has any clues on this?

    Thank you.

    Note: If we could create something like MDX's Font, it would be great.

  • 7/11/2007 7:20 AM In reply to

    Re: SpriteFont content in run-time

    The SpriteFont system probably won't be able to deal with this use case at all. The XBox 360 doesn't support the System.Drawing.Font class, so it was deliberately designed to operate on pre-processed sprite textures only.

    If your game is designed to run on the PC only (seeing as you're using IME this is likely), you could revert to using System.Drawing.Font for drawing the strings onto a texture. This will be a bit slower but it probably the most sane alternative.

    My own font rendering library ( used an approach similar to what you described in your last paragraph. The characters were placed on a big, shared texture at runtime as needed. But the character bitmaps themselfes were still taken out of a pregenerated bitmap list from the content pipeline importer. I guess it would be possible to modify this part of the code so it generates the bitmap using System.Drawing.Font instead. You'd have the best of both sides, good performance and the full unicode character set, but it would require a bit of work :)


  • 7/11/2007 7:50 AM In reply to

    Re: SpriteFont content in run-time

    Reply Quote

    Thanks for your answer. We got this working using your suggestion of using the System.Drawing components.
    Here is an example for people that will try to do this in future. Of course this is supposed to go into a nice and manable class and if you're not as desperate as we are, don't even think about using this because it will guarantee making your game slower. And yes, it won't work in XBOX.


    Font font = new Font("Arial Unicode MS", 18, FontStyle.Regular);
    Bitmap bitmap = new Bitmap(200, 200);
    Graphics g = Graphics.FromImage(bitmap);
    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    g.DrawString("Some Text", font, Brushes.Black, 0, 0, StringFormat.GenericDefault);
    bitmap.Save("buffer"); //this could be a memory stream

    Texture2D texture = Texture2D.FromFile(Device3D, "buffer");

    SpriteBatch batch = new SpriteBatch(Device3D);
    batch.Draw(texture, new Microsoft.Xna.Framework.Rectangle(100, 100, 200, 200),
Page 1 of 1 (3 posts) Previous Discussion Next Discussion