用于游戏编程的 C 与 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1569559/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:12:48  来源:igfitidea点击:

C vs. Java for game programming

javac

提问by Jonathan

I understand that C++ is generally considered better than Java for games (at least larger-scale games). Why is this?

我知道在游戏(至少是大型游戏)方面,C++ 通常被认为比 Java 更好。为什么是这样?

What is keeping Java from being competitive in this field? Which reasons against using Java for game programming have the most basis, and which ones are myths?

是什么阻碍了 Java 在该领域的竞争力?哪些反对使用 Java 进行游戏编程的理由最有根据,哪些是神话?

EDIT: Am a bit unfamiliar with C/C++, and did not think to differentiate between the two at line 1 >.<

编辑:我对 C/C++ 有点陌生,并且没想到在第 1 行区分两者 >.<

采纳答案by Sam Harwell

The reason Java (and C#/.NET) is not a viable option for AAA titles at this point is the established game engines and their toolchains are written in C++. Game development is all about getting a title on the market in the shortest amount of time, and budgets don't allow for piddling in things like a new language/engine when several are already available, work well, and have an extensive set of editors and tools backing them.

Java(和 C#/.NET)在这一点上不是 AAA 游戏的可行选择的原因是已建立的游戏引擎及其工具链是用 C++ 编写的。游戏开发就是在最短的时间内在市场上获得一个标题,并且预算不允许在新语言/引擎等已经可用、运行良好并拥有大量编辑器的情况下闲逛和支持它们的工具。

Moving to Java (or C#) would also require a new performance-driven JVM (or CLI) across the big-3 (PC, X360, PS3) or big-5 (add Wii, iPhone). It's technically doable, but not financially viable.

迁移到 Java(或 C#)还需要一个新的性能驱动的 JVM(或 CLI),跨越三大巨头(PC、X360、PS3)或五巨头(添加 Wii、iPhone)。它在技术上可行,但在财务上不可行。

Edit: Anyone with low-level knowledge of both virtual machines and the current state of game engines can tell you that a JVM or CLI could unquestionably be implemented with a new game engine to beat the performance of current C++ engines. The preventing factor is time and money, nothing more and nothing less.

编辑:任何对虚拟机和游戏引擎的当前状态有低级知识的人都可以告诉你,JVM 或 CLI 毫无疑问可以用新的游戏引擎来实现,以击败当前 C++ 引擎的性能。预防因素是时间和金钱,仅此而已。

回答by xyz

High performance and the inertia of C and C++ traditionally being used for games.

传统上用于游戏的 C 和 C++ 的高性能和惯性。

Choosing based on performance isn't that big a priority unless you are making a 3D extravaganza.

除非您正在制作 3D 盛会,否则根据性能进行选择并不是一个重要的优先事项。

回答by Ed S.

Because:

因为:

  1. Java is not compiled to native code, meaning that there is a performance hit the first time the code is run.
  2. Java does not give you a predictable memory model (console games need this)
  3. Java does not give you deterministic object finalization.
  4. Java is not as close to the hardware as C is, an essential for a lot of professional 3D game programming.
  5. Console programmers likely don't have a JVM that runs on the PS3, X-Box, etc.
  6. Runtime performance penalties.
  7. You will never be able to squeeze as much performance out of a Java app as you can with a C++ app.
  1. Java 不会编译为本机代码,这意味着第一次运行代码时性能会受到影响。
  2. Java 没有给你一个可预测的内存模型(控制台游戏需要这个)
  3. Java 不会为您提供确定性的对象终结。
  4. Java 不像 C 那样接近硬件,这是许多专业 3D 游戏编程必不可少的。
  5. 控制台程序员可能没有在 PS3、X-Box 等上运行的 JVM。
  6. 运行时性能损失。
  7. 您永远无法像使用 C++ 应用程序那样从 Java 应用程序中获得尽可能多的性能。

There are probably more reasons, like the fact that they are using pre-existing code that was written in C or C++.

可能还有更多原因,例如他们使用的是用 C 或 C++ 编写的预先存在的代码。

EDIT: As an aside, I don't think that many modern games are written in C. OOP lends itself to game development, and C++ is the de facto language of choice.

编辑:顺便说一句,我不认为许多现代游戏都是用 C 编写的。 OOP 适合游戏开发,而 C++ 是事实上的首选语言。

Also, I won't add it to my list, but as others have mentioned there is a lot of pre-existing code that works very well that is used in the game industry. It would not be practical to rewrite all/most of your tools just to switch to a new language especially when that switch could cause you a lot of headaches.

此外,我不会将它添加到我的列表中,但正如其他人所提到的,有很多预先存在的代码在游戏行业中使用得非常好。仅仅为了切换到一种新语言而重写所有/大部分工具是不切实际的,尤其是当这种切换可能会给您带来很多麻烦时。

回答by Heath Hunnicutt

I wouldn't say that it is always true that C is better than Java for game programming.

我不会说 C 在游戏编程方面比 Java 更好。

For example, if you want to write a game client which can be hosted in a web page, Java could be better.

例如,如果您想编写一个可以托管在网页中的游戏客户端,Java 可能会更好。

When writing games which produce amazing 3-D graphics (e.g., Halo on the X-box), usually there is barely enough computing speed to generate all the pixels for each frame. In this kind of game, C would be preferable to Java because it allows the programmer to write faster programs -- at a large expense in terms of difficulty in extracting that speed.

在编写产生惊人 3-D 图形的游戏(例如 X-box 上的 Halo)时,通常几乎没有足够的计算速度来为每一帧生成所有像素。在这种游戏中,C 比 Java 更可取,因为它允许程序员编写更快的程序——在提取这种速度的难度方面付出了很大的代价。

回答by Kris

I think it's mostly because c lets developers squeeze every last little bit of performance out of hardware, whereas Java doesn't, it's not low level enough for things like high end 3d video renderers. Basically c lets you squeeze out a couple more frames per second in your next gen shooter.

我认为这主要是因为 c 允许开发人员从硬件中榨取最后一点点性能,而 Java 没有,对于高端 3d 视频渲染器之类的东西来说,它的水平还不够低。基本上,c 可以让您在下一代射击游戏中每秒多出几帧。

回答by Mike Dinescu

I would say, despite the other answers pointing to a lack in speed caused mainly by the JVM, that the real reason people don't code games in Java is the lack of support for environments such as DirectX and OpenGL (which actually remove the need for your code to be close to he hardware as it was suggested by some answers). They are the base frameworks that people generally use to code games, especially nowadays with 3D games being everywhere - and lack of support for them is the reason why Java is not considered as a language for game development.

我会说,尽管其他答案指出主要由 JVM 引起的速度不足,但人们不在 Java 中编写游戏的真正原因是缺乏对 DirectX 和 OpenGL 等环境的支持(实际上消除了对使您的代码接近硬件,正如某些答案所建议的那样)。它们是人们通常用来编写游戏代码的基本框架,尤其是在 3D 游戏无处不在的今天——缺乏对它们的支持是 Java 不被视为游戏开发语言的原因。

To emphasize my point, I would suggest you take a look at Microsoft's XNAwhich is currently optimized for coding in C# via the .NET framework (which like Java is Just-In-Time-Compiled and doesn't run nativelyper se). The XNA framework interfaces with DirectX which talksto the hardware and so it is very fast.

为了强调我的观点,我建议您查看 Microsoft 的XNA,它目前已针对通过 .NET 框架在 C# 中编码进行了优化(与 Java 一样,它是即时编译的,并且本身不能在本机运行)。XNA 框架与 DirectX 接口,后者与硬件对话,因此速度非常快。

EDIT

编辑

@Ed Swangren's comment made me realize yet another distinction between .NET vs Java when considered for game development. I think another strong point to .NET is that if you do need to be able to squeeze out that last bit of performance and do some pointer math or implement a sophisticated high-performance algorithm it's a lot easier thanks to the unsafemode. Of course you can even go beyond that and write native libraries to be used by your C# code which is made pretty simple thanks to P/Invoke.

@Ed Swangren 的评论让我意识到在考虑游戏开发时 .NET 与 Java 之间的另一个区别。我认为 .NET 的另一个优点是,如果您确实需要能够挤出最后一点性能并进行一些指针数学运算或实现复杂的高性能算法,那么由于不安全模式,它会容易得多。当然,您甚至可以更进一步,编写供 C# 代码使用的本机库,这要归功于P/Invoke变得非常简单。

回答by Noldorin

Leaving C(++) aside for the moment... I am inclined to say much of the reason is that Java lacks anything like XNA. What advantages does Java actually have over a language such as C++ when it comes to game development? You have to consider that several of it's typical advantages disappear for the specific area of game development, while C++ gains several.

暂时把 C(++) 放在一边……我倾向于说大部分原因是 Java 缺少像XNA这样的东西。在游戏开发方面,Java 与 C++ 等语言相比实际上有什么优势?你必须考虑到它的一些典型优势在游戏开发的特定领域消失了,而 C++ 获得了一些优势。

XNA is what made C# a highly popular language for amateur game development, and contrary to common belief, a quite viable option for commercial development too. C#/.NET being a parallel to Java in so many ways (and arguably a better framework nowadays), when people now have the option for game development with a higher-level language, C# would seem like the much more appealing one, unless cross-platform support is essential (then again, we have Mono and OpenGL for .NET).

XNA 使 C# 成为非常流行的业余游戏开发语言,并且与普遍看法相反,它也是商业开发的一个非常可行的选择。C#/.NET 在很多方面都与 Java 相似(并且现在可以说是一个更好的框架),当人们现在可以选择使用高级语言进行游戏开发时,C# 似乎更有吸引力,除非交叉- 平台支持是必不可少的(再说一次,我们有用于 .NET 的 Mono 和 OpenGL)。

C (or rather, C++) has long been thelanguage of game development due to their low-level nature (thus performance benefits) and the host of graphics frameworks (DirectX, OpenGL) and engines that primarily target them. It's usage is embedded in game development and been used virtually since the inception of the industry - and won't disappear any time soon, I suspect.

C(或者更确切地说,C++)长期以来一直是游戏开发语言,因为它们具有低级性质(因此具有性能优势)以及主要针对它们的大量图形框架(DirectX、OpenGL)和引擎。它的用法嵌入在游戏开发中,并且自该行业成立以来就被虚拟使用 - 我怀疑它不会很快消失。

回答by Keith Nicholas

  1. Java doesn't have as controllable performance
  2. Its highly reversable so harder to protect
  3. Many Games employ scripting languages like lua or python to get "higher" level programming
  4. the API of most systems is C oriented.
  5. Java can be used for back end server systems that games connect to
  6. Flash games seems to of taken the niche Java games could have had.
  1. Java 没有那么可控的性能
  2. 它高度可逆,因此更难保护
  3. 许多游戏使用 lua 或 python 等脚本语言来获得“更高”级别的编程
  4. 大多数系统的 API 都是面向 C 的。
  5. Java 可用于游戏连接的后端服务器系统
  6. Flash 游戏似乎占据了 Java 游戏本来可以拥有的小众市场。

回答by BeWarned

I think C or C++ is a better language for building many types of games because it is closer to the hardware and likely to be the first language implemented on any new hardware. Not only that the libraries for accessing many of the advanced features of today's hardware are likely to be implemented in C.

我认为 C 或 C++ 是用于构建多种类型游戏的更好语言,因为它更接近硬件,并且可能是在任何新硬件上实现的第一种语言。不仅如此,用于访问当今硬件的许多高级功能的库很可能是用 C 实现的。

Your typically general purpose higher level language has no easy way to access features of the hardware unless it uses some type of binding layer to call the libraries which are written in C.

除非它使用某种类型的绑定层来调用用 C 编写的库,否则您通常通用的高级语言没有简单的方法来访问硬件的功能。

For instance how do you write code to access a GPU, or write a custom Shader, or write code that run well on a Cell chip, or run on an Iphone, or on a Blackberry in a high level language. Even when these things are supported, they come out well after other people are able to write games in C that use these features in games.

例如,您如何编写代码来访问 GPU,或编写自定义着色器,或编写在 Cell 芯片上运行良好的代码,或在 Iphone 或 Blackberry 上以高级语言运行的代码。即使这些东西得到支持,在其他人能够用 C 编写在游戏中使用这些功能的游戏之后,它们也会很好地出现。

One compromise you can make is to use a higher level language like Java for most things and C where its needed. You will limit the types of platforms you support though.

您可以做出的一个妥协是在大多数情况下使用更高级的语言,如 Java,在需要时使用 C。不过,您将限制您支持的平台类型。

Java might also be good for client/server games where the server is written in Java.

Java 也可能适用于服务器用 Java 编写的客户端/服务器游戏。

回答by Stephen

Inertia mainly... Although it's a bit old now, you can look at Jake2(which is a pure Java port of Quake 2 with jogl as the openGL lib).

惯性主要...虽然现在有点老了,你可以看看Jake2(它是Quake 2的纯Java端口,以jogl作为openGL lib)。

It can perform up to 85% as fast as the C++ original, which means it's fine for most games; especially modern ones which are more social and game play based rather than the limited "hard core" games.

它的执行速度最高可达 C++ 原始版本的 85%,这意味着它适用于大多数游戏;尤其是现代游戏,它们更多地基于社交和游戏玩法,而不是有限的“硬核”游戏。

I'd also suggest that most of the answers you get here are coming from a gaming geek/"I want the coolest hardware and games". For [these] hard core 3D games and gamers, that final 15% is hugely important, as that's what separates the $150 graphics card from the $500 one they just bought.

我还建议您在这里得到的大多数答案都来自游戏极客/“我想要最酷的硬件和游戏”。对于 [这些] 硬核 3D 游戏和游戏玩家来说,最后的 15% 非常重要,因为这将 150 美元的显卡与他们刚购买的 500 美元的显卡区分开来。

As John Carmack is reported to have said (something along the lines of): "If I were to enter games programming now, I'd program for the iPhone". e.g. it's not nearly as much fun to make the fastest game with the best 3d engine as it is to make the best game.

据报道,约翰·卡马克 (John Carmack) 曾说过(大致如此):“如果我现在进入游戏编程,我会为 iPhone 编程”。例如,用最好的 3d 引擎制作最快的游戏并不像制作最好的游戏那么有趣。