|
- #include <gtest/gtest.h>
- #include <thread>
- #include <cxxabi.h>
- #include <ecs.h>
- #include <ecs/core.h>
-
- #define MAKE_COMPONENT_TAG(x) \
- namespace tag \
- { \
- constexpr decltype(auto) x = ::ecs::tag::component::value<component::x>; \
- constexpr decltype(auto) warning_supressor_##x() { (void) x; } \
- }
-
- #define MAKE_SYSTEM_TAG(x) \
- namespace tag \
- { \
- constexpr decltype(auto) x = ::ecs::tag::system::value<system::x>; \
- constexpr decltype(auto) warning_supressor_##x() { (void) x; } \
- }
-
- namespace test
- {
- namespace component
- {
- struct position
- {
- double x;
- double y;
- };
-
- struct velocity
- {
- double x;
- double y;
- };
-
- struct acceleration
- {
- double x;
- double y;
- };
-
- MAKE_COMPONENT_TAG(position)
- MAKE_COMPONENT_TAG(velocity)
- MAKE_COMPONENT_TAG(acceleration)
- }
-
- namespace system
- {
- struct accelerate
- { };
-
- struct move
- { };
-
- struct render
- { };
-
- MAKE_SYSTEM_TAG(accelerate)
- MAKE_SYSTEM_TAG(move)
- MAKE_SYSTEM_TAG(render)
- }
- }
-
- using namespace test;
-
- namespace hana = ::boost::hana;
-
- namespace c = component;
- namespace ct = c::tag;
- namespace cs = ecs::signature::component;
- namespace csl = ecs::signature_list::component;
-
- namespace s = system;
- namespace st = s::tag;
- namespace ss = ecs::signature::system;
- namespace ssl = ecs::signature_list::system;
-
- constexpr size_t storage_size = 1024;
-
- constexpr decltype(auto) cs_position =
- cs::make(ct::position)
- .storage(cs::storage::dynamic<storage_size>);
-
- constexpr decltype(auto) cs_velocity =
- cs::make(ct::velocity)
- .storage(cs::storage::dynamic<storage_size>);
-
- constexpr decltype(auto) cs_acceleration =
- cs::make(ct::acceleration)
- .storage(cs::storage::dynamic<storage_size>);
-
- constexpr decltype(auto) cs_list = csl::make(
- cs_position,
- cs_velocity,
- cs_acceleration);
-
- constexpr decltype(auto) ss_accelerate =
- ss::make(st::accelerate)
- // .parallelism(ss::parallelism::split_evenly(hana::size_c<3>))
- .read(ct::acceleration)
- .write(ct::velocity);
-
- constexpr decltype(auto) ss_move =
- ss::make(st::move)
- .parallelism(ss::parallelism::split_every(hana::size_c<3>))
- .read(ct::velocity)
- .write(ct::position);
-
- constexpr decltype(auto) ss_render =
- ss::make(st::render)
- .parallelism(ss::parallelism::main())
- .read(ct::velocity, ct::position, ct::acceleration);
-
- constexpr decltype(auto) ss_list = ssl::make(
- ss_accelerate,
- ss_move,
- ss_render);
-
- constexpr decltype(auto) settings = ::ecs::settings::make()
- .component_signatures(cs_list)
- .system_signatures (ss_list)
- .refresh_parallelism (ecs::settings::refresh_parallelism::disable);
-
- namespace sea = ::ecs::system_execution_adapter;
-
- double frand(double min, double max)
- {
- double f = (double)rand() / RAND_MAX;
- return min + f * (max - min);
- }
-
- inline void log_system_execution(const std::string& system)
- {
- std::ostringstream os;
- os << system
- << " (thread=" << std::this_thread::get_id()
- << ")"
- << std::endl;
- std::cout << os.str();
- }
-
- template<typename T_data_proxy>
- inline void log_system_execution(const std::string& system, const T_data_proxy& data)
- {
- std::ostringstream os;
- os << system
- << " (thread=" << std::this_thread::get_id()
- << ", index=" << data._index
- << ", begin=" << data._begin
- << ", end=" << data._end
- << ", count=" << data.entity_count()
- << ")"
- << std::endl;
- std::cout << os.str();
- }
-
- TEST(DummyTest, fuu)
- {
- srand(static_cast<unsigned int>(time(nullptr)));
-
- auto context = ::ecs::context::make(settings);
-
- context.step([](auto& proxy){
- for (auto i = 0; i < 10; ++i) {
- auto handle = proxy.create_entity();
-
- auto& position = proxy.add_component(handle, ct::position);
- position.x = frand(-100, 100);
- position.y = frand(-100, 100);
-
- auto& velocity = proxy.add_component(handle, ct::velocity);
- velocity.x = frand(-1, 1);
- velocity.y = frand(-1, 1);
-
- auto& acceleration = proxy.add_component(handle, ct::acceleration);
- acceleration.x = frand(-1, 1);
- acceleration.y = frand(-1, 1);
- }
- });
-
- std::cout << "main " << std::this_thread::get_id() << std::endl;
-
- for (int i = 0; i < 10; ++i) {
- context.step([](auto& proxy){
- proxy.execute_systems()(
- sea::tags(st::accelerate)
- .for_subtasks([](auto& s, auto& data){
- log_system_execution("accelerate");
- data.for_entities([&data](auto& handle) {
- auto& velocity = data.get_component(ct::velocity, handle);
- auto& acceleration = data.get_component(ct::acceleration, handle);
-
- velocity.x += acceleration.x;
- velocity.y += acceleration.y;
- });
- }),
- sea::tags(st::move)
- .for_subtasks([](auto& s, auto& data){
- log_system_execution("move", data);
- data.for_entities([&data](auto& handle) {
- auto& position = data.get_component(ct::position, handle);
- auto& velocity = data.get_component(ct::velocity, handle);
-
- position.x += velocity.x;
- position.y += velocity.y;
- });
- }),
- sea::tags(st::render)
- .for_subtasks([](auto& s, auto& data){
- log_system_execution("render");
- data.for_entities([&data](auto& handle) {
- auto& position = data.get_component(ct::position, handle);
- auto& velocity = data.get_component(ct::velocity, handle);
- auto& acceleration = data.get_component(ct::acceleration, handle);
-
- std::cout << std::fixed
- << " " << handle.index() << ": "
- << "pos(" << position.x << ";" << position.y << ") "
- << "vel(" << velocity.x << ";" << velocity.y << ") "
- << "acc(" << acceleration.x << ";" << acceleration.y << ")"
- << std::endl;
- });
- })
- );
- });
- }
- }
|