Files
CPM.cmake/examples/entt/main.cpp

57 lines
1018 B
C++
Raw Normal View History

2019-09-06 11:52:18 +02:00
#include <cstdint>
#include <entt/entt.hpp>
2019-09-06 11:52:18 +02:00
struct position {
float x;
float y;
2019-09-06 11:52:18 +02:00
};
struct velocity {
float dx;
float dy;
2019-09-06 11:52:18 +02:00
};
void update(entt::registry &registry) {
auto view = registry.view<position, velocity>();
2019-09-06 11:52:18 +02:00
for (auto entity : view) {
// gets only the components that are going to be used ...
2019-09-06 11:52:18 +02:00
auto &vel = view.get<velocity>(entity);
2019-09-06 11:52:18 +02:00
vel.dx = 0.;
vel.dy = 0.;
2019-09-06 11:52:18 +02:00
// ...
}
2019-09-06 11:52:18 +02:00
}
void update(std::uint64_t dt, entt::registry &registry) {
registry.view<position, velocity>().each([dt](auto &pos, auto &vel) {
// gets all the components of the view at once ...
2019-09-06 11:52:18 +02:00
pos.x += vel.dx * dt;
pos.y += vel.dy * dt;
2019-09-06 11:52:18 +02:00
// ...
});
2019-09-06 11:52:18 +02:00
}
int main() {
entt::registry registry;
std::uint64_t dt = 16;
for (auto i = 0; i < 10; ++i) {
auto entity = registry.create();
registry.assign<position>(entity, i * 1.f, i * 1.f);
if (i % 2 == 0) {
registry.assign<velocity>(entity, i * .1f, i * .1f);
2019-09-06 11:52:18 +02:00
}
}
2019-09-06 11:52:18 +02:00
update(dt, registry);
update(registry);
2019-09-06 11:52:18 +02:00
// ...
2019-09-06 11:52:18 +02:00
}