Chapter 1(3):Notes about XNA

 
Notes about XNA
To finish this chapter, here are some additional tips and tricks about the XNA Framework and XNA Game Studio Express. As you saw, you can just start coding and it works great, but it is always good to have a couple of bookmarks for your browser you can rely on when you run into problems or don’t know how to solve a specific issue. Additionally, this section discusses the advantages of .NET and C# a little and checks out the differences between XNA and Managed DirectX.
作为本章的结束,这里有一些附加的关于 XNA 框架和 XNA GSE 的话题。就像你所看到的,你可以开始编写代码并且让它很好的运作,但是有多在你的浏览器中多放置几个书签总是有好处的,当你遇到问题时或不知如何解决时可以从那里得到一些技术上的支持。还有,这部分还讨论了一些 .NET C# 的优点,以及 XNA MDX 间的不同点。
Important Links重要链接
Just a couple of links for your bookmarks:
§  http://msdn.microsoft.com/directx/xna/ - XNA Developer Center on Microsoft’s MSDN page with the XNA Game Studio Forum and the XNA Framework Forum, which are the most active XNA forums you will find on the Internet. You can also download the latest XNA version here, read the FAQ, and see what’s new.
§  http://en.wikipedia.org/wiki/Microsoft_XNA - Entry on Wikipedia about XNA, constantly updated and contains many links to other topics. On the bottom of the page a few useful external links are listed.
§  http://xnadevelopment.com - Nice new site with many links, tutorials, and tips to get you started in the XNA world.
§  http://xnaresources.com - Another new site with lots of news and some really useful tutorials on tile engines. Also contains a lot of game components you might like.
§  http://learnxna.com - XNA news site in form of a blog, also contains some video tutorials and focuses on learning XNA and tips and tricks with graphic tools.
§  http://abi.exdream.com - Official website of the author. You can find more games I made here, as well as Rocket Commander XNA, the XNA Shooter, or the Racing Game from this book, and all the documentation and video tutorials I made for them. Additionally I suggest checking out the Rocket Commander Video Tutorials, which were pretty popular on Coding4Fun.
Is C# Good for Game Development?
There are constantly forum threads on sites like www.GameDev.net discussing the differences between C++ and C#. Usually after a few posts they all end in a senseless language war. Back in the early days of .NET (2002) I discussed quite a lot in those threads, but it was way too depressing when 99.9% of the programmers were on the C++ side and there was no way to convince anyone because they didn’t even take you seriously. The language war does not really have anything to do with C# as a language, except it might have some bad taste because Java failed as a game programming platform except for cell phone games, because both Java and C# are managed languages and look very similar. But if you think about it the same kind of wars happened in the days when C replaced Assembler and C++ replaced C. Even to this day, more than 20 years after C++ was developed by Bjarne Stroustrup, some game programmers still use C and do not really take full advantage of C++. Even if you take a look at the source code for popular game engines like Quake or Half-Life it looks more like C than C++.
在网上有几个像 GemeDev 这样的论坛来讨论 C++ C# 间的不同。常常是几个帖子后这个毫无意义的关于语言的争论就结束了。回到 .NET 2002 年开始)的早期时候,我在那些论坛上激烈的讨论着,但是非常郁闷的是 99.9% 的程序员站在 C++ 这一边,由于他们根本不把你当一回事儿所以你也根本无法说服他们。这场语言之争对 C# 没有产生什么实质性的影响,但也感到了一点点不舒服的味道, C# Java 都是托管语言,长得又是如此相像,而除了在手机平台上 Java 在其他地方的游戏开发是失败的。你仔细回味一下在过去发生的同样的争论,最终是 C 取代了汇编,而 C++ 又取代了 C 。即便到了现在, C++ 已经由 Bjarne Stroustrup开发出来20多年了,一些游戏程序仍然使用的是C语言而不完全是C++。如果你看看像Quake或者Half-Life这样流行的游戏引擎的话,你会发现它看起来更像C语言。
This is something really strange in the game programming world; everyone is afraid of losing too much performance by switching to a new language, and additionally might also lose their old code base or have a lot of work converting it to a new language. However, game programmers quickly adopt new techniques and scripting languages, and are always on the very latest hardware developments. One year before Shader Model 4 cards are even available we game developers had Direct3D 10 available and many people checked this out without even having the hardware to run it.
发生在游戏世界的一些事情确实非常奇怪:每一个人都害怕一个新的语言,这意味着将失去他们原本的代码基础或者需要用新的语言来重写原先的代码。然而游戏程序会非常快的适应新的技术和新的语言,这常常体现在最新的硬件发展上。在 Shader Model 4 D3D 10 一年前的硬件仍然可以运行这些程序,然而很多人却会抛弃它。
I adopted .NET and C# pretty quickly in the beginning of 2002 after checking out the early betas at the end of 2001. I just started a new game engine and our team had a new project we wanted to do. There were absolutely no graphic engines or anything but some simple 2D games around in the early years of .NET. This made it very hard to use OpenGL or DirectX directly in C#. It required a lot of calls to unmanaged dlls and involved a lot of nasty pointer logic, which is only available in the unsafe mode of C#. In 2003 Microsoft finally released the first beta of Managed DirectX, which made it possible to program new DirectX applications quite easily in .NET. It proved that using Managed DirectX instead of the native DirectX dlls has only a performance impact of 1%–2%, which is really not important if you think about it (just the CPU has a little more work; most games are GPU bound anyway).
2001 年底的早期测试版之后, 2002 年伊始我非常快的就适应了 .NET C# 。我开始了一个新的游戏引擎,我们的游戏团队有了一个他们想做的项目。在 .NET 的早期阶段除了一些简单的 2D 游戏外,是绝对没有任何使用 .NET 开发的图像引擎的。在 C# 中直接使用 OpenGL DX 是非常困难的。这需要调用大量的非托管的 dll 并且会陷入大量的指针运算,这只能在 C# 中的非安全模式中使用。到了 2003 年,微软终于放出了 MDX 的第一个 beta 版,这使得在 .NET 中编写新的 DX 程序变得非常方便。而使用 MDX 与使用原来的 DX dll 只有 1% 2% 的性能影响,如果你仔细考虑的话会发现这实际上并不重要( CPU 仅仅多做了一点点工作,而大多数游戏是在 GPU 的范畴内)。
However, this did not mean that game developers were jumping on C#; everyone was still very skeptical and even after I released the first commercial .NET game ever, Arena Wars, in 2004, it took another year until more and more developers finally gave .NET another chance. Students and beginners especially really appreciate the simplicity of C#, and more and more people started to develop games in .NET. C++ versus C# discussions still exist and the discussion points are still the same, but the result is more balanced right now (I stopped looking at any topic that has “vs” in the name a long time ago; it is just a waste of time reading the same arguments over and over again).
然而值并不意味着游戏开发者们会转向 C# ;即便是在 2004 年我发布了第一个商业化的 .NET 游戏 Arena Wars后,每一个人都还是抱着非常怀疑的眼光,后来花了一年的时间,越来越多的开发者们最终给了.NET一次机会。学生们和初学者们非常欣赏C#的简洁,越来越多的人们开始在.NET上开发游戏。C++C#之争仍在继续,话题依旧,但是现在却取得了更多的共识(我不看带有“vs”字样标题的论题已经很久了;这仅仅是重复的讨论,简直是浪费时间)。
Always remember this when you run into guys that might tell you C++ is superior and that C# is only for newbies. The same thing happened with Assembler and C++ few years ago, and in the future there will be new languages that make our life easier and there will still be people hesitant to adopt them right away. Most big game studios also can’t just adopt every new technology right away; they might be in the middle of some big project and they also have a very big code base, which is not easy to port. In the long run, however, code will get converted and we will move up the ladder of high-level languages.
记住上面那些,可能会有人跟你说 C++ 高高在上而 C# 只是一个新手。在几年前这同样发生在汇编和 C++ 身上,而在未来会后新的程序语言是你的生活更方便,而仍然有人在犹豫是否接受它。大多数大型的游戏工作室还不能非常快的适应心得技术,他们可能正处在一些大型项目的开发当中而且拥有大量的代码库,这是非常难转的。最终代码将被重写,我们将向高级语言前进。
Figure 1-15 is an old picture I did to show the differences between DirectX with C++ and Managed DirectX in C#. As you can see, MDX code can be half the size of unmanaged code. For XNA the code would even be shorter and it gets much easier to load textures and show them on the screen, but the comparison to MDX gets harder because the concepts are different.
1-15 是以前我用来表示使用 C++ DX 和使用 C# MDX 间区别的图。就像你看到的, MDX 代码量只有非托管代码量的一半。对于 XNA 来说这将会更短,加载纹理并显示出来会变得更简便,但相比 MDX 而言会更健壮,因为这是不同的概念。

