FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(Core): prevent systems from running multiple times by Miou-zora · Pull Request #113 · EngineSquared/EngineSquared · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
59cb12d
feat(Core): Add custom systems struct
Miou-zora Mar 11, 2025
b95c6e0
style: apply linter
github-actions[bot] Mar 11, 2025
423ba34
refactor(Core): make code sonar compliant
Miou-zora Mar 11, 2025
70e8882
Merge branch '111-remove-the-possiblity-to-have-multiple-same-systems…
Miou-zora Mar 11, 2025
341b7b2
style: apply linter
github-actions[bot] Mar 11, 2025
61cdc83
refactor(Core): make code sonar compliant
Miou-zora Mar 11, 2025
9ef4e04
refactor(Scheduler): fix warnings
Miou-zora Mar 12, 2025
c7c1524
fix(System): add a proper hashing way for lambda
Miou-zora Mar 12, 2025
e2688b8
style: apply linter
github-actions[bot] Mar 12, 2025
bc7e2de
feat(System): log system duplication
Miou-zora Mar 12, 2025
de78737
refactor(Core): use spdlog
Miou-zora Mar 12, 2025
c0486b1
fix(Core): add spdlog to test
Miou-zora Mar 12, 2025
69818b4
refactor(Core): make code sonar compliant
Miou-zora Mar 12, 2025
b3d75b5
Merge branch 'main' into 111-remove-the-possiblity-to-have-multiple-s…
Miou-zora Mar 12, 2025
49479ef
refactor(Scheduler): use "using" type rather than duplicating it ever…
Miou-zora Mar 12, 2025
2f7b0b1
refactor(System): separate hpp and impl of methods into inl file and …
Miou-zora Mar 12, 2025
6545907
test(System): add test for system duplication
Miou-zora Mar 12, 2025
a944d66
style: apply linter
github-actions[bot] Mar 12, 2025
4f6b6aa
refactor(System): make code sonar compliant
Miou-zora Mar 12, 2025
21415d4
style: apply linter
github-actions[bot] Mar 12, 2025
86b6a12
Merge branch 'main' into 111-remove-the-possiblity-to-have-multiple-s…
Miou-zora Mar 14, 2025
4b41c4d
fix(Window): add missing deps
Miou-zora Mar 14, 2025
File filter

Filter by extension

Filter by extension .cpp  (6) .hpp  (7) .inl  (2) .lua  (5) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 3 additions & 3 deletions src/engine/src/core/Core.cpp
This file contains hidden or 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
Expand Up @@ -20,12 +20,12 @@ ES::Engine::Entity ES::Engine::Core::CreateEntity()
void ES::Engine::Core::RunSystems()
{

for (auto &[schedulerIndex, scheduler] : this->_schedulers)
for (const auto &[schedulerIndex, scheduler] : this->_schedulers)
{
scheduler->RunSystems(this->_systems[schedulerIndex]);
scheduler->RunSystems(this->_systems[schedulerIndex].GetSystems());
}

for (auto &scheduler : this->_schedulersToDelete)
for (const auto &scheduler : this->_schedulersToDelete)
{
this->_schedulers.erase(scheduler);
this->_systems.erase(scheduler);
Expand Down
2 changes: 1 addition & 1 deletion src/engine/src/core/Core.hpp
This file contains hidden or 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
Expand Up @@ -128,7 +128,7 @@ class Core {
std::unique_ptr<entt::registry> _registry;
std::map<std::type_index, std::unique_ptr<Scheduler::IScheduler>> _schedulers;
std::vector<std::type_index> _schedulersToDelete;
std::unordered_map<std::type_index, std::vector<USystem>> _systems;
std::unordered_map<std::type_index, SystemContainer> _systems;
};
} // namespace ES::Engine

Expand Down
2 changes: 1 addition & 1 deletion src/engine/src/core/Core.inl
This file contains hidden or 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
Expand Up @@ -33,6 +33,6 @@ template <typename TScheduler> inline TScheduler &Core::GetScheduler()
template <typename TScheduler, typename... Systems>
inline void Core::RegisterSystem(Systems... systems)
{
this->_systems[std::type_index(typeid(TScheduler))].push_back([systems...](Core &registry) { (systems(registry), ...); });
this->_systems[std::type_index(typeid(TScheduler))].AddSystems<Systems...>(systems...);
}
} // namespace ES::Engine
6 changes: 3 additions & 3 deletions src/engine/src/scheduler/FixedTimeUpdate.cpp
This file contains hidden or 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
@@ -1,6 +1,6 @@
#include "FixedTimeUpdate.hpp"

