|
|
@@ -112,14 +112,10 @@ mind.
|
|
|
|
|
|
```cpp
|
|
|
#include <entt/entt.hpp>
|
|
|
-#include <cstdint>
|
|
|
|
|
|
struct position {
|
|
|
float x;
|
|
|
float y;
|
|
|
- position(float x, float y)
|
|
|
- : x(x), y(y)
|
|
|
- {}
|
|
|
};
|
|
|
|
|
|
struct velocity {
|
|
|
@@ -128,47 +124,37 @@ struct velocity {
|
|
|
};
|
|
|
|
|
|
void update(entt::registry ®istry) {
|
|
|
- auto view = registry.view<position, velocity>();
|
|
|
+ auto view = registry.view<const position, velocity>();
|
|
|
|
|
|
- for(auto entity: view) {
|
|
|
- // gets only the components that are going to be used ...
|
|
|
-
|
|
|
- auto &vel = view.get<velocity>(entity);
|
|
|
+ // use a callback
|
|
|
+ view.each([](const auto &pos, auto &vel) { /* ... */ });
|
|
|
|
|
|
- vel.dx = 0.;
|
|
|
- vel.dy = 0.;
|
|
|
+ // use an extended callback
|
|
|
+ view.each([](const auto entity, const auto &pos, auto &vel) { /* ... */ });
|
|
|
|
|
|
+ // use a range-for
|
|
|
+ for(auto [entity, pos, vel]: view.each()) {
|
|
|
// ...
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-void update(std::uint64_t dt, entt::registry ®istry) {
|
|
|
- registry.view<position, velocity>().each([dt](auto &pos, auto &vel) {
|
|
|
- // gets all the components of the view at once ...
|
|
|
-
|
|
|
- pos.x += vel.dx * dt;
|
|
|
- pos.y += vel.dy * dt;
|
|
|
|
|
|
+ // use forward iterators and get only the components of interest
|
|
|
+ for(auto entity: view) {
|
|
|
+ auto &vel = view.get<velocity>(entity);
|
|
|
// ...
|
|
|
- });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
int main() {
|
|
|
entt::registry registry;
|
|
|
- std::uint64_t dt = 16;
|
|
|
+ entt::entity entities[10u];
|
|
|
|
|
|
- for(auto i = 0; i < 10; ++i) {
|
|
|
- auto entity = registry.create();
|
|
|
- // uses parameterized constructor ...
|
|
|
+ for(auto i = 0u; i < 10u; ++i) {
|
|
|
+ const auto entity = registry.create();
|
|
|
registry.emplace<position>(entity, i * 1.f, i * 1.f);
|
|
|
- // uses aggregate initialization ...
|
|
|
if(i % 2 == 0) { registry.emplace<velocity>(entity, i * .1f, i * .1f); }
|
|
|
}
|
|
|
|
|
|
- update(dt, registry);
|
|
|
update(registry);
|
|
|
-
|
|
|
- // ...
|
|
|
}
|
|
|
```
|
|
|
|