Draw text in context
summary
Use the layer's depth value to contextualize the text. In the screen below, it looks like a filled rectangle is drawn, but "◆" with a larger font size is drawn so that the overlap of text is easy to understand.
Operating environment
Prerequisites
Supported XNA Versions |
|
Supported Platforms |
|
Windows Required Vertex Shader Version | 2.0 |
Windows Required Pixel Shader Version | 2.0 |
Operating environment
platform |
|
substance
Normally, when you draw text, it draws what comes later so that it is brought to the foreground, but by sorting by depth value, you can clearly see the context regardless of the order in which the SpriteBatch.DrawString method is called.
To sort by text depth value, specify "SpriteSortMode.BackToFront" as the first argument of the "SpriteBatch.Begin" method. The second argument is "BlendState", but here the default is null.
// 奥から描画するようにソート
this.spriteBatch.Begin(SpriteSortMode.BackToFront, null);
SpriteBatch.Begin
method
Call it before drawing the sprite. Internally, we are making the necessary settings for drawing sprites.
sortMode | SpriteSortMode | Specifies the order in which the sprites are drawn from the SpriteSortMode enumeration. Here, 0.0 is treated as the foreground and 1.0 is treated as the deepest, so we will draw from the sprite in the back and draw on the sprite in the foreground, so specify SpriteSortMode.BackToFront. |
blendState | BlendState | BlendState specifies how the color blends with the background when drawing. If null is specified, BlendState.AlphaBlend is used. |
Then, when drawing the text, specify the depth value in the ninth argument of the SpriteBatch.DrawString method. The values that can be set here are in the range of 0.0~1.0, with 0.0 being drawn at the foreground and 1.0 at the back.
// 手前に描画(白)
this.spriteBatch.DrawString(this.font, "◆",
new Vector2(150.0f, 100.0f), Color.White, 0.0f, Vector2.Zero,
1.0f, SpriteEffects.None, 0.0f);
// 奥に描画(赤)
this.spriteBatch.DrawString(this.font, "◆",
new Vector2(150.0f, 140.0f), Color.Red, 0.0f, Vector2.Zero,
1.0f, SpriteEffects.None, 1.0f);
// 2つの文字の間に描画(青)
this.spriteBatch.DrawString(this.font, "◆",
new Vector2(190.0f, 115.0f), Color.Blue, 0.0f, Vector2.Zero,
1.0f, SpriteEffects.None, 0.5f);
SpriteBatch.DrawString
method
Draw a string.
spriteFont | SpriteFont | Specifies the SpriteFont that contains the string image. |
text | string | Specifies the text to display. |
position | Vector2 | The position at which to display the text. Specify in screen coordinates relative to the top left of the screen. The origin of the text will be at the top left of the first character. |
color | Color | Text color |
rotation | float | The rotation angle of the text. Units are specified in radian. The axis of rotation will be at the top left of the text. |
origin | Vector2 | Specifies the position of the axis of rotation when rotating text. You specify which position of the text is the axis of rotation, but in reality, the axis of rotation is fixed to the top left of the text, and the display position of the text is moved by -origin. |
scale | float | Specifies the magnification of the text. Using 1.0 as a reference, scale vertically and horizontally. The origin of the expansion is at the top left of the text. There are two ways to specify it: float specifying, which is equal to the height and width, and Vector2, which can be specified with different magnifications |
effects | SpriteEffects | Specifies the effect of inverting the display of text. If you don't do anything else, specify SpriteEffects.None. |
layerDepth | float | Specifies the depth at which the text is displayed. It is mainly used to display text in the foreground or back. Specify in the range of 0.0~1.0, where 0.0 is the front and 1.0 is the back. |
In the above program, the SpriteBatch.DrawString method is called in the order of "white", "red", and "blue", but each depth value is set to "white (0.0)", "red (1.0)", and "blue (0.5)", so you can see that red is drawn at the back and white is drawn at the foreground.
All Codes
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 LayerDepthText
{
<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(SpriteSortMode.BackToFront, null);
// 手前に描画(白)
this.spriteBatch.DrawString(this.font, "◆",
new Vector2(150.0f, 100.0f), Color.White, 0.0f, Vector2.Zero,
1.0f, SpriteEffects.None, 0.0f);
// 奥に描画(赤)
this.spriteBatch.DrawString(this.font, "◆",
new Vector2(150.0f, 140.0f), Color.Red, 0.0f, Vector2.Zero,
1.0f, SpriteEffects.None, 1.0f);
// 2つの文字の間に描画(青)
this.spriteBatch.DrawString(this.font, "◆",
new Vector2(190.0f, 115.0f), Color.Blue, 0.0f, Vector2.Zero,
1.0f, SpriteEffects.None, 0.5f);
// スプライトの一括描画
this.spriteBatch.End();
// 登録された DrawableGameComponent を描画する
base.Draw(gameTime);
}
}
}