How I made a custom game engine for Satelital
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Seven years ago I decided to start learning how to make a game from scratch. And Last week I released Satelital, a rule-discovery game about learning alien logic. As a solo developer, I decided to start this project from scratch. My motivation was to learn the actual fundamentals behind the technology of videogames. This meant I had to build most of the tools I would need myself, including the game engine, renderer, puzzles, editors, UX, design, art and music. Now that I’ve finally managed to release the game, I think it’s a good moment to share how I built it.
七年前,我决定从零开始学习如何制作游戏。上周,我发布了《Satelital》,这是一款关于学习外星逻辑的规则探索游戏。作为一名独立开发者,我决定从头开始这个项目。我的动力是学习电子游戏技术背后的实际基础知识。这意味着我必须自己构建所需的大部分工具,包括游戏引擎、渲染器、谜题、编辑器、用户体验、设计、美术和音乐。既然我已经成功发布了这款游戏,我认为现在是分享我是如何构建它的好时机。
The main reason I got into programming was the idea of eventually working on games. I spent many years working on web and building mobile apps. It was fun, but I felt like I wasn’t going in the direction I really wanted to. So one day, after listening to every Jonathan Blow talk I could find and playing too much The Witness, I decided to finally give it a try. I spent many days and countless hours looking for the best tool I could use to make a game, but nothing left me satisfied. The closest I got to having fun with gamedev was with the LÖVE framework making a few jam games. But even with that, it felt quite limiting to not really know what was happening behind the graphics I somehow managed to animate on my screen. Early prototype of a tile-based level editor built with LÖVE and Lua.
我进入编程领域的主要原因是为了最终能从事游戏开发工作。我花了很多年时间从事网页开发和构建移动应用程序。这很有趣,但我感觉自己并没有朝着我真正想要的方向前进。所以有一天,在听遍了所有我能找到的 Jonathan Blow 的演讲并玩了太多的《见证者》(The Witness)之后,我决定最终尝试一下。我花了很多天和无数个小时寻找制作游戏的最佳工具,但没有任何东西让我满意。我在游戏开发中获得的最接近乐趣的体验是使用 LÖVE 框架制作了几款游戏果酱(jam games)作品。但即便如此,对于屏幕上那些我设法制作出动画的图形背后到底发生了什么一无所知,这让我感到非常受限。这是用 LÖVE 和 Lua 构建的基于瓦片的关卡编辑器的早期原型。
I remember hearing about shaders and parallel programming. The concept always felt very arcane and mysterious to me, but now writing them is the thing I enjoy the most while coding. I finally decided to jump into the swimming pool and actually learn C++. I had to learn how to manage memory and how not to shoot myself in the foot with the wrong tools. I started using SDL2 while trying to use the C++ standard library as little as possible. Before getting too deep into the engine, I actually finished a prototype of the puzzles not long after learning how to draw some simple graphics with C++. It had very early versions of four kinds of puzzles that mostly survived until the final game. It was made entirely with SDL2, but I soon realised that if I wanted more interesting graphics, I would need to use shaders and decided to switch to OpenGL. Early prototype gameplay with SDL2. This puzzle was the first one I made, also the only one that survived until the end.
我记得曾听说过着色器(shaders)和并行编程。这个概念对我来说一直非常深奥和神秘,但现在编写它们是我在编码时最享受的事情。我最终决定跳入深水区,真正学习 C++。我必须学习如何管理内存,以及如何避免因使用错误的工具而搬起石头砸自己的脚。我开始使用 SDL2,同时尽量少用 C++ 标准库。在深入研究引擎之前,我在学会用 C++ 绘制一些简单图形后不久,就完成了谜题的原型。它包含了四种谜题的早期版本,这些版本大部分保留到了最终游戏中。它完全是用 SDL2 制作的,但我很快意识到,如果想要更有趣的图形,我就需要使用着色器,于是我决定转向 OpenGL。这是使用 SDL2 的早期游戏原型。这个谜题是我制作的第一个,也是唯一一个保留到最后的谜题。
With the help of LearnOpenGL I quickly had a triangle on the screen. From there, learning things like lighting, tonemapping, normal maps, shader effects and everything else opened up a whole new way of looking at computers for me. Before, my idea of how a computer actually draws to the screen was quite naive. When I started learning graphics I just wanted to be able to manually change the color of each pixel individually, but in the end the idea of running a program in parallel for every pixel clicked for me. One thing that made a huge difference while working on shaders was having code reloading. Before that, I had a complex system that stored constant values in config files, which would later get passed through uniforms into shaders. Then a custom development menu would provide some UI to tweak those values at runtime. With that I stopped having to reload and compile everything for every little change I wanted to make. It worked, but having to maintain all this extra scaffolding was slowing me down.
在 LearnOpenGL 的帮助下,我很快就在屏幕上显示出了一个三角形。从那里开始,学习光照、色调映射、法线贴图、着色器效果以及其他所有东西,为我打开了一扇看待计算机的全新大门。在此之前,我对计算机如何实际在屏幕上绘图的想法非常天真。当我开始学习图形学时,我只是想能够手动改变每个像素的颜色,但最终,为每个像素并行运行程序的想法让我豁然开朗。在处理着色器时,代码重载(code reloading)带来了巨大的改变。在此之前,我有一个复杂的系统,将常量值存储在配置文件中,然后通过统一变量(uniforms)传递到着色器中。然后,一个自定义的开发菜单会提供一些用户界面,以便在运行时调整这些值。有了这个,我就不必为我想做的每一个小改动而重新加载和编译所有内容了。它确实有效,但维护所有这些额外的脚手架拖慢了我的速度。
Another unexpected downside of this setup was that, in some cases, the shader compiler couldn’t optimize the code as well as it otherwise could, since knowing constant values ahead of time helps it make better decisions. Having shader reloading made a huge difference. Being able to change some code, save the file and immediately see the result on screen was very useful. Iterating on shaders became much faster and more fun. This was probably my biggest regret about not having it from day one. Color modification preview using shader hot reloading.
这种设置的另一个意想不到的缺点是,在某些情况下,着色器编译器无法像预期的那样优化代码,因为提前知道常量值有助于它做出更好的决策。拥有着色器重载功能带来了巨大的不同。能够更改一些代码、保存文件并立即在屏幕上看到结果非常有用。对着色器进行迭代变得更快、更有趣了。这可能是我最大的遗憾,后悔没有从第一天起就拥有它。这是使用着色器热重载进行的颜色修改预览。
For a big part of the development I was using SDL2, mainly to create a window, read input and get a surface to draw on. But it felt wrong to depend on a library that was several times bigger than my entire game just to handle a few seemingly simple tasks. I decided to drop that dependency and make my own platform layer that I could understand and maintain. I wouldn’t have to wait for someone else to fix a bug anymore because now I could just change it myself. It wasn’t that hard to end up with a single file with a few thousand lines of code that supported both macOS and Windows. I’m now looking forward to working on mobile and console ports, it feels like it might be a fun thing to work on.
在开发的大部分时间里,我都在使用 SDL2,主要是为了创建窗口、读取输入并获取绘图表面。但仅仅为了处理几个看似简单的任务而依赖一个比我整个游戏大几倍的库,感觉是不对的。我决定放弃这个依赖,制作我自己的平台层,这样我就可以理解并维护它。我再也不用等待别人来修复错误了,因为现在我可以自己修改它。最终得到一个支持 macOS 和 Windows 的几千行代码的单个文件并不难。我现在很期待进行移动端和主机端的移植工作,感觉这可能会是一件有趣的事情。
At some point during development, compile times were getting longer, so I decided to look into what was going on. I checked where all that time was going, and to my surprise a lot of it was spent compiling standard library things. Stuff like std::function was particularly slow for example. Eventually I realised that I wasn’t actually forced to use the standard library just because it was there, and the subset I needed wasn’t that hard to implement myself. It took me a month to replace it with my own base library, but it was so worth it. Not only did compile times get faster, but I also learned how a lot of things I had always taken for granted in other languages actually worked.
在开发过程中的某个阶段,编译时间变得越来越长,所以我决定调查一下原因。我检查了所有时间都花在了哪里,令我惊讶的是,大部分时间都花在了编译标准库的内容上。例如,像 std::function 这样的东西特别慢。最终我意识到,我并不一定要因为标准库存在就必须使用它,而且我所需要的子集自己实现起来并不难。我花了一个月的时间用我自己的基础库替换了它,但这非常值得。不仅编译时间变快了,我还了解了许多我在其他语言中一直认为理所当然的事情实际上是如何工作的。
At some point I started looking into Vulkan because I wanted HDR. I couldn’t get it working on macOS through OpenGL and I was worried about OpenGL being deprecated in the future. Also I didn’t really want to build the whole engine around an API that might become deprecated. Testing alpha blending with my first vulkan triangles. Moving to Vulkan was rough at the beginning. There was so much boilerplate to type and understand. Also, when I started working on the engine, there wasn’t much documentation around how to implement a good backend with it. In hindsight, I feel that even though I got a lot of valuable things from tutorials and books, most of them were overcomplicating something that could be.
在某个阶段,我开始研究 Vulkan,因为我想要 HDR。我无法通过 OpenGL 在 macOS 上实现它,而且我担心 OpenGL 未来会被弃用。此外,我也不想围绕一个可能会被弃用的 API 来构建整个引擎。这是在测试我第一个 Vulkan 三角形的 alpha 混合。刚开始转向 Vulkan 时非常艰难。有太多的样板代码需要输入和理解。而且,当我开始开发引擎时,关于如何用它实现一个好的后端,并没有太多的文档。回想起来,我觉得尽管我从教程和书籍中学到了很多有价值的东西,但它们中的大多数都把一些本可以很简单的事情复杂化了。