ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
Loading...
Searching...
No Matches
Entity.hpp
Go to the documentation of this file.
1
11# pragma once
12
13#include <cstdint> // for uint32_t
14#include <cassert> // for error notification in make_entity
15
16namespace Imago::ECS
17{
18
20 using Entity = uint32_t;
21
22 // NOTE: for these variables, constexpr means that the variable is locked to its set value at compile time
23 // and that value is baked directly into the generated code everywhere it is used, meaning these values cannot be reassigned at any point
24
26 constexpr uint32_t ENTITY_INDEX_BITS = 20; // low 20 bits represent the Entity's index
28 constexpr uint32_t ENTITY_GENERATION_BITS = 12; // top 12 bits represent the Entity's generation (how many times it has been destroyed and recreated)
29
31 constexpr uint32_t ENTITY_INDEX_MASK = (1u << ENTITY_INDEX_BITS) - 1;
33 constexpr uint32_t ENTITY_GENERATION_MASK = (1u << ENTITY_GENERATION_BITS) -1;
34
41 constexpr Entity NULL_ENTITY = 0xFFFFFFFFu;
42
43 // NOTE: for these free functions, constexpr means that if every argument is known at compile time the compiler can simply
44 // run this function during compilation and bake in the result, meaning the value cannot be reassigned, and if every argument
45 // is not known at compile time it simply treats it as a runtime function call
46
52 constexpr uint32_t get_entity_index(Entity entity)
53 {
54 return entity & ENTITY_INDEX_MASK;
55 }
56
57
63 constexpr uint32_t get_entity_generation(Entity entity)
64 {
65 return (entity >> ENTITY_INDEX_BITS) & ENTITY_GENERATION_MASK;
66 }
67
77 constexpr Entity make_entity(uint32_t index, uint32_t generation)
78 {
79 // ensure that the Entity index and generation do not exceed their respective bit ranges
80 assert(index <= ENTITY_INDEX_MASK && "[ENTITY] Entity index exceeds 20-bit range");
81 assert(generation <= ENTITY_GENERATION_MASK && "[ENTITY] Entity generation exceeds 12-bit range");
82
83 return ((generation & ENTITY_GENERATION_MASK) << ENTITY_INDEX_BITS) | (index & ENTITY_INDEX_MASK);
84 }
85
86}
uint32_t Entity
A packed identifier: 20 bits of index, 12 bits of generation.
Definition Entity.hpp:20
constexpr Entity make_entity(uint32_t index, uint32_t generation)
Packs an index and generation into a single Entity.
Definition Entity.hpp:77
constexpr uint32_t ENTITY_INDEX_MASK
Mask isolating the low 20 index bits of a packed Entity.
Definition Entity.hpp:31
constexpr uint32_t ENTITY_INDEX_BITS
Number of bits allocated to an Entity's index.
Definition Entity.hpp:26
constexpr Entity NULL_ENTITY
Sentinel value representing "no entity".
Definition Entity.hpp:41
constexpr uint32_t ENTITY_GENERATION_MASK
Mask isolating the low 12 bits of a right shifted generation value.
Definition Entity.hpp:33
constexpr uint32_t ENTITY_GENERATION_BITS
Number of bits allocated to an Entity's generation.
Definition Entity.hpp:28
constexpr uint32_t get_entity_generation(Entity entity)
Extracts the generation bits from a packed Entity.
Definition Entity.hpp:63
constexpr uint32_t get_entity_index(Entity entity)
Extracts the index bits from a packed Entity.
Definition Entity.hpp:52