Chapter 1(1): Introducing XNA

 
 
Chapter 1: Introducing XNA
Welcome to the world of XNA. As a game programmer you probably know about DirectX and maybe even the basics of the XNA Framework. This chapter explains how to install XNA Game Studio Express and how to use it in a productive way. It also contains quite a lot of tips that might even be useful for anyone who already knows the basics.
In the next few chapters you will directly start developing some cool smaller games. This section contains the basic foundation and helper classes you will use later in this book to develop a fullblown graphics engine for more advanced game projects. To keep things simple you start with 2D programming in Part I and then advance to 3D in Part II.
Let’s get started.
Introduction to the XNA Framework
XNA is developed by Microsoft and was started a few years ago, but kept very secret. At the GDC (annually biggest Game Developers Conference) in 2004 Microsoft announced XNA for the first time. XNA is not just a framework like DirectX; it also contains a lot of tools and even a custom IDE derived from Visual Studio to make the game programmer’s life easier. Because no tools or bits were released until 2006, DirectX developers only noticed the “XNA” logo in the upper-right corner of the DirectX SDK documentation from 2004 to 2006 (see Figure 1-1 ). (XNA just means “XNAs Not Acronymed.”)

Figure 1-1
This means Microsoft was working on the XNA Framework for quite a while, but the developers did not really know what to expect. It could be a successor of the DirectX Framework, but when Direct3D 10 Beta for Windows Vista was released in the end of 2005, it seemed that DirectX was still the preferred graphics framework even for this new operating system. Then early in 2006 at the GDC the Microsoft XNA Build March 2006 CTP was released. XNA Build is a tool that allows you to manage complex build processes, similar to Msbuild and tools like Ants, but more complex and powerful. Because Microsoft’s MechCommander 2 was also released as a Shared Source Release, a lot of people downloaded it and tried to rebuild the MechCommander 2 game. But after a while not much happened, and especially small to mid-sized teams don’t really need a complex build management tool.
Then it was quiet for a while and only Microsoft personnel and DirectX MVPs (luckily I am one) got to know about the upcoming XNA Framework and XNA Game Studio releases. The rest of the world found out about that at the Gamefest conference in August (a new game developer conference by Microsoft), where Microsoft announced the XNA Game Studio Express beta 1 release on August 30, 2006. The first beta only contained one starter kit, “Space Wars,” and XNA did not include much 3D functionality. Many developers and hobbyists tried out XNA and wrote many small 2D games with the help of the Sprite classes in XNA. Although you could quickly create your Pong clone or some simple shoot-’em-up game, it was very hard to write your own 3D model importer and render code.
XNA Game Studio Express was initially targeted at beginners, hobbyists, and students to allow them to quickly develop their own games for the Windows and Xbox 360 platform. But this does not mean professional game studios cannot use XNA yet. A special XNA Game Studio Professional version targeted to professional game studios will be released in the summer of 2007, but if you want to start developing in Visual Studio 2005 Professional, follow the tricks I describe later in this chapter on how to work with VS and XNA productively.
Gladly, Microsoft released another beta a few months later in November 2006 before the final release of XNA Game Studio Express happened in December 2006, which includes the content pipeline and many new features you will learn about in Chapters 2 and 3 .
XNA is completely free and allows developers to create games for both the Windows platform and for the Xbox 360 platform simultaneously for the first time. But if you want to run your XNA game on the Xbox 360 console you have to join the “Creators Club” for an annual fee of $99.
XNA Game Studio Express
Figure 1-2 shows you XNA Game Studio Express. You learn how to install it in a second.