void ES::Engine::Scheduler::FixedTimeUpdate::RunSystems(std::vector<USystem> systems)
void ES::Engine::Scheduler::FixedTimeUpdate::RunSystems(USystemList &systems)
{
auto currentTime = std::chrono::high_resolution_clock::now();
_elapsedTime += std::chrono::duration<float>(currentTime - _lastTime).count();
Expand All @@ -9,9 +9,9 @@ void ES::Engine::Scheduler::FixedTimeUpdate::RunSystems(std::vector<USystem> sys

for (unsigned int i = 0; i < ticks; i++)
{
for (auto &system : systems)
for (auto const &system : systems)
{
system(_registry);
(*system)(_registry);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/engine/src/scheduler/FixedTimeUpdate.hpp
This file contains hidden or 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
Expand Up @@ -16,11 +16,11 @@ namespace ES::Engine::Scheduler {
*/
class FixedTimeUpdate : public AScheduler {
private:
inline static constexpr float DEFAULT_TICK_RATE = 1.0 / 50.0;
inline static constexpr float DEFAULT_TICK_RATE = 1.0f / 50.0f;

public:
FixedTimeUpdate(Core &registry, float tickRate = DEFAULT_TICK_RATE) : AScheduler(registry), _tickRate(tickRate) {}
void RunSystems(std::vector<USystem> systems) override;
void RunSystems(USystemList &systems) override;

/**
* @brief Get the fixed tick rate
Expand Down
6 changes: 3 additions & 3 deletions src/engine/src/scheduler/IScheduler.hpp
This file contains hidden or 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
@@ -1,14 +1,14 @@
#pragma once

#include "System.hpp"
#include <entt/entt.hpp>

namespace ES::Engine {
class Core;
}

namespace ES::Engine::Scheduler {
using USystem = std::function<void(Core &)>;

using USystemList = std::vector<std::unique_ptr<SystemBase>>;
/**
* @brief Interface to be implemented for every schedulers
*/
Expand All @@ -19,6 +19,6 @@ class IScheduler {
*
* @param systems The systems to run
*/
virtual void RunSystems(std::vector<USystem> systems) = 0;
virtual void RunSystems(USystemList &systems) = 0;
};
} // namespace ES::Engine::Scheduler
10 changes: 5 additions & 5 deletions src/engine/src/scheduler/RelativeTimeUpdate.cpp
This file contains hidden or 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
@@ -1,6 +1,6 @@
#include "RelativeTimeUpdate.hpp"

void ES::Engine::Scheduler::RelativeTimeUpdate::RunSystems(std::vector<USystem> systems)
void ES::Engine::Scheduler::RelativeTimeUpdate::RunSystems(USystemList &systems)
{
auto currentTime = std::chrono::high_resolution_clock::now();
auto diff = std::chrono::duration<float>(currentTime - _lastTime).count();
Expand All @@ -10,18 +10,18 @@ void ES::Engine::Scheduler::RelativeTimeUpdate::RunSystems(std::vector<USystem>
for (unsigned int i = 0; i < ticks; i++)
{
_deltaTime = _tickRate;
for (auto &system : systems)
for (auto const &system : systems)
{
system(_registry);
(*system)(_registry);
}
}

if (remainder > REMAINDER_THRESHOLD)
{
_deltaTime = remainder;
for (auto &system : systems)
for (auto const &system : systems)
{
system(_registry);
(*system)(_registry);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/engine/src/scheduler/RelativeTimeUpdate.hpp
This file contains hidden or 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
Expand Up @@ -13,7 +13,7 @@ namespace ES::Engine::Scheduler {
*/
class RelativeTimeUpdate : public AScheduler {
private:
inline static constexpr float DEFAULT_TARGET_TICK_RATE = 1.0 / 50.0;
inline static constexpr float DEFAULT_TARGET_TICK_RATE = 1.0f / 50.0f;
inline static constexpr float REMAINDER_THRESHOLD = 0.0001f;

public:
Expand All @@ -22,7 +22,7 @@ class RelativeTimeUpdate : public AScheduler {
{
}

void RunSystems(std::vector<USystem> systems) override;
void RunSystems(USystemList &systems) override;

/**
* @brief Get the target tick rate
Expand Down
6 changes: 3 additions & 3 deletions src/engine/src/scheduler/Startup.cpp
This file contains hidden or 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
@@ -1,10 +1,10 @@
#include "Startup.hpp"

void ES::Engine::Scheduler::Startup::RunSystems(std::vector<USystem> systems)
void ES::Engine::Scheduler::Startup::RunSystems(USystemList &systems)
{
for (auto &system : systems)
for (auto const &system : systems)
{
system(_registry);
(*system)(_registry);
}

_callback();
Expand Down
6 changes: 4 additions & 2 deletions src/engine/src/scheduler/Startup.hpp
This file contains hidden or 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
Expand Up @@ -10,8 +10,10 @@ namespace ES::Engine::Scheduler {
*/
class Startup : public AScheduler {
public:
explicit Startup(Core &registry, std::function<void()> callback) : AScheduler(registry), _callback(callback) {}
void RunSystems(std::vector<USystem> systems) override;
explicit Startup(Core &registry, const std::function<void()> &callback) : AScheduler(registry), _callback(callback)
{
}
void RunSystems(USystemList &systems) override;

private:
std::function<void()> _callback;
Expand Down
6 changes: 3 additions & 3 deletions src/engine/src/scheduler/Update.cpp
This file contains hidden or 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
@@ -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);
}
}
5 changes: 3 additions & 2 deletions src/engine/src/scheduler/Update.hpp
This file contains hidden or 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
Expand Up @@ -10,7 +10,8 @@ namespace ES::Engine::Scheduler {
*/
class Update : public AScheduler {
public:
explicit Update(Core &registry) : AScheduler(registry) {}
void RunSystems(std::vector<USystem> systems) override;
using AScheduler::AScheduler;
virtual ~Update() = default;
void RunSystems(USystemList &systems) override;
};
} // namespace ES::Engine::Scheduler
94 changes: 94 additions & 0 deletions src/engine/src/system/System.hpp
This file contains hidden or 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,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;
Comment thread
ripel2 marked this conversation as resolved.
};

/**
* @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"
25 changes: 25 additions & 0 deletions src/engine/src/system/System.inl
This file contains hidden or 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,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;
}
53 changes: 53 additions & 0 deletions src/engine/tests/engine/SystemTest.cpp
This file contains hidden or 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,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);
}
Loading

Back | FazBrowse Home | New Git URL