为什么只有少数视频游戏是用 Java 编写的?

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

Why are only a few video games written in Java?

java

提问by Sasha Chedygov

Why aren't many commercial, 3D video games (not random open source 2D ones) written in Java? In theory, it makes a lot of sense: you get a productivity boost and a cross-platform application almost for free, among other things, such as the vast amount of Java libraries, and built-in garbage collection (although I admit I'm not sure if the latter is a good thing). So why is it rarely used? I can only think of a couple popular commercial games written for the Java platform.

为什么没有很多商业 3D 视频游戏(不是随机的开源 2D 游戏)用 Java 编写?从理论上讲,这很有意义:您几乎可以免费获得生产力提升和跨平台应用程序,其中包括大量 Java 库和内置垃圾收集(尽管我承认我我不确定后者是否是一件好事)。那么为什么很少使用呢?我只能想到几个为 Java 平台编写的流行商业游戏。

Is it because of performance? If so, wouldn't most of the heavy lifting be done by the GPU anyway?

是因为性能吗?如果是这样,大部分繁重的工作不是由 GPU 完成的吗?

采纳答案by Michael Madsen

The game development world is a funny one: On one hand, they're often quick to accept new ideas, on the other hand, they're still in the stone age.

游戏开发世界是一个有趣的世界:一方面,他们通常会很快接受新想法,另一方面,他们仍处于石器时代。

The truth is, there's rarely that much incentive in switching to .NET/Java/anything other than C/C++.

事实是,很少有人愿意转向 .NET/Java/除 C/C++ 之外的任何东西。

Most game companies license parts of the game engine from other companies. These parts are written in C++, and although you might have access to the source so you could port it, that takes a lot of effort (and of course, the license needs to allow it).

大多数游戏公司从其他公司许可部分游戏引擎。这些部分是用 C++ 编写的,尽管您可能可以访问源代码以便移植它,但这需要付出很多努力(当然,许可证需要允许这样做)。

Also, a lot of legacy code already exists in C++. If code from previous projects can be reused (say, if you're writing a sequel), that counts even more in favor of sticking with the same language, instead of rewriting it in a new language (more so since you'll likely reintroduce a ton of bugs which you'll need to spend time ironing out.

此外,C++ 中已经存在许多遗留代码。如果可以重用以前项目的代码(例如,如果您正在编写续集),那么坚持使用相同的语言,而不是用新语言重写它更重要(更重要的是,因为您可能会重新引入您需要花时间解决大量错误。

Finally, it's rare for games to be written in 100% C++ anyway - a lot is done using scripting languages, whether they're custom or just integrating an existing languages (Lua being one of the more popular ones these days).

最后,无论如何,游戏很少使用 100% C++ 编写 - 很多都是使用脚本语言完成的,无论它们是自定义的还是只是集成了现有语言(Lua 是当今最流行的语言之一)。

As far as garbage collection is concerned, that can be a bit of a problem. The problem is not so much that it exists, it's more how it works - the garbage collector MUST be non-blocking (or at least be guaranteed to only block very briefly), since it's simply unacceptable to have the game freeze for 10 seconds while it scans all the allocated memory to see what can be freed. I know Java tends to choke quite a bit in GC'ing when it's close to running out of memory (and for some games out there, it will).

就垃圾收集而言,这可能有点问题。问题不在于它的存在,而在于它是如何工作的——垃圾收集器必须是非阻塞的(或者至少保证只阻塞非常短的时间),因为让游戏冻结 10 秒是不可接受的它扫描所有分配的内存以查看可以释放的内存。我知道当 Java 接近耗尽内存时,它往往会在 GC 中窒息很多(对于一些游戏来说,它会)。

You're also a bit more restricted in what you can do: you can't fully exploit the hardware due to the overhead of the runtime. Imagine Crysis being written in Java... even if that's the only visible difference, it just wouldn't be the same (I'm also pretty sure you'd need a Core i7 to run it.).

您可以执行的操作也受到更多限制:由于运行时的开销,您无法完全利用硬件。想象一下 Crysis 是用 Java 编写的……即使这是唯一可见的区别,它也不会相同(我也很确定你需要一个 Core i7 来运行它。)。

This doesn't mean these languages don't have their place in game development - and no, I'm not just referring to tool programming. For most games, you don't need that extra bit of performance you get from C++, including 3D games, and if you're writing it all from scratch, it can make perfect sense to use something like XNA - in fact, there's a good chance it will.

这并不意味着这些语言在游戏开发中没有自己的位置 - 不,我不只是指工具编程。对于大多数游戏,您不需要从 C++ 获得的额外性能,包括 3D 游戏,如果您是从头开始编写的,那么使用 XNA 之类的东西是非常有意义的 - 事实上,有一个很好的机会。

As far as commercial games are concerned - does RuneScapecount? That may well be the most succesful Java game out there.

就商业游戏而言 - RuneScape算不算?这很可能是最成功的 Java 游戏。

回答by Joel Martinez

I think .NET had (has) a lot of the same perceived issues that Java has. Microsoft has just done a better job at marketing to developers with XNA :-)

我认为 .NET 有(有)很多与 Java 相同的感知问题。微软刚刚在使用 XNA 向开发人员营销方面做得更好:-)

