文字に枠を付ける

Page creation date :

The page you are currently viewing does not support the selected display language.

概要

文字に枠をつけて表示させます。

文字に枠を付ける

動作環境

必須環境

対応 XNA バージョン
  • 2.0
  • 3.0
  • 3.1
  • 4.0
対応プラットフォーム
  • Windows (XP SP2 以降, Vista, 7)
  • Xbox 360
  • Windows Phone 7
Windows 必須頂点シェーダ バージョン 2.0
Windows 必須ピクセルシェーダ バージョン 2.0

動作確認環境

プラットフォーム
  • Windows 7
  • Xbox 360
  • Windows Phone 7 エミュレーター

内容

XNA では枠のついたテキストを表示させるような標準機能はありませんが、テキストを5回描画することによって擬似的に枠をつけることができます。

今回のテキストは上下左右にあらかじめ黒色でテキストを描画した後、白のテキストを中央に描画することによって黒枠を表現しています。

まず、上に2px移動させたテキストを描画しています。

上に2px移動したテキスト

// 表示するテキスト
string text = "Draw Frame text.";

// 文字の位置
Vector2 position = new Vector2(50.0f, 50.0f);

// 太さ
float thickness = 2.0f;

///// 枠となるテキストを描画する /////

// 上
this.spriteBatch.DrawString(this.font,
    text,
    new Vector2(position.X, position.Y - thickness),
    Color.Black);

下、左、右にも同じように描画します。

上下左右の枠用テキストを描画

// 左
this.spriteBatch.DrawString(this.font,
    text,
    new Vector2(position.X - thickness, position.Y),
    Color.Black);

// 下
this.spriteBatch.DrawString(this.font,
    text,
    new Vector2(position.X, position.Y + thickness),
    Color.Black);

// 右
this.spriteBatch.DrawString(this.font,
    text,
    new Vector2(position.X + thickness, position.Y),
    Color.Black);

最後に中央にテキストを描画して完了です。

枠付きテキストの完成

// 通常のテキストを描画する
this.spriteBatch.DrawString(this.font,
    text,
    new Vector2(position.X, position.Y),
    Color.White);

枠付きのテキストとしてはそれなりに表現できていますが、枠の太さを大きくしたり、フォントサイズを大きくしたりすると枠としての見栄えはあまりよくありません。また、少なくとも5回は描画しないと枠としての表現ができないので,、コストがやや高くあまりお勧めはしません。

全コード

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif

namespace FrameText
{
    /// <summary>
    /// ゲームメインクラス
    /// </summary>
    public class GameMain : Microsoft.Xna.Framework.Game
    {
        /// <summary>
        /// グラフィックデバイス管理クラス
        /// </summary>
        private GraphicsDeviceManager graphics = null;

        /// <summary>
        /// スプライトのバッチ化クラス
        /// </summary>
        private SpriteBatch spriteBatch = null;

        /// <summary>
        /// スプライトでテキストを描画するためのフォント
        /// </summary>
        private SpriteFont font = null;


        /// <summary>
        /// GameMain コンストラクタ
        /// </summary>
        public GameMain()
        {
            // グラフィックデバイス管理クラスの作成
            this.graphics = new GraphicsDeviceManager(this);

            // ゲームコンテンツのルートディレクトリを設定
            this.Content.RootDirectory = "Content";

#if WINDOWS_PHONE
            // Windows Phone のデフォルトのフレームレートは 30 FPS
            this.TargetElapsedTime = TimeSpan.FromTicks(333333);

            // バックバッファサイズの設定
            this.graphics.PreferredBackBufferWidth = 480;
            this.graphics.PreferredBackBufferHeight = 800;

            // フルスクリーン表示
            this.graphics.IsFullScreen = true;
#endif
        }

        /// <summary>
        /// ゲームが始まる前の初期化処理を行うメソッド
        /// グラフィック以外のデータの読み込み、コンポーネントの初期化を行う
        /// </summary>
        protected override void Initialize()
        {
            // TODO: ここに初期化ロジックを書いてください

            // コンポーネントの初期化などを行います
            base.Initialize();
        }

        /// <summary>
        /// ゲームが始まるときに一回だけ呼ばれ
        /// すべてのゲームコンテンツを読み込みます
        /// </summary>
        protected override void LoadContent()
        {
            // テクスチャーを描画するためのスプライトバッチクラスを作成します
            this.spriteBatch = new SpriteBatch(this.GraphicsDevice);

            // フォントをコンテンツパイプラインから読み込む
            this.font = this.Content.Load<SpriteFont>("Font");
        }

        /// <summary>
        /// ゲームが終了するときに一回だけ呼ばれ
        /// すべてのゲームコンテンツをアンロードします
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: ContentManager で管理されていないコンテンツを
            //       ここでアンロードしてください
        }

        /// <summary>
        /// 描画以外のデータ更新等の処理を行うメソッド
        /// 主に入力処理、衝突判定などの物理計算、オーディオの再生など
        /// </summary>
        /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Update(GameTime gameTime)
        {
            // Xbox 360 コントローラ、Windows Phone の BACK ボタンを押したときに
            // ゲームを終了させます
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            // TODO: ここに更新処理を記述してください

            // 登録された GameComponent を更新する
            base.Update(gameTime);
        }

        /// <summary>
        /// 描画処理を行うメソッド
        /// </summary>
        /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Draw(GameTime gameTime)
        {
            // 画面を指定した色でクリアします
            this.GraphicsDevice.Clear(Color.CornflowerBlue);

            // スプライトの描画準備
            this.spriteBatch.Begin();

            // 表示するテキスト
            string text = "Draw Frame text.";

            // 文字の位置
            Vector2 position = new Vector2(50.0f, 50.0f);

            // 太さ
            float thickness = 2.0f;

            ///// 枠となるテキストを描画する /////

            // 上
            this.spriteBatch.DrawString(this.font,
                text,
                new Vector2(position.X, position.Y - thickness),
                Color.Black);

            // 左
            this.spriteBatch.DrawString(this.font,
                text,
                new Vector2(position.X - thickness, position.Y),
                Color.Black);

            // 下
            this.spriteBatch.DrawString(this.font,
                text,
                new Vector2(position.X, position.Y + thickness),
                Color.Black);

            // 右
            this.spriteBatch.DrawString(this.font,
                text,
                new Vector2(position.X + thickness, position.Y),
                Color.Black);

            // 通常のテキストを描画する
            this.spriteBatch.DrawString(this.font,
                text,
                new Vector2(position.X, position.Y),
                Color.White);

            // スプライトの一括描画
            this.spriteBatch.End();

            // 登録された DrawableGameComponent を描画する
            base.Draw(gameTime);
        }
    }
}