private
GraphicsDeviceManager graphics;
private
SpriteBatch spriteBatch;
/// <summary>
/// load the font
/// </summary>
private
VectorFont vectorFont;
/// <summary>
/// text string
/// </summary>
private
Text text;
/// <summary>
/// draw the string
/// </summary>
private
TextBatch textBatch;
/// <summary>
/// camera viewMatrix
/// </summary>
private
Matrix viewMatrix = Matrix.Identity;
/// <summary>
/// camera projectionMatrix
/// </summary>
private
Matrix projectionMatrix = Matrix.Identity;
/// <summary>
/// record the update number
/// </summary>
private
int
Index = 0;
/// <summary>
/// text rotate matrix
/// </summary>
private
Matrix RotateMatrix = Matrix.Identity;
public
MyText()
{
graphics =
new
GraphicsDeviceManager(
this
);
Content.RootDirectory =
"Content"
;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected
override
void
Initialize()
{
base
.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected
override
void
LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch =
new
SpriteBatch(GraphicsDevice);
this
.textBatch =
new
TextBatch(
this
.graphics.GraphicsDevice);
viewMatrix = Matrix.CreateLookAt(
new
Vector3(0 , 0 , 50) ,
new
Vector3(0 , 0 , -100) , Vector3.Up);
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4 , GraphicsDevice.Viewport.AspectRatio , 1.0f , 1000);
this
.vectorFont =
this
.Content.Load<VectorFont>(
"Fonts/defaultFont"
);
this
.text =
this
.vectorFont.Extrude(
"H0"
);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected
override
void
UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected
override
void
Update(GameTime gameTime)
{
// Allows the game to exit
if
(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this
.Exit();
Index++;
if
(Index % 60 == 0)
{
string
str =
"H"
+ (Index / 60).ToString();
this
.text =
this
.vectorFont.Extrude(str);
}
// TODO: Add your update logic here
RotateMatrix *= Matrix.CreateRotationY(MathHelper.Pi / 180);
base
.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected
override
void
Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
this
.textBatch.ViewProjection = viewMatrix * projectionMatrix;
this
.textBatch.Begin();
this
.textBatch.DrawText(
this
.text , Matrix.CreateScale(0.1f) * RotateMatrix * Matrix.CreateTranslation(0 , 0 , 30) , Color.Red);
this
.textBatch.End();
base
.Draw(gameTime);
}
|
其中有几点需要注意的地方,首先是需要将名称为defaultFont的字体文件放在项目的Content目录下面,其次对于defaultFont.spritefont文件需要修改其属性:将其Content Porcessor修改为Vector Font,接着就可以编译通过了(测试版本VS2010+XNA4.0)......