回答by Welbog

For one thing, Java's lack of operator overloading makes all of the math you have to deal with to get a working graphics pipeline very, very annoying and hard to read.

一方面,Java 缺少运算符重载,这使得您必须处理所有数学运算才能获得有效的图形管道,这非常非常烦人且难以阅读。

All of the matrix multiplication and affine vectors you need to deal with are a lot easier to follow if they're in well-formed mathematical expressions rather than object-oriented expressions like

如果您需要处理的所有矩阵乘法和仿射向量都使用格式良好的数学表达式而不是面向对象的表达式,那么它们将更容易理解

product = vector.multiply(projectionMatrix).dotProduct(otherVector);

That's just terrible. Math shouldn't look like that.

那太可怕了。数学不应该是那样的。

回答by Justin Niessner

.NET definitely has some of the same issues that Java has when it comes to intense 3D performance. Microsoft has also invested a lot more time and money in the development of the libraries when it comes to working with 3D heavy operations.

在激烈的 3D 性能方面,.NET 肯定存在一些与 Java 相同的问题。在处理 3D 繁重操作时,Microsoft 还在库的开发上投入了大量时间和金钱。

(...personally, I also think they had a leg up when it comes to the magic between DirectX and .NET)

(……就我个人而言,我也认为他们在 DirectX 和 .NET 之间的魔力方面有优势)

回答by cgp

  • Are there any good ports of gaming engines/libraries?
  • Many C/C++ developers, particularly the ones on Windows (where most commercial games are written) are familiar with Visual Studio. There is no comparison in IDEs.
  • In general, Java has been sold to businesses because of it's solid typing and it has a perception of not having memory management issues.
  • And yes, Java still suffers from a perception that it is slow, and it's memory management is poor, and for games, it probably is ill-suited to the task. As stated in some of the other answers, garbage collection just isn't going to cut it when you are dealing with real-time high-performance requirements. Video games push CPUs and GPUs to their limits.
  • 有没有好的游戏引擎/库端口?
  • 许多 C/C++ 开发人员,尤其是 Windows(编写大多数商业游戏的地方)上的开发人员,熟悉 Visual Studio。IDE 中没有可比性。
  • 一般而言,Java 已被出售给企业,因为它具有可靠的类型并且不存在内存管理问题。
  • 是的,Java 仍然被认为它很慢,它的内存管理很差,对于游戏,它可能不适合这项任务。正如其他一些答案中所述,当您处理实时高性能要求时,垃圾收集不会减少它。视频游戏将 CPU 和 GPU 推向了极限。

回答by lumpynose

I'd guess that speed is still the issue. Cross platform is going to be an issue isn't it since you don't know what 3d card is available when you write the code? Does java have anything to support auto discovery of 3d capabilities? And I'd guess that there are tools to ease porting a game between the wii, xbox, and ps3, but expensive I'll bet.

我猜速度仍然是问题。跨平台将是一个问题,是不是因为您在编写代码时不知道什么 3d 卡可用?java 有什么东西可以支持自动发现 3d 功能吗?而且我猜想有一些工具可以简化在 wii、xbox 和 ps3 之间移植游戏的过程,但我敢打赌,价格昂贵。

The ps3 has java, via the blue ray support. Check the bd-j site.

ps3 有 java,通过蓝光支持。检查 bd-j 站点。

回答by San Jacinto

A large reason is that video games require direct knowledge of the hardware underneath, often times, and there really is no great implementation for many architectures. It's the knowledge of the underlying hardware architecture that allows developers to squeeze every ounce of performance out of a gaming system. Why would you take the time to port Java to a gaming platform, and then write a game on top of that port when you could just write the game?

