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

Microsoft XNA Game Studio 4.0 Refreshをインストール。
自動生成されるプログラムはごちゃごちゃして読み辛いのだが、空のプロジェクトから始めるのは手間がかかる。
Windows Game (4.0)で自動生成し、Game1.csをプロジェクトから除外する。
ソリューションエクスプローラーのXnaSampleContent (Content)を右クリックして[追加]-[新しい項目]を選択する。
Sprite Fontを選択、名前はデフォルトのSpriteFont1.spritefontとする。

参考

Program.cs
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
 
class Game1 : Game
{
	GraphicsDeviceManager gdm;
	SpriteBatch sprBat;
	SpriteFont sprFont;
	VertexBuffer vtxBuf;
	BasicEffect basEff;
	DateTime prevTime;
	int count = 0;
	int fps = 0;
	float rotate = 0.0f;
 
	public Game1()
	{
		gdm = new GraphicsDeviceManager(this);
		Content.RootDirectory = "Content";
	}
 
	protected override void Initialize()
	{
//		Window.Title = "XnaSample";
		prevTime = DateTime.Now;
 
		base.Initialize();
	}
 
	protected override void LoadContent()
	{
		sprBat = new SpriteBatch(GraphicsDevice);
		// ContentにSprite Fontを追加しておく
		sprFont = Content.Load<SpriteFont>("SpriteFont1");
 
		// 基本レンダリングエフェクト
		basEff = new BasicEffect(GraphicsDevice);
		basEff.VertexColorEnabled = true;
		basEff.View = Matrix.CreateLookAt(
			new Vector3(0.0f, 0.0f, 15.0f),
			Vector3.Zero,
			Vector3.Up);
		float aspRat =
			(float)GraphicsDevice.Viewport.Width /
			(float)GraphicsDevice.Viewport.Height;
		basEff.Projection = Matrix.CreatePerspectiveFieldOfView(
			MathHelper.ToRadians(45.0f), aspRat, 1.0f, 100.0f);
 
		// 頂点バッファ
		vtxBuf = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), 3,
			BufferUsage.None);
		VertexPositionColor[] vpc = new VertexPositionColor[3];
		vpc[0] = new VertexPositionColor(new Vector3( 0.0f,  3.0f, 0.0f), Color.Green);
		vpc[1] = new VertexPositionColor(new Vector3( 3.0f, -2.0f, 0.0f), Color.Blue);
		vpc[2] = new VertexPositionColor(new Vector3(-3.0f, -2.0f, 0.0f), Color.Red);
		vtxBuf.SetData(vpc);
 
		base.LoadContent();
	}
 
	protected override void Update(GameTime gameTime)
	{
		rotate += (float)gameTime.ElapsedGameTime.TotalSeconds;
 
		base.Update(gameTime);
	}
 
	protected override void Draw(GameTime gameTime)
	{
		GraphicsDevice.Clear(Color.CornflowerBlue);
 
		// ポリゴン描画
		GraphicsDevice.SetVertexBuffer(vtxBuf);
		foreach (EffectPass pass in basEff.CurrentTechnique.Passes) {
			pass.Apply();
			basEff.World = Matrix.CreateRotationY(rotate);
			GraphicsDevice.RasterizerState = RasterizerState.CullNone;
			GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
		}
 
		// フレームレート
		count++;
		DateTime now = DateTime.Now;
		TimeSpan t = now - prevTime;
		if (t.TotalMilliseconds >= 1000) {
			fps = count;
			count = 0;
			prevTime = now;
		}
		sprBat.Begin();
		sprBat.DrawString(sprFont, "fps = " + fps, new Vector2(0, 0), Color.White);
		sprBat.End();
 
		base.Draw(gameTime);
	}
}
 
class Program
{
	static void Main()
	{
		using (Game1 game = new Game1()) {
			game.IsMouseVisible = true;
			game.Run();
		}
	}
}
 
最終更新:2012年11月05日 11:15