Skip to content

Commit

Permalink
Rename metadata::del_component to metadata::remove_component, add has…
Browse files Browse the repository at this point in the history
…_component test
  • Loading branch information
vittorioromeo committed Jan 8, 2017
1 parent da13a6b commit bf751c9
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 43 deletions.
2 changes: 1 addition & 1 deletion include/ecst/context/entity/metadata/metadata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ECST_CONTEXT_ENTITY_NAMESPACE
void reset() noexcept;

template <typename TComponent>
auto del_component() noexcept;
auto remove_component() noexcept;

/// @brief Returns `true` if the entity has the `TComponent` bit.
template <typename TComponent>
Expand Down
2 changes: 1 addition & 1 deletion include/ecst/context/entity/metadata/metadata.inl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ECST_CONTEXT_ENTITY_NAMESPACE

template <typename TBitset, typename TChunkMetadata>
template <typename TComponent>
auto metadata<TBitset, TChunkMetadata>::del_component() noexcept
auto metadata<TBitset, TChunkMetadata>::remove_component() noexcept
{
return _bitset.template set_component<TComponent>(false);
}
Expand Down
164 changes: 164 additions & 0 deletions test/ecst/has_component.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#include <ecst.hpp>
#include "./settings_generator.hpp"

namespace example
{
using vrm::core::uint;
using vrm::core::sz_t;

namespace c
{
struct c0 { };
struct c1 { };
}

namespace ct
{
constexpr auto c0 = ecst::tag::component::v<c::c0>;
constexpr auto c1 = ecst::tag::component::v<c::c1>;
}

namespace s
{
struct s0
{
template <typename TData>
void process(TData& data)
{
data.for_entities([&](auto eid)
{
TEST_ASSERT(data.has(ct::c0, eid));
TEST_ASSERT(!data.has(ct::c1, eid));

data.defer([eid](auto& proxy)
{
proxy.remove_component(ct::c0, eid);
proxy.add_component(ct::c1, eid);
});
});
}
};

struct s1
{
template <typename TData>
void process(TData& data)
{
data.for_entities([&](auto eid)
{
TEST_ASSERT(!data.has(ct::c0, eid));
TEST_ASSERT(data.has(ct::c1, eid));

data.defer([eid](auto& proxy)
{
proxy.remove_component(ct::c1, eid);
proxy.add_component(ct::c0, eid);
});
});
}
};
}

#define SYS_TAG(x) \
namespace s \
{ \
struct x; \
} \
namespace st \
{ \
constexpr auto x = ecst::tag::system::v<s::x>; \
}

SYS_TAG(s0)
SYS_TAG(s1)

namespace ecst_setup
{
constexpr auto make_csl()
{
namespace c = example::c;
namespace sc = ecst::signature::component;
namespace slc = ecst::signature_list::component;

return slc::make(
sc::make(ct::c0).contiguous_buffer(),
sc::make(ct::c1).contiguous_buffer());
}

constexpr auto entity_count = ecst::sz_v<100>;

constexpr auto make_ssl()
{
using ecst::sz_v;

namespace c = example::c;
namespace s = example::s;
namespace ss = ecst::signature::system;
namespace sls = ecst::signature_list::system;
namespace ips = ecst::inner_parallelism::strategy;
namespace ipc = ecst::inner_parallelism::composer;

constexpr auto test_p =
ipc::none_below_threshold::v(ecst::sz_v<10>,
ips::split_evenly_fn::v_cores());

constexpr auto ssig_s0 = // .
ss::make(st::s0) // .
.parallelism(test_p) // .
.write(ct::c0); // .

constexpr auto ssig_s1 =
ss::make(st::s1)
.parallelism(test_p)
.write(ct::c1);

return sls::make(ssig_s0, ssig_s1);
}
}

namespace sea = ::ecst::system_execution_adapter;

auto execute_systems = [&](auto& proxy)
{
proxy.execute_systems_from(st::s0, st::s1)( // .
sea::all().for_subtasks([](auto& s, auto& data)
{
s.process(data);
}));
};

auto test_impl_f = [](auto& ctx)
{
ctx.step([&](auto& proxy)
{
for(sz_t ie = 0; ie < ecst_setup::entity_count; ++ie)
{
auto e = proxy.create_entity();
TEST_ASSERT(!proxy.has_component(ct::c0, ecst::entity_id(ie)));
TEST_ASSERT(!proxy.has_component(ct::c1, ecst::entity_id(ie)));
proxy.add_component(ct::c0, e);
}
});

ctx.step(execute_systems);
ctx.step(execute_systems);

ctx.step([&](auto& proxy)
{
for(sz_t ie = 0; ie < ecst_setup::entity_count; ++ie)
{
TEST_ASSERT(proxy.has_component(ct::c0, ecst::entity_id(ie)));
TEST_ASSERT(!proxy.has_component(ct::c1, ecst::entity_id(ie)));
}
});
};
}

