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

refactor(Engine): rename motherclass "Registry" to "Core" by Miou-zora · Pull Request #91 · EngineSquared/EngineSquared · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cpp  (21) .hpp  (21) .inl  (1) .lua  (1) 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
2 changes: 1 addition & 1 deletion src/engine/src/Engine.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,7 +1,7 @@
#pragma once

#include "core/Core.hpp"
#include "entity/Entity.hpp"
#include "registry/Registry.hpp"

#include "scheduler/FixedTimeUpdate.hpp"
#include "scheduler/Startup.hpp"
Expand Down
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,35 +1,35 @@
#include "Registry.hpp"
ES::Engine::Registry::Registry() : _registry(nullptr)
{
this->_registry = std::make_unique<entt::registry>();
this->RegisterScheduler<ES::Engine::Scheduler::Startup>(
[this]() { this->DeleteScheduler<ES::Engine::Scheduler::Startup>(); });
this->RegisterScheduler<ES::Engine::Scheduler::Update>();
this->RegisterScheduler<ES::Engine::Scheduler::FixedTimeUpdate>();
this->RegisterScheduler<ES::Engine::Scheduler::RelativeTimeUpdate>();
}
entt::entity ES::Engine::Registry::CreateEntity() { return this->_registry->create(); }
void ES::Engine::Registry::RunSystems()
{
for (auto &[schedulerIndex, scheduler] : this->_schedulers)
{
scheduler->RunSystems(this->_systems[schedulerIndex]);
}
for (auto &scheduler : this->_schedulersToDelete)
{
this->_schedulers.erase(scheduler);
this->_systems.erase(scheduler);
}
this->_schedulersToDelete.clear();
}
bool ES::Engine::Registry::IsEntityValid(entt::entity entity) { return GetRegistry().valid(entity); }
void ES::Engine::Registry::ClearEntities() { this->_registry->clear(); }
#include "Core.hpp"

ES::Engine::Core::Core() : _registry(nullptr)
{
this->_registry = std::make_unique<entt::registry>();

this->RegisterScheduler<ES::Engine::Scheduler::Startup>(
[this]() { this->DeleteScheduler<ES::Engine::Scheduler::Startup>(); });
this->RegisterScheduler<ES::Engine::Scheduler::Update>();
this->RegisterScheduler<ES::Engine::Scheduler::FixedTimeUpdate>();
this->RegisterScheduler<ES::Engine::Scheduler::RelativeTimeUpdate>();
}

entt::entity ES::Engine::Core::CreateEntity() { return this->_registry->create(); }

void ES::Engine::Core::RunSystems()
{

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

for (auto &scheduler : this->_schedulersToDelete)
{
this->_schedulers.erase(scheduler);
this->_systems.erase(scheduler);
}

this->_schedulersToDelete.clear();
}

bool ES::Engine::Core::IsEntityValid(entt::entity entity) { return GetRegistry().valid(entity); }

