Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

228 řádky
7.1 KiB

  1. #include <gtest/gtest.h>
  2. #include <thread>
  3. #include <cxxabi.h>
  4. #include <ecs.h>
  5. #include <ecs/core.h>
  6. #define MAKE_COMPONENT_TAG(x) \
  7. namespace tag \
  8. { \
  9. constexpr decltype(auto) x = ::ecs::tag::component::value<component::x>; \
  10. constexpr decltype(auto) warning_supressor_##x() { (void) x; } \
  11. }
  12. #define MAKE_SYSTEM_TAG(x) \
  13. namespace tag \
  14. { \
  15. constexpr decltype(auto) x = ::ecs::tag::system::value<system::x>; \
  16. constexpr decltype(auto) warning_supressor_##x() { (void) x; } \
  17. }
  18. namespace test
  19. {
  20. namespace component
  21. {
  22. struct position
  23. {
  24. double x;
  25. double y;
  26. };
  27. struct velocity
  28. {
  29. double x;
  30. double y;
  31. };
  32. struct acceleration
  33. {
  34. double x;
  35. double y;
  36. };
  37. MAKE_COMPONENT_TAG(position)
  38. MAKE_COMPONENT_TAG(velocity)
  39. MAKE_COMPONENT_TAG(acceleration)
  40. }
  41. namespace system
  42. {
  43. struct accelerate
  44. { };
  45. struct move
  46. { };
  47. struct render
  48. { };
  49. MAKE_SYSTEM_TAG(accelerate)
  50. MAKE_SYSTEM_TAG(move)
  51. MAKE_SYSTEM_TAG(render)
  52. }
  53. }
  54. using namespace test;
  55. namespace hana = ::boost::hana;
  56. namespace c = component;
  57. namespace ct = c::tag;
  58. namespace cs = ecs::signature::component;
  59. namespace csl = ecs::signature_list::component;
  60. namespace s = system;
  61. namespace st = s::tag;
  62. namespace ss = ecs::signature::system;
  63. namespace ssl = ecs::signature_list::system;
  64. constexpr size_t storage_size = 1024;
  65. constexpr decltype(auto) cs_position =
  66. cs::make(ct::position)
  67. .storage(cs::storage::dynamic<storage_size>);
  68. constexpr decltype(auto) cs_velocity =
  69. cs::make(ct::velocity)
  70. .storage(cs::storage::dynamic<storage_size>);
  71. constexpr decltype(auto) cs_acceleration =
  72. cs::make(ct::acceleration)
  73. .storage(cs::storage::dynamic<storage_size>);
  74. constexpr decltype(auto) cs_list = csl::make(
  75. cs_position,
  76. cs_velocity,
  77. cs_acceleration);
  78. constexpr decltype(auto) ss_accelerate =
  79. ss::make(st::accelerate)
  80. // .parallelism(ss::parallelism::split_evenly(hana::size_c<3>))
  81. .read(ct::acceleration)
  82. .write(ct::velocity);
  83. constexpr decltype(auto) ss_move =
  84. ss::make(st::move)
  85. .parallelism(ss::parallelism::split_every(hana::size_c<3>))
  86. .read(ct::velocity)
  87. .write(ct::position);
  88. constexpr decltype(auto) ss_render =
  89. ss::make(st::render)
  90. .parallelism(ss::parallelism::main())
  91. .read(ct::velocity, ct::position, ct::acceleration);
  92. constexpr decltype(auto) ss_list = ssl::make(
  93. ss_accelerate,
  94. ss_move,
  95. ss_render);
  96. constexpr decltype(auto) settings = ::ecs::settings::make()
  97. .component_signatures(cs_list)
  98. .system_signatures (ss_list)
  99. .refresh_parallelism (ecs::settings::refresh_parallelism::disable);
  100. namespace sea = ::ecs::system_execution_adapter;
  101. double frand(double min, double max)
  102. {
  103. double f = (double)rand() / RAND_MAX;
  104. return min + f * (max - min);
  105. }
  106. inline void log_system_execution(const std::string& system)
  107. {
  108. std::ostringstream os;
  109. os << system
  110. << " (thread=" << std::this_thread::get_id()
  111. << ")"
  112. << std::endl;
  113. std::cout << os.str();
  114. }
  115. template<typename T_data_proxy>
  116. inline void log_system_execution(const std::string& system, const T_data_proxy& data)
  117. {
  118. std::ostringstream os;
  119. os << system
  120. << " (thread=" << std::this_thread::get_id()
  121. << ", index=" << data._index
  122. << ", begin=" << data._begin
  123. << ", end=" << data._end
  124. << ", count=" << data.entity_count()
  125. << ")"
  126. << std::endl;
  127. std::cout << os.str();
  128. }
  129. TEST(DummyTest, fuu)
  130. {
  131. srand(static_cast<unsigned int>(time(nullptr)));
  132. auto context = ::ecs::context::make(settings);
  133. context.step([](auto& proxy){
  134. for (auto i = 0; i < 10; ++i) {
  135. auto handle = proxy.create_entity();
  136. auto& position = proxy.add_component(handle, ct::position);
  137. position.x = frand(-100, 100);
  138. position.y = frand(-100, 100);
  139. auto& velocity = proxy.add_component(handle, ct::velocity);
  140. velocity.x = frand(-1, 1);
  141. velocity.y = frand(-1, 1);
  142. auto& acceleration = proxy.add_component(handle, ct::acceleration);
  143. acceleration.x = frand(-1, 1);
  144. acceleration.y = frand(-1, 1);
  145. }
  146. });
  147. std::cout << "main " << std::this_thread::get_id() << std::endl;
  148. for (int i = 0; i < 10; ++i) {
  149. context.step([](auto& proxy){
  150. proxy.execute_systems()(
  151. sea::tags(st::accelerate)
  152. .for_subtasks([](auto& s, auto& data){
  153. log_system_execution("accelerate");
  154. data.for_entities([&data](auto& handle) {
  155. auto& velocity = data.get_component(ct::velocity, handle);
  156. auto& acceleration = data.get_component(ct::acceleration, handle);
  157. velocity.x += acceleration.x;
  158. velocity.y += acceleration.y;
  159. });
  160. }),
  161. sea::tags(st::move)
  162. .for_subtasks([](auto& s, auto& data){
  163. log_system_execution("move", data);
  164. data.for_entities([&data](auto& handle) {
  165. auto& position = data.get_component(ct::position, handle);
  166. auto& velocity = data.get_component(ct::velocity, handle);
  167. position.x += velocity.x;
  168. position.y += velocity.y;
  169. });
  170. }),
  171. sea::tags(st::render)
  172. .for_subtasks([](auto& s, auto& data){
  173. log_system_execution("render");
  174. data.for_entities([&data](auto& handle) {
  175. auto& position = data.get_component(ct::position, handle);
  176. auto& velocity = data.get_component(ct::velocity, handle);
  177. auto& acceleration = data.get_component(ct::acceleration, handle);
  178. std::cout << std::fixed
  179. << " " << handle.index() << ": "
  180. << "pos(" << position.x << ";" << position.y << ") "
  181. << "vel(" << velocity.x << ";" << velocity.y << ") "
  182. << "acc(" << acceleration.x << ";" << acceleration.y << ")"
  183. << std::endl;
  184. });
  185. })
  186. );
  187. });
  188. }
  189. }