TExcelTAD
Home › Game Engine Architecture in C++ › Field Notes: Architecture

Field Notes: Architecture

The lessons so far show how engine pieces work. These notes are about how engine projects go wrong. None of them is a law, but each one is a mistake that has cost real teams months.

1. Don't write an engine to make a game

If the goal is a game, use Unity, Unreal, Godot or a framework. A custom engine pays off when the game needs something the general engines handle badly (huge simulations, unusual rendering, a voxel world, tight determinism for lockstep multiplayer), when the engine is the product, or when learning engine programming is the point, as in this course. Even then, write the engine for one game. An engine without a game grows features nobody uses and never finishes the ones a game needs: save files, controller remapping, localisation, a pause menu that works.

A good test: can you name the game, and is there a playable build of it every week?

2. Use existing libraries, write the glue

SDL3 or GLFW for windows and input, a graphics API wrapper or bgfx/sokol, Jolt or Box2D, miniaudio, Dear ImGui, EnTT or flecs if you want them, stb_image, cgltf or fastgltf for models, Tracy. Each one is years of someone else's platform bugs already fixed. The interesting engine work is in how they are connected: the frame loop, the data model, the asset pipeline and the tools.

3. Keep the editor and the runtime separate

The shipped game should not contain the editor, and the editor should not be a mode the game can fall into.

  • Build the engine as a library; the game, the editor and the asset tools are separate executables linking it (Toolkit: CMake).
  • Editor-only data (gizmo state, undo history, selection, source file paths) lives in editor-side structures, never in runtime components.
  • The editor writes the same files the runtime loads, ideally through the same serialization code, and "play in editor" runs the runtime in a separate world that is thrown away on stop.

Teams that mix the two end up with shipping builds that try to open D:\art\source\… or crash because an editor singleton wasn't created.

4. Make simulation deterministic and record inputs

If the game state is a pure function of the starting state and the input stream, any bug can be replayed exactly: record the inputs on a tester's machine, replay them at your desk under the debugger. It also gives you lockstep multiplayer, killcams and automated regression tests. The requirements are a fixed timestep, seeded random number generators owned by the simulation, no reading the wall clock inside gameplay, stable iteration order, and care with floating point across compilers.

cpp · try it yourself
#include <cstdint>
#include <iostream>
#include <vector>

struct Input { std::uint32_t frame; std::int8_t moveX; bool fire; };

struct Sim {
    std::uint64_t rng = 12345;                  // seeded, owned by the simulation
    std::int32_t playerX = 0, ammo = 10, score = 0;
    std::uint32_t nextRng() { rng = rng * 6364136223846793005ull + 1442695040888963407ull; return std::uint32_t(rng >> 33); }
    void step(const Input& in) {
        playerX += in.moveX;
        if (in.fire && ammo > 0) {
            ammo--;
            if (nextRng() % 100 < 60) score += 10;   // 60% hit chance
        }
    }
    std::uint64_t checksum() const {             // compare states cheaply
        std::uint64_t h = 1469598103934665603ull;
        for (std::uint64_t v : {std::uint64_t(playerX), std::uint64_t(ammo), std::uint64_t(score), rng}) { h ^= v; h *= 1099511628211ull; }
        return h;
    }
};

std::uint64_t run(const std::vector<Input>& inputs, bool print) {
    Sim sim;
    for (const Input& in : inputs) sim.step(in);
    if (print) std::cout << "x " << sim.playerX << ", ammo " << sim.ammo << ", score " << sim.score << "\n";
    return sim.checksum();
}

int main() {
    std::vector<Input> recording;
    for (std::uint32_t f = 0; f < 120; f++)
        recording.push_back({f, std::int8_t(f % 40 < 20 ? 1 : -1), f % 15 == 0});

    std::uint64_t live = run(recording, true);
    std::uint64_t replay = run(recording, true);
    std::cout << "live and replay match: " << (live == replay ? "yes" : "no") << "\n";

    recording[30].fire = false;                  // one changed input
    std::uint64_t changed = run(recording, true);
    std::cout << "with one input changed: " << (changed == live ? "same" : "diverged") << "\n";
}
x 0, ammo 2, score 30
x 0, ammo 2, score 30
live and replay match: yes
x 0, ammo 3, score 30
with one input changed: diverged

Without the shot in frame 30, every later hit roll uses a different random number; the score happens to come out at 30 again, yet the checksum still catches the divergence, because the ammo count and the generator state differ. Log a checksum every N frames in both runs; the first frame where they differ is where to start debugging a desync.

5. Prefer composition and data over deep inheritance

GameObject → Actor → Pawn → Character → Enemy → FlyingEnemy → FlyingBossEnemy breaks the day you need a flying turret. Components (with or without a full ECS) let behaviour be combined in data. Keep inheritance for genuine interfaces (a renderer backend, a file system), one level deep.

6. Keep the platform layer thin

Wrap the OS where it differs (window, input, files, threads, time) and stop there. A Platform::Window that exposes width, height, events and a native handle is enough. Designing abstractions for consoles you don't have dev kits for produces interfaces that are wrong when you finally get the hardware.

7. Process assets offline

Runtime code should load cooked data: textures already compressed to GPU formats (BCn, ASTC), meshes already in your vertex layout with indices optimised (meshoptimizer), scenes already in a binary format. Importers (FBX, glTF, PNG, WAV) belong in the tools. Loading becomes a memory copy, and a bad asset fails in the build pipeline, not on a player's machine.

8. Build hot reload and debug drawing early

Hot reload of shaders, textures, tuning data and scripts (Hot Reload) and an immediate debug-draw API (debugLine(a, b, color, seconds), debugSphere, debugText) multiply everyone's speed. Added late, they fight against assumptions baked into every system.

9. Version every file format

Put a magic number and a version in every saved and cooked file (Binary Serialization). Save games outlive builds; without a version you cannot write the converter you will need.

10. Ship crash reporting before you ship anything

Minidumps plus a crash reporter (Crashpad, Sentry, Backtrace, or the platform's own) and archived symbol files for every build. A crash you can see the call stack of is a bug ticket; a crash you can't is a one-star review.

11. Avoid hidden global state

Singletons (Renderer::Get()) make the order of initialisation implicit and testing hard. Pass the systems a module needs explicitly, or keep one Engine struct that owns the subsystems and is created and destroyed in a known order (Engine Layers and Subsystems).

Test yourself

Which requirement is NOT needed for replaying a recorded input stream exactly?

  1. A fixed simulation timestep
  2. Random numbers from a seeded generator owned by the simulation
  3. Reading the wall clock inside gameplay code
  4. A stable order of updating entities

Why should importers for FBX, PNG and WAV live in the tools rather than the shipped runtime?

  1. They are too small
  2. Runtime loading of pre-cooked data is faster and simpler, and broken assets fail at build time instead of on players' machines
  3. Players don't have those files
  4. The runtime cannot use C++

What is the main danger of an engine built without a specific game?

  1. It compiles slowly
  2. It accumulates general features nobody needs while the features a real game needs stay unfinished
  3. It can't use libraries
  4. It must use Vulkan