Make the sprite appear
summary
Load the texture (image) and display it on the screen.
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
What is a sprite?
A rectangular area for drawing images on the screen. This feature is often used in 2D games, but it is also often used in 3D as a pseudo-2D image drawing.
In XNA, everything must be drawn in 3D polygons, so the image you draw on the screen is a pseudo-sprite using polygons. However, there is no need to be aware of polygons, and you can easily draw them.
Preparing images
Since the image is displayed, an image file must be prepared. In the sample, the image below is used.
I'm using a PNG file with a size of 128×128, but it can be any size. In addition, the format of the image file that can be read is the following file by default.
- .bmp
- .dds
- .dib
- .hdr
- .jpg
- .pfm
- .png
- .ppm
- .tga
Add an image to your project
In the sample, the image file is put in the "Content" project. Please refer to the following page for details on how to add image files to your project.
field
<summary>
スプライトのバッチ化クラス
</summary>
private SpriteBatch spriteBatch = null;
<summary>
テクスチャー
</summary>
private Texture2D texture = null;
To draw a sprite, prepare "SpriteBatch" to batch the sprites to be drawn and "Texture2D" to store 2D image data.
Creating and loading resources
<summary>
ゲームが始まるときに一回だけ呼ばれ
すべてのゲームコンテンツを読み込みます
</summary>
protected override void LoadContent()
{
// テクスチャーを描画するためのスプライトバッチクラスを作成します
this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
// テクスチャーをコンテンツパイプラインから読み込む
this.texture = this.Content.Load<Texture2D>("Texture");
}
Inside the LoadGraphicsContent method, create a SpriteBatch class. Pass a GraphicsDevice as the argument.
The ContentManager.Load method is then used to load and create the texture. Specify the "Texture2D" class for the type parameter and the asset name for the argument.
SpriteBatch
constructor
Create an instance of the class "SpriteBatch" that batches the drawing of sprites.
graphicsDevice | GraphicsDevice | Specifies the GraphicsDevice to use for drawing. |
drawing
// スプライトの描画準備
this.spriteBatch.Begin();
// スプライトを描画する
this.spriteBatch.Draw(this.texture, Vector2.Zero, Color.White);
// スプライトの一括描画
this.spriteBatch.End();
Draw the text in the Draw method.
To draw a sprite, use the "SpriteBatch.Draw" method, but you need to call the "SpriteBatch.Begin" and "SpriteBatch.End" methods, respectively, before and after drawing.
The first argument to the SpriteBatch.Draw method is the "Texture2D" class you created. This will allow you to view the imported image file.
SpriteBatch.Begin
method
Call it before drawing the sprite. Internally, we are making the necessary settings for drawing sprites.
SpriteBatch.Draw
method
Adds a sprite to the sprite drawing batch list.
texture | Texture2D | Specifies the texture to display as a sprite. |
position | Vector2 | The position where the sprite should be displayed. Specify in screen coordinates relative to the top left of the screen. The origin of the sprite will be in the upper left position. |
color | Color | Specifies the color to multiply the color of the sprite. If you specify Color.White, it is displayed in the primary color of the sprite. If Color.Black is specified, the sprite is displayed in full black, regardless of its color. The formula is "Result = sprite color * color". |
SpriteBatch.End
method
Draw all batched sprites. The drawing order, drawing state, and so on are specified in the SpriteBatch.Begin method. If necessary, the render state is returned to the state it was in before the SpriteBatch.Begin method was called.
There is no problem with a display used on a computer, etc., but when outputting from an Xbox 360 to an analog TV, you may not be able to see the sprite displayed in the upper left. In this case, you need to adjust the display position of the sprite, but that will be explained in another section.
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 DrawSprite
{
<summary>
ゲームメインクラス
</summary>
public class GameMain : Microsoft.Xna.Framework.Game
{
<summary>
グラフィックデバイス管理クラス
</summary>
private GraphicsDeviceManager graphics = null;
<summary>
スプライトのバッチ化クラス
</summary>
private SpriteBatch spriteBatch = null;
<summary>
テクスチャー
</summary>
private Texture2D texture = 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.texture = this.Content.Load<Texture2D>("Texture");
}
<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();
// スプライトを描画する
this.spriteBatch.Draw(this.texture, Vector2.Zero, Color.White);
// スプライトの一括描画
this.spriteBatch.End();
// 登録された DrawableGameComponent を描画する
base.Draw(gameTime);
}
}
}