Valve 等公司如何设法将游戏发布到所有三大游戏平台?我对特别是在 Windows、Xbox360 和 PS3 之间共享代码的最佳实践感兴趣,因为理想的解决方案是尽可能多地重用代码,而不是为每个平台重写整个代码。
7 回答
这与在其他上下文中编写独立于平台的代码没有任何不同。将特定于平台的细节(输入、窗口交互、主事件循环、线程等)隐藏在通用接口后面,并在您打算支持的所有平台上定期测试。
请注意,Cell 的线程模型非常不寻常,因此“一般”地进行线程处理需要一些小心。我不是 Valve 的员工,我也不知道他们的秘密,但据我了解,大多数想要以 PS3 为目标的游戏开发者都使用工作队列,各个单元处理器根据需要从其中获取任务。这不一定是使用 Cell 的最佳方式,但它可以很好地推广到更传统的线程模型(例如,frex,PC 和 360 都使用的模型)。
有很多关于这个主题的游戏开发者杂志文章和 GDC 演讲。事实上,既然你提到了 Valve,他们在 GDC08 上发表了一次演讲,描述了他们的方法。
这确实是一个巨大的话题,我可以(并且已经)谈论了几个小时,但电梯摘要是:
- 确定引擎的哪些部分是完全特定于平台的,并将它们放在抽象后面。例如,文件和资产加载需要为每个控制台重写;但是您可以将其隐藏在 IFileSystem 接口后面,该接口提供了游戏代码与之对话的统一 API。
- PS3 很难做到这一点,因为它的抽象点必须与其他平台完全不同。即使是碰撞和导航等游戏功能也必须为 Cell 编写不同的内容。
- 尽量保持叶子游戏代码(实体、AI、sim)与平台无关......
- 但是请接受即使是最复杂的游戏代码有时也需要一些特定于平台的#ifdefs,以用于性能、内存或 TCR 的原因。由于制造商的认证要求相互冲突,因此必须重写许多 UI。
- 任何说“我不担心表现”或“记忆不是问题”的人都不应该在工资单上。
This question can be divided up into two separate questions. "How can I write portable code?" and "What are the divergent requirements of mainstream gaming platforms?".
The first question is relatively easy to answer. Best practices for abstracting your non-portable code are covered in Write Portable Code: http://books.google.ca/books?id=4VOKcEAPPO0C&printsec=frontcover
Turning theory into practice, the Quake 3 source code does a pretty good job of dividing out different platforms into separate areas for a C codebase, available at http://www.idsoftware.com/business/techdownloads/ However, it does not demonstrate C++ patterns such as abstract interfaces, implemented once per platform.
The second part of your question, "What are the divergent requirements of mainstream gaming platforms?" is tougher. However, it is notable that your largest areas of change are still your renderer, your audio subsystem and your networking.
Each console platform has a series of certification requirements, available under an agreement with the respective console owners. The requirements drive consistency in user experience and are not focused on gameplay or qualitative, high level issues. For instance, your game may need to display a reasonably interesting animating loading screen, and black screens are unacceptable.
Getting your hands on this documentation as soon as possible is key to making the right choices in developing for a specific console platform.
Finally, if you can't get your hands on a console devkit, I suggest you port your code to the Mac from Windows. The Mac gets you an OS port ensuring you are not tied to Windows as well as a processor port if you support universal binaries. This ensures your code is endian agnostic.
If you support both PC and Mac, you will be well positioned to support a third platform, should you gain access to it in the future.
Addendum You wrote:
the ideal solution is to reuse as much code as possible instead of rewriting the whole thing for every platform
In many game porting scenarios, the ideal solution is not to reuse as much code as possible, but to write the optimal code for each platform. Code can be reused between projects and is relatively inexpensive as compared to the content that the engine takes in. A more reasonable goal is to aim for lowest common denominator content that runs on all platforms without modification (a build phase that packs the content for media is okay).
同步开发很棒。您会发现仅在一个平台上找不到的各种错误。
我记得 DOS 中的程序员一直都有空指针,因为写入低内存并不会立即使它们崩溃。当您移植到 Amiga、Atari ST 或 Macintosh 时,砰!我记得告诉一个 DOS 程序员,他在一个已发布的游戏上有几个空指针。他想了几秒,咧嘴一笑,“这说明了一些事情。”
既然游戏的预算如此之大,那么同时发布它们很重要,这样您就不会浪费营销和广告预算。
我对同步开发的建议是选择一个领先平台,但永远不要让其他平台落后一周以上。当您编写代码的哪些部分对所有平台通用以及哪些部分不同时,这一点会变得很明显。将差异提取到一个或多个特定于平台的区域。
我的经验是 C/C++。如果您必须移植不同的语言(例如,Java 和 Objective-c),这是一个更大的问题。
几年前,Opera CEO 在一次采访中表示,为独立平台开发的关键是摆脱任何单一的操作系统/平台库。他接着说,他们开发了自己的库来提高操作系统的性能。
我的假设是,大公司会有一个共同的、Xbox、PS、windows、FooOS、独立的团队。每个平台都需要进行不同的调整,需要不同的实现方法。我不认为他们为所有平台提供一个来源。相反,他们为每个操作系统构建一个,从而提高效率。我记得 EA 曾经发布过一些比 PC 版本更早的主机游戏,反之亦然。
另一个问题是不同的控制台具有不同的硬件,因此需要不同的编程技术。
有两个极端,构建一个适合所有人的源代码(例如 java),但您冒着效率低下或编写 40 个版本的风险;一个针对每个平台进行了优化
当我有一个朋友从事教育电脑游戏时(在 The Learning Company 淘汰该领域之前),他非常喜欢创建跨平台库来做所有事情。
这对于游戏来说比其他应用程序更容易。例如,如果您有一个要在 Mac 和 Windows 上运行的文字处理应用程序,它的外观和行为确实需要像 Mac 上的 Mac 应用程序和 Windows 上的 Windows 应用程序。编写一个游戏,它不必符合原生的行为、外观和感觉。
如果您想要开源示例,您可以查看 Quake 1、2 和 3 引擎的源代码。它们的结构非常便携。(当然,不支持 ps3 或 xbox360,但同样的原则适用)