■XNAでスプライト Prev  Top  Next

今回は、テクスチャーを貼り付けたスプライトを表示します。テクスチャーなどのリソースを使用する場合、リソースをプロジェクトに追加する必要がありますが、これについてはXNAでリソースの追加 を参照してください。


namespace Tutrial
{
   /// 
   /// This is the main type for your game
   /// 
   public class Game1 : Microsoft.Xna.Framework.Game
   {
      GraphicsDeviceManager graphics;
      ContentManager content;

      //****************************************************************
      //オブジェクトの定義
      //****************************************************************

      private SpriteBatch spriteBatch = null;   //スプライトオブジェクト
      private Texture2D texture2D = null;       //テクスチャーオブジェクト

      public Game1()
      {
         graphics = new GraphicsDeviceManager(this);

         //リソースフォルダのルートパスを指定(注意1)
         content = new ContentManager(Services, "Resource");
      }


      /// 
      /// Allows the game to perform any initialization it needs to before starting to run.
      /// This is where it can query for any required services and load any non-graphic
      /// related content.  Calling base.Initialize will enumerate through any components
      /// and initialize them as well.
      /// 
      protected override void Initialize()
      {
         //****************************************************************
         //オブジェクトの初期化
         //****************************************************************

         //スプライトの初期化
         this.spriteBatch = new SpriteBatch(graphics.GraphicsDevice);   

         //テクスチャーのロード(注意2)
         this.texture2D = this.content.Load("Texture");

         base.Initialize();
      }


      /// 
      /// Load your graphics content.  If loadAllContent is true, you should
      /// load content from both ResourceManagementMode pools.  Otherwise, just
      /// load ResourceManagementMode.Manual content.
      /// 
      /// Which type of content to load.
      protected override void LoadGraphicsContent(bool loadAllContent)
      {
         if (loadAllContent)
         {
            // TODO: Load any ResourceManagementMode.Automatic content
         }

         // TODO: Load any ResourceManagementMode.Manual content
      }


      /// 
      /// Unload your graphics content.  If unloadAllContent is true, you should
      /// unload content from both ResourceManagementMode pools.  Otherwise, just
      /// unload ResourceManagementMode.Manual content.  Manual content will get
      /// Disposed by the GraphicsDevice during a Reset.
      /// 
      /// Which type of content to unload.
      protected override void UnloadGraphicsContent(bool unloadAllContent)
      {
         if (unloadAllContent == true)
         {
            content.Unload();
         }
      }


      /// 
      /// Allows the game to run logic such as updating the world,
      /// checking for collisions, gathering input and playing audio.
      /// 
      /// Provides a snapshot of timing values.
      protected override void Update(GameTime gameTime)
      {
         // Allows the default game to exit on Xbox 360 and Windows
         if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

         // TODO: Add your update logic here

         base.Update(gameTime);
      }


      /// 
      /// This is called when the game should draw itself.
      /// 
      /// Provides a snapshot of timing values.
      protected override void Draw(GameTime gameTime)
      {
         //****************************************************************
         //レンダリング
         //****************************************************************

         //背景色を黒で塗りつぶす
         graphics.GraphicsDevice.Clear(Color.Black);

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

         //スプライトを描画する(注意3)
         this.spriteBatch.Draw(this.texture2D, new Rectangle( 0, 0, 800, 600 ), Color.White);

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


         base.Draw(gameTime);
      }
   }
}


スプライトが表示されました。

(注意1) プロジェクトに追加したリソースのルートパスを設定します。前回「Resource」フォルダ内にリソースを追加したので"Resource"と設定します。

(注意2) ロードするリソースのファイル名を設定します。拡張子は除きます。

(注意3) スプライトの描画を行います。Rectangleクラスは、矩形を設定するためのクラスです。なおスプライトはスクリーン座標系で座標を指定します。 つまり画面左上端の座標は( 0, 0 )となり、画面右下端の座標はウィンドウの解像度のサイズとなります。

これで2Dポリゴンはレンダリングできるようになったので、次は3Dポリゴンの表示ですかね。

Prev  Top  Next
inserted by FC2 system