🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.1 KiB

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