ホームに戻る
 XNA(移動とアニメ)

/*
*  XNA(移動とアニメ)
*/

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;

namespace WindowsGame1
{
  // メインクラス
  public class Game1 : Microsoft.Xna.Framework.Game
  {
    // グラフィックデバイス管理
    GraphicsDeviceManager graphics;
    // スプライトのバッチ化クラス
    SpriteBatch spriteBatch;
    // エフェクト
    BasicEffect basicEffect = null;
    // 頂点バッファ
    VertexBuffer vertexBuffer = null;

    float rotate = 0.0f;

    // コンストラクタ
    public Game1()
    {
      graphics = new GraphicsDeviceManager(this);
      Content.RootDirectory = "Content";

      // タイトル
      this.Window.Title = "XNA Test";

      // 解像度
      this.graphics.PreferredBackBufferWidth = 1280;
      this.graphics.PreferredBackBufferHeight = 720;

      // フルスクリーンで起動
      //this.graphics.IsFullScreen = true;
    }

    // 初期化
    protected override void Initialize()
    {
      base.Initialize();
    }

    // ゲームのロード(開始時に1回だけ呼ばれる)
    protected override void LoadContent()
    {
      spriteBatch = new SpriteBatch(GraphicsDevice);

      this.basicEffect = new BasicEffect(this.GraphicsDevice);
      this.basicEffect.VertexColorEnabled = true;

      // (0, 0, 1) から原点を見る
      this.basicEffect.View = Matrix.CreateLookAt(
        new Vector3(0.0f, 0.0f, 1.0f),
        Vector3.Zero,
        Vector3.Up
      );

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

      // 頂点の数
      int vertexCount = 3;

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

      VertexPositionColor[] pointList = new VertexPositionColor[vertexCount];

      pointList[0] = new VertexPositionColor(new Vector3(0.0f, 0.3f, 0.0f), Color.Red);
      pointList[1] = new VertexPositionColor(new Vector3(0.3f, -0.2f, 0.0f), Color.Blue);
      pointList[2] = new VertexPositionColor(new Vector3(-0.3f, -0.2f, 0.0f), Color.Green);

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

      // 1秒間に30フレームで更新
      this.TargetElapsedTime = TimeSpan.FromSeconds(1.0 / 30.0);
    }

    // ゲームのアンロード(終了時に1回だけ呼ばれる)
    protected override void UnloadContent()
    {
      // アンロードの処理
    }

    // 描画以外の更新(入力、衝突判定、サウンドなど)
    protected override void Update(GameTime gameTime)
    {
      if(Keyboard.GetState().IsKeyDown(Keys.Escape))
      {
        this.Exit();
        return;
      }

      rotate += 0.05f;

      base.Update(gameTime);
    }

    // 描画
    protected override void Draw(GameTime gameTime)
    {
      GraphicsDevice.Clear(Color.Black);

      // カリングの有効化
      this.GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;

      // 深度バッファの有効化
      this.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

      // 頂点バッファをセット
      this.GraphicsDevice.SetVertexBuffer(this.vertexBuffer);

      EffectPass pass = this.basicEffect.CurrentTechnique.Passes[0];

      Matrix transform = Matrix.Identity;

      Vector3 position = Vector3.Zero;

      position.X = 0.1f;
      position.Y = -0.1f;
      position.Z = 0.0f;

      float scale = 0.5f;

      // 拡大縮小
      transform *= Matrix.CreateScale(scale);

      // 回転
      transform *= Matrix.CreateRotationZ(rotate);

      // 移動
      transform *= Matrix.CreateTranslation(position);

      this.basicEffect.World = transform;

      pass.Apply();

      this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

      base.Draw(gameTime);
    }
  }
}

inserted by FC2 system