ホームに戻る
 XNA(文字描画)

リソースに SpriteFont1.spritefont を用意する

/*
*  XNA(文字描画)
*/

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;

namespace WindowsGame1
{
  // メインクラス
  public class Game1 : Microsoft.Xna.Framework.Game
  {
    // グラフィックデバイス管理
    GraphicsDeviceManager graphics;
    // スプライトのバッチ化クラス
    SpriteBatch spriteBatch;
    // フォント
    SpriteFont font = null;

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

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

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

    // 初期化
    protected override void Initialize()
    {
      base.Initialize();
    }

    // ゲームのロード(開始時に1回だけ呼ばれる)
    protected override void LoadContent()
    {
      // テクスチャーを描画するためのスプライトバッチクラスを作成します
      this.spriteBatch = new SpriteBatch(this.GraphicsDevice);

      // フォントの登録
      this.font = this.Content.Load("SpriteFont1");
    }

    // ゲームのアンロード(終了時に1回だけ呼ばれる)
    protected override void UnloadContent()
    {
      // アンロードの処理
    }

    // 描画以外の更新(入力、衝突判定、サウンドなど)
    protected override void Update(GameTime gameTime)
    {
      if(Keyboard.GetState().IsKeyDown(Keys.Escape))
      {
        this.Exit();
        return;
      }

      base.Update(gameTime);
    }

    // 描画
    protected override void Draw(GameTime gameTime)
    {
      // 画面を指定した色でクリアします
      this.GraphicsDevice.Clear(Color.Black);

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

      // 50% 透過の文字
      this.spriteBatch.DrawString(
        this.font,
        "Test.",
        new Vector2(50.0f, 50.0f),
        Color.White * 0.5f);

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

      base.Draw(gameTime);
    }
  }
}

inserted by FC2 system