nazolabo

フリーランスのWebエンジニアが近況や思ったことを発信しています。

XNA Game Studio Expressで画像を表示

β2になってContent Piplelineが実装されたので

  • 画像をプロジェクトに追加する(PNGがおすすめ?)
  • 追加した画像のプロパティの「XNA Framework Content」を「true」にする
  • 以下の定義を追加
        GraphicsDeviceManager graphics;
        ContentManager content;
+       Texture2D texture;
+       SpriteBatch spriteBatch;
  • LoadGraphicsContent内に以下の記述を追加("image"は画像のAsset Name)
            if (loadAllContent)
            {
                // TODO: Load any ResourceManagementMode.Automatic content
+               ContentManager loader = new ContentManager(Services);
+
+               texture = content.Load<Texture2D>("image") as Texture2D;
+               spriteBatch = new SpriteBatch(this.graphics.GraphicsDevice);
            }
  • Draw内に以下の記述を追加(ここはβ1と同じ)
            // TODO: Add your drawing code here
+           spriteBatch.Begin();
+           spriteBatch.Draw(texture, new Vector2(0, 0), Color.White);
+           spriteBatch.End();