$ open ./work/cpp-engine
C++ engine
The 2D game engine I wrote in C++ in my last two years of high school. It drew through Vulkan, Metal, DirectX or OpenGL depending on the device, ran games written in Lua on an entity-component system, and shipped two games to Google Play and the App Store.
renderers
4
Vulkan · Metal · DirectX · OpenGL
platforms
Windows · Android · iOS
games shipped
2
Bug · Watch out
Google Play and App Store
when
last two years of high school
1 developer (me)
// a note first
why there are no screenshotsThe source code of the engine is lost, and so are the builds. This page describes how it worked from memory, with diagrams. The two games at the bottom are small sketches of how Bug and Watch out played, rebuilt in the browser for this page. They are not the engine or the original code.
// platforms and renderers
one engine, four graphics APIsWindows
Native C++.
Android
Native C++ with a Java layer, connected through JNI.
iOS
Native C++ on Apple's own graphics API.
// how it works
architecturegame Lua scripts │ ▼ core entity-component system custom memory manager · multi-threading │ ▼ renderer one API for the engine, batching geometry, sprites and text into shared buffers text: FreeType glyphs packed into a texture atlas │ ├─ Vulkan Windows, Android ├─ DirectX Windows ├─ OpenGL Android └─ Metal iOS │ ▼ platform Windows (native C++) · Android (C++ ⇄ Java through JNI) · iOS (native C++)
// the renderer
where most of the time wentMost of my time went into the renderer, and into making it fast. Drawing every sprite on its own would mean one draw call each, and on a phone that runs out quickly. So the renderer collected all the geometry and sprites of a frame into shared buffers and drew them together, a few draw calls instead of one per sprite.
Text went through the same path. Fonts were rendered with FreeType, the glyphs packed into a texture atlas, and every letter on screen became one more quad in the same batch as the sprites.
// hard parts
what I'd talk about in an interviewMulti-threading
The hardest problem in the engine. Running its work on several threads at once means every piece of shared data needs a clear owner, or two threads change it at the same time and the bug appears only now and then.
A custom memory manager
The other half of the hardest problem. The engine managed its own memory instead of leaving every allocation to the system, and with several threads using it at once. A mistake there shows up much later and somewhere else, which makes it slow to track down.
Four graphics APIs behind one
Vulkan, Metal, DirectX and OpenGL think about buffers, pipelines and frames very differently. The rest of the engine talked to one renderer interface, and each backend translated it for its own API.
Batching everything
Geometry, sprites and text all ended up in the same shared buffers, so a busy frame still cost only a few draw calls, which matters most on phones.
C++ on Android
The engine was C++, but Android apps start in Java. JNI connected the two, so the Java side and the C++ engine could call each other.
// the two games
both shipped on Google Play and the App Storebug
A one-tap game about a small worm. It crawls toward the bottom edge of the screen until you tap, then turns and crawls toward the top. Tap again and it turns back. In between you dodge obstacles and eat the food.
watch out
Things fall from the top of the screen, and you have to stay out of their way. You keep moving across the bottom, and a tap turns you from left to right and back. The longer you last, the faster they fall.
// stack
tools- language
- c++, lua (game scripts), java (android layer, through jni)
- graphics
- vulkan, metal, directx, opengl
- text
- freetype, texture atlases
- platforms
- windows, android, ios
Written alone in my last two years of high school. The source code is lost; everything here is from memory, and the diagrams and the game sketches were made for this page.