Figure 1-2
The screen does not only look similar to Visual C# 2005 Express, it actually is just that. There are only some minor changes to your project settings if you create an XNA project. There is also an extra option in Tools Options, which allows you to select your Xbox 360 device and enter the encryption key. Additionally, there are some new features inside the IDE; for example, the content pipeline that allows you to import textures, models, and shaders very quickly into your project. More about all that in a little bit.
XNA Game Studio Express is currently the only available IDE for developing games with the XNA Framework, but Microsoft will ship XNA Game Studio Pro sometime in 2007 (summer or later). XNA Game Studio Professional will be based on the Visual Studio 2005 Professional Edition and allows you to use all Visual Studio plugins and features. If you own Visual Studio 2005 and try to create an XNA project, there will be no templates in it. Even worse: If you try to open a .csproj file you created with XNA Game Studio Express, Visual Studio cannot open the project. It is not supported. Read on for a few tips on how to get XNA working with Visual Studio 2005 in the section “ What about Visual Studio 2005 Professional?
Microsoft also mentions another version of the XNA IDE called just XNA Studio, which is based on the Visual Studio Team System version and is targeted to large AAA studios. Read more about the XNA Framework at http://msdn.microsoft.com/directx/xna/faq .
Application Model
The XNA Framework is divided into three essential parts (see Figure 1-3 ):
XNA 框架由三个主要的部分组成
§ XNA Graphic Engine in the Microsoft.Xna.Framework.dll
§ XNA Game Application Model in the Microsoft.Xna.Framework.Game.dll
§ XNA Content Pipeline in the Microsoft.Xna.Framework.Content.Pipeline.dlls

Figure 1-3
 
All of these dlls are written in C# and are completely managed dlls. This means you can open them up with a tool like Reflection (get it from http://www.aisto.com/roeder/dotnet/ ) and see directly how they work (see Figure 1-4 ). Most internal functionality just calls to the DirectX dlls and simplifies things a little bit. The content pipeline is discussed in a little bit.
所有这些 dll 是用 C# 来写的,是完全的托管 dll 。这意味着你可以用像 Reflection 这样的工具打开并直接查看它们是如何工作的。大部分内部技能是通过调用 DX dll 来实现的,并作了一点简化。还要稍稍论及素材管线。

Figure 1-4
 
Take a look at the Application Model. Each XNA project uses a Game class, which contains all the important game components, the graphic device, the window settings, and the content manager. You can also add the input and sound handling here. Basically everything that your game does on a very high level somehow roots in the game class or at least is in some component that can be accessed from the game class.
看看应用模型。每一个 XNA 项目适用一个 Game 类,这个类里面包含了所有重要的游戏成分,图形设备,窗口设置和素材管理器。你在这里同样可以添加输入和处理音效。基本上你游戏中所有高层的都会在 game 类里开始,或至少在从 game 类里接入的部分。
The following are the three most important methods in the game class (see Figure 1-5 ). For the final release Microsoft also added the LoadGraphicsContent and UnloadGraphicsContent helper methods by default to the Game1.cs class, which is created automatically for you when you create a new XNA project, but these methods will never be used in this book because it is much simpler to have all initialization and loading code at one place in the Initialize function.
以下是 game 类里最重要的三个方法。在最终放出的版本中 MS 添加了 LoadGraphicsContent UnloadGraphicsContent 两个辅助方法,默认在 Game1.cs 类中,当你创建一个 XNA 项目时,这两个方法就被自动创建了,但是这两个方法在本书中不会被用到,因为在 Initialize 函数中写初始化和加载代码更简便。

