Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
entities_manager.cpp
Go to the documentation of this file.
2
4#include <helios/core_pch.hpp>
5
6#include <atomic>
7#include <cstddef>
8#include <cstdint>
9
10namespace helios::ecs::details {
11
13 const Entity::IndexType current_next_index = next_index_.load(std::memory_order_relaxed);
14
15 // Ensure generations array is large enough
16 if (current_next_index > generations_.size()) {
18 }
19
20 // Initialize any indices that were reserved but not yet created
21 size_t new_entities_count = 0;
22 for (size_t i = 0; i < current_next_index; ++i) {
23 if (generations_[i] == Entity::kInvalidGeneration) {
24 generations_[i] = 1; // Start with generation 1 for new entities
26 }
27 }
28
29 // Update entity count to reflect newly created entities
30 if (new_entities_count > 0) {
31 entity_count_.fetch_add(new_entities_count, std::memory_order_relaxed);
32 }
33}
34
38
39 // Try to reuse a recycled index first
40 int64_t cursor = free_cursor_.load(std::memory_order_relaxed);
41 if (cursor > 0) {
42 // Attempt to pop from free list
43 const int64_t new_cursor = cursor - 1;
44 if (free_cursor_.compare_exchange_strong(cursor, new_cursor, std::memory_order_relaxed)) {
45 // Successfully claimed an index from the free list
46 const auto free_index = static_cast<size_t>(new_cursor);
47 if (free_index < free_indices_.size()) {
48 index = free_indices_[free_index];
49 if (index < generations_.size()) {
50 generation = generations_[index];
51 return CreateEntityWithId(index, generation);
52 }
53 }
54 }
55 }
56
57 // No free indices available, allocate a new one
58 index = next_index_.fetch_add(1, std::memory_order_relaxed);
59
60 // Ensure generations array is large enough
61 if (index >= generations_.size()) {
62 generations_.resize(index + 1, Entity::kInvalidGeneration);
63 }
64
65 // Start with generation 1 for new entities
66 generation = 1;
67 generations_[index] = generation;
68
69 return CreateEntityWithId(index, generation);
70}
71
72} // namespace helios::ecs::details
Unique identifier for entities with generation counter to handle recycling.
Definition entity.hpp:21
uint32_t IndexType
Definition entity.hpp:23
uint32_t GenerationType
Definition entity.hpp:24
static constexpr GenerationType kInvalidGeneration
Definition entity.hpp:27
static constexpr IndexType kInvalidIndex
Definition entity.hpp:26
Entity CreateEntity()
Creates a new entity.
void FlushReservedEntities()
Creates reserved entities in the metadata.
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481