アットウィキロゴ

game0227

using System;
using System.Collections.Generic;
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace game0227
{
   /// <summary>
   /// ゲームメインクラス
   /// </summary>
   public class Game1 : Microsoft.Xna.Framework.Game
   {
       /// <summary>
       /// グラフィックデバイス管理クラス
       /// </summary>
       private GraphicsDeviceManager graphics = null;

       /// <summary>
       /// スプライトのバッチ化クラス
       /// </summary>
       private SpriteBatch spriteBatch = null;

       /// <summary>
       /// 頂点データ定義
       /// </summary>
       private VertexDeclaration vertexDeclaration = null;

       /// <summary>
       /// 基本エフェクト
       /// </summary>
       private BasicEffect basicEffect = null;

       /// <summary>
       /// 頂点バッファ
       /// </summary>
       private VertexBuffer vertexBuffer = null;

       /// <summary>
       /// インデックスバッファ
       /// </summary>
       private IndexBuffer indexBuffer = null;

       /// <summary>
       /// インデックスバッファの各頂点番号配列
       /// </summary>
       private static readonly Int16[] vertexIndices = new Int16[] {
          2, 0, 1, // 1枚目の[[ポリゴン]]
          1, 3, 2, // 2枚目のポリゴン
          4, 0, 2, // 3枚目のポリゴン
          2, 6, 4, // 4枚目のポリゴン
          5, 1, 0, // 5枚目のポリゴン
          0, 4, 5, // 6枚目のポリゴン
          7, 3, 1, // 7枚目のポリゴン
          1, 5, 7, // 8枚目のポリゴン
          6, 2, 3, // 9枚目のポリゴン
          3, 7, 6, // 10枚目のポリゴン
          4, 6, 7, // 11枚目のポリゴン
          7, 5, 4  // 12枚目のポリゴン
      };


       /// <summary>
       /// GameMain コンストラクタ
       /// </summary>
       public Game1()
       {
           // グラフィックデバイス管理クラスの作成
           this.graphics = [[new]] GraphicsDeviceManager(this);

           // ゲームコンテンツのルートディレクトリを設定
           this.Content.RootDirectory = "Content";
       }

       /// <summary>
       /// ゲームが始まる前の初期化処理を行うメソッド
       /// グラフィック以外のデータの読み込み、コンポーネントの初期化を行う
       /// </summary>
       protected override void Initialize()
       {
           // TODO: ここに初期化ロジックを書いてください

           // コンポーネントの初期化などを行います
           base.Initialize();
       }

       /// <summary>
       /// ゲームが始まるときに一回だけ呼ばれ
       /// すべてのゲームコンテンツを読み込みます
       /// </summary>
       protected override void LoadContent()
       {
           // テクスチャーを描画するためのスプライトバッチクラスを作成します
           this.spriteBatch = new SpriteBatch(this.GraphicsDevice);

           // 頂点定義データを作成
           this.vertexDeclaration = new VertexDeclaration(
               this.GraphicsDevice, VertexPositionColor.VertexElements);

           // エフェクトを作成
           this.basicEffect = new BasicEffect(this.GraphicsDevice, null);

           // エフェクトで頂点カラーを有効にする
           this.basicEffect.VertexColorEnabled = true;

           // ビューマトリックスをあらかじめ設定 ((10, 10, 10) から原点を見る)
           this.basicEffect.View = Matrix.CreateLookAt(
                   new Vector3(10.0f, 10.0f, 10.0f),
                   Vector3.Zero,
                   Vector3.Up
               );

           // プロジェクションマトリックスをあらかじめ設定
           this.basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(
                   MathHelper.ToRadians(45.0f),
                   (float)this.GraphicsDevice.Viewport.Width /
                       (float)this.GraphicsDevice.Viewport.Height,
                   1.0f,
                   [[100]].0f
               );

           // 頂点の数
           int vertexCount = 8;

           // 頂点バッファ作成
           this.vertexBuffer = new VertexBuffer(this.GraphicsDevice,
               VertexPositionColor.SizeInBytes * vertexCount, BufferUsage.None);

           // 頂点データを作成する
           VertexPositionColor[] vertives = new VertexPositionColor[vertexCount];

           vertives[0] = new VertexPositionColor(new Vector3(-2.0f, 2.0f, -2.0f), Color.Yellow);
           vertives[1] = new VertexPositionColor(new Vector3(2.0f, 2.0f, -2.0f), Color.Gray);
           vertives[2] = new VertexPositionColor(new Vector3(-2.0f, 2.0f, 2.0f), Color.Purple);
           vertives[3] = new VertexPositionColor(new Vector3(2.0f, 2.0f, 2.0f), Color.Red);
           vertives[4] = new VertexPositionColor(new Vector3(-2.0f, -2.0f, -2.0f), Color.SkyBlue);
           vertives[5] = new VertexPositionColor(new Vector3(2.0f, -2.0f, -2.0f), Color.Orange);
           vertives[6] = new VertexPositionColor(new Vector3(-2.0f, -2.0f, 2.0f), Color.Green);
           vertives[7] = new VertexPositionColor(new Vector3(2.0f, -2.0f, 2.0f), Color.Blue);

           // 頂点データを頂点バッファに書き込む
           this.vertexBuffer.SetData<VertexPositionColor>(vertives);

           // インデックスバッファを作成
           this.indexBuffer = new IndexBuffer(this.GraphicsDevice,
               3 * 12 * sizeof(Int16), BufferUsage.None, IndexElementSize.SixteenBits);

           // 頂点インデックスを書き込む
           this.indexBuffer.SetData<Int16>(vertexIndices);
       }

       /// <summary>
       /// ゲームが終了するときに一回だけ呼ばれ
       /// すべてのゲームコンテンツをアンロードします
       /// </summary>
       protected override void UnloadContent()
       {
           // TODO: ContentManager で管理されていないコンテンツを
           //       ここでアンロードしてください
       }

       /// <summary>
       /// 描画以外のデータ更新等の処理を行うメソッド
       /// 主に入力処理、衝突判定などの物理計算、オーディオの再生など
       /// </summary>
       /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
       protected override void Update(GameTime gameTime)
       {
           // Xbox 360 コントローラの 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.GraphicsDevice.VertexDeclaration = this.vertexDeclaration;

           // 描画に使用する頂点バッファをセット
           this.GraphicsDevice.Vertices[0].SetSource(
               this.vertexBuffer,
               0,
               VertexPositionColor.SizeInBytes
               );

           // インデックスバッファをセット
           this.GraphicsDevice.Indices = this.indexBuffer;

           // エフェクトの使用を開始します
           this.basicEffect.Begin();

           // パスの数だけ繰り替えし描画 (といっても BasicEffect は通常1回)
           [[foreach]] (EffectPass pass in this.basicEffect.CurrentTechnique.Passes)
           {
               // パスの開始
               pass.Begin();

               // インデックスを使用してポリゴンを描画する
               this.GraphicsDevice.DrawIndexedPrimitives(
                   PrimitiveType.TriangleList,
                   0,
                   0,
                   8,
                   0,
                   12
               );

               // パスの終了
               pass.End();
           }

           // エフェクトの使用を終了する
           this.basicEffect.End();

           // 登録された DrawableGameComponent を描画する
           base.Draw(gameTime);
       }
   }
}
最終更新:2011年02月16日 10:26