mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-13 23:58:18 +07:00
few helper functions for components extracting
This commit is contained in:
@@ -37,3 +37,108 @@ namespace e2d::systems
|
||||
using render_system = ecs::system<render_event>;
|
||||
using post_render_system = ecs::system<post_render_event>;
|
||||
}
|
||||
|
||||
namespace e2d::systems
|
||||
{
|
||||
template < typename... Ts, typename Iter >
|
||||
std::size_t extract_components(ecs::registry& owner, Iter iter) {
|
||||
std::size_t count{0u};
|
||||
owner.for_joined_components<Ts...>(
|
||||
[&iter, &count](const ecs::entity& e, Ts&... cs){
|
||||
iter++ = std::make_tuple(e, std::ref(cs)...);
|
||||
++count;
|
||||
});
|
||||
return count;
|
||||
}
|
||||
|
||||
template < typename... Ts, typename Iter >
|
||||
std::size_t extract_components(const ecs::registry& owner, Iter iter) {
|
||||
std::size_t count{0u};
|
||||
owner.for_joined_components<Ts...>(
|
||||
[&iter, &count](const ecs::const_entity& e, const Ts&... cs){
|
||||
iter++ = std::make_tuple(e, std::cref(cs)...);
|
||||
++count;
|
||||
});
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
namespace e2d::systems
|
||||
{
|
||||
template < typename... Ts, typename F >
|
||||
void for_extracted_components(ecs::registry& owner, F&& f) {
|
||||
//TODO(BlackMat): replace it to frame allocator
|
||||
static thread_local vector<std::tuple<
|
||||
ecs::entity,
|
||||
std::reference_wrapper<Ts>...>> components;
|
||||
try {
|
||||
extract_components<Ts...>(owner, std::back_inserter(components));
|
||||
for ( auto& t : components ) {
|
||||
std::apply(f, t);
|
||||
}
|
||||
} catch (...) {
|
||||
components.clear();
|
||||
throw;
|
||||
}
|
||||
components.clear();
|
||||
}
|
||||
|
||||
template < typename... Ts, typename F >
|
||||
void for_extracted_components(const ecs::registry& owner, F&& f) {
|
||||
//TODO(BlackMat): replace it to frame allocator
|
||||
static thread_local vector<std::tuple<
|
||||
ecs::const_entity,
|
||||
std::reference_wrapper<const Ts>...>> components;
|
||||
try {
|
||||
extract_components<Ts...>(owner, std::back_inserter(components));
|
||||
for ( const auto& t : components ) {
|
||||
std::apply(f, t);
|
||||
}
|
||||
} catch (...) {
|
||||
components.clear();
|
||||
throw;
|
||||
}
|
||||
components.clear();
|
||||
}
|
||||
}
|
||||
|
||||
namespace e2d::systems
|
||||
{
|
||||
template < typename... Ts, typename Comp, typename F >
|
||||
void for_extracted_components(ecs::registry& owner, Comp&& comp, F&& f) {
|
||||
//TODO(BlackMat): replace it to frame allocator
|
||||
static thread_local vector<std::tuple<
|
||||
ecs::entity,
|
||||
std::reference_wrapper<Ts>...>> components;
|
||||
try {
|
||||
extract_components<Ts...>(owner, std::back_inserter(components));
|
||||
std::sort(components.begin(), components.end(), comp);
|
||||
for ( auto& t : components ) {
|
||||
std::apply(f, t);
|
||||
}
|
||||
} catch (...) {
|
||||
components.clear();
|
||||
throw;
|
||||
}
|
||||
components.clear();
|
||||
}
|
||||
|
||||
template < typename... Ts, typename Comp, typename F >
|
||||
void for_extracted_components(const ecs::registry& owner, Comp&& comp, F&& f) {
|
||||
//TODO(BlackMat): replace it to frame allocator
|
||||
static thread_local vector<std::tuple<
|
||||
ecs::const_entity,
|
||||
std::reference_wrapper<const Ts>...>> components;
|
||||
try {
|
||||
extract_components<Ts...>(owner, std::back_inserter(components));
|
||||
std::sort(components.begin(), components.end(), comp);
|
||||
for ( const auto& t : components ) {
|
||||
std::apply(f, t);
|
||||
}
|
||||
} catch (...) {
|
||||
components.clear();
|
||||
throw;
|
||||
}
|
||||
components.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,57 +19,36 @@ namespace
|
||||
using namespace e2d;
|
||||
using namespace e2d::render_system_impl;
|
||||
|
||||
template < typename T, typename Comp, typename F >
|
||||
void for_each_by_sorted_components(ecs::registry& owner, Comp&& comp, F&& f) {
|
||||
static vector<std::pair<ecs::const_entity,T>> temp_components;
|
||||
try {
|
||||
temp_components.reserve(owner.component_count<T>());
|
||||
owner.for_each_component<T>([](const ecs::const_entity& e, const T& t){
|
||||
temp_components.emplace_back(e, t);
|
||||
void for_all_scenes(drawer::context& ctx, const ecs::registry& owner) {
|
||||
const auto comp = [](const auto& l, const auto& r) noexcept {
|
||||
return std::get<1>(l).get().depth() < std::get<1>(r).get().depth();
|
||||
};
|
||||
const auto func = [&ctx](
|
||||
const ecs::const_entity&,
|
||||
const scene&,
|
||||
const actor& scn_a)
|
||||
{
|
||||
nodes::for_extracted_nodes(scn_a.node(), [&ctx](const const_node_iptr& node){
|
||||
ctx.draw(node);
|
||||
});
|
||||
std::sort(
|
||||
temp_components.begin(),
|
||||
temp_components.end(),
|
||||
[&comp](const auto& l, const auto& r){
|
||||
return comp(l.second, r.second);
|
||||
});
|
||||
for ( auto& p : temp_components ) {
|
||||
f(p.first, p.second);
|
||||
}
|
||||
} catch (...) {
|
||||
temp_components.clear();
|
||||
throw;
|
||||
}
|
||||
temp_components.clear();
|
||||
};
|
||||
systems::for_extracted_components<scene, actor>(owner, comp, func);
|
||||
}
|
||||
|
||||
void for_all_scenes(drawer::context& ctx, ecs::registry& owner) {
|
||||
const auto comp = [](const scene& l, const scene& r) noexcept {
|
||||
return l.depth() < r.depth();
|
||||
void for_all_cameras(drawer& drawer, const ecs::registry& owner) {
|
||||
const auto comp = [](const auto& l, const auto& r) noexcept {
|
||||
return std::get<1>(l).get().depth() < std::get<1>(r).get().depth();
|
||||
};
|
||||
const auto func = [&ctx](const ecs::const_entity& scn_e, const scene&) {
|
||||
const actor* scn_a = scn_e.find_component<actor>();
|
||||
if ( scn_a && scn_a->node() ) {
|
||||
nodes::for_extracted_nodes(scn_a->node(), [&ctx](const const_node_iptr& node){
|
||||
ctx.draw(node);
|
||||
});
|
||||
}
|
||||
};
|
||||
for_each_by_sorted_components<scene>(owner, comp, func);
|
||||
}
|
||||
|
||||
void for_all_cameras(drawer& drawer, ecs::registry& owner) {
|
||||
const auto comp = [](const camera& l, const camera& r) noexcept {
|
||||
return l.depth() < r.depth();
|
||||
};
|
||||
const auto func = [&drawer, &owner](const ecs::const_entity& cam_e, const camera& cam) {
|
||||
const actor* const cam_a = cam_e.find_component<actor>();
|
||||
const const_node_iptr cam_n = cam_a ? cam_a->node() : nullptr;
|
||||
drawer.with(cam, cam_n, [&owner](drawer::context& ctx){
|
||||
const auto func = [&drawer, &owner](
|
||||
const ecs::const_entity&,
|
||||
const camera& cam,
|
||||
const actor& cam_a)
|
||||
{
|
||||
drawer.with(cam, cam_a.node(), [&owner](drawer::context& ctx){
|
||||
for_all_scenes(ctx, owner);
|
||||
});
|
||||
};
|
||||
for_each_by_sorted_components<camera>(owner, comp, func);
|
||||
systems::for_extracted_components<camera, actor>(owner, comp, func);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user