2

Windows Mobile 7 で SpriteSheet (Atlas) を実装する方法は?

4

1 に答える 1

1

モバイル デバイスの課題の 1 つは、多くの画像をロードし、アプリケーションの有効期間中にモバイル デバイスから良好なパフォーマンスを得る方法です。ここでは、windows mobile 7 でスプライト シートを使用する方法について簡単に説明します。

アプリケーションやゲームを作成するとき、通常、多くの画像を提示する必要があります。コード内で各画像を個別に呼び出すと、アプリケーションの存続期間中に膨大なオーバーヘッド パフォーマンスが作成されます。ハードウェア リソースが限られているモバイル デバイスでは、これらのスプライト (画像) を効率的に使用して優れたパフォーマンスを得ることが非常に重要です。では、スプライトシートはどのように役立つのでしょうか? スプライト シートは、多数の小さなスプライト (画像) を含む大きな画像ファイルであるため、多数の画像を使用する代わりに、1 つの画像ファイルのみを使用します。私たちのコードはそれを一度呼び出します。画像はその画像ファイルに整然と保存されるため、不要な未使用スペースも節約され、ロード時のメモリが少なくなります。ここでは、Windows Mobile 7 で行う方法について説明します。Microsoft が公開した元の KB にいくつかの変更を加えました。

  1. http://create.msdn.com/en-US/education/catalog/sample/sprite_sheetからコードをダウンロードします
  2. それを抽出します。
  3. このコードを独自のゲームで再利用するには、SpriteSheetPipeline および SpriteSheetRuntime プロジェクトをソリューションに追加します。

A. パイプライン プロジェクトをコンテンツの構築に使用できるようにするため

  1. コンテンツを右クリック | コンテンツ プロジェクトの参照アイテム。
  2. [参照の追加] をクリックします。
  3. [プロジェクト] タブをクリックし、SpriteSheetPipeline プロジェクトを選択します。

B. SpriteSheet クラスをゲームで使用できるようにするため

  1. メイン ゲーム プロジェクトの [参照] アイテムを右クリックします。
  2. [参照の追加] をクリックします。
  3. [プロジェクト] タブをクリックし、SpriteSheetRuntime プロジェクトを選択します。

インポートしたコードを実際に使用するには、まず新しい xml ファイルを作成し、コンテンツ ディレクトリに配置します。XML の形式は次のようになります。

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
  <Asset Type ="System.String[]">
         <Item>L01_480_0.png</Item>
         <Item>L01_480_1.png</Item>
         <Item>L01_480_2.png</Item>
         <Item>L01_480_3.png</Item>
         <Item>L01_480_4.png</Item>
         <Item>L01_480_5.png</Item>
         <Item>L01_480_6.png</Item>
         <Item>L01_480_7.png</Item>
         <Item>L01_480_8.png</Item>
         <Item>L01_480_9.png</Item>
         <Item>L01_480_10.png</Item>
         <Item>L01_480_11.png</Item>
         <Item>L01_480_12.png</Item>
  </Asset>
</XnaContent>

この xml には、SpriteSheet(atlas) を作成するイメージの名前が含まれていることがわかりますこれらのスプライト イメージを Visual Studio 経由でプロジェクトに追加する必要はありません。イメージを物理的なコンテンツ ディレクトリにコピーするだけです。もちろん、 Visual Studio を介して XML ファイルをコンテンツ フォルダー プロジェクトに追加する必要があります (これはメイン プロジェクトではないため、注意してください) 実際に XML ファイルを使用するには、いくつかのことを行う必要があります。xml ファイルのプロパティを設定する必要があります。Content Importer は XML Content になります。XNA Framework Content Processor はSpriteSheetProcessor になります プロパティ を設定した後、実際にファイルを呼び出すことができます。まず、コードに SpriteSheetRuntime を使用するように指示します

追加します

using SpriteSheetRuntime;

新しい spritebatch と spritesheet オブジェクトを宣言します

namespace SpriteSheetGame
    {

      /// This is the main type for your game

      public class Game: Microsoft.Xna.Framework.Game
      {
       /* Handles the configuration and management of the graphics device.*/

        GraphicsDeviceManager graphics;

     /* Enables a group of sprites to be
        drawn using the same settings */

        SpriteBatch spriteBatch;

     /* A sprite sheet contains many individual sprite images, packed into different
        areas of a single larger texture, along with information describing where in
        that texture each sprite is located */

         SpriteSheet spriteSheet;
       }

読み込みコンテンツでは、次のことを行います。

       protected override void LoadContent()
        {
          spriteBatch = new SpriteBatch(GraphicsDevice);
          spriteSheet = Content.Load<SpriteSheet>("SpriteSheet");
        }

この場合の XML ファイルは、上記の例のように SpriteSheet.xml になります。ここで、スプライトをアニメーション化して表示するか、一度にすべて表示する必要があります。したがって、次の spriteBatch.Draw を使用しますが、その前に spriteBatch を開始します。

protected override void Draw(GameTime gameTime)
{
    spriteBatch.Begin();

クラス (SpriteBatch) を見ると、画面に描画するオプションがほとんどないことがわかります。最適なものを選択できます。

public void Draw(Texture2D texture, Vector2 position, Color color);

public void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color);

public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color);

public void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth);

public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth);

public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);

したがって、最終的には次のように書くことができます。

protected override void Draw(GameTime gameTime)
{
    spriteBatch.Begin();




// Draw an animating effect, by rapidly cycling
      // through 13 slightly different sprite images.

      const int animationFramesPerSecond = 2;
      const int animationFrameCount = 13;

      // Look up the index of the first sprite.

      int index = spriteSheet.GetIndex("L01_480_0");

      // Modify the index to select the current frame of the animation.

      index += (int)(time * animationFramesPerSecond) % animationFrameCount;

      // Draw the current sprite.

       spriteBatch.Draw(spriteSheet.Texture, new Rectangle(0, 0, 100, 100),
                       spriteSheet.SourceRectangle(index), Color.White);

      spriteBatch.End();
            base.Draw(gameTime);
}
于 2011-04-28T06:49:20.013 に答える