void ES::Engine::Core::ClearEntities() { this->_registry->clear(); }
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,129 +1,129 @@
#pragma once
#include <entt/entt.hpp>
#include <functional>
#include <memory>
#include <typeindex>
#include <unordered_map>
#include <vector>
#include "FixedTimeUpdate.hpp"
#include "IScheduler.hpp"
#include "RelativeTimeUpdate.hpp"
#include "Startup.hpp"
#include "Update.hpp"
namespace ES::Engine {
/**
* The registry is used to create, kill and manage entities.
* It is also used to register and get components. These registered components can be used by systems.
* After registering all the components and systems, the user can call the RunSystems() method to run all the systems.
* To use ECS, you need to create a registry and register all the components and systems you need and after that
* you can create entities and add components to them.
*/
class Registry {
private:
using USystem = std::function<void(Registry &)>;
public:
Registry();
~Registry() = default;
/**
* Get the entt::registry that contains all components.
* It should be used to update component through systems.
*
* @return registry that contains all components.
*/
inline entt::registry &GetRegistry() { return *_registry; }
/**
* Create an entity.
*
* @return The entity created.
*/
entt::entity CreateEntity();
/**
* Store a resource instance.
* Resources are unique struct or class (like a singleton) that contains global informations.
* Example: AssetsManager, TimeProvider, WindowResource, SceneManager, InputManager or NetworkManager.
*
* @tparam TResource type of the resource to add
* @param resource rvalue of the resource to add
* @return reference of the added resource
*/
template <typename TResource> TResource &RegisterResource(TResource &&resource);
/**
* Get a reference's resource.
* Resources are unique struct or class (like a singleton) that contains global informations.
* Example: AssetsManager, TimeProvider, WindowResource, SceneManager, InputManager or NetworkManager.
*
* @tparam TResource type of the resource to get
* @return reference of the resource
*/
template <typename TResource> TResource &GetResource();
/**
* Add a new scheduler to the registry.
* A scheduler is a class that inherit from IScheduler and that will be called by the registry.
*
* @tparam TScheduler The type of scheduler to use.
* @param scheduler The scheduler to add.
*/
template <typename TScheduler, typename... Args> TScheduler &RegisterScheduler(Args &&...args);
/**
* Get a scheduler from the registry.
*
* @tparam TScheduler The type of scheduler to get.
* @return The scheduler.
*/
template <typename TScheduler> TScheduler &GetScheduler();
/**
* Add one or multiple systems to the registry. A system is a function that will be called by the registry.
* The function must take a Registry as first parameter.
* The function must return void.
* The function will be called by the registry according to the scheduler choosen.
* If multiple systems are added, they will be called as a group, in the order they were added.
*
* @tparam TScheduler The type of scheduler to use.
* @param systems The systems to add.
* @see IScheduler
*/
template <typename TScheduler = ES::Engine::Scheduler::Update, typename... Systems>
void RegisterSystem(Systems... systems);
/**
* Deletes a scheduler from the registry.
*
* @tparam TScheduler The type of scheduler to delete.
* @note This will delete the scheduler at the end of the frame.
*/
template <typename TScheduler> void DeleteScheduler();
/**
* Run all the systems. The systems will be called in the order they were added. It will also update the delta time.
*/
void RunSystems();
/**
* Check if entity is valid in the context of the registry. It check if the id of the entity exist.
*/
bool IsEntityValid(entt::entity entity);
/**
* Clear all entities and components from the registry.
*/
void ClearEntities();
private:
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;
};
} // namespace ES::Engine
#include "Registry.inl"
#pragma once

#include <entt/entt.hpp>
#include <functional>
#include <memory>
#include <typeindex>
#include <unordered_map>
#include <vector>

#include "FixedTimeUpdate.hpp"
#include "IScheduler.hpp"
#include "RelativeTimeUpdate.hpp"
#include "Startup.hpp"
#include "Update.hpp"

