← Back to blog
DevlogC++Game EngineProgramming

Midnight Engine 2D: learning C++ by building my own game engine

Entities, components, prefabs, render layers and a lot of late nights. How Midnight Engine 2D helped me understand what actually happens under the hood of a game.

Midnight Engine 2D logo.

Midnight Engine 2D began as a question I could not stop asking myself: what actually happens between pressing a key and moving a sprite?

Why did I even build an engine?

I could have spent that time developing another game. Instead I chose to develop the thing that runs the game in C++ as I wanted to know how the engine works under the hood. What is an entity? How does a component get stored? Who defines the order of drawing? What makes an editor feel like an editor?

The engine uses an Entity Component System architecture. Initially it was a project aimed at education, but it turned out to be a real piece of software. It has an editor, a prefab pipeline, an asset system, input bindings and a small example game used for testing.

The architecture partially resembles the teachings of Professor Dave Churchill from Memorial University of Newfoundland. I followed his materials to learn the basics, then extended the engine based on that. The name comes from the time I usually spent on working on it. Most of the work was done at night, so Midnight seemed appropriate.

Entities, components and deferred changes

An entity is any object in a scene. It holds components, but components are stored and managed separately. An entity can have only one component of a certain type; if you need two of the same type, you create a child entity.

One thing that became clear to me over time is that adds and destroys are deferred. An entity becomes part of the live lists only after an update by the entity manager, which happens once per frame. It may sound like an insignificant implementation detail, but it prevents a whole class of bugs: you can safely spawn or destroy things during an update without mutating the list you are iterating over.

Hierarchies work in the same way. Children can be added, removed and reordered, and the engine will prevent an entity from becoming a child of one of its descendants.

Making the engine usable

An engine is not just a runtime. It is also a set of tools. I added an ImGui-based inspector, and two small registries that make it easy to extend components:

  • a component loader registry, so a component can be declared in a prefab file and created.
  • a component drawer registry, which allows the same component to have its own inspector UI in the editor.

Prefabs are regular text files. They have support for components, external child prefabs and inline child entity definitions. Assets are registered using one config file with the following Type name path format: textures, fonts, sounds, animations and prefabs go there. Actions are input events, so the game code receives "Move_Up" and "Shoot" input events instead of reading raw keyboard state.

Those decisions made the engine feel less like a bunch of classes and more like a place where a game could be developed.

Rendering layers and hierarchy

The default renderer uses render layer and order to group and sort root entities. It draws lower layers first, children are drawn after their parents. This is a very simple system, and it can be changed by providing a custom rendering system to a scene.

What is next

I am working on the engine further, with a long list of planned improvements such as refactoring scene logic to use scene systems, sprite flipping and origin configuration, Y-sorting, tile painting from the editor, trigger colliders, automatic collision geometry generation for tilemaps and improved

The project is open source under the MIT license. If you want to see how it is put together, the code and the full README are on GitHub.

Midnight Engine 2D on GitHub

CONTACT ME

lucozzilavoro@gmail.com

+39 3450156916


2026 - all rights reserved

made with love by Jason