ニュース速報XNA360

How to: スプライトを描画する

最終更新:

匿名ユーザー

- view
だれでも歓迎! 編集

スプライトを描画する 原文


この文章ではSpriteBatchクラスを用いてスプライトを描画する方法を示します。

スプライトを画面に表示する。


1. Gameクラスを継承します。

2. ゲームクラスのフィールド中にSpriteBatchオブジェクトを定義します。

3. LoadGraphicsContent関数内で、loadAllContentのif文のなかでSpriteBatchオブジェクトをコンストラクトします(newします)。その際、カレントのグラフィックデバイスを渡します。

4. LoadGraphicsContent関数内でスプライトが使うテクスチャをロードする。今回の例ではXNA Framework Content Pipelineからテクスチャをロードするために、メンバ変数contentを用いる。テクスチャはLoadに渡したのと同じ名前でプロジェクトに追加されている必要があります。

private Texture2D SpriteTexture;
private SpriteBatch ForegroundBatch;

protected override void LoadGraphicsContent( bool loadAllContent )
{
   if (loadAllContent)
   {
       // TODO: Load any ResourceManagementMode.Automatic content
       ForegroundBatch = new SpriteBatch( graphics.GraphicsDevice );
       SpriteTexture = content.Load<Texture2D>( "Sprite" );
   }

   // TODO: Load any ResourceManagementMode.Manual content
}

5. オーバーライドされたDraw関数内でClearを呼びます。

6. Clearの後、SpriteBatchオブジェクトのBeginを呼びます。

7. スプライトのスクリーン上での位置を表すためにVector2を宣言します。Xbox360では、フォアグラウンドのスプライトは画面の10-20パーセントより外側に描画しないよう注意を払ってください-いくつかのテレビでは、画面の端がどこなのかははっきりしません。GetTitleSafeArea関数は、任意のパーセンテージから安全なビューポートの範囲を計算します。

   protected Rectangle GetTitleSafeArea( float percent )
   {
       Rectangle retval = new Rectangle(
           graphics.GraphicsDevice.Viewport.X,
           graphics.GraphicsDevice.Viewport.Y,
           graphics.GraphicsDevice.Viewport.Width,
           graphics.GraphicsDevice.Viewport.Height );
   #if XBOX
           // Find Title Safe Xbox 360
           float border = (1 - percent) / 2;
           retval.X      = (int)( border  * retval.Width  );
           retval.Y      = (int)( border  * retval.Height );
           retval.Width  = (int)( percent * retval.Width  );
           retval.Height = (int)( percent * retval.Height );
   #endif

           return retval;
   }

8. SpriteBatchオブジェクトのDraw関数を呼びます。引数には描画するテクスチャとスクリーン上での位置と適用する色を指定します。色の効果を付けずにテクスチャを描画したかったらColor.Whiteを使用してください。

9. 全てのスプライトを描画し終えたらSpriteBatchオブジェクトのEnd関数を呼んでください。

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

   // TODO: Add your drawing code here
   ForegroundBatch.Begin();
   Rectangle titlesafe = GetTitleSafeArea( .8f );
   Vector2 pos = new Vector2( titlesafe.Left, titlesafe.Top );

   ForegroundBatch.Draw( SpriteTexture, pos, Color.White );
   ForegroundBatch.End();

   base.Draw( gameTime );
}

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
private Texture2D SpriteTexture;
private SpriteBatch ForegroundBatch;

protected override void LoadGraphicsContent( bool loadAllContent )
{
   if (loadAllContent)
   {
       // TODO: Load any ResourceManagementMode.Automatic content
       ForegroundBatch = new SpriteBatch( graphics.GraphicsDevice );
       SpriteTexture = content.Load<Texture2D>( "Sprite" );
   }

   // TODO: Load any ResourceManagementMode.Manual content
}

protected Rectangle GetTitleSafeArea( float percent )
{
   Rectangle retval = new Rectangle(
       graphics.GraphicsDevice.Viewport.X,
       graphics.GraphicsDevice.Viewport.Y,
       graphics.GraphicsDevice.Viewport.Width,
       graphics.GraphicsDevice.Viewport.Height );
 #if XBOX
   // Find Title Safe area of Xbox 360
   float border = (1 - percent) / 2;
   retval.X      = (int)( border  * retval.Width  );
   retval.Y      = (int)( border  * retval.Height );
   retval.Width  = (int)( percent * retval.Width  );
   retval.Height = (int)( percent * retval.Height );
 #endif

   return retval;
}

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

   // TODO: Add your drawing code here
   ForegroundBatch.Begin();
   Rectangle titlesafe = GetTitleSafeArea( .8f );
   Vector2 pos = new Vector2( titlesafe.Left, titlesafe.Top );

   ForegroundBatch.Draw( SpriteTexture, pos, Color.White );
   ForegroundBatch.End();

   base.Draw( gameTime );
}

See Also


Reference

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

人気記事ランキング
目安箱バナー