| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| #include "Update.hpp" | ||
|
|
||
| void ES::Engine::Scheduler::Update::RunSystems(std::vector<USystem> systems) | ||
| void ES::Engine::Scheduler::Update::RunSystems(USystemList &systems) | ||
| { | ||
| for (auto &system : systems) | ||
| for (auto const &system : systems) | ||
| { | ||
| system(_registry); | ||
| (*system)(_registry); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| #pragma once | ||
|
|
||
| #include <entt/entt.hpp> | ||
| #include <functional> | ||
| #include <iostream> | ||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| #include "Logger.hpp" | ||
|
|
||
| namespace ES::Engine { | ||
| // Forward declaration of Core class. | ||
| class Core; | ||
|
|
||
| /** | ||
| * @brief Base class for all systems in the engine. | ||
| */ | ||
| class SystemBase { | ||
| public: | ||
| virtual ~SystemBase() = default; | ||
|
|
||
| /** | ||
| * @brief Pure virtual function call operator to be implemented by derived systems. | ||
| * @param core Reference to the Core object. | ||
| */ | ||
| virtual void operator()(Core &core) const = 0; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Template class for systems, derived from SystemBase. Used to store any kind of callable system. | ||
| * @tparam TSystem Type of the system. | ||
| */ | ||
| template <typename TCallable> class System : public SystemBase { | ||
| public: | ||
| /** | ||
| * @brief Constructor for System. | ||
| * @param system The system object. | ||
| */ | ||
| explicit System(TCallable system) : SystemBase(), _system(system) {} | ||
| ~System() final = default; | ||
|
|
||
| /** | ||
| * @brief Call operator to execute the system. | ||
| * @param core Reference to the Core. | ||
| */ | ||
| void operator()(Core &core) const final { _system(core); } | ||
|
|
||
| private: | ||
| [[no_unique_address]] TCallable _system; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Container class for managing multiple systems. | ||
| */ | ||
| class SystemContainer { | ||
| public: | ||
| /** | ||
| * @brief Default constructor for SystemContainer. | ||
| */ | ||
| SystemContainer() = default; | ||
|
|
||
| /** | ||
| * @brief Default destructor for SystemContainer. | ||
| */ | ||
| ~SystemContainer() = default; | ||
|
|
||
| /** | ||
| * @brief Adds one or multiple systems to the container. | ||
| * @tparam TSystem Variadic template parameter for system types. | ||
| * @param systems The systems to be added. | ||
| */ | ||
| template <typename... TSystem> inline void AddSystems(TSystem... systems) { (AddSystem(systems), ...); } | ||
|
|
||
| /** | ||
| * @brief Retrieves the vector of systems. | ||
| * @return Reference to the vector of unique pointers to SystemBase. | ||
| */ | ||
| inline std::vector<std::unique_ptr<SystemBase>> &GetSystems() { return _orderedSystems; } | ||
|
|
||
| private: | ||
| /** | ||
| * @brief Adds a single system to the container. | ||
| * @tparam TCallable Type of the callable system. | ||
| * @param callable The callable system to be added. | ||
| */ | ||
| template <typename TSystem> void AddSystem(TSystem callable); | ||
|
|
||
| std::unordered_map<entt::id_type, std::size_t> _idToIndex; ///< Map to store unique ids for each system. | ||
| std::vector<std::unique_ptr<SystemBase>> _orderedSystems; ///< Vector to store systems in order. | ||
| }; | ||
|
|
||
| } // namespace ES::Engine | ||
|
|
||
| #include "System.inl" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #include "System.hpp" | ||
|
|
||
| template <typename TCallable> void ES::Engine::SystemContainer::AddSystem(TCallable callable) | ||
| { | ||
| std::size_t id = 0; | ||
|
|
||
| if constexpr (std::is_class_v<TCallable>) | ||
| { | ||
| id = typeid(callable).hash_code(); | ||
| } | ||
| else | ||
| { | ||
| id = std::hash<TCallable>{}(callable); | ||
| } | ||
|
|
||
| if (_idToIndex.find(id) != _idToIndex.end()) | ||
| { | ||
| ES::Utils::Log::Warn("System already exists"); | ||
| return; | ||
| } | ||
| std::size_t index = _orderedSystems.size(); | ||
| auto system = std::make_unique<System<TCallable>>(callable); | ||
| _orderedSystems.push_back(std::move(system)); | ||
| _idToIndex[id] = index; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "Core.hpp" | ||
| #include "Entity.hpp" | ||
|
|
||
| using namespace ES::Engine; | ||
|
|
||
| struct A { | ||
| int value = 0; | ||
| }; | ||
|
|
||
| struct B { | ||
| int value = 0; | ||
| }; | ||
|
|
||
| struct C { | ||
| int value = 0; | ||
| }; | ||
|
|
||
| class TestSystemClass { | ||
| public: | ||
| void operator()(Core &core) const { core.GetResource<A>().value++; } | ||
| }; | ||
|
|
||
| void TestSystemFunction(Core &core) { core.GetResource<B>().value++; } | ||
|
|
||
| TEST(Systems, Casual) | ||
| { | ||
| Core reg; | ||
|
|
||
| reg.RegisterResource<A>({}); | ||
| reg.RegisterResource<B>({}); | ||
| reg.RegisterResource<C>({}); | ||
|
|
||
| // Test for class Systems | ||
| reg.RegisterSystem(TestSystemClass(), TestSystemClass()); | ||
| reg.RegisterSystem(TestSystemClass()); | ||
|
|
||
| // Test for function Systems | ||
| reg.RegisterSystem(TestSystemFunction, TestSystemFunction); | ||
| reg.RegisterSystem(TestSystemFunction); | ||
|
|
||
| // Test for lambda Systems | ||
| reg.RegisterSystem([](Core &core) { core.GetResource<C>().value++; }, | ||
| [](Core &core) { core.GetResource<C>().value++; }); | ||
| reg.RegisterSystem([](Core &core) { core.GetResource<C>().value++; }); | ||
|
|
||
| reg.RunSystems(); | ||
|
|
||
| ASSERT_EQ(reg.GetResource<A>().value, 1); | ||
| ASSERT_EQ(reg.GetResource<B>().value, 1); | ||
| ASSERT_EQ(reg.GetResource<C>().value, 3); | ||
| } |
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.