← all work

$ 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 screenshots

The 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 APIs

Windows

VulkanDirectX

Native C++.

Android

VulkanOpenGL

Native C++ with a Java layer, connected through JNI.

iOS

Metal

Native C++ on Apple's own graphics API.

// how it works

architecture
game       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 went

Most 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.

How sprites are batched Many sprites and text glyphs, each with its own position and texture region, are written into one shared vertex buffer and drawn with a single draw call. sprites and glyphs this frame Abc1 each: position, size, texture region, colour one vertex buffer, filled on the CPU 4 vertices per quad the GPU few draw calls not one each glyphs come from a FreeType texture atlas, so text joins the same batch as sprites

// hard parts

what I'd talk about in an interview

Multi-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 Store

bug

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.

eaten 0 · best 0tap, click or press space
A sketch of how Bug played, rebuilt in the browser for this page. Not the original game or engine code.

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.

dodged 0 · best 0tap, click or press space
A sketch of how Watch out played, rebuilt in the browser for this page. Not the original game or engine code.

// 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.