Jelajahi Sumber

added a test that goes across boundaries

Michele Caini 7 tahun lalu
induk
melakukan
33fddcb289

+ 4 - 2
src/entt/core/type_traits.hpp

@@ -129,7 +129,8 @@ constexpr auto is_shared_v = is_shared<Type>::value;
 /*! @brief Utility function to simulate macro overloading. */
 #define ENTT_SHARED_STRUCT_OVERLOAD(_1, _2, _3, FUNC, ...) FUNC
 /*! @brief Defines a type as shareable (to use for structs). */
-#define ENTT_SHARED_STRUCT(...) ENTT_SHARED_STRUCT_OVERLOAD(__VA_ARGS__, ENTT_SHARED_STRUCT_WITH_NAMESPACE, ENTT_SHARED_STRUCT_ONLY)(__VA_ARGS__)
+#define ENTT_SHARED_STRUCT(...) ENTT_SHARED_STRUCT_OVERLOAD(__VA_ARGS__, ENTT_SHARED_STRUCT_WITH_NAMESPACE, ENTT_SHARED_STRUCT_ONLY, 0)(__VA_ARGS__)
+// 0 is used to suppress warning: ISO C++11 requires at least one argument for the "..." in a variadic macro
 
 
 /**
@@ -158,7 +159,8 @@ constexpr auto is_shared_v = is_shared<Type>::value;
 /*! @brief Utility function to simulate macro overloading. */
 #define ENTT_SHARED_CLASS_MACRO(_1, _2, _3, FUNC, ...) FUNC
 /*! @brief Defines a type as shareable (to use for classes). */
-#define ENTT_SHARED_CLASS(...) ENTT_SHARED_CLASS_MACRO(__VA_ARGS__, ENTT_SHARED_CLASS_WITH_NAMESPACE, ENTT_SHARED_CLASS_ONLY)(__VA_ARGS__)
+#define ENTT_SHARED_CLASS(...) ENTT_SHARED_CLASS_MACRO(__VA_ARGS__, ENTT_SHARED_CLASS_WITH_NAMESPACE, ENTT_SHARED_CLASS_ONLY, 0)(__VA_ARGS__)
+// 0 is used to suppress warning: ISO C++11 requires at least one argument for the "..." in a variadic macro
 
 
 #endif // ENTT_CORE_TYPE_TRAITS_HPP

+ 8 - 0
test/lib/a_module.cpp

@@ -1,4 +1,5 @@
 #include <entt/entity/registry.hpp>
+#include "component.h"
 
 #ifndef LIB_EXPORT
 #if defined _WIN32 || defined __CYGWIN__
@@ -32,3 +33,10 @@ LIB_EXPORT typename entt::registry<>::component_type a_module_char_type() {
 
     return registry.type<char>();
 }
+
+LIB_EXPORT void update_position(int delta, entt::registry<> &registry) {
+    registry.view<position, velocity>().each([delta](auto &pos, auto &vel) {
+        pos.x += delta * vel.dx;
+        pos.y += delta * vel.dy;
+    });
+}

+ 7 - 0
test/lib/another_module.cpp

@@ -1,4 +1,5 @@
 #include <entt/entity/registry.hpp>
+#include "component.h"
 
 #ifndef LIB_EXPORT
 #if defined _WIN32 || defined __CYGWIN__
@@ -38,3 +39,9 @@ LIB_EXPORT typename entt::registry<>::component_type another_module_char_type()
 
     return registry.type<char>();
 }
+
+LIB_EXPORT void assign_velocity(int vel, entt::registry<> &registry) {
+    for(auto entity: registry.view<position>()) {
+        registry.assign<velocity>(entity, vel, vel);
+    }
+}

+ 11 - 0
test/lib/component.h

@@ -0,0 +1,11 @@
+#include <entt/core/type_traits.hpp>
+
+ENTT_SHARED_STRUCT(position, {
+    int x;
+    int y;
+})
+
+ENTT_SHARED_STRUCT(velocity, {
+    int dx;
+    int dy;
+})

+ 25 - 0
test/lib/lib.cpp

@@ -1,11 +1,15 @@
 #include <entt/entity/registry.hpp>
 #include <gtest/gtest.h>
+#include "component.h"
 
 extern typename entt::registry<>::component_type a_module_int_type();
 extern typename entt::registry<>::component_type a_module_char_type();
 extern typename entt::registry<>::component_type another_module_int_type();
 extern typename entt::registry<>::component_type another_module_char_type();
 
+extern void update_position(int delta, entt::registry<> &);
+extern void assign_velocity(int, entt::registry<> &);
+
 ENTT_SHARED_TYPE(int)
 ENTT_SHARED_TYPE(char)
 
@@ -25,3 +29,24 @@ TEST(Lib, Shared) {
     ASSERT_EQ(registry.type<char>(), another_module_char_type());
     ASSERT_EQ(registry.type<int>(), another_module_int_type());
 }
+
+TEST(Lib, PositionVelocity) {
+    entt::registry<> registry;
+
+    for(auto i = 0; i < 3; ++i) {
+        const auto entity = registry.create();
+        registry.assign<position>(entity, i, i+1);
+    }
+
+    assign_velocity(2, registry);
+
+    ASSERT_EQ(registry.size<position>(), entt::registry<>::size_type{3});
+    ASSERT_EQ(registry.size<velocity>(), entt::registry<>::size_type{3});
+
+    update_position(1, registry);
+
+    registry.view<position>().each([](auto entity, auto &position) {
+        ASSERT_EQ(position.x, entity + 2);
+        ASSERT_EQ(position.y, entity + 3);
+    });
+}