一个很大的原因是视频游戏需要直接了解底层硬件,很多时候,对于许多架构来说确实没有很好的实现。正是底层硬件架构的知识使开发人员能够从游戏系统中榨取每一盎司的性能。为什么要花时间将 Java 移植到游戏平台,然后在可以编写游戏的情况下在该移植上编写游戏?

edit: this is to say that it's more than a "speed" or "don't have the right libraries" issue. Those two things go hand-in-hand with this, but it's more a matter of "how do I make a system like the cell b.e. run my java code? there aren't really any good java compilers that can manage the pipelines and vectors like i need.."

编辑:这意味着它不仅仅是“速度”或“没有正确的库”问题。这两件事与此密切相关,但更多的是“我如何使像单元这样的系统运行我的 Java 代码?实际上没有任何好的 Java 编译器可以管理管道和向量就像我需要的那样……”

回答by Ksempac

Performance issue is the first reason. When you see the kind of hyper optimized C++ code that are in the Quake engines ( http://www.codemaestro.com/reviews/9), you know they're not gonna waste their time with a virtual machine.

性能问题是第一个原因。当您看到 Quake 引擎 ( http://www.codemaestro.com/reviews/9) 中的那种超优化的 C++ 代码时,您知道他们不会在虚拟机上浪费时间。

Sure there may be some .NET games (which ones ? I'm interested. Are there some really CPU/GPU-intensive ones ?), but I guess it's more because lot of people are experts in MS technologies and followed Microsoft when they launched their new technology.

当然可能有一些 .NET 游戏(哪些?我感兴趣。有没有一些真正的 CPU/GPU 密集型游戏?),但我想更多是因为很多人是 MS 技术的专家,并且在微软推出时就跟随微软他们的新技术。

Oh and cross-platform just isn't in the mind of video games companies. Linux is just around 1% of market, Mac OS a few % more. They definitely think it's not worth dumping Windows-only technologies and librairies such as DirectX.

哦,跨平台并不是视频游戏公司的想法。Linux 仅占市场的 1% 左右,Mac OS 多出几个百分点。他们绝对认为放弃 Windows 独有的技术和库(例如 DirectX)是不值得的。

回答by Chris Dail

One of the biggest reasons Java and other Virtual Machine languages are not used for games is due to Garbage Collection. The same thing goes for .NET. Garbage collection has come a long ways and works great in most types of applications. In order to do garbage collection though, you do need to pause and interrupt the application to collect the trash. This can cause periodic lag when collection happens.

Java 和其他虚拟机语言不用于游戏的最大原因之一是垃圾收集。.NET 也是如此。垃圾收集已经走过了漫长的道路,并且在大多数类型的应用程序中都表现出色。但是,为了进行垃圾收集,您确实需要暂停和中断应用程序来收集垃圾。这可能会导致收集发生时出现周期性滞后。

Java has the same problem for realtime applications. When tasks must run at a specific time, it is hard to have an automated task such as garbage collection respect that.

Java 对实时应用程序也有同样的问题。当任务必须在特定时间运行时,很难让垃圾收集等自动化任务尊重这一点。

It is not that Java is slow. It is that Java is not good at handling realtime tasks.

并不是说 Java 慢。Java 不擅长处理实时任务。

回答by John Simon

I'm playing the Sims 3, and I did some poking around. The graphics engine is C++, while the scripting and behavior engine is C#/Mono. So while C++ is there for time critical bits, other stuff like .interaction, game logic, AI is in an object oriented managed language.

我正在玩《模拟人生 3》,并四处闲逛。图形引擎是 C++,而脚本和行为引擎是 C#/Mono。因此,虽然 C++ 用于时间关键位,但其他东西如 .interaction、游戏逻辑、AI 是一种面向对象的托管语言。

回答by Markus Lux

You can ask why web applications aren't written in C or C++, too. The power of Java lies in its network stack and object oriented design. Of course C and C++ have that, too. But on a lower abstraction. Thats nothing negative, but you don't want to reinvent the wheel every time, do you?

您也可以问为什么 Web 应用程序不是用 C 或 C++ 编写的。Java 的强大之处在于其网络堆栈和面向对象的设计。当然,C 和 C++ 也有。但是在较低的抽象上。这没什么负面影响,但是您不想每次都重新发明轮子,是吗?

Java also has no direct hardware access, which means you are stuck with the API of any frameworks.

Java 也没有直接的硬件访问权限,这意味着您必须使用任何框架的 API。