Figure 1-5
§ Initialize ()
§ Update (GameTime time)
§ Draw (GameTime time)  
You can probably already guess what all these do. Initialize loads all your game content, sets all your startup settings, and initializes everything you need. If you want to follow the design patterns Microsoft provides for XNA you would do all the loading in the LoadGraphicsContent method. Update is called before each frame is drawn to update your game time, input, sound, and everything else that is not visible on the screen. If your game is GPU limited it can very well happen that Update is called more often than Draw , but your update code should run separate from the drawing code anyway and none of the samples in this book will need special care for the number of times Update and Draw are called. And finally, Draw is called each frame to draw everything to the screen. The separation of Update and Draw might not always be important and can almost always be ignored for unit tests, but for the final game it is quite important to make sure the game logic runs independent of the draw code. For example, on the Windows platform the user could press Alt and Tab or minimize the game, in which case Draw does not need to be called anymore, but maybe you want the game to continue to run in the background. This is especially important for network games to make sure the player is still synchronized.
你可能已经猜到这些是做什么的了。 Initialize 加载你游戏中的所有素材,设置开始设置,还有初始化你所需要的所有内容。如果你想跟着 MS XNA 提供的设计模式,你要在 LoadGraphicsContent 方法中进行所有的加载 . Update 在绘制每一帧前被调用,用于更新游戏时间、输入、声音和所有其他在屏幕上不可见的东西。如果你的游戏瓶颈在于 GPU ,那么就会发生 Update Draw 更频繁被调用的情况,但是无论如何你的 update 代码应该保持与绘制代码的独立,在本书中没有一个例子会特别的关注 Update Draw 方法的调用次数。最后,每一帧调用 Draw 方法来在屏幕上绘制所有的东西。 Update Draw 的独立并不总是那么重要,并且在单元测试的大多数情况下可以被忽略,但是,对于最终的游戏来说这是非常重要的,要确保游戏逻辑不依赖于 / 独立于 绘制代码运行。举个例子,在 Windows 平台上,玩家可以按 Alt Tab 键或最小化游戏,在这种情况下 Draw 就不再需要被调用,但是可能你希望游戏在后台继续运行。确定玩家仍在同步对于网络游戏来说是极其重要的。
Additionally, you can add GameComponent classes to your game class, which again have an Update and a Draw method. Both these methods are automatically called from your game Update and Draw methods. The initialization can happen directly in the constructor there. Initially Microsoft wanted the developers to create and add game components with the designer of Visual Studio, which can be seen in the first beta of XNA Game Studio Express (30 August, 2006). The designer feature was later removed because it did not work well, was not supported for the Xbox 360 platform, and not many developers used it anyway.
还要说的是,你可以在你的游戏类中添加许多 GameComponent 类,其中每个可以再次包含一个 Update 和一个 Draw 方法。这两个方法都会被你游戏中的 update Draw 方法自动调用。在构造函数中直接进行初始化。最初 MS 希望开发者们自己通过 VS 的设计器创建并添加游戏组件,这可见于 2006 8 30 日发布的 XNA GSE 的第一个 beta 版。设计器特征再后来的版本中被移除了,因为它不是很好,不被 Xbox360 平台支持,而且也没什么开发者适用。
The idea with the game components is to reuse parts of your code and make it very easy to just plug them into your games. Examples would be a frame counter or maybe a sky cube mapping renderer for the 3D background. In my opinion there are two major drawbacks: No standard game components are shipped with XNA, and it is not really hard to code such an application model yourself and even extend it. I will not use many GameComponent classes in this book, but feel free to plug them in on your own. Read Chapter 4 for more details of the GameComponent class and learn about its advantages and disadvantages. Because the game class has a Components property, it is very easy to add more components.
游戏组件【 game component 】的思想就是让你的代码得到复用,并很容易的插入到你的游戏中。例子就是一个帧计数器或可能是一个 3D 背景的天空立方体贴图渲染【 sky cube mapping renderer 】。在我看来,这里主要有两个缺点:一个是 xna 中并没有包含标准游戏组件,而且像自己编写并扩展一个程序模型也不是什么难事。我在本书中不会使用很多 GameComponent 类,你可以自由的在你的游戏中加入他们。阅读第 4 章会有更详细的关于 GameComponent 类的优缺点的论述。因为 game 类有一个 Components 属性,这就可以非常方便的加入更多的组件。
Don’t get me wrong, the basic idea of game components is really great; there was a small webcast from Mitch Walker, the Program Manager of the XNA Framework at Microsoft, at the time the first XNA beta was released about the game components and how to combine them. At first I was not very sure what to think of the content pipeline and the game components idea; it looked cool in the webcasts, but when I started coding my first game projects in XNA I did not find it very useful to work with the designer that was presented in the first beta. In the second beta and the final release, most of the initial game component code was removed as well as the graphical designer component in XNA Game Studio. This happened because Microsoft was not able to find a way to implement the designer and game components idea in a useful way for the Xbox 360 console (it only worked on the Windows platform before). This just confirmed my first feeling that the game component idea is not very useful and may even limit you in certain respects, like having to worry about the drawing yourself (see Chapter 4 for more details).
不要理解错我的意思,我的意思是游戏组件的基本思想是非常好的; Mitch Walker MS XNA 框架项目经理)有过一个短小的网络广播,讲述 XNA 框架下怎样去结合组件使用,那时使用的是 XNA 的第一个 beta 版本。一开始我我对素材管线和游戏组件的感觉还不明确;他在网络广播中看起来很酷,但当我开始编写我第一个 XNA 游戏时,我发现它和设计器在第一个 beta 版本中表现的不是很有用。在第二个 beta 版和最终版的 XNA GSE 中,大多数最初的游戏组件和图像设计器组件代码被移除了。之所以这样是因为 MS 无法将设计器和游戏组件的想法有效的移植到 Xbox360 平台上(此前只能在 Windows 平台上运行)。这就确定了我的第一感觉,觉得 game component 不是非常有用,甚至会在某些情况下限制你,例如像担心要自己绘制。
This is similar to the fact that every game implements its own UI and menu logic, which is quite a hassle. But if you think about it, just some standard menu system like Windows uses for every app is very boring and it is always a nice experience to see a new way in which menus are created in every game. You can still extract code very easily and create your own game components if you want to create a new game, and then you can just reuse the code (for example, the SkyCubeMapping class). Some game components like the camera and screenshot capturer classes from Chapter 4 are good examples for components that can be reused in many other games, but almost everything else in the games from this book is implemented without using game components.
就像每一个游戏都执行它自己的 UI 和菜单逻辑一样,这是具有激烈的争议的。但是如果你考虑它,就像标准的 Windows 菜单系统一样非常枯燥无聊,但它又总是很好的体验,去寻求新的方法,来为每一个游戏创建菜单。如果你想创建一个新的游戏,你可以很容易的提取代码并创建你自己的 game component ,然后仅仅是重用代码就行了(例如 SkyCubeMapping 类)。一些像第四章中的照相机和屏幕截图类之类的 game component 是非常好的例子,这些可以在很多其它的游戏中使用,但本书中的绝大多数游戏是不使用 game component 的。
One of the hopes Microsoft has with this application model is that the community of game developers can create and share their game components quite easily and improve the community aspect of XNA. For more information you can check out the eXperience project on www.codeplex.com.
Content Pipeline
素材管线
The content pipeline is used to import, compile, and load game assets like textures, 3D models, shaders, and sound files to your game project (see Figure 1-6 ). It greatly reduces the amount of custom code you have to do to get graphics, 3D data, and shaders into your game. For example, if you drop a model file into your project and it uses two textures and a specific shader, the content pipeline will process your model file and automatically find and add the required textures and shaders. You don’t have to do this for yourself; the content importer “compiles” the data into a binary format and in the process it picks up everything you need.
素材管线用于输入,编译和加载游戏资源,例如纹理、 3D 模型和声音文件。它大大减轻了用户在游戏中自己获取图像、 3D 数据和着色器的代码量。例如,你想将一个模型文件加入到你的项目中,要使用到两个纹理和一个专门的着色器,这时素材管线将处理你的模型文件并自动寻找和添加所需的纹理和着色器。你不需要自己做这些工作;素材导入器将数据“编译”成为二进制格式,处理过程中可以根据需要使用任何资源。

