アットウィキロゴ

cs0222x

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 game0222
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {

        private GraphicsDeviceManager graphics = null;
        private SpriteBatch spriteBatch = null;
        private VertexDeclaration vertexDeclaration = null;
        private VertexPositionColor[] triangleVertives = null;
        private VertexPositionColor[] lineVertices = null;
        private BasicEffect basicEffect = null;
        private float cameraRotate = 0.0f;


        public Game1()
        {
            this.graphics = new GraphicsDeviceManager(this);
            this.Content.RootDirectory = "Content";
        }


        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;

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

            // ポリゴンの頂点データを作成する
            this.triangleVertives = new VertexPositionColor[3];

            this.triangleVertives[0] = new VertexPositionColor(new Vector3(0.0f, 3.0f, 0.0f),
                                                               Color.Red);
            this.triangleVertives[1] = new VertexPositionColor(new Vector3(3.0f, -2.0f, 0.0f),
                                                               Color.Blue);
            this.triangleVertives[2] = new VertexPositionColor(new Vector3(-3.0f, -2.0f, 0.0f),
                                                               Color.Green);

            // 面の表側を指すようにラインを作成
            this.lineVertices = new VertexPositionColor[2];

            this.lineVertices[0] = new VertexPositionColor(new Vector3(0.0f, -1.0f, 0.0f),
                                                           Color.Blue);
            this.lineVertices[1] = new VertexPositionColor(new Vector3(0.0f, -1.0f, 10.0f),
                                                           Color.Blue);

            
        }

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

        /// <summary>
        /// 描画以外のデータ更新等の処理を行うメソッド
        /// 主に入力処理、衝突判定などの物理計算、オーディオの再生など
        /// </summary>
        /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Update(GameTime gameTime)
        {
            // キーボードの情報取得
            KeyboardState keyState = Keyboard.GetState();

            // ゲームパッドの情報取得
            GamePadState padState = GamePad.GetState(PlayerIndex.One);

            // Xbox360 コントローラの BACK ボタンを押したときにゲームを終了させます
            if (padState.Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            ///// カリングの設定 /////
            if (keyState.IsKeyDown(Keys.A) || padState.Buttons.A == ButtonState.Pressed)
            {
                // 反時計回りをカリング
                this.GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
            }
            else if (keyState.IsKeyDown(Keys.B) || padState.Buttons.B == ButtonState.Pressed)
            {
                // 時計回りをカリング
                this.GraphicsDevice.RenderState.CullMode = CullMode.CullClockwiseFace;
            }
            else if (keyState.IsKeyDown(Keys.X) || padState.Buttons.X == ButtonState.Pressed)
            {
                // カリングなし
                this.GraphicsDevice.RenderState.CullMode = CullMode.None;
            }

            ///// カメラの位置回転 /////
            if (keyState.IsKeyDown(Keys.Left))
            {
                this.cameraRotate -= MathHelper.Pi / 16.0f;
            }
            else if (keyState.IsKeyDown(Keys.Right))
            {
                this.cameraRotate += MathHelper.Pi / 16.0f;
            }
            this.cameraRotate += padState.ThumbSticks.Left.X * MathHelper.Pi / 16.0f;

            // ビューマトリックスを設定
            this.basicEffect.View = Matrix.CreateLookAt(
                    Vector3.Transform(new Vector3(0.0f, 0.0f, 10.0f),
                        Matrix.CreateRotationY(this.cameraRotate)),
                    Vector3.Zero,
                    Vector3.Up
                );

            // 登録された 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.basicEffect.Begin();

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

                // 三角形を描画する
                this.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                    PrimitiveType.TriangleList,
                    this.triangleVertives,
                    0,
                    1
                );

                // 面の表側を示すラインを描画
                this.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                    PrimitiveType.LineList,
                    this.lineVertices,
                    0,
                    1
                );

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

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

            // スプライトの描画準備
            this.spriteBatch.Begin(
                SpriteBlendMode.AlphaBlend,
                SpriteSortMode.BackToFront,
                SaveStateMode.SaveState);

            

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

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