Figure 1-15
Getting Used to the Content Pipeline
使用素材管线
As you saw earlier with your first XNA project it is quite easy to just drag and drop a texture into your game project inside XNA Studio. Although it is nice to have all your game content in one place and directly side by side with the code, there are a couple of things you have to remember. Most projects I saw in my life did not put the textures, models, or shaders directly in the Visual Studio project. The reason for that is the fact that it is much easier to just copy a few files over and then let the game load them directly. By default Visual Studio will also not copy any content files to your output directory; you have to change the Build Action from None to Content and set the Copy to Output Directory setting to Copy if newer. This is quite a hassle and XNA makes it a little easier.
正如在你第一个 XNA 项目里看到的那样,使用 XNA Studio 所需要做的仅仅是很简单的讲纹理拖入到你的项目中。尽管将所有素材都拖入到同一个地方并和代码放在一起是好的,但有些东西你需要记住。在我一生中见过的大多数项目里,都不会吧纹理、模型还有着色器直接放在 VS 项目中,这样做的原因是将一些文件复制并直接在游戏中加载会更方便。而 VS 在默认情况下是不会复制任何素材文件到你的输出目录中的;你就需要将 Build Action (生成操作)选项改为“ None (无)”,将 Copy to Output Directory (复制到输出目录)设为 Copy if newer(如果较新则复制)。这是具有极大争议的,XNA可以让这些变得容易些。
You might even ask why change the way you loaded textures before. If you just want your game to run on the Windows platform, you can certainly load textures and shaders dynamically and without importing them first into your project as XNA content files. This has the advantage that you can change textures or shaders while the game is running (a feature that is not supported by the XNA content pipeline). However, for model files this will not work because there are no load methods other than from the ContentManager class, which only loads compiled .xnb content files.
你可能要问为什么要改变以前加载纹理的方法。如果你只想让你的游戏在 Windows 上运行,那当然可以加载它们然后动态着色,而不需要首先将它们作为 XNA 素材文件导入到你的项目中。如果你想在游戏运行时改变文理或着色器,这样做是有优势的(这个特性不被 XNA 素材管线所支持)。然而对于模型文件来说这是不起作用的,因为在 ContentManager 类中没有用于加载的方法。它只会加载编译后的 .xnb 素材文件。
On the Xbox 360 you can only load content files; there is no support to load textures or shaders directly. If you use direct texture or shader loading, make sure you exclude that code from the Xbox 360 platform; the dlls for the Xbox will not support loading anything but the content files.
Xbox360 上你可以只加载素材文件;没有对加载纹理或着色器的直接支持。如果你使用直接的纹理或着色加载,毫无疑问 Xbox360 将会排除你的代码; Xbox dll 不支持除了素材文件外的其它任何加载。
For more details about important model files, read the chapters in Part II of this book. This topic is not as easy as using textures. For that reason this section sticks with sprites and 2D textures and keeps things simple. For simple 2D games it is nice and easy, but to write a serious game you will need 3D models and many cool shader effects. I also suggest that you get an artist to do the textures and 3D models for you. It is a quite complex topic and you might waste a lot of time doing all this yourself. Other people might also be more talented doing textures and 3D models; take advantage of your coding skills and let other people do the painting. If you don’t have any artists available, try to use some of the models and textures from this book and the XNA starter kits to get started.
关于重要的模型文件的详细知识将在本书的第二部分中学到。这个主题不像使用纹理般容易。因为这个原因,在这一部分我们使用了精灵和 2D 纹理来使例子保持简单。对于简单的 2D 游戏来说这是易于学习的,但是写一个正式的游戏你需要 3D 模型和许多很酷的着色特效。我建议你找一个美工为你制作纹理和 3D 模型。这个主题是很复杂的,如果你想自己全包这会花费你大量的时间。别人在制作纹理和 3D 模型方面可能更有天分,你要发挥自己的编程优势并让别人为你做美工。如果你找不到美工,那可以试着从使用本书中的或 XNA 初学者开发包中的模型开始。
Differences with MDX
MDX的不同
If you are coming from MDX (Managed DirectX) and want to migrate your game to the XNA Framework, there is a great guide on the official XNA pages at http://msdn.microsoft.com/directx/xna/migration/ .
如果你是由 MDX 转过来的,并且想将你的游戏一直到 XNA ,在 XNA 的网页上有一份极好的官方指导。
You can find the same help in the XNA documentation. I won’t repeat all that here, but basically you have to remember that XNA uses right-handed matrices and MDX was left handed by default. Also there are some new classes and structures to make sure you don’t need the Windows.Forms namespace anymore. Because there is no fixed function pipeline support, the old way of working with Windows Forms and handles and thinking of supporting older PC configurations is no longer necessary. Graphics and shaders are discussed in Part II of this book.
你也可以在 XNA 的文档中获得同样的帮助。我在这里就不一一复述了,但是基本的一点你要记住的就是 XNA 使用右手坐标系而 MDX 默认使用左手坐标系。有些新的类和结构让你不再需要使用 Windows.Forms 命名空间。因为【没有固定功能的管线支持 there is no fixed function pipeline support 】,所以使用 Windows 窗体和句柄的工作方式以及考虑支持旧的 PC 配置将不再需要。图形和着色将在本书的第二部分讨论。
Additional Tools and Tips
附加工具和提示
Additionally you should take a look at the Getting Started help topics in the XNA documentation, which can be accessed from XNA Studio Help Contents XNA Game Studio Express. You can find more information about connecting to your Xbox 360, writing your first project, and about all the starter kits.
你可能看了 XNA 文档的 Getting Started 主题(可以通过 XNA Studio->Help->Contents->XNA Game Studio Express 查看)。你可以找到更多的关于连接到 Xbox360 、写你第一个项目还有关于 starter kits 的信息
As I mentioned before, TestDriven.NET is a nice tool for Visual Studio and test-driven development is a very important methodology in this book (see Chapter 3 ). Another great tool for .NET development is the Ants Profiler. Unlike all other tools and programs I mentioned so far it is not free, but there are alternatives available on the net, which might help you as well. The Ants Profiler can be used to directly see how much time every single line of your project takes. In my opinion this is much more useful than using some high-level application performance tool like NvPerf from Nvidia, PIX from the DirectX SDK, or using the Performance Counters of Windows. You can quickly figure out why parts of your render code gets slow, detect bugs that call slow methods way too often, and it is always good to review your code by having some new interesting information available.
就像我之前提到的, TestDriven.NET VS 一个非常好的工具,在本书是一个非常重要的测试开发方法(见第三章)。另一个不错的 .NET 开发工具是 Ants Profiler 。不像我提到的其他工具那样,这个不是免费的,但在网上也有其他的可供利用的工具可以帮到你。通过 Ants Profiler 你可以直接了解到你项目的每一行所花费的时间。在我看来这比使用一些像 Nvidia NvPerf DX SDK PIX 或者 Windows Performance Counters 这样的高级程序执行工具有用得多。你可以很快发现为什么你的部分渲染代码会变慢,寻找经常调用执行慢的方法的 bug ,当你获得了更有趣的信息时还有利于你重新查看你的代码。
For more links on the Internet, check out the great collection of XNA links at http://xnadevelopment.com/links.shtml.
这里收集了很多很好的关于 XNA 的链接
There are many more sites about XNA and it seems every day you see a couple of new community sites and more resources. Do a Google search to find out which site is the most popular.
每天都有关于 XNA 的新网站、新资源,用 Google 搜索看看那些最受欢迎~
 
PS 。好了,第一章翻译完毕,主要是 XNA 的介绍,接下来就是使用 XNA 来真正编写游戏了~~~
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值