doc: minor changes

This commit is contained in:
Michele Caini
2020-12-13 23:52:16 +01:00
parent 7154678347
commit f0d7408f94
2 changed files with 20 additions and 65 deletions

View File

@@ -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 &registry) {
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 ...
// use a callback
view.each([](const auto &pos, auto &vel) { /* ... */ });
auto &vel = view.get<velocity>(entity);
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 &registry) {
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);
// ...
}
```

View File

@@ -230,43 +230,12 @@ vel.dx = 0.;
vel.dy = 0.;
```
The `emplace` member function utilizes [std::forward](https://en.cppreference.com/w/cpp/utility/forward)
to allow you to pass your component's parameterized constructor's arguments:
The default storage _detects_ aggregate types internally and exploits aggregate
initialization when possible.<br/>
Therefore, it's not strictly necessary to define a constructor for each type, in
accordance with the rules of the language.
```cpp
struct position {
float x;
float y;
position(float x, float y)
: x(x), y(y)
{}
};
// ...
auto &vel = registry.emplace<position>(entity, 100.0f, 100.0f);
```
Using `emplace`, you can take advantage of [aggregate initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization). As long as your component structs adhere to
the rules of aggregate types, registry.emplace will utilize aggregate
initializatiion which sets property values in the order in which they are
declared in the structure.
```cpp
struct velocity {
float dx;
float dy;
}
// ...
auto &vel = registry.emplace<velocity>(entity, 0.0f, 0.0f);
```
Similarly, `insert` does it for multiple entities and accepts a range rather
than a single entity in order to:
On the other hand, `insert` works with _ranges_ and can be used to:
* Assign the same component to all entities at once when a type is specified as
a template parameter or an instance is passed as an argument:
@@ -279,7 +248,7 @@ than a single entity in order to:
registry.insert(from, to, position{0., 0.});
```
* Assign a range of components to the entities when a range is provided (the
* Assign a set of components to the entities when a range is provided (the
length of the range of components must be the same of that of entities):
```cpp