Figure 1-6 
Say the shader you added through the model you dropped into your project contains an error. In the past you would have to start your game project, and then get an exception telling you that the shader could not be compiled and the game would crash. Now the shader is compiled in the build process and you don’t have to start your game to see that. You can quickly fix the error through the line and error message from the build output in XNA Game Studio Express and then rebuild.
比方说通过拖入到项目里的模型添加的着色器包含一个错误。在过去你不得不启动你的游戏项目,然后获得一个异常告诉你着色器不能被编译,游戏将失败【 crash 】。现在着色器在在建立过程中被编译,你不需要启动你的游戏去看【它没有运行( it does not work yet )】。你可以通过 XNA GSE 的【 build output 】的【 line 】和错误信息迅速修复错误然后重建项目。
The content pipeline does not just consist of one dll; there are five different dlls:
素材管线不仅包括一个 dll 文件,它总共有 5 个不同的 dll 组成:
§  Microsoft.Xna.Framework.Content.Pipeline.dll contains the basic functions for the content pipeline.
§  Microsoft.Xna.Framework.Content.Pipeline.dll 包括素材管线的基本功能
§  Microsoft.Xna.Framework.Content.Pipeline.EffectImporter.dll is used to compile and import shaders.
§  Microsoft.Xna.Framework.Content.Pipeline.EffectImporter.dll 用于编译和导入着色器
§  Microsoft.Xna.Framework.Content.Pipeline.FBXImporter.dll is the biggest of all dlls and contains a lot of code to import .fbx 3D model files and supports many features, for example skinning and bones.
§  Microsoft.Xna.Framework.Content.Pipeline.FBXImporter.dll 是所有 dll 中最大的,包含了许多导入 .fbx 3D 模型文件和支持例如皮肤和骨骼等许多特征的代码
§  Microsoft.Xna.Framework.Content.Pipeline.TextureImporter.dll is used to import texture files to your game. These files can be dds files already in the DirectX format (which is the best format for textures and supports hardware compression), but .png, .jpg, .bmp, and .tga files are also supported. 2D sprites are just textures too and usually use the uncompressed 32-bit format.
§  Microsoft.Xna.Framework.Content.Pipeline.TextureImporter.dll 用于向你的游戏中导入纹理文件。这些文件可以是 DX 格式的 dds 文件(这是最好的纹理格式,支持硬件压缩),也可以是 .png, .jpg, .bmp 或者是 .tga 文件。 2D 精灵仅仅是纹理,经常使用未压缩的 32 位格式。
§  Microsoft.Xna.Framework.Content.Pipeline.XImporter.dll allows you to import .x 3D model files, a format that was used by many DirectX applications and samples.
§  Microsoft.Xna.Framework.Content.Pipeline.XImporter.dll 允许你导入 .x 格式的 3D 模型文件,这是一个用于许多 DX 程序和例子的格式。
Your game itself will never require any of these dlls; they are just used to build and compile the content into .xnb (XNA Binary) files in your build process. This makes the distribution easier because you don’t have to worry about the game content files anymore; it is easier to make sure all the content files are there when you start your game. Don’t modify the .xnb files, they are just the output format like .exe files and should not be modified directly. The data can also not be converted back to textures, models, or shaders (well it might be possible, but there are no tools for that). The .xnb files are also very different on the Windows platform and the Xbox 360 platform, whereas the game source code and the content files might be exactly the same.
你的游戏将不会需要这些 dll ,这些只是在 build 过程中用于将素材建立和编译成为 .xnb 文件时使用到。这就使得发布非常容易简单,因为你再也不需要担心游戏的素材文件,保证在游戏运行时,所有素材文件都已经存在了。不要修改 .xnb 文件,这些紧紧是像 .exe 一样的输出格式,不能直接修改。数据也不能转回纹理、模型或着色器(即使这是可能的,但也没有工具能做到)。 .xnb 文件在 Windows 平台和 Xbox360 平台上是大相径庭的,但游戏源代码和素材文件确实极其相似的。
Additionally you can create your own custom content processors, which allow you to compile any other game asset you have (for example, another model format) into .xnb files. You explore that in Part II when you have to make sure all your 3D models have tangent data for the normal mapping shaders.
还要说一点,你可以创建自己的素材处理器,使你可以编译其他的资源(例如其他的模型格式)成为 .xnb 文件。
Ok, that’s all the basics you need for now. It is time to get going and code your first game.
好了,这些就是现在你所需要的基础。是时候往前走了,开始你的第一个游戏吧~~~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值