開発環境 Microsoft Visual C# 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 Windows Game (4.0)
プロジェクト名 XnaLissajous

Game1.cs
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
 
namespace XnaLissajous
{
    class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch sprite;
        SpriteFont font;
        BasicEffect effect;
 
        const int histNum = 240;
        VertexPositionColor[] vertices = new VertexPositionColor[histNum + 2];
        int head = histNum - 1;
        int deg = 0;
        float xFreq = 3;
        float yFreq = 2;
 
        string[] MenuList =
        {
            "X Freq (1.0 - 10.0):",
            "Y Freq (1.0 - 10.0):",
        };
        int menuSelect = 0;
        KeyboardState kStateOld = new KeyboardState();
 
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }
 
        protected override void LoadContent()
        {
            font = Content.Load<SpriteFont>("SpriteFont1");
            sprite = new SpriteBatch(GraphicsDevice);
            effect = new BasicEffect(GraphicsDevice);
            effect.VertexColorEnabled = true;
            effect.View = Matrix.CreateLookAt(new Vector3(0, 0, 3), Vector3.Zero, Vector3.Up);
            effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 1, 4);
            base.LoadContent();
        }
 
        protected override void Update(GameTime gameTime)
        {
            KeyboardState kState = Keyboard.GetState();
            if (kState.IsKeyDown(Keys.Escape)) Exit();
 
            // メニュー選択
            if (kState.IsKeyDown(Keys.Up) && kStateOld.IsKeyUp(Keys.Up))
            {
                menuSelect = (menuSelect + MenuList.Length - 1) % MenuList.Length;
            }
            if (kState.IsKeyDown(Keys.Down) && kStateOld.IsKeyUp(Keys.Down))
            {
                menuSelect = (menuSelect + 1) % MenuList.Length;
            }
 
            // 値変更
            float moveValue = 0;
            if (kState.IsKeyDown(Keys.Left) && kStateOld.IsKeyUp(Keys.Left))
            {
                moveValue -= 1;
            }
            if (kState.IsKeyDown(Keys.Right) && kStateOld.IsKeyUp(Keys.Right))
            {
                moveValue += 1;
            }
            switch (menuSelect)
            {
                case 0:
                    xFreq = MathHelper.Clamp(xFreq + moveValue, 1, 10);
                    break;
                case 1:
                    yFreq = MathHelper.Clamp(yFreq + moveValue, 1, 10);
                    break;
            }
            kStateOld = kState;
 
            // エージング
            int i = head;
            for (int n = 0; n < histNum; n++)
            {
                vertices[i].Color = Color.White * ((histNum - n) / (float)histNum);
                if (--i < 0)
                {
                    vertices[histNum + 1].Color = vertices[0].Color;
                    i = histNum;
                }
            }
 
            // 先頭インクリメント
            if (histNum + 1 < ++head)
            {
                vertices[0] = vertices[histNum + 1];
                head = 1;
            }
 
            deg = (deg + 1) % 360;
            float rad = MathHelper.ToRadians(deg);
            vertices[head] = new VertexPositionColor(
                new Vector3((float)Math.Cos(rad * xFreq), (float)Math.Sin(rad * yFreq), 0),
                Color.White);
 
            base.Update(gameTime);
        }
 
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            GraphicsDevice.BlendState = BlendState.AlphaBlend;
 
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                if (head < histNum)
                {
                    GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                        PrimitiveType.LineStrip, vertices, head + 1, histNum - head);
                    GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                        PrimitiveType.LineStrip, vertices, 0, head);
                }
                else
                {
                    GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                        PrimitiveType.LineStrip, vertices, head - histNum, histNum);
                }
            }
 
            sprite.Begin();
            string[] param = new string[MenuList.Length];
            param[0] = xFreq.ToString("f1");
            param[1] = yFreq.ToString("f1");
 
            for (int n = 0; n < MenuList.Length; n++)
            {
                sprite.DrawString(font, MenuList[n], new Vector2(40, 24 + 24 * n), Color.White);
                sprite.DrawString(font, param[n], new Vector2(280, 24 + 24 * n), Color.White);
            }
            sprite.DrawString(font, "*", new Vector2(20, 24 + 24 * menuSelect), Color.White);
            sprite.End();
 
            base.Draw(gameTime);
        }
    }
}
 
最終更新:2012年12月10日 21:57