用 c++ 还是 c# 玩游戏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/931502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
gaming with c++ or c#?
提问by Sherif
What is the best language for programming a game project and why?
什么是编程游戏项目的最佳语言,为什么?
Why is the game programing world dominated by c++?
为什么游戏编程世界由 C++ 主导?
采纳答案by jrista
This is kind of a difficult question to answer. For the most part, C++ is a "better" language for programming games in that it gives you so much direct control over memory management that you have more options to fine tune your performance. That, along with the fact that C++ has been around ages longer than C#, have lead to its current dominance in the game industry.
这是一个很难回答的问题。在大多数情况下,C++ 是一种“更好”的游戏编程语言,因为它让您可以直接控制内存管理,从而有更多选项来微调性能。再加上 C++ 比 C# 存在更久的事实,导致其目前在游戏行业中占据主导地位。
However, these days, with the .NET platform and C# becoming so much more mature, its not easy to rule it out as a very strong contender in the game programming arena. I think this is especially true for the near future, as .NET 4.0 and C# 4.0 will bring in a whole new era of multi-threaded and concurrent programming that could bring some massive performance gains to their platform. With multicore CPU's taking the desktop computing world by storm, and massively-cored CPU's only a short step behind .NET/C# 4.0, I think the ease of developing multi-threaded applications with .NET and C# 4.0 will give C++ a run for its money.
然而,如今,随着 .NET 平台和 C# 变得越来越成熟,很难将其排除在游戏编程领域的强大竞争者之外。我认为在不久的将来尤其如此,因为 .NET 4.0 和 C# 4.0 将带来一个全新的多线程和并发编程时代,这可能会为其平台带来一些巨大的性能提升。随着多核 CPU 席卷桌面计算世界,而大核 CPU 仅落后 .NET/C# 4.0 一小步,我认为使用 .NET 和 C# 4.0 开发多线程应用程序的便利性将使 C++ 获得成功钱。
In the end, it will entirely depend upon how much control you think you need over memory management, and whether that control is worth the effort in the arena of massively parallel games. If you need that control, and think you can deal with the effort of writing a highly parallel game in C++, then you'll probably gain something from it. Otherwise, I would say C# 4.0 would be the better option.
最后,这完全取决于您认为需要对内存管理进行多少控制,以及这种控制是否值得在大规模并行游戏领域付出努力。如果您需要这种控制,并认为您可以用 C++ 编写高度并行的游戏,那么您可能会从中有所收获。否则,我会说 C# 4.0 将是更好的选择。
2011 Update:
2011年更新:
It should probably be noted that, now two years on from when I originally wrote this, Silverlight games, particularly Silverlight on Windows Phone 7, have become a large and rapidly growing platform upon which games, often quite advanced games, are being developed. Gaming on WP7 has exploded since the platform first launched in Nov. 2010, and already WP7 has more games developed for it than competing platforms. Given the ubiquity of mobile devices and the popularity of playing games on them, I think that is a strong indication of C#'s power and capability as a game development language. Quite a few WP7 games are fully 3D, graphics intensive games.
应该注意的是,从我最初写这篇文章到现在两年了,Silverlight 游戏,特别是 Windows Phone 7 上的 Silverlight,已经成为一个庞大且快速发展的平台,游戏,通常是非常高级的游戏,正在开发平台上开发。自该平台于 2010 年 11 月首次推出以来,WP7 上的游戏已经爆炸式增长,而且 WP7 为其开发的游戏已经超过了竞争平台。考虑到移动设备的普遍性和在移动设备上玩游戏的流行,我认为这有力地表明了 C# 作为游戏开发语言的力量和能力。相当多的 WP7 游戏是完全 3D 的图形密集型游戏。
回答by Stefan Mai
Games are some of the most hardware intensive applications you can run. People program games in C++ for the performance, although there are a few games coming out nowadays written in C#.
游戏是您可以运行的一些硬件密集型应用程序。人们用 C++ 编写游戏是为了性能,尽管现在有一些游戏是用 C# 编写的。
This question has been asked before, check the following posts for more details: Viability of C#/.NET as the new standard game dev platform?https://stackoverflow.com/questions/19642/suggested-gaming-platform-to-learn-direct-x-c-open-gl-or-xna
这个问题以前有人问过,查看以下帖子了解更多细节: C#/.NET 作为新标准游戏开发平台的可行性?https://stackoverflow.com/questions/19642/suggested-gaming-platform-to-learn-direct-xc-open-gl-or-xna
回答by schnaader
If you use C++, you have a much larger codebase, although this shouldn't matter for most games (except 3D games or games that'll need libraries like physics engines). C# could be easier to write, depending on how much experience you have with it (or how much less/more experience you have with C++).
如果您使用 C++,您将拥有一个更大的代码库,尽管这对于大多数游戏(3D 游戏或需要物理引擎等库的游戏除外)来说应该无关紧要。C# 可能更容易编写,具体取决于您对它的经验(或您对 C++ 的经验少/多)。
Also consider using some sort of game creation tool like XNA Game Studio or similar, they often can give you all sort of help with usual stuff.
还可以考虑使用某种游戏创建工具,例如 XNA Game Studio 或类似工具,它们通常可以为您提供各种常见东西的帮助。
回答by JP Alioto
It took years to get game developers off assembly onto C, then it took more years to get the industry onto C++. The reason is always the same ... "C will be too slow", "Objects will be to slow". There is enormous investment in code (engines, previous games, etc.) and talent in the industry in C++. It will take a long time for that to shift -- just as it has in the past. Someday, games will be developed in managed code, but it will take some time.
让游戏开发人员从 C 语言中脱离组装需要数年时间,然后将整个行业引入 C++ 需要更多时间。原因总是一样的......“C 会太慢”,“对象会很慢”。在代码(引擎、以前的游戏等)和行业内 C++ 人才方面有大量投资。这种转变需要很长时间——就像过去一样。总有一天,游戏将以托管代码开发,但这需要一些时间。
Meanwhile there are engines written for .NET in C#.
同时,也有用 C# 为 .NET 编写的引擎。
回答by MH114
For commercial / bigger game projects, C++is probably the best option. However, if you're thinking of doing smaller, indie type games, there's nothing stopping you going with C#or even Java.
对于商业/大型游戏项目,C++可能是最佳选择。但是,如果您正在考虑制作较小的独立类型游戏,那么使用C#甚至Java都不会阻止您前进。
I've done several small games with C++, and one with C# using the XNA Framework, mostly for curiosity. Currently I'm working on a platformer game using Java and LWJGL(Lightweight Java Game Library). And guess what? It's working absolutely fine, plus I get cross-platform compatibility practically for free! The reason I started this project with Java was to see if I could create a nice OpenGL-accelerated game using Java instead of C++ I've normally used. So far it's working very nicely for me, I was really suprised actually. =) To be honest I've become a bit tired with C++, coding in Java feels much more productive for me currently (big thanks goes to EclipseIDE!).
我用 C++ 做了几个小游戏,还有一个用 C# 用XNA Framework 做的,主要是为了好奇。目前我正在使用 Java 和LWJGL(轻量级 Java 游戏库)开发平台游戏。你猜怎么着?它工作得非常好,而且我几乎免费获得跨平台兼容性!我用 Java 开始这个项目的原因是想看看我是否可以使用 Java 而不是我通常使用的 C++ 来创建一个很好的 OpenGL 加速游戏。到目前为止,它对我来说效果很好,实际上我真的很惊讶。=) 老实说,我对 C++ 有点厌倦了,目前用 Java 编码对我来说感觉效率更高(非常感谢EclipseIDE!)。
So, for smaller games you can definitely have success with Java or C#, no need to bother with C++, unless you're doing something that needs 100% of the possible performance. XNA might be the easiest option to get started with, although as for myself I most probably won't be using it again since it's basically Windows only (plus Xbox 360)..
因此,对于较小的游戏,您肯定可以使用 Java 或 C# 取得成功,无需费心使用 C++,除非您正在做的事情需要 100% 的可能性能。XNA 可能是最容易上手的选择,尽管对于我自己,我很可能不会再使用它,因为它基本上只适用于 Windows(加上 Xbox 360)。
回答by CurtainDog
'Performance' is a red herring. Yes, games are just about the most demanding application that most consumers will run. However, most of the heavy lifting can be done with dedicated hardware, which will beat a software solution in any language any day of the week. The proliferation of Flash games should be proof enough that in most cases we have performance to burn (and now that we have some decent implementations I think we'll soon see games apearing in Javascript too).
“性能”是一条红鲱鱼。是的,游戏只是大多数消费者将运行的要求最高的应用程序。然而,大部分繁重的工作都可以通过专用硬件完成,这将击败一周中任何一天的任何语言的软件解决方案。Flash 游戏的激增应该足以证明,在大多数情况下,我们有性能需要消耗(现在我们有了一些不错的实现,我想我们很快就会看到游戏也出现在 Javascript 中)。
I think it's just that many other languages are billed as business focused, or academic, or statistical, and developers on the whole haven't really felt the need to challenge those assumptions.
我认为只是许多其他语言被标榜为专注于业务、学术或统计,而开发人员总体上并没有真正觉得有必要挑战这些假设。
回答by JulianR
I think you'll find that, when you create your own game, that being CPU bottlenecked (the only aspect on which language choice has any effect) is the last thing you need to be worried about. It's much, much easier to be bottlenecked by the GPU. Hell, with today's minimum of 2 cores, I would find it impressive if you even managed to ever reach 100% CPU usage because that would require a perfect workload division to utilize the 2 or 4 cores of the CPU.
我想你会发现,当你创建自己的游戏时,CPU 瓶颈(语言选择有任何影响的唯一方面)是你最不需要担心的事情。GPU 的瓶颈要容易得多。见鬼,今天最少有 2 个内核,如果您甚至设法达到 100% 的 CPU 使用率,我会发现它令人印象深刻,因为这需要一个完美的工作负载划分来利用 CPU 的 2 或 4 个内核。
Generally, it's much too common for me to run a game (presumably written in C++) to see it use at most 50%-60% of my CPU. And can you then really speak of a bottleneck with the language that you use, or a bottleneck of the algorithms used that don't allow you to scale across more than 1 CPU core? So even in a situation where you run into the limits of a single core, it's still possible to (theoretically) double your performance by optimizing your algorithms before you can truly say you're being limited by the language.
通常,运行游戏(大概是用 C++ 编写的)以看到它最多使用我的 CPU 的 50%-60% 对我来说太常见了。那么,您真的可以谈论您使用的语言的瓶颈,或者所使用的算法的瓶颈,这些瓶颈不允许您跨 1 个以上的 CPU 内核进行扩展吗?因此,即使在遇到单核限制的情况下,仍然可以(理论上)通过优化算法将性能提高一倍,然后才能真正说自己受到语言的限制。
And this is only going to increase in the future with the advent of 8, 16 and 32 cores. You reallydon't want to have a game that's only capable of using only 1 core out of 32.
随着 8、16 和 32 核的出现,这只会在未来增加。您真的不希望有一款只能使用 32 个内核中的 1 个内核的游戏。
The real reason why C# isn't being used in the industry: there's no reason compelling enough for game studios that have invested a lot of money into their C++ code to switch to C#. Not just their code, but their developers are already very good in C++ so it would be odd to switch to C# for "ease of use" when that actually means a lot of new challenges to overcome and a lot of money to rewrite existing code. And after all that, you've got new code that's only supported on the PC (with Mono for Linux support) and Xbox 360. No PS3, no Wii. So you've just limited yourself for little gain.
C# 没有在行业中使用的真正原因是:对于在 C++ 代码上投入大量资金的游戏工作室来说,没有理由转而使用 C#。不仅是他们的代码,而且他们的开发人员在 C++ 方面已经非常出色,因此为了“易于使用”而切换到 C# 会很奇怪,因为这实际上意味着要克服许多新挑战,并且需要花费大量资金来重写现有代码。毕竟,您已经获得了仅在 PC(支持 Linux 的 Mono)和 Xbox 360 上支持的新代码。没有 PS3,没有 Wii。所以你只是为了一点收获而限制了自己。