Skip to content

Commit

Permalink
Merge pull request #48 from YGNI-RType/dev
Browse files Browse the repository at this point in the history
Release v0.2.0
  • Loading branch information
Popochounet authored Oct 26, 2024
2 parents 04d4e96 + 0646d0b commit e0517d3
Show file tree
Hide file tree
Showing 82 changed files with 2,474 additions and 582 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ Temporary Items
### VisualStudioCode ###
.vscode/*
# !.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
# !.vscode/tasks.json
# !.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

Expand Down
14 changes: 5 additions & 9 deletions include/GEngine/BaseEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,15 @@ class BaseEngine {

void start(void);

const world_t &getWorld(void) {
return m_ecs.getComponentMap();
} // TODO keep ?
const world_t &getWorld(void);

template <typename Type>
void subscribeCallback(std::function<void(Type &)> callback) {
m_ecs.subscribeCallback(callback);
}
void subscribeCallback(std::function<void(Type &)> callback);

template <typename T>
void publishEvent(T &e) {
m_ecs.publishEvent<T>(e);
}
void publishEvent(T &e);

void setFirstEntity(ecs::entity::Entity start);

private:
ecs::ECS m_ecs;
Expand Down
10 changes: 10 additions & 0 deletions include/GEngine/BaseEngine.inl
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ template <typename T>
inline void BaseEngine::registerComponent(void) {
m_ecs.registerComponent<T>();
}

template <typename Type>
void BaseEngine::subscribeCallback(std::function<void(Type &)> callback) {
m_ecs.subscribeCallback(callback);
}

template <typename T>
void BaseEngine::publishEvent(T &e) {
m_ecs.publishEvent<T>(e);
}
} // namespace gengine
212 changes: 212 additions & 0 deletions include/GEngine/GEngine.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/*
** EPITECH PROJECT, 2024
** GameEngine
** File description:
** GEngine.hpp
*/

#pragma once

#include "GEngine/BaseEngine.hpp"
#include "GEngine/driver/Engine.hpp"
#include "GEngine/game/Engine.hpp"
#include "GEngine/libdev/Component.hpp"
#include <memory>
#include <mutex>
#include <utility> // For std::forward
#include <vector>

#include "GEngine/libdev/System.hpp"

/**
* @brief Registry class for managing components and systems registration.
*/
struct Registry {
/**
* @brief Constructor for the Registry class.
*
* @param engines A vector of reference wrappers to engines for component and system registration.
*/
Registry(gengine::BaseEngine &local, gengine::BaseEngine &remote)
: m_local(local)
, m_remote(remote) {
}

/**
* @brief Register a component type with the engines.
*
* This method iterates through the engines and registers the specified component type.
*
* @tparam ComponentType The type of component to register.
*/
template <typename ComponentType>
void registerComponent(void) {
m_local.registerComponent<ComponentType>();
m_remote.registerComponent<ComponentType>();
}

/**
* @brief Register a system type with the engines.
*
* This method iterates through the engines and registers the specified system type
* with the provided parameters.
*
* @tparam SystemType The type of system to register.
* @tparam Args Variadic template parameters for system construction arguments.
* @param params Parameters for constructing the system.
*/
template <typename SystemType, class... Args>
void registerSystem(Args &&...params) {
if constexpr (!std::is_base_of<gengine::LocalSystem, SystemType>::value)
m_remote.registerSystem<SystemType>(std::forward<Args>(params)...);
if constexpr (!std::is_base_of<gengine::RemoteSystem, SystemType>::value)
m_local.registerSystem<SystemType>(std::forward<Args>(params)...);
}

private:
gengine::BaseEngine &m_local;
gengine::BaseEngine &m_remote;
};

extern "C" {
/**
* @brief Declare game components and systems for the engine.
*
* This function declares all game components and systems needed for the engine.
* It is intended to be called during the initialization of the GEngine instance.
*
* @param r Pointer to the Registry instance for registering components and systems.
*/
void GEngineDeclareComponents(Registry *r);

/**
* @brief Declare driver components and systems for the engine.
*
* This function declares all driver components and systems needed for the engine.
* It is intended to be called during the initialization of the GEngine instance.
*
* @param r Pointer to the Registry instance for registering components and systems.
*/
void GEngineDeclareSystems(Registry *r);

/**
* @brief Declare shared components and events for the engine.
*
* This function declares shared components and events that can be used across different systems in the engine.
* It is intended to be called during the initialization of the GEngine instance.
*
* @param r Pointer to the Registry instance for registering components and systems.
*/
void GEngineDeclareEvents(Registry *r);
}

