-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
205 lines (180 loc) · 7.14 KB
/
main.cpp
File metadata and controls
205 lines (180 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (c) November 2025 Félix-Olivier Dumas. All rights reserved.
// Licensed under the terms described in the LICENSE file
#pragma once
#include <iostream>
#include <vector>
#include <chrono>
#include <cstdint>
#include <stdexcept>
class EntityId {
private:
static std::uint32_t _nextId;
public:
static std::uint32_t Next() { return _nextId++; }
};
std::uint32_t EntityId::_nextId = 0;
struct Entity {
std::uint32_t Value;
Entity() : Value(EntityId::Next()) {}
};
enum class ComponentMask : std::uint8_t {
None = 0,
Position = 1 << 0,
Velocity = 1 << 1,
Rotation = 1 << 2,
Scale = 1 << 3,
Color = 1 << 4,
};
struct Position { std::uint32_t X, Y; };
struct Velocity { std::uint32_t VX, VY; };
struct Rotation { std::uint8_t Angle; };
struct Scale { std::uint8_t X, Y; };
struct Color { std::uint8_t R, G, B, A; };
class Registry {
private:
static constexpr std::uint32_t InitialEntityCapacity = 131072;
static constexpr std::uint32_t InitialPoolCapacity = 262143;
std::vector<ComponentMask> _cmask;
std::vector<ComponentMask> _ctype;
std::vector<Position> _positions;
std::vector<int> _entityToPosIndex;
std::vector<Velocity> _velocities;
std::vector<int> _entityToVelIndex;
std::vector<Rotation> _rotations;
std::vector<int> _entityToRotIndex;
std::vector<Scale> _scales;
std::vector<int> _entityToScaleIndex;
std::vector<Color> _colors;
std::vector<int> _entityToColorIndex;
private:
template <typename T> static ComponentMask MaskOf() {
static_assert(std::is_trivially_copyable_v<T>);
if constexpr (std::is_same_v<T, Position>) return ComponentMask::Position;
if constexpr (std::is_same_v<T, Velocity>) return ComponentMask::Velocity;
if constexpr (std::is_same_v<T, Rotation>) return ComponentMask::Rotation;
if constexpr (std::is_same_v<T, Scale>) return ComponentMask::Scale;
if constexpr (std::is_same_v<T, Color>) return ComponentMask::Color;
throw std::runtime_error("Component type not supported");
}
public:
Registry(std::size_t maxEntities, std::size_t maxPool) {
_cmask.resize(maxEntities, ComponentMask::None);
_entityToPosIndex.resize(maxEntities, -1);
_entityToVelIndex.resize(maxEntities, -1);
_entityToRotIndex.resize(maxEntities, -1);
_entityToScaleIndex.resize(maxEntities, -1);
_entityToColorIndex.resize(maxEntities, -1);
_positions.reserve(maxPool);
_velocities.reserve(maxPool);
_rotations.reserve(maxPool);
_scales.reserve(maxPool);
_colors.reserve(maxPool);
}
template <typename T> void Add(std::uint32_t eidx) {
if constexpr (std::is_same_v<T, Position>) {
if (_entityToPosIndex[eidx] != -1) {
printf("[ERROR] Entity %u already has Position component\n", eidx);
return;
}
_entityToPosIndex[eidx] = _positions.size();
_positions.push_back(Position{});
}
else if constexpr (std::is_same_v<T, Velocity>) {
if (_entityToVelIndex[eidx] != -1) {
printf("[ERROR] Entity %u already has Velocity component\n", eidx);
return;
}
_entityToVelIndex[eidx] = _velocities.size();
_velocities.push_back(Velocity{});
}
else if constexpr (std::is_same_v<T, Rotation>) {
if (_entityToRotIndex[eidx] != -1) {
printf("[ERROR] Entity %u already has Rotation component\n", eidx);
return;
}
_entityToRotIndex[eidx] = _rotations.size();
_rotations.push_back(Rotation{});
}
else if constexpr (std::is_same_v<T, Scale>) {
if (_entityToScaleIndex[eidx] != -1) {
printf("[ERROR] Entity %u already has Scale component\n", eidx);
return;
}
_entityToScaleIndex[eidx] = _scales.size();
_scales.push_back(Scale{});
}
else if constexpr (std::is_same_v<T, Color>) {
if (_entityToColorIndex[eidx] != -1) {
printf("[ERROR] Entity %u already has Color component\n", eidx);
return;
}
_entityToColorIndex[eidx] = _colors.size();
_colors.push_back(Color{});
}
}
template<typename T> T& Get(std::uint32_t eidx) {
if constexpr (std::is_same_v<T, Position>) {
if (_entityToPosIndex[eidx] == -1)
throw std::runtime_error(
"Entity " + std::to_string(eidx) + " does NOT have Position component"
);
return _positions[_entityToPosIndex[eidx]];
}
else if constexpr (std::is_same_v<T, Velocity>) {
if (_entityToVelIndex[eidx] == -1)
throw std::runtime_error(
"Entity " + std::to_string(eidx) + " does NOT have Velocity component"
);
return _velocities[_entityToVelIndex[eidx]];
}
else if constexpr (std::is_same_v<T, Rotation>) {
if (_entityToRotIndex[eidx] == -1)
throw std::runtime_error(
"Entity " + std::to_string(eidx) + " does NOT have Rotation component"
);
return _rotations[_entityToRotIndex[eidx]];
}
else if constexpr (std::is_same_v<T, Scale>) {
if (_entityToScaleIndex[eidx] == -1)
throw std::runtime_error(
"Entity " + std::to_string(eidx) + " does NOT have Scale component"
);
return _scales[_entityToScaleIndex[eidx]];
}
else if constexpr (std::is_same_v<T, Color>) {
if (_entityToColorIndex[eidx] == -1)
throw std::runtime_error(
"Entity " + std::to_string(eidx) + " does NOT have Color component"
);
return _colors[_entityToColorIndex[eidx]];
}
throw std::runtime_error("Component type not supported");
}
};
int main() {
constexpr size_t entityCount = 100000000;
constexpr size_t maxPool = 200000;
Registry registry(entityCount, maxPool);
std::vector<Entity> entities(entityCount);
auto start = std::chrono::high_resolution_clock::now();
for (uint32_t i = 0; i < entityCount; ++i) {
entities[i].Value = i;
registry.Add<Position>(i);
registry.Add<Velocity>(i);
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
std::cout << "Setup " << entityCount << " entities with 2 components each: "
<< elapsed.count() << " ms\n";
start = std::chrono::high_resolution_clock::now();
for (uint32_t i = 0; i < entityCount; ++i) {
auto& pos = registry.Get<Position>(entities[i].Value);
auto& vel = registry.Get<Velocity>(entities[i].Value);
pos.X = i; pos.Y = i * 2;
vel.VX = i; vel.VY = i * 2;
}
end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
std::cout << "Accessed and modified Position and Velocity components: "
<< elapsed.count() << " ms\n";
}