アットウィキロゴ

cs0201

using System;
using System.Collections.Generic;
using System.Linq;
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 game0201
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;

        Matrix viewMatrix;
        Matrix projectionMatrix;
        BasicEffect basicEffect;

        VertexDeclaration vertexDeclaration;
        VertexPositionNormalTexture[] pointList;
        VertexBuffer vertexBuffer;
        int points = 5;

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

        /// <summary>
        /// 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.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            InitializeTransform();
            InitializeEffect();
            InitializePointList();
        }

        private void InitializeTransform()
        {
            viewMatrix = Matrix.CreateLookAt(
            new Vector3(0, 0, 10), Vector3.Zero, Vector3.Up);
            //new Vector3( 0, 0, 5 ), Vector3.Zero, Vector3.Up );

            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45),
                (float)graphics.GraphicsDevice.Viewport.Width /
                (float)graphics.GraphicsDevice.Viewport.Height, 1.0f, 100.0f);
        }

        private void InitializeEffect()
        {
            basicEffect = new BasicEffect(graphics.GraphicsDevice, null);
            //basicEffect.DiffuseColor = new Vector3( 1.0f, 1.0f, 1.0f );
            basicEffect.DiffuseColor = new Vector3(1.0f, 1.0f, 0.0f);

            basicEffect.View = viewMatrix;
            basicEffect.Projection = projectionMatrix;
        }

        private void InitializePointList()
        {
            vertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice,
                VertexPositionNormalTexture.VertexElements);

            double angle = MathHelper.TwoPi / points;

            pointList = new VertexPositionNormalTexture[points + 1];

            pointList[0] = new VertexPositionNormalTexture(
                Vector3.Zero, Vector3.Forward, Vector2.One);

            for (int i = 1; i <= points; i++)
            {
                pointList[i] = new VertexPositionNormalTexture(
                    new Vector3(
                        (float)Math.Round(Math.Sin(angle * i), 4),
                        (float)Math.Round(Math.Cos(angle * i), 4),
                        0.0f),
                    Vector3.Forward,
                    new Vector2());
            }

            // 頂点バッファを初期化し、各頂点についてメモリを割り当てます。
            vertexBuffer = new VertexBuffer(graphics.GraphicsDevice,
                VertexPositionNormalTexture.SizeInBytes * (pointList.Length),
                BufferUsage.None);

            // 頂点の配列に頂点バッファのデータを設定します。
            vertexBuffer.SetData<VertexPositionNormalTexture>(pointList);
        }
    

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

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

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
            graphics.GraphicsDevice.VertexDeclaration = vertexDeclaration;

            basicEffect.Begin();
            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Begin();
                graphics.GraphicsDevice.RenderState.PointSize = 10;
                //graphics.GraphicsDevice.RenderState.PointSize =  4;
                graphics.GraphicsDevice.Vertices[0].SetSource(vertexBuffer, 0,
                    VertexPositionNormalTexture.SizeInBytes);

                graphics.GraphicsDevice.DrawPrimitives(
                    PrimitiveType.PointList,
                    0,          // 最初に描画する頂点のインデックス
                    points + 1); // プリミティブの数
                pass.End();
            }
            basicEffect.End();

            base.Draw(gameTime);
        }
    }
}
最終更新:2011年02月10日 21:20