/
d.vision
/
division_engine_cpp
Обзор
Документация
Войти
/
d.vision
/
division_engine_cpp
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
include/division_engine/canvas/render_manager.hpp
103 строки
2 KB
den163
Added user defined rendering order support
05 ноя 2024, 22:20
05 ноя 2024, 22:20
f4a24bb
Код
Авторство
О чём код?
#pragma once #include "components/render_batch.hpp" #include "components/render_order.hpp" #include "renderer.hpp" #include "state.hpp" #include "division_engine/utility/algorithm.hpp" #include <array> #include <flecs.h> #include <memory> #include <optional> #include <span> #include <tuple> #include <type_traits> #include <typeindex> #include <typeinfo> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> namespace division_engine::canvas { class RenderManager { public: template<typename... TRenderers> RenderManager() : _renderers() , _batch(std::nullopt) , _render_order(0) { } template<typename TRenderer, typename... TArgs> void register_renderer(TArgs&... args) { static_assert(std::is_base_of<Renderer, TRenderer>()); auto ptr = std::make_unique<TRenderer>(args...); _renderers.push_back(std::move(ptr)); } template<typename... TComponents> flecs::entity create_renderer( State& state, std::tuple<TComponents...> components, int custom_order = 0, std::optional<flecs::entity_t> batch_entity = std::nullopt ) { using components::RenderBatch; using components::RenderOrder; using division_engine::utility::algorithm::tuple_foreach; auto renderer_id = std::type_index(typeid(components)); if (!_batch.has_value() || _batch->first != renderer_id) { _batch = std::make_pair( renderer_id, BatchInfo { state.world.entity().set(RenderBatch {}).id(), custom_order } ); } auto new_entity = state.world.entity(); tuple_foreach([&](auto& comp) { new_entity.set(comp); }, components); if (batch_entity.has_value()) { new_entity.is_a(batch_entity.value()); } new_entity.set(RenderOrder { _render_order++, custom_order }); new_entity.is_a(_batch->second.batch_entity_id); return new_entity; } void update(State& state) { for (auto& rend : _renderers) { rend->fill_render_queue(state); } } private: struct BatchInfo { flecs::entity_t batch_entity_id; int custom_order; }; std::vector<std::unique_ptr<Renderer>> _renderers; std::optional<std::pair<std::type_index, BatchInfo>> _batch; uint32_t _render_order; }; }