plugin.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <cr.h>
  2. #include <entt/core/type_info.hpp>
  3. #include <entt/entity/registry.hpp>
  4. #include "type_context.h"
  5. #include "types.h"
  6. struct ctx {
  7. inline static type_context *ref;
  8. };
  9. template<typename Type>
  10. struct entt::type_index<Type> {
  11. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  12. static const entt::id_type value = ctx::ref->value(entt::type_hash<Type>::value());
  13. return value;
  14. }
  15. };
  16. CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
  17. switch (operation) {
  18. case CR_STEP:
  19. if(!ctx::ref) {
  20. ctx::ref = static_cast<type_context *>(ctx->userdata);
  21. } else {
  22. // forces things to break
  23. auto &registry = *static_cast<entt::registry *>(ctx->userdata);
  24. registry.prepare<velocity>();
  25. const auto view = registry.view<position>();
  26. registry.insert(view.begin(), view.end(), velocity{1., 1.});
  27. registry.view<position, velocity>().each([](position &pos, velocity &vel) {
  28. pos.x += static_cast<int>(16 * vel.dx);
  29. pos.y += static_cast<int>(16 * vel.dy);
  30. });
  31. }
  32. break;
  33. case CR_CLOSE:
  34. case CR_LOAD:
  35. case CR_UNLOAD:
  36. // nothing to do here, this is only a test.
  37. break;
  38. }
  39. return 0;
  40. }