namespace ES::Engine {
/**
* The registry is used to create, kill and manage entities.
* It is also used to register and get components. These registered components can be used by systems.
* After registering all the components and systems, the user can call the RunSystems() method to run all the systems.
* To use ECS, you need to create a registry and register all the components and systems you need and after that
* you can create entities and add components to them.
*/
class Core {
private:
using USystem = std::function<void(Core &)>;

public:
Core();
~Core() = default;

/**
* Get the entt::registry that contains all components.
* It should be used to update component through systems.
*
* @return registry that contains all components.
*/
inline entt::registry &GetRegistry() { return *_registry; }
/**
* Create an entity.
*
* @return The entity created.
*/
entt::entity CreateEntity();

/**
* Store a resource instance.
* Resources are unique struct or class (like a singleton) that contains global informations.
* Example: AssetsManager, TimeProvider, WindowResource, SceneManager, InputManager or NetworkManager.
*
* @tparam TResource type of the resource to add
* @param resource rvalue of the resource to add
* @return reference of the added resource
*/
template <typename TResource> TResource &RegisterResource(TResource &&resource);

/**
* Get a reference's resource.
* Resources are unique struct or class (like a singleton) that contains global informations.
* Example: AssetsManager, TimeProvider, WindowResource, SceneManager, InputManager or NetworkManager.
*
* @tparam TResource type of the resource to get
* @return reference of the resource
*/
template <typename TResource> TResource &GetResource();

/**
* Add a new scheduler to the registry.
* A scheduler is a class that inherit from IScheduler and that will be called by the registry.
*
* @tparam TScheduler The type of scheduler to use.
* @param scheduler The scheduler to add.
*/
template <typename TScheduler, typename... Args> TScheduler &RegisterScheduler(Args &&...args);

/**
* Get a scheduler from the registry.
*
* @tparam TScheduler The type of scheduler to get.
* @return The scheduler.
*/
template <typename TScheduler> TScheduler &GetScheduler();

/**
* Add one or multiple systems to the registry. A system is a function that will be called by the registry.
* The function must take a Registry as first parameter.
* The function must return void.
* The function will be called by the registry according to the scheduler choosen.
* If multiple systems are added, they will be called as a group, in the order they were added.
*
* @tparam TScheduler The type of scheduler to use.
* @param systems The systems to add.
* @see IScheduler
*/
template <typename TScheduler = ES::Engine::Scheduler::Update, typename... Systems>
void RegisterSystem(Systems... systems);

/**
* Deletes a scheduler from the registry.
*
* @tparam TScheduler The type of scheduler to delete.
* @note This will delete the scheduler at the end of the frame.
*/
template <typename TScheduler> void DeleteScheduler();

/**
* Run all the systems. The systems will be called in the order they were added. It will also update the delta time.
*/
void RunSystems();

/**
* Check if entity is valid in the context of the registry. It check if the id of the entity exist.
*/
bool IsEntityValid(entt::entity entity);

/**
* Clear all entities and components from the registry.
*/
void ClearEntities();

private:
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;
};
} // namespace ES::Engine

#include "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
@@ -1,38 +1,38 @@
#include "Registry.hpp"
#include "Core.hpp"

namespace ES::Engine {

template <typename TResource>
inline TResource& Registry::RegisterResource(TResource&& resource)
inline TResource& Core::RegisterResource(TResource&& resource)
{
return this->_registry->ctx().emplace<TResource>(std::forward<TResource>(resource));
}


template <typename TResource> inline TResource &Registry::GetResource()
template <typename TResource> inline TResource &Core::GetResource()
{
return this->_registry->ctx().get<TResource>();
}

template <typename TScheduler, typename... Args> inline TScheduler &Registry::RegisterScheduler(Args &&... args)
template <typename TScheduler, typename... Args> inline TScheduler &Core::RegisterScheduler(Args &&... args)
{
this->_schedulers[std::type_index(typeid(TScheduler))] = std::make_unique<TScheduler>(*this, std::forward<Args>(args)...);
return *static_cast<TScheduler *>(this->_schedulers[std::type_index(typeid(TScheduler))].get());
}

template <typename TScheduler> void Registry::DeleteScheduler()
template <typename TScheduler> void Core::DeleteScheduler()
{
this->_schedulersToDelete.push_back(std::type_index(typeid(TScheduler)));
}

template <typename TScheduler> inline TScheduler &Registry::GetScheduler()
template <typename TScheduler> inline TScheduler &Core::GetScheduler()
{
return *static_cast<TScheduler *>(this->_schedulers[std::type_index(typeid(TScheduler))].get());
}

template <typename TScheduler, typename... Systems>
inline void Registry::RegisterSystem(Systems... systems)
inline void Core::RegisterSystem(Systems... systems)
{
this->_systems[std::type_index(typeid(TScheduler))].push_back([systems...](Registry &registry) { (systems(registry), ...); });
this->_systems[std::type_index(typeid(TScheduler))].push_back([systems...](Core &registry) { (systems(registry), ...); });
}
} // namespace ES::Engine
Loading

Back | FazBrowse Home | New Git URL