-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: toggle stream + new indexed fields api (#151)
Co-authored-by: Ezekiel Warren <ezekiel@seaube.com>
- Loading branch information
Showing
35 changed files
with
844 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#pragma once | ||
|
||
#include <entt/entt.hpp> | ||
|
||
#include "ecsact/entt/detail/internal_markers.hh" | ||
|
||
namespace ecsact::entt::detail { | ||
template<typename C> | ||
auto apply_component_stream_data( | ||
::entt::registry& main_reg, | ||
::entt::registry& stream_reg | ||
) -> void { | ||
auto view = main_reg.template view< | ||
C>(::entt::exclude<ecsact::entt::detail::run_on_stream<C>>); | ||
|
||
for(auto entity : view) { | ||
if(!stream_reg.any_of<C>(entity)) { | ||
continue; | ||
} | ||
|
||
auto& in_component = stream_reg.get<C>(entity); | ||
if(main_reg.any_of<C>(entity)) { | ||
auto& current_comp = main_reg.get<C>(entity); | ||
|
||
auto& beforechange = | ||
main_reg.template get<exec_beforechange_storage<C>>(entity); | ||
|
||
if(!beforechange.has_update_occurred) { | ||
beforechange.value = current_comp; | ||
beforechange.has_update_occurred = true; | ||
} | ||
current_comp = in_component; | ||
} | ||
} | ||
} | ||
} // namespace ecsact::entt::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#pragma once | ||
|
||
#include <thread> | ||
#include <map> | ||
#include <mutex> | ||
#include <shared_mutex> | ||
#include <vector> | ||
#include <ranges> | ||
#include <memory> | ||
#include <entt/entt.hpp> | ||
#include "ecsact/runtime/common.h" | ||
#include "ecsact/entt/entity.hh" | ||
|
||
namespace ecsact::entt::stream::detail { | ||
using child_reg_thread_map = | ||
std::unordered_map<std::thread::id, std::unique_ptr<::entt::registry>>; | ||
using reg_thread_map = | ||
std::unordered_map<ecsact_registry_id, child_reg_thread_map>; | ||
} // namespace ecsact::entt::stream::detail | ||
|
||
namespace ecsact::entt::stream { | ||
class stream_registries { | ||
private: | ||
detail::reg_thread_map registry_stream_threads; | ||
std::shared_mutex stream_mutex; | ||
|
||
template<typename C> | ||
auto ensure_and_add_entity_and_component( | ||
std::unique_ptr<::entt::registry>& registry, | ||
::ecsact::entt::entity_id entity, | ||
const C& component | ||
) -> void { | ||
if(!registry->valid(entity)) { | ||
auto new_entity = registry->create(entity); | ||
assert(new_entity == entity.as_entt()); | ||
} | ||
|
||
registry->template emplace_or_replace<C>(entity, component); | ||
} | ||
|
||
public: | ||
stream_registries() = default; | ||
|
||
template<typename C> | ||
auto handle_stream( | ||
ecsact_registry_id registry_id, | ||
ecsact_entity_id entity_id, | ||
const C& component | ||
) -> void { | ||
// Add to map if new threads/registries are introduced | ||
|
||
auto thread_id = std::this_thread::get_id(); | ||
auto entity = ::ecsact::entt::entity_id(entity_id); | ||
|
||
std::shared_lock shared_lk(stream_mutex); | ||
std::unique_lock lk(stream_mutex, std::defer_lock); | ||
|
||
auto reg_threads_itr = registry_stream_threads.find(registry_id); | ||
assert(reg_threads_itr != registry_stream_threads.end()); | ||
|
||
auto& reg_threads = reg_threads_itr->second; | ||
auto reg_thread_itr = reg_threads.find(thread_id); | ||
|
||
if(reg_thread_itr == reg_threads.end()) { | ||
auto registry = std::make_unique<::entt::registry>(); | ||
ensure_and_add_entity_and_component(registry, entity, component); | ||
shared_lk.unlock(); | ||
lk.lock(); | ||
reg_threads.insert( | ||
reg_threads.end(), | ||
std::pair(thread_id, std::move(registry)) | ||
); | ||
lk.unlock(); | ||
} else { | ||
auto& registry = reg_thread_itr->second; | ||
ensure_and_add_entity_and_component(registry, entity, component); | ||
} | ||
} | ||
|
||
auto get_stream_registries() | ||
-> std::vector<std::unique_ptr<::entt::registry>>; | ||
auto add_registry(ecsact_registry_id) -> void; | ||
}; | ||
} // namespace ecsact::entt::stream |
Oops, something went wrong.