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

Game1.cs
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
 
namespace XnaFixTarget
{
    class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch sprite;
        SpriteFont font;
        BasicEffect effect;
        VertexBuffer vertexBuffer;
        IndexBuffer indexBuffer;
        const int objNum = 10;
 
        // fps
        int sec;
        int draw = 0;
        int fps = 0;
 
        // カメラ
        int camLat = 0;
        int camLong = 90;
        float camDist = 4;
 
        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.Projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 0.1f, 100);
 
            // 頂点
            VertexPositionColor[] vertices = new VertexPositionColor[4 * objNum];
            int i = 0;
            for (int m = 0; m < objNum; m++)
            {
                for (int n = 0; n < 4; n++)
                {
                    int u = n & 1;
                    int v = (n >> 1) & 1;
                    vertices[i++] = new VertexPositionColor(
                        new Vector3(m - objNum * 0.5f + u, 0.5f - v, 0),
                        u == 0 ? Color.Red : Color.Yellow);
                }
            }
 
            vertexBuffer = new VertexBuffer(GraphicsDevice,
                typeof(VertexPositionColor), vertices.Length, BufferUsage.WriteOnly);
            vertexBuffer.SetData(vertices);
 
            // 索引
            short[] indices = new short[6 * objNum];
            i = 0;
            for (int m = 0; m < objNum; m++)
            {
                for (int n = 0; n < 6; n++)
                {
                    indices[i++] = (short)(4 * m + (n < 3 ? n : 6 - n));
                }
            }
 
            indexBuffer = new IndexBuffer(GraphicsDevice,
                typeof(short), indices.Length, BufferUsage.WriteOnly);
            indexBuffer.SetData(indices);
 
            base.LoadContent();
        }
 
        protected override void Update(GameTime gameTime)
        {
            KeyboardState kState = Keyboard.GetState();
            if (kState.IsKeyDown(Keys.Escape)) Exit();
            if (kState.IsKeyDown(Keys.Up)) camLat++;
            if (kState.IsKeyDown(Keys.Down)) camLat--;
            if (kState.IsKeyDown(Keys.Left)) camLong = (camLong + 1) % 360;
            if (kState.IsKeyDown(Keys.Right)) camLong = (camLong + 359) % 360;
            if (kState.IsKeyDown(Keys.PageUp)) camDist -= 0.1f;
            if (kState.IsKeyDown(Keys.PageDown)) camDist += 0.1f;
 
            base.Update(gameTime);
        }
 
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
 
            // カメラ位置
            float rad = MathHelper.ToRadians(camLat);
            float y = (float)Math.Sin(rad) * camDist;
            float r = (float)Math.Cos(rad) * camDist;
            rad = MathHelper.ToRadians(camLong);
            float x = (float)Math.Cos(rad) * r;
            float z = (float)Math.Sin(rad) * r;
            effect.View = Matrix.CreateLookAt(new Vector3(x, y, z), Vector3.Zero, Vector3.Up);
 
            // SpriteBatch描画するときは毎回セットする必要がある
            GraphicsDevice.SetVertexBuffer(vertexBuffer);
            GraphicsDevice.Indices = indexBuffer;
 
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
                    0, 0, vertexBuffer.VertexCount, 0, vertexBuffer.VertexCount / 2);
            }
 
            // fps
            draw++;
            if (gameTime.TotalGameTime.Seconds != sec)
            {
                fps = draw;
                draw = 0;
                sec = gameTime.TotalGameTime.Seconds;
            }
 
            // 2D描画
            sprite.Begin();
            string text = String.Format(
                "fps={0} lat={1} long={2} dist={3:f1}", fps, camLat, camLong, camDist);
            sprite.DrawString(font, text, new Vector2(0, 0), Color.White);
            sprite.End();
 
            base.Draw(gameTime);
        }
    }
}
 
最終更新:2012年12月11日 12:52