FONTに甘かった

XNAなんだから、文字くらい簡単に出るだろうと思っていたのですが、そうではないようです「ん・ぱか工房」を参考にさせていただきました。

■どうもFONTデータを設定しないと文字が出力できないらしい。
ソリューションエクスプローラからプロジェクト名を右クリックし「追加」の「新しい項目」を選択してください。そしてダイアログから「Sprite Font」を選択して「SpriteFont1.spritefont」を作成してください。

■SpriteFont1.spritefont ファイルを編集しましょう。

<?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>MS UI Gothic</FontName>

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

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

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

    <!--
    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>

ここで、FontNameを開発のWindows環境で使用できるフォント名に変更します。ここでは「MS UI Gothic」とします。

<FontName>MS UI Gothic</FontName>

私は大きなフォントが好きなので、サイズを64に変更してみました。

<Size>64</Size>

■ソース

namespace WindowsGame
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;
        private ContentManager content;
        private SpriteBatch sprite;
        private SpriteFont font;
        
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
        }

        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                sprite = new SpriteBatch(graphics.GraphicsDevice);
                font = content.Load<SpriteFont>("SpriteFont1");
            }
        }

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.White);

            sprite.Begin();
            sprite.DrawString(font, "test OK", new Vector2( 20, 20 ), Color.Black);
            sprite.End();

            base.Draw(gameTime);
        }
    }

}

■ソース説明
ここでFONTデータを登録します。

 sprite = new SpriteBatch(graphics.GraphicsDevice);
 font = content.Load<SpriteFont>("SpriteFont1");

Fontの設定、文字列、位置、色を設定する事で文字の表示をしています。

 sprite.Begin();
 sprite.DrawString(font, "test OK", new Vector2( 20, 20 ), Color.Black);
 sprite.End();

■このような結果になればOKです。

でも、このままでは日本語の表記ができないです。「つづく」