XNA批处理文本输出(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/xna-batched-text-output-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2566 个词 阅读量 0XNA批处理文本输出(译文)
原文地址:https://www.codeproject.com/Articles/19440/XNA-Batched-Text-Output
原文作者:Bob Bradley, Kurt Wesner
译文由本站 robot-v1.0 翻译
前言
A batched text output component that is useful for debugging XNA projects.
批处理文本输出组件,对于调试XNA项目很有用.
介绍(Introduction)
注意:这将是一系列XNA入门文章中的第一篇.(Note: This will be the first in a series of beginning XNA articles.) 启动XNA项目时,您会很快发现困难的事情很容易,而困难的事情也很困难.您要做的第一件事就是输出一些文本.这里介绍的TextOutput组件是一个XNA可绘制组件,可以以高效的批处理方式为您输出该文本.它主要是用作调试辅助工具而开发的.(When starting an XNA project, you quickly discover that hard things are easy, and easy things are hard. One of the first things you want to do is to output some text. The TextOutput component presented here is an XNA drawable component that can output that text for you in an efficient batched manner. It was developed mainly for use as a debugging aid.)
背景(Background)
XNA是一套软件工具,可简化游戏和多媒体应用程序的开发. XNA建立在.NET 2.0 Framework之上,但它包括自己的环境和其他库.当前仅XNA正式支持C#,但是从技术上讲,任何.NET语言都可以在其下工作.整洁的地方是,可以将编写为XNA的程序编译为可在Windows和XBox-360下运行.(XNA is a set of software tools that make developing games and multimedia applications a lot easier. XNA is built on top of the .NET 2.0 Framework, but it includes its own environment and additional libraries. Currently only C# is officially supported by XNA, but technically any .NET language should be able to work under it. The neat part is that programs written to XNA can be compiled to run under both Windows and XBox-360.)
- 您可以从Microsoft获得最新的XNA Game Studio Express.(You can get the latest XNA Game Studio Express from Microsoft) 这里(here) .(.)
- 您还需要Microsoft的(You will also need Microsoft’s) Visual C#Express(Visual C# Express) . (您可以在VS的专业版中创建XNA项目,但仍然需要Express才能安装Express并编译其中的某些内容.)(. (You can create XNA projects in the pro versions of VS, but you still need Express to install Express and to compile some of the content.)) 信不信由你,XNA的前几个版本都没有内置支持来显示文本.开发社区很快想出了几种解决方案.但是其中大多数涉及到必须运行一个单独的程序来将自己的字体编译为位图,必须将该位图文件复制到您的项目中,然后使用一个库将该位图加载到spriteBatch中,以便可以将其渲染到屏幕上.幸运的是,微软决定在XNA的最新版本中包含一个内置解决方案,该方案通过使字体由XNA的内置内容管理系统自动转换来简化事情.(Believe it or not, the first few versions of XNA had no built in support for displaying text what-so-ever. The development community quickly came up with several solutions. But most of them involved having to run a separate program to compile your own fonts to bitmap, having to copy that bitmap file to your project and then using a library to load that bitmap into a spriteBatch so that it could be rendered onto the screen. Luckily, Microsoft decided to include a built-in solution in the latest version of XNA that simplifies things a little by having the font automatically converted by XNA’s built in content management system.)
安装了最新版本的XNA GSE后,您可以进入"帮助"下以了解如何在屏幕上绘制文本.在当前版本中,它位于:(Once you have the latest version of XNA GSE installed, you can go under Help to learn how to draw text on the screen. In the current version it is under:) “帮助:内容:XNA Game Studio Express:编程指南:如何:绘制文本."(“Help:Contents:XNA Game Studio Express:Programming Guide:How to: Draw Text.") 该帮助中的代码示例为:(The code example from that help is:)
ForegroundBatch.Begin();
// Draw Hello World
string output = "Hello World";
// Find the center of the string
Vector2 FontOrigin = CourierNew.MeasureString( output ) / 2;
// Draw the string
ForegroundBatch.DrawString( CourierNew, output, FontPos, Color.LightGreen,
FontRotation, FontOrigin, 1.0f, SpriteEffects.None, 0.5f );
...
ForegroundBatch.End();
那还不错,但是有点麻烦.另外,多次调用子画面批处理可能很慢.(That is not that bad, but it is a little cumbersome. Plus, it can be slow calling the sprite batch many times.)
使用代码(Using the code)
要使用(To use the) TextOutput
组件,添加(component, add the)**TextOutput.cs(TextOutput.cs)**文件到您的项目.注意,您还必须创建一个名为(file to your project. Note, you will also have to create a file called)Arial.SpriteFont(Arial.SpriteFont)(只需从示例项目中复制它).((just copy it from the sample project).)
将using语句添加到以下名称空间:(Add a using statement to the following namespace:)
using UTM.CSIS.Xedge;
在您的主游戏类中为新对象添加一个成员变量:(Add a member variable for the new object inside of your main game class:)
TextOutput textOutput;
将组件添加到游戏的构造函数中:(Add the component to your game in its constructor:)
// Do this in the game's constructor
this.Components.Add(textOutput = new TextOutput(this));
现在,我们可以轻松地将文本输出到屏幕上的任何地方(Now we can easily output text to anywhere on the screen by using the) WriteAt
方法,或者我们可以使用来输出跟随鼠标的文本(method, or we can output text that follows the mouse around by using the) writeAtMouse
方法:(method:)
textOutput.WriteAt(50, 50, "Hello World!");
textOutput.writeAtMouse("Mouse is\r\n here");
文本将存储在列表中,当组件自行绘制时,它将自动绘制您提交的所有文本.(The text will be stored in a list and when the component draws itself, it will automatically draw all of the text you have submitted.)
兴趣点(Points of Interest)
您还可以输出不同颜色或不同旋转角度的文本.(You can also output text in different colors or at different rotations.)
textOutput.WriteAt(100, 100, "This will be red.", Color.Red);
代码如何工作(How the Code Works)
这是从继承的组件(This is a component that inherits from) DrawableGameComponent
.因此,一旦将此组件添加到主游戏中,它将自动自动加载并绘制.(. Because of this, once you add this component to your main game, it will automatically Load and Draw itself.)
这是一个类图,显示了如何设置类.顺便说一句,您知道您可以通过右键单击类直接从Visual Studio生成这些整洁的图吗?(Here is a class diagram that shows how the class is setup. By the way, did you know you can generate these neat diagrams right from Visual Studio just by right-clicking on the class?)
最重要的方法是(The most important method is the) WriteAt
方法.它只是将文本和属性添加到列表中,如下所示.它已重载,因此您可以使用或不使用color参数来调用它.(method. It simply adds the text and attributes to a list as shown below. It is overloaded so that you can call it with or without the color parameter.)
public void WriteAt(int x, int y, string s, Color c)
{
//create a new textNode
textNode n = new textNode();
//populate x pos
n.X = x;
//populate y pos
n.Y = y;
//populate string
n.Text = s;
//set color
n.FontColor = c;
//set rotation
n.Rotation = m_FontRotation;
//add to the batch list
m_List.Add(n);
}
的(The) WriteAt
方法使用以下(method uses the following) struct
保留文本和属性:(to hold the text and attributes:)
struct textNode
{
public int X, Y;
public string Text;
public Color FontColor;
public float Rotation;
}
现在,到了绘制的时候,代码简单地循环遍历列表,并以一个快速的sprite批处理输出所有文本.(Now, when it comes time to draw, the code simply loops though the list and outputs all of the text in one fast sprite batch.)
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
if (m_Enabled)//if we want text rendered
{
m_SpriteBatch.Begin(SpriteBlendMode.AlphaBlend,
SpriteSortMode.FrontToBack, SaveStateMode.SaveState);
//foregBatch.Begin();
// This is quicker, but may cause side-effects
foreach (textNode n in m_List)
{
m_SpriteBatch.DrawString(m_SpriteFont, n.Text,
new Vector2(n.X, n.Y), n.FontColor,
n.Rotation, new Vector2(0, 0), 1.0f, SpriteEffects.None, 0.5f);
}
m_SpriteBatch.End();
m_List.Clear();
}//end if (m_Enabled)
}
注意,(Note, the) Draw
方法会自动为我们调用,因为我们从继承了该类(method will be called for us automatically, since we inherited the class from the) DrawableGameComponent
基类.(base class.)
历史(History)
- 1.0版-2007年7月2日(Version 1.0 - July 2, 2007)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# C#2.0 Windows .NET XBox .NET2.0 Visual-Studio VS2005 Dev 新闻 翻译