/**
* @brief Main class for the Game Engine singleton.
*
* This class manages the initialization and access to the game engine components and systems.
* It ensures that the engine is instantiated only once and provides methods to access its features.
*/
class GEngine {
public:
/**
* @brief Initialize the GEngine singleton instance.
*
* This function ensures that the GEngine instance is initialized only once using a thread-safe mechanism.
* It also registers the necessary game engine elements by calling the registerElements method.
*/
static void init(void) {
std::call_once(initFlag, []() {
instance.reset(new GEngine());
instance->registerElements();
});
}

/**
* @brief Get the singleton instance of GEngine.
*
* This method returns a reference to the GEngine instance. If the instance is not yet created,
* it calls the init method to create it.
*
* @return Reference to the GEngine instance.
*/
static GEngine &getInstance(void) {
if (!instance)
init();
return *instance;
}

/**
* @brief Get the pointer to the singleton instance of GEngine.
*
* This method returns a pointer to the GEngine instance. If the instance is not yet created,
* it calls the init method to create it.
*
* @return Pointer to the GEngine instance.
*/
static GEngine *getInstancePointer(void) {
if (!instance)
init();
return instance.get();
}

/**
* @brief Destructor.
*
* This destructor is private to prevent deletion of the singleton instance through
* deletion of pointers to GEngine.
*/
~GEngine() = default;

static gengine::BaseEngine &getLocal(void) {
if (!instance)
init();
return instance->m_local;
}

static gengine::BaseEngine &getRemote(void) {
if (!instance)
init();
return instance->m_remote;
}

private:
/**
* @brief Private constructor to prevent direct instantiation.
*
* This constructor is private to enforce the singleton pattern, preventing the creation
* of multiple instances of GEngine.
*/
GEngine() = default;

/**
* @brief Register all game engine components and systems.
*
* This function calls the necessary functions to declare the various components and systems of the game engine.
* It ensures that all necessary elements are properly registered during initialization.
*/
static void registerElements(void) {
if (!instance)
init();

auto sharedRegistry = std::make_unique<Registry>(instance->m_local, instance->m_remote);

GEngineDeclareComponents(sharedRegistry.get());
GEngineDeclareSystems(sharedRegistry.get());
GEngineDeclareEvents(sharedRegistry.get());
}

// Delete copy constructor and assignment operator to enforce singleton pattern
GEngine(const GEngine &) = delete;
GEngine &operator=(const GEngine &) = delete;

// Singleton instance and initialization flag
static std::unique_ptr<GEngine> instance;
static std::once_flag initFlag;

gengine::BaseEngine m_local; ///< Instance of the game engine.
gengine::BaseEngine m_remote; ///< Instance of the driver engine.
};

// Initialize static members
// std::unique_ptr<GEngine> GEngine::instance = nullptr;
// std::once_flag GEngine::initFlag;
23 changes: 23 additions & 0 deletions include/GEngine/GEngine.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
** EPITECH PROJECT, 2024
** GameEngine
** File description:
** GEngine.inl
*/

#include "GEngine.hpp"

// Registry::Registry(std::vector<std::reference_wrapper<gengine::BaseEngine>> &engines)
// : m_engines(engines) {}

// template <typename ComponentType>
// void Registry::registerComponent(void) {
// for (gengine::BaseEngine &engine : m_engines)
// engine.registerComponent<ComponentType>();
// }

// template <typename SystemType, class ...Args>
// void Registry::registerSystem(Args &&...params) {
// for (gengine::BaseEngine &engine : m_engines)
// engine.registerSystem<SystemType>(std::forward<Args>(params)...);
// }
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
** EPITECH PROJECT, 2024
** GameEngine
** File description:
** RemoteDriver.hpp
** RemoteLocal.hpp
*/

#pragma once
Expand All @@ -14,41 +14,52 @@

namespace gengine::interface::component {

class RemoteDriver : public gengine::Component<RemoteDriver> {
class RemoteLocal : public gengine::Component<RemoteLocal> {
public:
// Constructor - Generates a new UUID upon object creation
RemoteDriver();
RemoteLocal();

RemoteLocal(const uuids::uuid &uuid);

// Copy constructor
RemoteDriver(const RemoteDriver &other);
RemoteLocal(const RemoteLocal &other);

// Assignment operator
RemoteDriver &operator=(const RemoteDriver &other);
RemoteLocal &operator=(const RemoteLocal &other);

// Overloading the == operator to compare based on UUID
bool operator==(const RemoteDriver &other) const;
bool operator==(const RemoteLocal &other) const;

// Getter for the UUID as a string (hexadecimal format)
std::string getUUIDString() const;

// Getter for the raw UUID bytes (for network transmission)
const uuids::uuid &getUUIDBytes() const;

bool operator<(const RemoteDriver &other) const {
bool operator<(const RemoteLocal &other) const {
return false;
}

static void generateUUID(uuids::uuid &toGenerate);

void setWhoIAm(const uuids::uuid &toSet) {
m_whoIAm = toSet;
}

uuids::uuid getWhoIAm(void) const {
return m_whoIAm;
}

private:
uuids::uuid m_uuid;

void generateUUID();
uuids::uuid m_whoIAm;
};
} // namespace gengine::interface::component

namespace std {
template <>
struct hash<gengine::interface::component::RemoteDriver> {
std::size_t operator()(const gengine::interface::component::RemoteDriver &driver) const {
struct hash<gengine::interface::component::RemoteLocal> {
std::size_t operator()(const gengine::interface::component::RemoteLocal &driver) const {
const uuids::uuid &uuid = driver.getUUIDBytes();
std::size_t hash_value = 0;
auto bytes = uuid.as_bytes();
Expand Down
33 changes: 0 additions & 33 deletions include/GEngine/interface/events/RemoteDriver.hpp

This file was deleted.

Loading

0 comments on commit e0517d3

Please sign in to comment.