開発環境 |
Microsoft Visual C# 2010 Express (SP1) |
実行環境 |
Microsoft Windows XP Home Edition (SP3) |
プロジェクトの種類 |
Windows Game (4.0) |
プロジェクト名 |
XnaTemplate |
[ソリューションのディレクトリを作成]にチェックを付けないと.slnファイルが指定した場所の直下に作られる。
ソリューション エクスプローラーの(Content)に新しい項目を追加する。
テンプレート:Sprite Font
名前:SpriteFont1.spritefont
Game1.cs
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace XnaTemplate
{
class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch sprite;
SpriteFont font;
DateTime prevTime;
int draw = 0;
int fps = 0;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
sprite = new SpriteBatch(GraphicsDevice);
prevTime = DateTime.Now;
base.Initialize();
}
protected override void LoadContent()
{
// ソリューション エクスプローラーの(Content)に追加しておく
font = Content.Load<SpriteFont>("SpriteFont1"); // 新しい項目:Sprite Font
base.LoadContent();
}
protected override void Update(GameTime gameTime)
{
KeyboardState state = Keyboard.GetState();
if (state[Keys.Escape] == KeyState.Down) Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// フレームレート
draw++;
DateTime now = DateTime.Now;
TimeSpan t = now - prevTime;
if (t.TotalMilliseconds >= 1000)
{
fps = draw;
draw = 0;
prevTime = now;
}
sprite.Begin();
string text = "fps=" + fps;
sprite.DrawString(font, text, new Vector2(0, 0), Color.White);
sprite.End();
base.Draw(gameTime);
}
}
}
Program.cs
namespace XnaTemplate
{
static class Program
{
static void Main(string[] args)
{
using (Game1 game = new Game1())
{
game.Run();
}
}
}
}
最終更新:2012年12月02日 06:28