Burntbite Engine
Personal Project
A lightweight Entity Component System (ECS) game engine built from scratch in C++, focused on decoupling state from behavior and supporting data-driven entity instantiation.
Type-Indexed Storage
ECS Core · C++To create a truly decoupled ECS, the core registry must be able to hold any Plain Old Data (POD) struct without requiring hardcoded lists. I implemented a polymorphic hash-map storage system driven by runtime type information.
- Type Erasure Pattern: The core
ECSclass mapsstd::type_indexkeys to an abstractIStorageinterface, allowing new component types to be registered dynamically at runtime. - Direct Mapping: Under the hood,
StorageImpl<C>maintains anstd::unordered_map<Entity, C>, ensuring fast lookup times by mapping the unique entity ID directly to its component data.
struct IStorage {
virtual ~IStorage() = default;
virtual void EraseEntity(Entity e) = 0;
};
template<typename C>
struct StorageImpl : IStorage {
std::unordered_map<Entity, C> mMap;
void EraseEntity(Entity e) override { mMap.erase(e); }
};
Data-Driven Instantiation via XML
Serialization · PugiXMLHardcoding component values in C++ slows down iteration and requires recompiling for simple tweaks. To keep entity definitions flexible, I integrated PugiXML into an entity factory that constructs game objects directly from external XML files.
- Dynamic Parsing: The
EntityFactoryreads XML nodes such as<transform>or<velocity>and attaches the corresponding components to new entity IDs at runtime. - Safe Fallbacks: When optional XML attributes are omitted, the parser falls back to sensible default values, preventing malformed data files from breaking entity creation.
pugi::xml_node oTransformNode = oNode.child("transform");
if (oTransformNode) {
Transform oTransform;
oTransform.fPosX = oTransformNode.attribute("x").as_float(240.f);
oTransform.fPosY = oTransformNode.attribute("y").as_float(440.f);
_rEcs.AddComponent<Transform>(iEntity, oTransform);
}
Overcoming Global State: The Manager Entity
Game State · MemoryA persistent architectural challenge in pure ECS is tracking global state such as high scores or universal spawn timers, which do not logically belong to physical actors.
- Headless Architecture: The
GameStatecreates a "Manager Entity" during initialization. This is a headless entity ID that exclusively holdsSpawnTimerandScorecomponents. - Selective Board Cleanup: When a run ends,
ResetForNewGame()iterates through the component maps and erases every entity ID except the Manager Entity, clearing gameplay objects while preserving global session state.
auto& rTransforms = _rEcs.Storage<Transform>();
std::vector<Entity> lTransformsToRemove;
// Collect all physical entities except the global manager
for (auto& rKeyValue : rTransforms) {
if (rKeyValue.first != m_iManager) {
lTransformsToRemove.push_back(rKeyValue.first);
}
}
// Clear gameplay entities while preserving global state
for (Entity iEntity : lTransformsToRemove) {
rTransforms.erase(iEntity);
}
Polymorphic System Iteration
Main Loop · KinematicsGame logic in Burntbite is handled entirely by Systems. To keep the main loop in main() clean and extensible, all systems inherit from a shared ISystem interface.
- Component Filtering: Each system queries only the entities holding the specific components it requires, while the main loop invokes their
Update()methods polymorphically. - Tag Querying: The
MovementSystemdirectly applies kinematic calculations frame-by-frame, querying empty structs likeEnemyTagto selectively apply environmental forces like gravity.
void MovementSystem::Update(ECS& _rEcs, float _fDeltaTime) {
auto& rVelocities = _rEcs.Storage<Velocity>();
auto& rTransforms = _rEcs.Storage<Transform>();
for (auto& rKeyValue : rVelocities) {
Entity iEntity = rKeyValue.first;
auto oIterator = rTransforms.find(iEntity);
if (oIterator == rTransforms.end()) continue;
// Apply gravity for enemies via Tag querying
if (_rEcs.HasComponent<EnemyTag>(iEntity)) {
rKeyValue.second.fVelocityY += m_fGravity * _fDeltaTime;
}
oIterator->second.fPosX += rKeyValue.second.fVelocityX * _fDeltaTime;
oIterator->second.fPosY += rKeyValue.second.fVelocityY * _fDeltaTime;
}
}