test_package.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <entt/entt.hpp>
  2. #include <cstdint>
  3. #include <cstdlib>
  4. struct position {
  5. float x;
  6. float y;
  7. };
  8. struct velocity {
  9. float dx;
  10. float dy;
  11. };
  12. void update(entt::registry<> &registry) {
  13. auto view = registry.view<position, velocity>();
  14. for(auto entity: view) {
  15. // gets only the components that are going to be used ...
  16. auto &vel = view.get<velocity>(entity);
  17. vel.dx = 0.;
  18. vel.dy = 0.;
  19. // ...
  20. }
  21. }
  22. void update(std::uint64_t dt, entt::registry<> &registry) {
  23. registry.view<position, velocity>().each([dt](const auto, auto &pos, auto &vel) {
  24. // gets all the components of the view at once ...
  25. pos.x += vel.dx * dt;
  26. pos.y += vel.dy * dt;
  27. // ...
  28. });
  29. }
  30. int main() {
  31. entt::registry<> registry;
  32. std::uint64_t dt = 16;
  33. for(auto i = 0; i < 10; ++i) {
  34. auto entity = registry.create();
  35. registry.assign<position>(entity, i * 1.f, i * 1.f);
  36. if(i % 2 == 0) { registry.assign<velocity>(entity, i * .1f, i * .1f); }
  37. }
  38. update(dt, registry);
  39. update(registry);
  40. return EXIT_SUCCESS;
  41. }