int main()
{
using namespace example;
using namespace example::ecst_setup;

test::run_tests(test_impl_f, entity_count, make_csl(), make_ssl());
return 0;
}
55 changes: 14 additions & 41 deletions test/ecst/particles_death.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ using ft = float;

namespace example
{
// Boundaries of the simulation.
constexpr auto left_bound = 0;
constexpr auto right_bound = 1440;
constexpr auto top_bound = 0;
constexpr auto bottom_bound = 900;

// Data of a collision contact.
struct contact
{
Expand Down Expand Up @@ -126,20 +120,6 @@ namespace example
float _v;
};
}

// In order to avoid the annoying `data.template <component_type>` syntax
// and to have an uniform "type-value encoding" interface both in the
// implementation and user code, we define component and system "tags".

// These "tags" are empty `constexpr` objects that wrap the type of the
// component/system in a value, which ECST's functions accept:
/*
// Traditional:
data.template get<c::position>(eid);
// With tags:
data.get(ct::position, eid);
*/
}

// Component tags, in namespace `example::ct`.
Expand All @@ -150,12 +130,6 @@ EXAMPLE_COMPONENT_TAG(circle);
EXAMPLE_COMPONENT_TAG(color);
EXAMPLE_COMPONENT_TAG(life);

// A macro is used to define tags to suppress "unused variable" warnings and
// to avoid code repetition. Essentially, it expands to:
/*
constexpr auto x = ecst::tag::component::vc::x>;
*/

// System tags, in namespace `example::st`.
EXAMPLE_SYSTEM_TAG(acceleration);
EXAMPLE_SYSTEM_TAG(velocity);
Expand Down Expand Up @@ -384,10 +358,10 @@ namespace example
}

// Compile-time `std::size_t` entity limit.
constexpr auto entity_limit = ecst::sz_v<65536>;
constexpr auto entity_limit = ecst::sz_v<5000>;

// Compile-time initial particle count.
constexpr auto entity_count = ecst::sz_v<10000>;
constexpr auto entity_count = ecst::sz_v<4000>;

namespace ecst_setup
{
Expand Down Expand Up @@ -437,8 +411,11 @@ namespace example
namespace ips = ecst::inner_parallelism::strategy;
namespace ipc = ecst::inner_parallelism::composer;
constexpr auto none = ips::none::v();
constexpr auto split_evenly_per_core =
ips::split_evenly_fn::v_cores();

// TODO:
constexpr auto split_evenly_per_core = // .
ipc::none_below_threshold::v(ecst::sz_v<100>, // .
ips::split_evenly_fn::v_cores());

// Acceleration system.
// * Multithreaded.
Expand Down Expand Up @@ -578,8 +555,8 @@ namespace example
});
}

template <typename TContext, typename TRenderTarget>
void update_ctx(TContext& ctx, TRenderTarget& rt, ft dt)
template <typename TContext>
void update_ctx(TContext& ctx, ft dt)
{
namespace sea = ::ecst::system_execution_adapter;

Expand All @@ -589,7 +566,7 @@ namespace example
auto nonft_tags = sea::t(st::keep_in_bounds, st::collision,
st::solve_contacts, st::render_colored_circle);

ctx.step([&rt, dt, &ft_tags, &nonft_tags](auto& proxy)
ctx.step([dt, &ft_tags, &nonft_tags](auto& proxy)
{
proxy.execute_systems()(
ft_tags.for_subtasks([dt](auto& s, auto& data)
Expand Down Expand Up @@ -623,33 +600,29 @@ namespace example
}));

proxy.for_system_outputs(st::render_colored_circle,
[&rt](auto&, auto& va)
[](auto&, auto&)
{
(void)va;
(void)rt;
});
});
}

auto test_impl_f = [](auto& ctx)
{
int rt = 0;

init_ctx(ctx);

for(int i = 0; i < 100; ++i)
{
update_ctx(ctx, rt, 0.5f);
update_ctx(ctx, 0.5f);
}

while(ctx.any_entity_in(st::acceleration))
{
update_ctx(ctx, rt, 0.5f);
update_ctx(ctx, 0.5f);
}

for(int i = 0; i < 100; ++i)
{
update_ctx(ctx, rt, 0.5f);
update_ctx(ctx, 0.5f);
}
};
}
Expand Down

0 comments on commit bf751c9

Please sign in to comment.