From 233aca0328186c61bebabe553b1039281b221324 Mon Sep 17 00:00:00 2001 From: Yann Date: Tue, 26 Mar 2024 18:45:21 +0100 Subject: [PATCH 01/13] feat: add method render entities kind nastly --- core/src/core/Core.cpp | 71 +++++++++++++++++++++++++++++ core/src/core/Core.hpp | 44 ++++++++++++++++++ shared/games/IGame.hpp | 11 ++++- shared/games/types/GameManifest.hpp | 29 ++++++------ shared/graphics/ITexture.hpp | 2 +- 5 files changed, 139 insertions(+), 18 deletions(-) create mode 100644 core/src/core/Core.cpp create mode 100644 core/src/core/Core.hpp diff --git a/core/src/core/Core.cpp b/core/src/core/Core.cpp new file mode 100644 index 0000000..4ed54c6 --- /dev/null +++ b/core/src/core/Core.cpp @@ -0,0 +1,71 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** Core +*/ + +#include +#include "Core.hpp" +#include "shared/games/components/IComponent.hpp" +#include "shared/games/components/IDisplayableComponent.hpp" + +Core::Core(GameProviders gameProviders, GraphicsProviders graphicsProviders) { + this->_gameProviders = gameProviders; + this->_graphicsProviders = graphicsProviders; +} + +Core::~Core() {} + +void Core::_setup() +{ + shared::graphics::WindowInitProps windowInitProps { + {0, 0}, + shared::graphics::WINDOWED, + 60, + "Arcade", + "icon" + }; + + this->_gameProvider = this->_gameProviders.at(0); + this->_graphicsProvider = this->_graphicsProviders.at(0); + + this->_game = this->_gameProvider.get()->createInstance(); + windowInitProps.size = this->_game.get()->getSize(); + this->_window = this->_graphicsProvider.get()->createWindow(windowInitProps); +} + +void Core::_renderEntities() +{ + shared::games::entity::EntitiesMap entities = this->_game.get()->getEntities(); + + for (auto &entity : entities) { + auto components = entity.second.get()->getComponents(); + for (auto &component : components) { + if (component.second.get()->getType() == shared::games::components::DISPLAYABLE) { + auto displayable = std::dynamic_pointer_cast(component.second); + auto textureProps = displayable.get()->getTextureProps(); + shared::graphics::EntityTextureProps entityTextureProps { + this->_graphicsProvider.get()->createTexture(textureProps.sources.bin, textureProps.sources.ascii), + textureProps.sources.binTileSize, + textureProps.origin + }; + shared::graphics::EntityProps entityProps { + entityTextureProps, + displayable.get()->getSize(), + displayable.get()->getPosition() + }; + this->_window.get()->render(entityProps); + } + } + } +} + +void Core::run() +{ + this->_setup(); + while (this->_window.get()->isOpen()) { + this->_window.get()->display(); + this->_window.get()->clear(); + } +} diff --git a/core/src/core/Core.hpp b/core/src/core/Core.hpp new file mode 100644 index 0000000..6ac48a3 --- /dev/null +++ b/core/src/core/Core.hpp @@ -0,0 +1,44 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** Core +*/ + +#pragma once + +#include "types/Providers.hpp" + +class Core { + public: + Core(GameProviders gameProviders, GraphicsProviders graphicsProviders); + ~Core(); + + /** + * @brief Run the core + * + */ + void run(); + + + protected: + private: + std::shared_ptr _game; + std::shared_ptr _window; + std::shared_ptr _gameProvider; + std::shared_ptr _graphicsProvider; + GameProviders _gameProviders; + GraphicsProviders _graphicsProviders; + + /** + * @brief Initialize the core + * + */ + void _setup(); + + /** + * @brief Render all entities + * + */ + void _renderEntities(); +}; diff --git a/shared/games/IGame.hpp b/shared/games/IGame.hpp index 291ac21..a187ce5 100644 --- a/shared/games/IGame.hpp +++ b/shared/games/IGame.hpp @@ -37,14 +37,21 @@ class shared::games::IGame * @brief Manifest with informations of the game * */ - virtual const GameManifest &getManifest(void) const noexcept = 0; + virtual const GameManifest &getManifest() const noexcept = 0; /** * @brief Number of tiles that represent the game * Tile size is managed by the renderer * */ - virtual const Vector2u getSize(void) const noexcept = 0; + virtual const Vector2u getSize() const noexcept = 0; + + /** + * @brief Get fps of the game + * + * @return The number of frame per seconds of the game + */ + virtual const unsigned int getFps() const noexcept = 0; /** * @brief Get map of entities diff --git a/shared/games/types/GameManifest.hpp b/shared/games/types/GameManifest.hpp index 4c6efbf..25da00a 100644 --- a/shared/games/types/GameManifest.hpp +++ b/shared/games/types/GameManifest.hpp @@ -6,23 +6,22 @@ */ #pragma once + #include #include -namespace shared::games -{ - typedef struct - { - std::string name; // Name of the author - std::string email; // Public contact email - std::string website; // Website of the author (`github`, `gitlab`, etc.) - } Author; +namespace shared::games { + typedef struct { + std::string name; // Name of the author + std::string email; // Public contact email + std::string website; // Website of the author (`github`, `gitlab`, etc.) + } Author; - typedef struct - { - const std::string name; // Name of the game - const std::string description; // Description of the game - const std::string version; // Version of the game - const std::vector authors; // Authors - } GameManifest; + typedef struct { + const std::string name; // Name of the game + const std::string description; // Description of the game + const std::string version; // Version of the game + const std::vector authors; // Authors + const std::string iconPath; // Path of the icon game + } GameManifest; } diff --git a/shared/graphics/ITexture.hpp b/shared/graphics/ITexture.hpp index 69649da..f82f3e8 100644 --- a/shared/graphics/ITexture.hpp +++ b/shared/graphics/ITexture.hpp @@ -11,7 +11,7 @@ namespace shared::graphics { class ITexture; } -class ITexture { +class shared::graphics::ITexture { public: virtual ~ITexture() = default; }; From 2eccb5963e5e6cecdd9ca6978610cd1640e51ad3 Mon Sep 17 00:00:00 2001 From: Yann Date: Tue, 26 Mar 2024 19:22:52 +0100 Subject: [PATCH 02/13] feat: add cache system for textures --- core/src/core/Core.cpp | 51 ++++++++++++++++++++++++------------------ core/src/core/Core.hpp | 16 +++++++++++-- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/core/src/core/Core.cpp b/core/src/core/Core.cpp index 4ed54c6..c6ddb9a 100644 --- a/core/src/core/Core.cpp +++ b/core/src/core/Core.cpp @@ -8,16 +8,18 @@ #include #include "Core.hpp" #include "shared/games/components/IComponent.hpp" -#include "shared/games/components/IDisplayableComponent.hpp" -Core::Core(GameProviders gameProviders, GraphicsProviders graphicsProviders) { +Core::Core(GameProviders gameProviders, GraphicsProviders graphicsProviders) +{ this->_gameProviders = gameProviders; this->_graphicsProviders = graphicsProviders; + this->_gameProvider = this->_gameProviders.at(0); + this->_graphicsProvider = this->_graphicsProviders.at(0); } Core::~Core() {} -void Core::_setup() +void Core::_initWindow() { shared::graphics::WindowInitProps windowInitProps { {0, 0}, @@ -27,14 +29,32 @@ void Core::_setup() "icon" }; - this->_gameProvider = this->_gameProviders.at(0); - this->_graphicsProvider = this->_graphicsProviders.at(0); - this->_game = this->_gameProvider.get()->createInstance(); windowInitProps.size = this->_game.get()->getSize(); this->_window = this->_graphicsProvider.get()->createWindow(windowInitProps); } +std::shared_ptr Core::_getTexture(std::string bin, std::string ascii) +{ + if (this->_textures.find(bin + ascii) == this->_textures.end()) + this->_textures[bin + ascii] = this->_graphicsProvider.get()->createTexture(bin, ascii); + return this->_textures[bin + ascii]; +} + +void Core::_renderDisplayableEntity(std::shared_ptr displayable) +{ + auto textureProps = displayable.get()->getTextureProps(); + shared::graphics::EntityTextureProps entityTextureProps{ + this->_getTexture(textureProps.sources.bin, textureProps.sources.ascii), + textureProps.sources.binTileSize, + textureProps.origin}; + shared::graphics::EntityProps entityProps{ + entityTextureProps, + displayable.get()->getSize(), + displayable.get()->getPosition()}; + this->_window.get()->render(entityProps); +} + void Core::_renderEntities() { shared::games::entity::EntitiesMap entities = this->_game.get()->getEntities(); @@ -42,28 +62,15 @@ void Core::_renderEntities() for (auto &entity : entities) { auto components = entity.second.get()->getComponents(); for (auto &component : components) { - if (component.second.get()->getType() == shared::games::components::DISPLAYABLE) { - auto displayable = std::dynamic_pointer_cast(component.second); - auto textureProps = displayable.get()->getTextureProps(); - shared::graphics::EntityTextureProps entityTextureProps { - this->_graphicsProvider.get()->createTexture(textureProps.sources.bin, textureProps.sources.ascii), - textureProps.sources.binTileSize, - textureProps.origin - }; - shared::graphics::EntityProps entityProps { - entityTextureProps, - displayable.get()->getSize(), - displayable.get()->getPosition() - }; - this->_window.get()->render(entityProps); - } + if (component.second.get()->getType() == shared::games::components::DISPLAYABLE) + this->_renderDisplayableEntity(std::dynamic_pointer_cast(component.second)); } } } void Core::run() { - this->_setup(); + this->_initWindow(); while (this->_window.get()->isOpen()) { this->_window.get()->display(); this->_window.get()->clear(); diff --git a/core/src/core/Core.hpp b/core/src/core/Core.hpp index 6ac48a3..a95fb71 100644 --- a/core/src/core/Core.hpp +++ b/core/src/core/Core.hpp @@ -8,6 +8,7 @@ #pragma once #include "types/Providers.hpp" +#include "shared/games/components/IDisplayableComponent.hpp" class Core { public: @@ -20,13 +21,13 @@ class Core { */ void run(); - protected: private: std::shared_ptr _game; std::shared_ptr _window; std::shared_ptr _gameProvider; std::shared_ptr _graphicsProvider; + std::map> _textures; GameProviders _gameProviders; GraphicsProviders _graphicsProviders; @@ -34,11 +35,22 @@ class Core { * @brief Initialize the core * */ - void _setup(); + void _initWindow(); /** * @brief Render all entities * */ void _renderEntities(); + + /** + * @brief Get a texture + * + * @param bin Path to the binary file + * @param ascii Path to the ascii file + * @return The correct texture + */ + std::shared_ptr _getTexture(std::string bin, std::string ascii); + + void _renderDisplayableEntity(std::shared_ptr displayable); }; From 3e2349e2c7def1cbca975f611e55f38b4d5244a7 Mon Sep 17 00:00:00 2001 From: Yann Date: Wed, 27 Mar 2024 10:36:59 +0100 Subject: [PATCH 03/13] refactor: update code from new shared version --- core/src/core/Core.cpp | 53 +++++++---- core/src/core/Core.hpp | 17 +++- shared/games/IEntity.hpp | 16 +--- shared/games/IGame.hpp | 13 +-- shared/games/components/IComponent.hpp | 8 -- shared/graphics/IGraphicsProvider.hpp | 9 +- shared/graphics/ISound.hpp | 17 ++-- shared/graphics/{window => }/IWindow.hpp | 39 ++++---- shared/graphics/events/IEvent.hpp | 4 +- shared/graphics/events/IKeyEvent.hpp | 61 +++++++++++++ shared/graphics/events/IMouseButtonEvent.hpp | 31 +++++++ shared/graphics/events/IMouseEvent.hpp | 27 ++++++ shared/graphics/events/key/AKeyEvent.hpp | 87 ------------------ shared/graphics/events/key/KeyPressEvent.hpp | 19 ---- .../graphics/events/key/KeyReleaseEvent.hpp | 18 ---- .../events/mouse/AMouseButtonEvent.hpp | 39 -------- shared/graphics/events/mouse/AMouseEvent.hpp | 50 ----------- .../events/mouse/MouseButtonPressEvent.hpp | 24 ----- .../events/mouse/MouseButtonReleaseEvent.hpp | 24 ----- .../graphics/events/mouse/MouseMoveEvent.hpp | 22 ----- .../events/window/WindowCloseEvent.hpp | 29 ------ .../events/window/WindowResizeEvent.hpp | 43 --------- shared/graphics/types/EntityProps.hpp | 2 +- shared/types/UUId.cpp | 63 ------------- shared/types/UUId.hpp | 89 ------------------- shared/types/types.hpp | 11 --- 26 files changed, 212 insertions(+), 603 deletions(-) rename shared/graphics/{window => }/IWindow.hpp (82%) create mode 100644 shared/graphics/events/IKeyEvent.hpp create mode 100644 shared/graphics/events/IMouseButtonEvent.hpp create mode 100644 shared/graphics/events/IMouseEvent.hpp delete mode 100644 shared/graphics/events/key/AKeyEvent.hpp delete mode 100644 shared/graphics/events/key/KeyPressEvent.hpp delete mode 100644 shared/graphics/events/key/KeyReleaseEvent.hpp delete mode 100644 shared/graphics/events/mouse/AMouseButtonEvent.hpp delete mode 100644 shared/graphics/events/mouse/AMouseEvent.hpp delete mode 100644 shared/graphics/events/mouse/MouseButtonPressEvent.hpp delete mode 100644 shared/graphics/events/mouse/MouseButtonReleaseEvent.hpp delete mode 100644 shared/graphics/events/mouse/MouseMoveEvent.hpp delete mode 100644 shared/graphics/events/window/WindowCloseEvent.hpp delete mode 100644 shared/graphics/events/window/WindowResizeEvent.hpp delete mode 100644 shared/types/UUId.cpp delete mode 100644 shared/types/UUId.hpp delete mode 100644 shared/types/types.hpp diff --git a/core/src/core/Core.cpp b/core/src/core/Core.cpp index c6ddb9a..eded119 100644 --- a/core/src/core/Core.cpp +++ b/core/src/core/Core.cpp @@ -19,18 +19,22 @@ Core::Core(GameProviders gameProviders, GraphicsProviders graphicsProviders) Core::~Core() {} +void Core::_initGame() +{ + this->_game = this->_gameProvider.get()->createInstance(); +} + void Core::_initWindow() { - shared::graphics::WindowInitProps windowInitProps { - {0, 0}, - shared::graphics::WINDOWED, - 60, - "Arcade", - "icon" + auto gameManifest = this->_game.get()->getManifest(); + shared::graphics::IWindow::WindowInitProps windowInitProps { + this->_game.get()->getSize(), + shared::graphics::IWindow::WINDOWED, + this->_game.get()->getFps(), + gameManifest.name, + gameManifest.iconPath }; - this->_game = this->_gameProvider.get()->createInstance(); - windowInitProps.size = this->_game.get()->getSize(); this->_window = this->_graphicsProvider.get()->createWindow(windowInitProps); } @@ -41,38 +45,49 @@ std::shared_ptr Core::_getTexture(std::string bin, s return this->_textures[bin + ascii]; } -void Core::_renderDisplayableEntity(std::shared_ptr displayable) +shared::graphics::EntityProps Core::_getDisplayableEntity(std::shared_ptr displayable) { auto textureProps = displayable.get()->getTextureProps(); - shared::graphics::EntityTextureProps entityTextureProps{ + shared::graphics::EntityTextureProps entityTextureProps { this->_getTexture(textureProps.sources.bin, textureProps.sources.ascii), textureProps.sources.binTileSize, - textureProps.origin}; - shared::graphics::EntityProps entityProps{ + textureProps.origin + }; + shared::graphics::EntityProps entityProps { entityTextureProps, displayable.get()->getSize(), - displayable.get()->getPosition()}; - this->_window.get()->render(entityProps); + displayable.get()->getPosition() + }; + + return entityProps; } void Core::_renderEntities() { shared::games::entity::EntitiesMap entities = this->_game.get()->getEntities(); + std::map entitiesProps; for (auto &entity : entities) { - auto components = entity.second.get()->getComponents(); + auto components = entity.get()->getComponents(); for (auto &component : components) { - if (component.second.get()->getType() == shared::games::components::DISPLAYABLE) - this->_renderDisplayableEntity(std::dynamic_pointer_cast(component.second)); + if (component.get()->getType() == shared::games::components::DISPLAYABLE) { + auto displayable = std::dynamic_pointer_cast(component); + unsigned int index = displayable.get()->getZIndex(); + entitiesProps.insert(std::make_pair(index, this->_getDisplayableEntity(displayable))); + } } } + this->_window.get()->clear(); + for (auto &entity : entitiesProps) + this->_window.get()->render(entity.second); + this->_window.get()->display(); } void Core::run() { + this->_initGame(); this->_initWindow(); while (this->_window.get()->isOpen()) { - this->_window.get()->display(); - this->_window.get()->clear(); + this->_renderEntities(); } } diff --git a/core/src/core/Core.hpp b/core/src/core/Core.hpp index a95fb71..86e8762 100644 --- a/core/src/core/Core.hpp +++ b/core/src/core/Core.hpp @@ -7,6 +7,7 @@ #pragma once +#include #include "types/Providers.hpp" #include "shared/games/components/IDisplayableComponent.hpp" @@ -32,11 +33,17 @@ class Core { GraphicsProviders _graphicsProviders; /** - * @brief Initialize the core + * @brief Initialize the window * */ void _initWindow(); + /** + * @brief Initialize the game + * + */ + void _initGame(); + /** * @brief Render all entities * @@ -52,5 +59,11 @@ class Core { */ std::shared_ptr _getTexture(std::string bin, std::string ascii); - void _renderDisplayableEntity(std::shared_ptr displayable); + /** + * @brief Get a displayable entity + * + * @param displayable The displayable component + * @return The displayable entity + */ + shared::graphics::EntityProps _getDisplayableEntity(std::shared_ptr displayable); }; diff --git a/shared/games/IEntity.hpp b/shared/games/IEntity.hpp index e251824..9741616 100644 --- a/shared/games/IEntity.hpp +++ b/shared/games/IEntity.hpp @@ -7,11 +7,9 @@ #pragma once -#include +#include #include -#include "../types/UUId.hpp" - namespace shared::games { class IGame; @@ -20,14 +18,15 @@ namespace shared::games { class IEntity; - typedef std::map> EntitiesMap; + typedef std::shared_ptr EntityPtr; + typedef std::vector EntitiesMap; } namespace components { class IComponent; - typedef std::map> ComponentsMap; + typedef std::vector> ComponentsMap; } } @@ -36,13 +35,6 @@ class shared::games::entity::IEntity public: virtual ~IEntity() = default; - /** - * @brief Get the id of the entity - * - * @return Entity unique id - */ - virtual const types::UUId &getId(void) const noexcept = 0; - /** * @brief Get the components of the entity * diff --git a/shared/games/IGame.hpp b/shared/games/IGame.hpp index a187ce5..bc8353d 100644 --- a/shared/games/IGame.hpp +++ b/shared/games/IGame.hpp @@ -9,7 +9,7 @@ #include #include "IEntity.hpp" -#include "../types/types.hpp" +#include "../types/Vector.hpp" #include "types/GameManifest.hpp" using namespace shared::types; @@ -36,6 +36,7 @@ class shared::games::IGame /** * @brief Manifest with informations of the game * + * @return Manifest of the game */ virtual const GameManifest &getManifest() const noexcept = 0; @@ -43,6 +44,7 @@ class shared::games::IGame * @brief Number of tiles that represent the game * Tile size is managed by the renderer * + * @return The number of tiles of the game */ virtual const Vector2u getSize() const noexcept = 0; @@ -56,14 +58,7 @@ class shared::games::IGame /** * @brief Get map of entities * + * @return Entities map of the game */ virtual const entity::EntitiesMap &getEntities(void) const = 0; - - /** - * @brief Get entity by id - * - * @param id Id of the entity - * @return The specific entity - */ - virtual std::shared_ptr getEntityById(const UUId &id) const = 0; }; diff --git a/shared/games/components/IComponent.hpp b/shared/games/components/IComponent.hpp index 4ee1093..6f71dde 100644 --- a/shared/games/components/IComponent.hpp +++ b/shared/games/components/IComponent.hpp @@ -8,7 +8,6 @@ #pragma once #include "../IEntity.hpp" -#include "../../types/UUId.hpp" namespace shared::games::components { typedef enum { @@ -33,13 +32,6 @@ class shared::games::components::IComponent { */ virtual const ComponentType getType() const noexcept = 0; - /** - * @brief Get the uuid of component - * - * @return Component id - */ - virtual const types::UUId &getId() const noexcept = 0; - /** * @brief Get the parent entity of the component * diff --git a/shared/graphics/IGraphicsProvider.hpp b/shared/graphics/IGraphicsProvider.hpp index 2d2b7cf..85178d8 100644 --- a/shared/graphics/IGraphicsProvider.hpp +++ b/shared/graphics/IGraphicsProvider.hpp @@ -11,7 +11,7 @@ #include "ISound.hpp" #include "ITexture.hpp" -#include "window/IWindow.hpp" +#include "IWindow.hpp" #include "types/GraphicsManifest.hpp" namespace shared::graphics { @@ -22,7 +22,6 @@ class shared::graphics::IGraphicsProvider { public: virtual ~IGraphicsProvider() = default; - /** * @brief Get the manifest of the graphics library * @@ -31,12 +30,12 @@ class shared::graphics::IGraphicsProvider { virtual const GraphicsManifest &getManifest(void) const noexcept = 0; /** - * @brief Create a renderer object + * @brief Create a new window object * * @param windowProps Properties to use to init the window - * @return Created renderer object + * @return Created window object */ - virtual std::unique_ptr createWindow(const WindowInitProps &windowProps) = 0; + virtual std::unique_ptr createWindow(const IWindow::WindowInitProps &windowProps) = 0; /** * @brief Create a sound object diff --git a/shared/graphics/ISound.hpp b/shared/graphics/ISound.hpp index 96f192c..00da6af 100644 --- a/shared/graphics/ISound.hpp +++ b/shared/graphics/ISound.hpp @@ -10,20 +10,21 @@ namespace shared::graphics { class ISound; - - typedef unsigned char SoundVolume; - typedef enum - { - PLAY, - PAUSE, - STOP - } SoundState; } class shared::graphics::ISound { public: virtual ~ISound() = default; + typedef unsigned char SoundVolume; + + typedef enum + { + PLAY, + PAUSE, + STOP + } SoundState; + /** * @brief Get the state of the sound * diff --git a/shared/graphics/window/IWindow.hpp b/shared/graphics/IWindow.hpp similarity index 82% rename from shared/graphics/window/IWindow.hpp rename to shared/graphics/IWindow.hpp index 22f3fc3..09d1083 100644 --- a/shared/graphics/window/IWindow.hpp +++ b/shared/graphics/IWindow.hpp @@ -9,36 +9,35 @@ #include #include +#include -#include "../events/IEvent.hpp" -#include "../../types/types.hpp" -#include "../types/EntityProps.hpp" +#include "events/IEvent.hpp" +#include "types/EntityProps.hpp" using namespace shared::types; -namespace shared::graphics -{ +namespace shared::graphics { class IWindow; - - typedef enum - { - WINDOWED, - FULLSCREEN - } WindowMode; - - typedef struct { - Vector2u size; //Initial size of the window - WindowMode mode; //Initial mode of the window - unsigned int fps; //Initial framerate of the window - const std::string title; //Initial title of the window - const std::string icon; //Initial icon of the window - } WindowInitProps; } class shared::graphics::IWindow { public: virtual ~IWindow() = default; + typedef enum + { + WINDOWED, + FULLSCREEN + } WindowMode; + + typedef struct { + Vector2u size; //Initial size of the window + WindowMode mode; //Initial mode of the window + unsigned int fps; //Initial framerate of the window + const std::string title; //Initial title of the window + const std::string icon; //Initial icon of the window + } WindowInitProps; + /** * @brief Set the title of current window * @@ -150,5 +149,5 @@ class shared::graphics::IWindow { * but make another call `B` (directly after call `A`) `eventsB` * will result to an empty vector */ - virtual std::vector getEvents(void) = 0; + virtual std::vector getEvents(void) = 0; }; diff --git a/shared/graphics/events/IEvent.hpp b/shared/graphics/events/IEvent.hpp index ab455d0..52fa61b 100644 --- a/shared/graphics/events/IEvent.hpp +++ b/shared/graphics/events/IEvent.hpp @@ -21,6 +21,8 @@ namespace shared::graphics::events WINDOW_CLOSE, // Window closed WINDOW_RESIZE, // Window resized } EventType; + + typedef std::unique_ptr EventPtr; } class shared::graphics::events::IEvent @@ -31,5 +33,5 @@ class shared::graphics::events::IEvent /** * @brief Event type */ - virtual const EventType getType() const noexcept = 0; + virtual EventType getType() const noexcept = 0; }; diff --git a/shared/graphics/events/IKeyEvent.hpp b/shared/graphics/events/IKeyEvent.hpp new file mode 100644 index 0000000..db6cab4 --- /dev/null +++ b/shared/graphics/events/IKeyEvent.hpp @@ -0,0 +1,61 @@ +/* +** EPITECH PROJECT, 2024 +** arcade-shared +** File description: +** AKeyEvent +*/ + +#pragma once + +#include "IEvent.hpp" + +namespace shared::graphics::events { + class IKeyEvent; +} + +class shared::graphics::events::IKeyEvent : public IEvent { +public: + virtual ~IKeyEvent() = default; + + typedef enum { + CONTROL, // Control key (`Ctrl`, `Shift`, `Alt`) + ARROW, // Arrow key (`Up`, `Down`, `Left`, `Right`) + FUNC, // Function key (`F1`, `F2`, `F3`, etc.) + CHAR, // Character key (`a`, `1`, `&`, etc.) + UNKNOWN // Unknown key + } KeyType; + + typedef enum { + CTRL, // `Ctrl` key + SHIFT, // `Shift` key + ALT // `Alt` key + } ControlCode; + + typedef enum { + UP, // `Up` arrow key + DOWN, // `Down` arrow key + LEFT, // `Left` arrow key + RIGHT // `Right` arrow key + } ArrowCode; + + typedef union { + ControlCode control; // Control key + ArrowCode arrow; // Arrow key + char character; // ASCII character value + unsigned char func; // Function key number + } KeyCode; + + /** + * @brief Key code content + * + * @return Content of the key code + */ + virtual const KeyCode getKeyCode(void) const noexcept = 0; + + /** + * @brief Key type + * + * @return Type of the key pressed + */ + virtual const KeyType getKeyType(void) const noexcept = 0; +}; diff --git a/shared/graphics/events/IMouseButtonEvent.hpp b/shared/graphics/events/IMouseButtonEvent.hpp new file mode 100644 index 0000000..b70ab17 --- /dev/null +++ b/shared/graphics/events/IMouseButtonEvent.hpp @@ -0,0 +1,31 @@ +/* +** EPITECH PROJECT, 2024 +** arcade-shared +** File description: +** IMouseEvent +*/ + +#pragma once + +#include "IMouseEvent.hpp" + +namespace shared::graphics::events{ + class IMouseButtonEvent; +} + +class shared::graphics::events::IMouseButtonEvent : public IMouseEvent { +public: + virtual ~IMouseButtonEvent() = default; + + typedef enum { + LEFT, + RIGHT + } MouseButton; + + /** + * @brief Mouse button released + * + * @return Button released or pressed + */ + virtual const MouseButton getButton(void) const noexcept = 0; +}; diff --git a/shared/graphics/events/IMouseEvent.hpp b/shared/graphics/events/IMouseEvent.hpp new file mode 100644 index 0000000..fadf730 --- /dev/null +++ b/shared/graphics/events/IMouseEvent.hpp @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2024 +** arcade-shared +** File description: +** IMouseEvent +*/ + +#pragma once + +#include "../../types/Vector.hpp" +#include "IEvent.hpp" + +namespace shared::graphics::events { + class IMouseEvent; +} + +class shared::graphics::events::IMouseEvent : public IEvent { +public: + virtual ~IMouseEvent() = default; + + /** + * @brief Mouse position + * + * @return Position of the mouse + */ + virtual const shared::types::Vector2f getPosition(void) const noexcept = 0; +}; diff --git a/shared/graphics/events/key/AKeyEvent.hpp b/shared/graphics/events/key/AKeyEvent.hpp deleted file mode 100644 index ff4c436..0000000 --- a/shared/graphics/events/key/AKeyEvent.hpp +++ /dev/null @@ -1,87 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** AKeyEvent -*/ - -#pragma once - -#include "../IEvent.hpp" - -namespace shared::graphics::events { - template - class AKeyEvent; - - typedef enum - { - CONTROL, // Control key (`Ctrl`, `Shift`, `Alt`) - ARROW, // Arrow key (`Up`, `Down`, `Left`, `Right`) - FUNC, // Function key (`F1`, `F2`, `F3`, etc.) - CHAR, // Character key (`a`, `1`, `&`, etc.) - UNKNOWN // Unknown key - } KeyType; - - typedef enum - { - CTRL, // `Ctrl` key - SHIFT, // `Shift` key - ALT // `Alt` key - } ControlCode; - - typedef enum - { - UP, // `Up` arrow key - DOWN, // `Down` arrow key - LEFT, // `Left` arrow key - RIGHT // `Right` arrow key - } ArrowCode; - - typedef union - { - ControlCode control; // Control key - ArrowCode arrow; // Arrow key - char character; // ASCII character value - unsigned char func; // Function key number - } KeyCode; -} - -template -class shared::graphics::events::AKeyEvent: public IEvent { - public: - ~AKeyEvent() = default; - - /** - * @brief Event type - * - */ - const EventType getType(void) const noexcept - { - return this->_type; - } - - /** - * @brief Key code content - * - */ - const KeyCode getKeyCode(void) const noexcept - { - return this->_keyCode; - } - - /** - * @brief Key type - * - */ - const KeyType getKeyType(void) const noexcept - { - return this->_keyType; - } - - protected: - AKeyEvent(KeyType keyType, KeyCode keyCode) : _keyType(keyType), _keyCode(keyCode) {} - - EventType _type = T; - KeyType _keyType; - KeyCode _keyCode; -}; diff --git a/shared/graphics/events/key/KeyPressEvent.hpp b/shared/graphics/events/key/KeyPressEvent.hpp deleted file mode 100644 index 58016b8..0000000 --- a/shared/graphics/events/key/KeyPressEvent.hpp +++ /dev/null @@ -1,19 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** AKeyEvent -*/ - -#pragma once - -#include "AKeyEvent.hpp" - -namespace shared::graphics::events { - class KeyPressEvent; -} - -class shared::graphics::events::KeyPressEvent: public AKeyEvent { - public: - KeyPressEvent(KeyType keyType, KeyCode keyCode): AKeyEvent(keyType, keyCode) {} -}; diff --git a/shared/graphics/events/key/KeyReleaseEvent.hpp b/shared/graphics/events/key/KeyReleaseEvent.hpp deleted file mode 100644 index 8800f90..0000000 --- a/shared/graphics/events/key/KeyReleaseEvent.hpp +++ /dev/null @@ -1,18 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** IKeyEvent -*/ - -#pragma once - -#include "AKeyEvent.hpp" - -namespace shared::graphics::events { - class KeyReleaseEvent; -} -class shared::graphics::events::KeyReleaseEvent: public AKeyEvent { - public: - KeyReleaseEvent(KeyType keyType, KeyCode keyCode): AKeyEvent(keyType, keyCode) {} -}; diff --git a/shared/graphics/events/mouse/AMouseButtonEvent.hpp b/shared/graphics/events/mouse/AMouseButtonEvent.hpp deleted file mode 100644 index 17342c3..0000000 --- a/shared/graphics/events/mouse/AMouseButtonEvent.hpp +++ /dev/null @@ -1,39 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** IMouseEvent -*/ - -#pragma once - -#include "AMouseEvent.hpp" - -namespace shared::graphics::events -{ - template - class AMouseButtonEvent; -} - -template -class shared::graphics::events::AMouseButtonEvent: public AMouseEvent -{ - public: - ~AMouseButtonEvent() = default; - - /** - * @brief Mouse button released - * - */ - const MouseButton getButton(void) const noexcept { - return this->_button; - } - - protected: - AMouseButtonEvent( - MouseButton button, - types::Vector2f position - ): AMouseEvent(position), _button(button) {} - - MouseButton _button; -}; diff --git a/shared/graphics/events/mouse/AMouseEvent.hpp b/shared/graphics/events/mouse/AMouseEvent.hpp deleted file mode 100644 index 18bc030..0000000 --- a/shared/graphics/events/mouse/AMouseEvent.hpp +++ /dev/null @@ -1,50 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** IMouseEvent -*/ - -#pragma once - -#include "../../../types/types.hpp" -#include "../IEvent.hpp" - -namespace shared::graphics::events -{ - template - class AMouseEvent; - - typedef enum { - LEFT, - RIGHT - } MouseButton; -} - -template -class shared::graphics::events::AMouseEvent : public IEvent -{ - public: - ~AMouseEvent() = default; - - /** - * @brief Event type - * - */ - const EventType getType(void) const noexcept { - return T; - } - - /** - * @brief Mouse position - * - */ - const shared::types::Vector2f getPosition(void) const noexcept { - return this->_position; - } - - protected: - AMouseEvent(types::Vector2f position): _position(position) {} - - types::Vector2f _position; -}; diff --git a/shared/graphics/events/mouse/MouseButtonPressEvent.hpp b/shared/graphics/events/mouse/MouseButtonPressEvent.hpp deleted file mode 100644 index 3a92d1e..0000000 --- a/shared/graphics/events/mouse/MouseButtonPressEvent.hpp +++ /dev/null @@ -1,24 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** IMouseEvent -*/ - -#pragma once - -#include "AMouseButtonEvent.hpp" - -namespace shared::graphics::events -{ - class MouseButtonPressEvent; -} - -class shared::graphics::events::MouseButtonPressEvent : - public AMouseButtonEvent -{ - public: - MouseButtonPressEvent(MouseButton button, types::Vector2f position) - : AMouseButtonEvent(button, position) {} - ~MouseButtonPressEvent() = default; -}; diff --git a/shared/graphics/events/mouse/MouseButtonReleaseEvent.hpp b/shared/graphics/events/mouse/MouseButtonReleaseEvent.hpp deleted file mode 100644 index 960c6bc..0000000 --- a/shared/graphics/events/mouse/MouseButtonReleaseEvent.hpp +++ /dev/null @@ -1,24 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** IMouseEvent -*/ - -#pragma once - -#include "AMouseButtonEvent.hpp" - -namespace shared::graphics::events -{ - class MouseButtonReleaseEvent; -} - -class shared::graphics::events::MouseButtonReleaseEvent : - public AMouseButtonEvent -{ - public: - MouseButtonReleaseEvent(MouseButton button, types::Vector2f position) - : AMouseButtonEvent(button, position) {} - ~MouseButtonReleaseEvent() = default; -}; diff --git a/shared/graphics/events/mouse/MouseMoveEvent.hpp b/shared/graphics/events/mouse/MouseMoveEvent.hpp deleted file mode 100644 index c0be8db..0000000 --- a/shared/graphics/events/mouse/MouseMoveEvent.hpp +++ /dev/null @@ -1,22 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** IMouseEvent -*/ - -#pragma once - -#include "AMouseEvent.hpp" - -namespace shared::graphics::events -{ - class MouseMoveEvent; -} - -class shared::graphics::events::MouseMoveEvent : public AMouseEvent -{ - public: - MouseMoveEvent(types::Vector2f position) : AMouseEvent(position) {} - ~MouseMoveEvent() = default; -}; diff --git a/shared/graphics/events/window/WindowCloseEvent.hpp b/shared/graphics/events/window/WindowCloseEvent.hpp deleted file mode 100644 index dcf9d52..0000000 --- a/shared/graphics/events/window/WindowCloseEvent.hpp +++ /dev/null @@ -1,29 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** IWindowCloseEvent -*/ - -#pragma once - -#include "../IEvent.hpp" - -namespace shared::graphics::events { - class WindowCloseEvent; -} - -class shared::graphics::events::WindowCloseEvent: public IEvent { - public: - WindowCloseEvent() = default; - ~WindowCloseEvent() = default; - - /** - * @brief Event type - * - */ - const EventType getType() const noexcept - { - return WINDOW_CLOSE; - } -}; diff --git a/shared/graphics/events/window/WindowResizeEvent.hpp b/shared/graphics/events/window/WindowResizeEvent.hpp deleted file mode 100644 index e8bddce..0000000 --- a/shared/graphics/events/window/WindowResizeEvent.hpp +++ /dev/null @@ -1,43 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** IWindowResizeEvent -*/ - -#pragma once - -#include "../IEvent.hpp" -#include "../../../types/types.hpp" - -namespace shared::graphics::events { - class WindowResizeEvent; -} - -class shared::graphics::events::WindowResizeEvent: public IEvent { - public: - WindowResizeEvent(types::Vector2u newSize) : _newSize(newSize) {} - ~WindowResizeEvent() = default; - - /** - * @brief Event type - * - */ - const EventType getType() const noexcept - { - return WINDOW_RESIZE; - } - - /** - * @brief Get the new window size - * - * @return New window size - */ - const types::Vector2u &getNewSize() const noexcept - { - return this->_newSize; - } - - protected: - types::Vector2u _newSize; -}; diff --git a/shared/graphics/types/EntityProps.hpp b/shared/graphics/types/EntityProps.hpp index 2e3b8e5..88f9f89 100644 --- a/shared/graphics/types/EntityProps.hpp +++ b/shared/graphics/types/EntityProps.hpp @@ -10,7 +10,7 @@ #include #include "../ITexture.hpp" -#include "../../types/types.hpp" +#include "../../types/Vector.hpp" using namespace shared::types; diff --git a/shared/types/UUId.cpp b/shared/types/UUId.cpp deleted file mode 100644 index 87086f2..0000000 --- a/shared/types/UUId.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** UUId -*/ - -#include "UUId.hpp" - - -shared::types::UUId::UUId() -{ - uuid_generate(_uuid); -} - -shared::types::UUId::~UUId() -{ -} - -shared::types::UUId &shared::types::UUId::operator=(const UUId &other) -{ - if (this != &other) - uuid_copy(_uuid, other._uuid); - return *this; -} - -bool shared::types::UUId::operator==(const UUId &other) const -{ - return uuid_compare(_uuid, other._uuid) == 0; -} - -bool shared::types::UUId::operator!=(const UUId &other) const -{ - return uuid_compare(_uuid, other._uuid) != 0; -} - -bool shared::types::UUId::operator<(const UUId &other) const -{ - return uuid_compare(_uuid, other._uuid) < 0; -} - -bool shared::types::UUId::operator>(const UUId &other) const -{ - return uuid_compare(_uuid, other._uuid) > 0; -} - -bool shared::types::UUId::operator<=(const UUId &other) const -{ - return uuid_compare(_uuid, other._uuid) <= 0; -} - -bool shared::types::UUId::operator>=(const UUId &other) const -{ - return uuid_compare(_uuid, other._uuid) >= 0; -} - -std::string shared::types::UUId::toString() const -{ - char str[37]; - - uuid_unparse(_uuid, str); - return std::string(str); -} diff --git a/shared/types/UUId.hpp b/shared/types/UUId.hpp deleted file mode 100644 index 39ad2ad..0000000 --- a/shared/types/UUId.hpp +++ /dev/null @@ -1,89 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** UUId -*/ - -#pragma once - -#include - -#include - -namespace shared::types { - class UUId; -} - -class shared::types::UUId -{ - public: - UUId(void); - ~UUId(); - - /** - * @brief Assign same value to this UUId - * - * @param other UUId in whcih value is to be assigned - * @return Passed instance of UUId - */ - UUId &operator=(const UUId &other); - - /** - * @brief Equality operator - * - * @param other UUId to be compared - * @return Equality status - */ - bool operator==(const UUId &other) const; - - /** - * @brief Different operator - * - * @param other UUId to be compared - * @return Difference status - */ - bool operator!=(const UUId &other) const; - - /** - * @brief Operator to compare if current UUId is less than other UUId - * - * @param other Other UUId to be compared - * @return Status of comparison - */ - bool operator<(const UUId &other) const; - - /** - * @brief Operator to compare if current UUId is greter than other UUId - * - * @param other Other UUId to be compared - * @return Status of comparison - */ - bool operator>(const UUId &other) const; - - /** - * @brief Operator to compare if current UUId is less than or equal to other UUId - * - * @param other Other UUId to be compared - * @return Status of comparison - */ - bool operator<=(const UUId &other) const; - - /** - * @brief Operator to compare if current UUId is greater than or equal to other UUId - * - * @param other Other UUId to be compared - * @return Status of comparison - */ - bool operator>=(const UUId &other) const; - - /** - * @brief Convert UUId to string - * - * @return String representation of UUId - */ - std::string toString(void) const; - - private: - uuid_t _uuid; -}; diff --git a/shared/types/types.hpp b/shared/types/types.hpp deleted file mode 100644 index 2fa7a26..0000000 --- a/shared/types/types.hpp +++ /dev/null @@ -1,11 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** types -*/ - -#pragma once - -#include "Vector.hpp" -#include "UUId.hpp" From 83244edccf025fd2899750218b77e548471afc0d Mon Sep 17 00:00:00 2001 From: Yann Date: Wed, 27 Mar 2024 10:56:29 +0100 Subject: [PATCH 04/13] feat: compute game with deltaTime in run loop --- core/src/core/Core.cpp | 8 ++++++++ core/src/core/Core.hpp | 2 ++ 2 files changed, 10 insertions(+) diff --git a/core/src/core/Core.cpp b/core/src/core/Core.cpp index eded119..43429ba 100644 --- a/core/src/core/Core.cpp +++ b/core/src/core/Core.cpp @@ -5,6 +5,7 @@ ** Core */ +#include #include #include "Core.hpp" #include "shared/games/components/IComponent.hpp" @@ -85,9 +86,16 @@ void Core::_renderEntities() void Core::run() { + auto previousTime = std::chrono::high_resolution_clock::now(); + this->_initGame(); this->_initWindow(); while (this->_window.get()->isOpen()) { + auto currentTime = std::chrono::high_resolution_clock::now(); + auto deltaTime = std::chrono::duration_cast(previousTime - currentTime).count(); + previousTime = currentTime; + + this->_game.get()->compute(deltaTime); this->_renderEntities(); } } diff --git a/core/src/core/Core.hpp b/core/src/core/Core.hpp index 86e8762..118c76e 100644 --- a/core/src/core/Core.hpp +++ b/core/src/core/Core.hpp @@ -66,4 +66,6 @@ class Core { * @return The displayable entity */ shared::graphics::EntityProps _getDisplayableEntity(std::shared_ptr displayable); + + }; From 16843550440882df2921befc24596a6833a3c297 Mon Sep 17 00:00:00 2001 From: Yann Date: Wed, 27 Mar 2024 16:32:55 +0100 Subject: [PATCH 05/13] refactor: updated code structure from last shared refactor --- core/src/core/Core.cpp | 48 +++++++----- core/src/core/Core.hpp | 13 ++-- shared/games/components/IComponent.hpp | 43 +++++----- .../components/IDisplayableComponent.hpp | 78 +++++++------------ shared/games/components/ITextComponent.hpp | 53 +++++++++++++ shared/games/components/ITextureComponent.hpp | 38 +++++++++ shared/games/export.cpp.example | 11 ++- shared/graphics/IFont.hpp | 17 ++++ shared/graphics/IGraphicsProvider.hpp | 8 ++ shared/graphics/IWindow.hpp | 22 ++---- shared/graphics/events/IEvent.hpp | 4 +- shared/graphics/events/IMouseEvent.hpp | 2 +- shared/graphics/export.cpp.example | 13 ++-- shared/graphics/types/EntityProps.hpp | 32 -------- shared/graphics/types/TextProps.hpp | 45 +++++++++++ shared/graphics/types/TextureProps.hpp | 25 ++++++ shared/types/Color.hpp | 25 ++++++ 17 files changed, 325 insertions(+), 152 deletions(-) create mode 100644 shared/games/components/ITextComponent.hpp create mode 100644 shared/games/components/ITextureComponent.hpp create mode 100644 shared/graphics/IFont.hpp delete mode 100644 shared/graphics/types/EntityProps.hpp create mode 100644 shared/graphics/types/TextProps.hpp create mode 100644 shared/graphics/types/TextureProps.hpp create mode 100644 shared/types/Color.hpp diff --git a/core/src/core/Core.cpp b/core/src/core/Core.cpp index 43429ba..5bc454e 100644 --- a/core/src/core/Core.cpp +++ b/core/src/core/Core.cpp @@ -46,41 +46,50 @@ std::shared_ptr Core::_getTexture(std::string bin, s return this->_textures[bin + ascii]; } -shared::graphics::EntityProps Core::_getDisplayableEntity(std::shared_ptr displayable) +shared::graphics::TextureProps Core::_getTextureEntity(std::shared_ptr texture) { - auto textureProps = displayable.get()->getTextureProps(); - shared::graphics::EntityTextureProps entityTextureProps { + auto textureProps = texture.get()->getTextureProps(); + shared::graphics::TextureProps entityTextureProps { this->_getTexture(textureProps.sources.bin, textureProps.sources.ascii), textureProps.sources.binTileSize, - textureProps.origin - }; - shared::graphics::EntityProps entityProps { - entityTextureProps, - displayable.get()->getSize(), - displayable.get()->getPosition() + textureProps.origin, + texture.get()->getSize(), + texture.get()->getPosition() }; - return entityProps; + return entityTextureProps; } void Core::_renderEntities() { - shared::games::entity::EntitiesMap entities = this->_game.get()->getEntities(); - std::map entitiesProps; + std::map> entitiesTextureProps; + std::map> entitiesTextProps; - for (auto &entity : entities) { + for (auto &entity : this->_gameEntities) { auto components = entity.get()->getComponents(); for (auto &component : components) { - if (component.get()->getType() == shared::games::components::DISPLAYABLE) { - auto displayable = std::dynamic_pointer_cast(component); - unsigned int index = displayable.get()->getZIndex(); - entitiesProps.insert(std::make_pair(index, this->_getDisplayableEntity(displayable))); + if (component.get()->getType() == shared::games::components::TEXTURE) { + auto texture = std::dynamic_pointer_cast(component); + unsigned int index = texture.get()->getZIndex(); + entitiesTextureProps[index].push_back(this->_getTextureEntity(texture)); } } } this->_window.get()->clear(); - for (auto &entity : entitiesProps) - this->_window.get()->render(entity.second); + auto textPropsIt = entitiesTextProps.begin(); + auto texturePropsIt = entitiesTextureProps.begin(); + while (texturePropsIt != entitiesTextureProps.end() || textPropsIt != entitiesTextProps.end()) { + if (texturePropsIt != entitiesTextureProps.end()) { + for (auto &textureProps : texturePropsIt->second) + this->_window.get()->render(textureProps); + texturePropsIt++; + } + if (textPropsIt != entitiesTextProps.end()) { + for (auto &textProps : textPropsIt->second) + this->_window.get()->render(textProps); + textPropsIt++; + } + } this->_window.get()->display(); } @@ -96,6 +105,7 @@ void Core::run() previousTime = currentTime; this->_game.get()->compute(deltaTime); + this->_gameEntities = this->_game.get()->getEntities(); this->_renderEntities(); } } diff --git a/core/src/core/Core.hpp b/core/src/core/Core.hpp index 118c76e..77d7487 100644 --- a/core/src/core/Core.hpp +++ b/core/src/core/Core.hpp @@ -9,6 +9,8 @@ #include #include "types/Providers.hpp" +#include "shared/games/components/ITextComponent.hpp" +#include "shared/games/components/ITextureComponent.hpp" #include "shared/games/components/IDisplayableComponent.hpp" class Core { @@ -31,6 +33,7 @@ class Core { std::map> _textures; GameProviders _gameProviders; GraphicsProviders _graphicsProviders; + shared::games::entity::EntitiesMap _gameEntities; /** * @brief Initialize the window @@ -60,12 +63,10 @@ class Core { std::shared_ptr _getTexture(std::string bin, std::string ascii); /** - * @brief Get a displayable entity + * @brief Get the texture entity * - * @param displayable The displayable component - * @return The displayable entity + * @param texture The texture component + * @return The texture entity */ - shared::graphics::EntityProps _getDisplayableEntity(std::shared_ptr displayable); - - + shared::graphics::TextureProps _getTextureEntity(std::shared_ptr texture); }; diff --git a/shared/games/components/IComponent.hpp b/shared/games/components/IComponent.hpp index 6f71dde..e03cc51 100644 --- a/shared/games/components/IComponent.hpp +++ b/shared/games/components/IComponent.hpp @@ -10,32 +10,33 @@ #include "../IEntity.hpp" namespace shared::games::components { - typedef enum { - DISPLAYABLE, - SOUND, - COLLIDABLE, - POSITION, - KEYBOARD - } ComponentType; + typedef enum { + TEXTURE, + TEXT, + SOUND, + COLLIDABLE, + POSITION, + KEYBOARD + } ComponentType; - class IComponent; + class IComponent; } class shared::games::components::IComponent { public: - virtual ~IComponent() = default; + virtual ~IComponent() = default; - /** - * @brief Get the type of the component - * - * @return Type of the component - */ - virtual const ComponentType getType() const noexcept = 0; + /** + * @brief Get the type of the component + * + * @return Type of the component + */ + virtual const ComponentType getType() const noexcept = 0; - /** - * @brief Get the parent entity of the component - * - * @return Entity of the component - */ - virtual const entity::IEntity &getEntity() noexcept = 0; + /** + * @brief Get the parent entity of the component + * + * @return Entity of the component + */ + virtual const entity::IEntity &getEntity() noexcept = 0; }; diff --git a/shared/games/components/IDisplayableComponent.hpp b/shared/games/components/IDisplayableComponent.hpp index c492e3b..0a60cec 100644 --- a/shared/games/components/IDisplayableComponent.hpp +++ b/shared/games/components/IDisplayableComponent.hpp @@ -1,8 +1,8 @@ /* ** EPITECH PROJECT, 2024 -** arcade-shared [WSL: Ubuntu-22.04] +** arcade-shared ** File description: -** ADisplaybleComponent +** IDisplaybleComponent */ #pragma once @@ -12,60 +12,40 @@ #include "../../types/Vector.hpp" namespace shared::games::components { - class IDisplayableComponent; - - typedef struct - { - const std::string ascii; // ASCII image representation path - const std::string bin; // Binary image path - Vector2f binTileSize; // Size of the binary tile - } TextureSources; - - typedef struct - { - TextureSources sources; // Sources of textures - Vector2u origin; // Origin of the texture - } TextureProps; + class IDisplayableComponent; } -class shared::games::components::IDisplayableComponent: public virtual IPositionComponent -{ +class shared::games::components::IDisplayableComponent : public virtual IPositionComponent { public: - virtual ~IDisplayableComponent() = default; + virtual ~IDisplayableComponent() = default; /** - * @brief Get size of the entity (tiles) - * - */ - virtual Vector2u &getSize(void) noexcept = 0; + * @brief Get size of the entity (tiles) + * + */ + virtual Vector2u &getSize() noexcept = 0; - /** - * @brief Get Z index that is usefull for display prioroty - * - */ - virtual unsigned int &getZIndex(void) noexcept = 0; - - /** - * @brief Get texture properties - * - */ - virtual TextureProps &getTextureProps(void) noexcept = 0; + /** + * @brief Get Z index that is usefull for display prioroty + * + */ + virtual unsigned int &getZIndex() noexcept = 0; - /** - * @brief On click event handler for the entity - * @param ctx Context of the game - */ - virtual void onMousePress(std::shared_ptr &ctx) = 0; + /** + * @brief On click event handler for the entity + * @param ctx Context of the game + */ + virtual void onMousePress(std::shared_ptr &ctx) = 0; - /** - * @brief On release event handler for the entity - * @param ctx Context of the game - */ - virtual void onMouseRelease(std::shared_ptr &ctx) = 0; + /** + * @brief On release event handler for the entity + * @param ctx Context of the game + */ + virtual void onMouseRelease(std::shared_ptr &ctx) = 0; - /** - * @brief On hover event handler for the entity - * @param ctx Context of the game - */ - virtual void onMouseHover(std::shared_ptr &ctx) = 0; + /** + * @brief On hover event handler for the entity + * @param ctx Context of the game + */ + virtual void onMouseHover(std::shared_ptr &ctx) = 0; }; diff --git a/shared/games/components/ITextComponent.hpp b/shared/games/components/ITextComponent.hpp new file mode 100644 index 0000000..c0a269c --- /dev/null +++ b/shared/games/components/ITextComponent.hpp @@ -0,0 +1,53 @@ +/* +** EPITECH PROJECT, 2024 +** ITextComponent.hpp +** File description: +** ITextComponent class +*/ + +#pragma once + +#include "IDisplayableComponent.hpp" +#include "../../types/Vector.hpp" +#include "../../types/Color.hpp" + +namespace shared::games::components { + class ITextComponent; + + typedef enum { + LEFT, + CENTER, + RIGHT + } TextAlign; + + typedef enum { + BOTTOM, + MIDDLE, + TOP + } TextVerticalAlign; + + typedef struct { + std::string path; // Path of the font + types::Vector2u size; // Font size + } TextFontProps; + + typedef struct { + std::string content; // Content of the text + TextAlign align; // Alignment of the text + TextVerticalAlign verticalAlign; // Vertical alignment of the text + TextFontProps font; // Font of the text + types::Color color; // Color of the text + } TextProps; +} + +class shared::games::components::ITextComponent : public virtual IDisplayableComponent { +public: + virtual ~ITextComponent() = default; + + /** + * @brief Get text props of the entity + * + * @return text props + */ + virtual TextProps getTextProps() noexcept = 0; +}; diff --git a/shared/games/components/ITextureComponent.hpp b/shared/games/components/ITextureComponent.hpp new file mode 100644 index 0000000..930215c --- /dev/null +++ b/shared/games/components/ITextureComponent.hpp @@ -0,0 +1,38 @@ +/* +** EPITECH PROJECT, 2024 +** arcade-shared +** File description: +** ITextureComponent +*/ + +#pragma once + +#include "IDisplayableComponent.hpp" +#include "../IGame.hpp" +#include "../../types/Vector.hpp" + +namespace shared::games::components { + class ITextureComponent; + + typedef struct { + const std::string ascii; // ASCII image representation path + const std::string bin; // Binary image path + Vector2f binTileSize; // Size of the binary tile + } TextureSources; + + typedef struct { + TextureSources sources; // Sources of textures + Vector2u origin; // Origin of the texture + } TextureProps; +} + +class shared::games::components::ITextureComponent : public virtual IDisplayableComponent { +public: + virtual ~ITextureComponent() = default; + + /** + * @brief Get texture properties + * + */ + virtual TextureProps &getTextureProps() noexcept = 0; +}; diff --git a/shared/games/export.cpp.example b/shared/games/export.cpp.example index 12b4ae3..5dd7c4a 100644 --- a/shared/games/export.cpp.example +++ b/shared/games/export.cpp.example @@ -8,14 +8,17 @@ #include "IGame.hpp" #include "../types/Libraries.hpp" +using namespace shared::games; +using namespace shared::types; + extern "C" { - shared::types::LibraryType SHARED_LIBRARY_TYPE_GETTER_NAME(void) + LibraryType SHARED_LIBRARY_TYPE_GETTER_NAME(void) { - return shared::types::LibraryType::GAME; + return LibraryType::GAME; } - std::shared_ptr SHARED_GAME_PROVIDER_LOADER_NAME(void) + IGameProvider* SHARED_GAME_PROVIDER_LOADER_NAME(void) { - return std::make_shared(...) + return new YOUR_CLASS(); } } diff --git a/shared/graphics/IFont.hpp b/shared/graphics/IFont.hpp new file mode 100644 index 0000000..63b7829 --- /dev/null +++ b/shared/graphics/IFont.hpp @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2024 +** arcade-shared +** File description: +** IFont +*/ + +#pragma once + +namespace shared::graphics { + class IFont; +} + +class shared::graphics::IFont { + public: + virtual ~IFont() = default; +}; diff --git a/shared/graphics/IGraphicsProvider.hpp b/shared/graphics/IGraphicsProvider.hpp index 85178d8..4200a5c 100644 --- a/shared/graphics/IGraphicsProvider.hpp +++ b/shared/graphics/IGraphicsProvider.hpp @@ -53,4 +53,12 @@ class shared::graphics::IGraphicsProvider { * @return Created texture object */ virtual std::shared_ptr createTexture(const std::string &bin, const std::string &ascii) = 0; + + /** + * @brief Create a font object + * + * @param path Path of the font file + * @return Created font object + */ + virtual std::shared_ptr createFont(const std::string &path) = 0; }; diff --git a/shared/graphics/IWindow.hpp b/shared/graphics/IWindow.hpp index 09d1083..ac1746a 100644 --- a/shared/graphics/IWindow.hpp +++ b/shared/graphics/IWindow.hpp @@ -12,7 +12,8 @@ #include #include "events/IEvent.hpp" -#include "types/EntityProps.hpp" +#include "types/TextureProps.hpp" +#include "types/TextProps.hpp" using namespace shared::types; @@ -45,13 +46,6 @@ class shared::graphics::IWindow { */ virtual void setTitle(const std::string &title) = 0; - /** - * @brief Get the title of current window - * - * @return Title of the window - */ - virtual std::string getTitle() const = 0; - /** * @brief Set the size of the window * @@ -102,18 +96,18 @@ class shared::graphics::IWindow { virtual void setIcon(const std::string &icon) = 0; /** - * @brief Get the icon of the window + * @brief Render the texture of entity with given properties * - * @return Icon object of the window + * @param props Properties of the entity & texture to render */ - virtual const std::string &getIcon(void) const = 0; + virtual void render(const TextureProps &props) = 0; /** - * @brief Render the entity with given properties + * @brief Render the text of entity with given properties * - * @param props Properties of the entity to render + * @param props Properties of the entity & text to render */ - virtual void render(const EntityProps &props) = 0; + virtual void render(const TextProps &props) = 0; /** * @brief Clear the content of the window diff --git a/shared/graphics/events/IEvent.hpp b/shared/graphics/events/IEvent.hpp index 52fa61b..84c9492 100644 --- a/shared/graphics/events/IEvent.hpp +++ b/shared/graphics/events/IEvent.hpp @@ -7,6 +7,8 @@ #pragma once +#include + namespace shared::graphics::events { class IEvent; @@ -22,7 +24,7 @@ namespace shared::graphics::events WINDOW_RESIZE, // Window resized } EventType; - typedef std::unique_ptr EventPtr; + typedef std::shared_ptr EventPtr; } class shared::graphics::events::IEvent diff --git a/shared/graphics/events/IMouseEvent.hpp b/shared/graphics/events/IMouseEvent.hpp index fadf730..a85ae4b 100644 --- a/shared/graphics/events/IMouseEvent.hpp +++ b/shared/graphics/events/IMouseEvent.hpp @@ -23,5 +23,5 @@ class shared::graphics::events::IMouseEvent : public IEvent { * * @return Position of the mouse */ - virtual const shared::types::Vector2f getPosition(void) const noexcept = 0; + virtual const shared::types::Vector2i getPosition(void) const noexcept = 0; }; diff --git a/shared/graphics/export.cpp.example b/shared/graphics/export.cpp.example index d3e00de..2a6ebf5 100644 --- a/shared/graphics/export.cpp.example +++ b/shared/graphics/export.cpp.example @@ -5,17 +5,20 @@ ** export */ -#include "IGraphicsFactory.hpp" +#include "IGraphicsProvider.hpp" #include "../types/Libraries.hpp" +using namespace shared::graphics; +using namespace shared::types; + extern "C" { - shared::types::LibraryType SHARED_LIBRARY_TYPE_GETTER_NAME(void) + LibraryType SHARED_LIBRARY_TYPE_GETTER_NAME(void) { - return shared::types::LibraryType::GRAPHIC; + return LibraryType::GRAPHIC; } - std::shared_ptr SHARED_GRAPHICS_PROVIDER_LOADER_NAME(void) + IGraphicsProvider *SHARED_GRAPHICS_PROVIDER_LOADER_NAME(void) { - return std::make_shared(...); + return new YOUR_CLASS(); } } diff --git a/shared/graphics/types/EntityProps.hpp b/shared/graphics/types/EntityProps.hpp deleted file mode 100644 index 88f9f89..0000000 --- a/shared/graphics/types/EntityProps.hpp +++ /dev/null @@ -1,32 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** Texture -*/ - -#pragma once - -#include - -#include "../ITexture.hpp" -#include "../../types/Vector.hpp" - -using namespace shared::types; - -namespace shared::graphics -{ - typedef struct - { - const std::shared_ptr texture; // Texture of the entity - const Vector2f binTileSize; // Size of a binary tile - const Vector2u origin; // Origin of the texture - } EntityTextureProps; - - typedef struct - { - EntityTextureProps textureProps; // Properties to use with the texture for the entity - Vector2u size; // Size of the entity - Vector2i position; // Position of the entity - } EntityProps; -} diff --git a/shared/graphics/types/TextProps.hpp b/shared/graphics/types/TextProps.hpp new file mode 100644 index 0000000..2d7187a --- /dev/null +++ b/shared/graphics/types/TextProps.hpp @@ -0,0 +1,45 @@ +/* +** EPITECH PROJECT, 2024 +** arcade-shared +** File description: +** Text +*/ + +#pragma once + +#include + +#include "../IFont.hpp" +#include "../../types/Vector.hpp" +#include "../../types/Color.hpp" + +using namespace shared::types; + +namespace shared::graphics { + typedef enum { + LEFT, + CENTER, + RIGHT + } TextAlign; + + typedef enum { + BOTTOM, + MIDDLE, + TOP + } TextVerticalAlign; + + typedef struct { + std::string path; // Path of the font + types::Vector2u size; // Font size + } TextFontProps; + + typedef struct { + std::shared_ptr font; // Font of the text + std::string content; // Content of the text + TextAlign align; // Alignment of the text + TextVerticalAlign verticalAlign; // Vertical alignment of the text + types::Color color; // Color of the text + Vector2u size; // Size of the entity + Vector2i position; // Position of the entity + } TextProps; +} diff --git a/shared/graphics/types/TextureProps.hpp b/shared/graphics/types/TextureProps.hpp new file mode 100644 index 0000000..5847f37 --- /dev/null +++ b/shared/graphics/types/TextureProps.hpp @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2024 +** arcade-shared +** File description: +** Texture +*/ + +#pragma once + +#include + +#include "../ITexture.hpp" +#include "../../types/Vector.hpp" + +using namespace shared::types; + +namespace shared::graphics { + typedef struct { + std::shared_ptr texture; // Texture of the entity + Vector2f binTileSize; // Size of a binary tile + Vector2u origin; // Origin of the texture + Vector2u size; // Size of the entity + Vector2i position; // Position of the entity + } TextureProps; +} diff --git a/shared/types/Color.hpp b/shared/types/Color.hpp new file mode 100644 index 0000000..b6fe6e2 --- /dev/null +++ b/shared/types/Color.hpp @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2024 +** Color.hpp +** File description: +** Color class +*/ + +#pragma once + +namespace shared::types +{ + typedef struct ColorType { + ColorType( + unsigned char r, + unsigned char g, + unsigned char b, + unsigned char a + ) : r(r), g(g), b(b), a(a) {} + + unsigned char r; + unsigned char g; + unsigned char b; + unsigned char a; + } Color; +} From 23d99dc2f1ab6acf5d9f5876c7461a51fc19c927 Mon Sep 17 00:00:00 2001 From: Yann Date: Wed, 27 Mar 2024 17:34:03 +0100 Subject: [PATCH 06/13] feat(core): init methods for text Entities --- core/src/core/Core.cpp | 7 +++++++ core/src/core/Core.hpp | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/core/src/core/Core.cpp b/core/src/core/Core.cpp index 5bc454e..864fcfb 100644 --- a/core/src/core/Core.cpp +++ b/core/src/core/Core.cpp @@ -46,6 +46,13 @@ std::shared_ptr Core::_getTexture(std::string bin, s return this->_textures[bin + ascii]; } +std::shared_ptr Core::_getFont(std::string path) +{ + if (this->_fonts.find(path) == this->_fonts.end()) + this->_fonts[path] = this->_graphicsProvider.get()->createFont(path); + return this->_fonts[path]; +} + shared::graphics::TextureProps Core::_getTextureEntity(std::shared_ptr texture) { auto textureProps = texture.get()->getTextureProps(); diff --git a/core/src/core/Core.hpp b/core/src/core/Core.hpp index 77d7487..3ecc8b9 100644 --- a/core/src/core/Core.hpp +++ b/core/src/core/Core.hpp @@ -30,6 +30,7 @@ class Core { std::shared_ptr _window; std::shared_ptr _gameProvider; std::shared_ptr _graphicsProvider; + std::map> _fonts; std::map> _textures; GameProviders _gameProviders; GraphicsProviders _graphicsProviders; @@ -62,6 +63,14 @@ class Core { */ std::shared_ptr _getTexture(std::string bin, std::string ascii); + /** + * @brief Get a font + * + * @param path Path to the font file + * @return The correct font + */ + std::shared_ptr _getFont(std::string path); + /** * @brief Get the texture entity * @@ -69,4 +78,12 @@ class Core { * @return The texture entity */ shared::graphics::TextureProps _getTextureEntity(std::shared_ptr texture); + + /** + * @brief Get the text entity + * + * @param text The text component + * @return The text entity + */ + shared::graphics::TextProps _getTextEntity(std::shared_ptr text); }; From 14bf683ca8f9115624c990adc0d9ec23c4403f04 Mon Sep 17 00:00:00 2001 From: Yann Date: Fri, 29 Mar 2024 18:50:08 +0100 Subject: [PATCH 07/13] feat(core): add textProps in render --- core/src/core/CMakeLists.txt | 3 +++ shared/graphics/exceptions/IFontException.hpp | 19 +++++++++++++++ .../exceptions/IGraphicsException.hpp | 23 +++++++++++++++++++ .../graphics/exceptions/ISoundException.hpp | 19 +++++++++++++++ .../graphics/exceptions/ITextureException.hpp | 19 +++++++++++++++ .../graphics/exceptions/IWindowException.hpp | 19 +++++++++++++++ 6 files changed, 102 insertions(+) create mode 100644 core/src/core/CMakeLists.txt create mode 100644 shared/graphics/exceptions/IFontException.hpp create mode 100644 shared/graphics/exceptions/IGraphicsException.hpp create mode 100644 shared/graphics/exceptions/ISoundException.hpp create mode 100644 shared/graphics/exceptions/ITextureException.hpp create mode 100644 shared/graphics/exceptions/IWindowException.hpp diff --git a/core/src/core/CMakeLists.txt b/core/src/core/CMakeLists.txt new file mode 100644 index 0000000..481f307 --- /dev/null +++ b/core/src/core/CMakeLists.txt @@ -0,0 +1,3 @@ +target_sources(${CMAKE_PROJECT_NAME} PRIVATE + Core.cpp +) diff --git a/shared/graphics/exceptions/IFontException.hpp b/shared/graphics/exceptions/IFontException.hpp new file mode 100644 index 0000000..7b875e6 --- /dev/null +++ b/shared/graphics/exceptions/IFontException.hpp @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2024 +** arcade-shared +** File description: +** IFontException.hpp +*/ + +#pragma once + +#include "IGraphicsException.hpp" + +namespace shared::graphics::exceptions { + class IFontException; +} + +class shared::graphics::exceptions::IFontException : public virtual IGraphicsException { +public: + virtual ~IFontException() = default; +}; diff --git a/shared/graphics/exceptions/IGraphicsException.hpp b/shared/graphics/exceptions/IGraphicsException.hpp new file mode 100644 index 0000000..a899775 --- /dev/null +++ b/shared/graphics/exceptions/IGraphicsException.hpp @@ -0,0 +1,23 @@ +/* +** EPITECH PROJECT, 2024 +** arcade-shared +** File description: +** IGraphicsException.hpp +*/ + +#pragma once + +#include + +namespace shared::graphics::exceptions { + class IGraphicsException; +} + +class shared::graphics::exceptions::IGraphicsException: public std::exception { +public: + /** + * @brief Get error location + * @return String containing error location + */ + virtual const char *where() const noexcept = 0; +}; diff --git a/shared/graphics/exceptions/ISoundException.hpp b/shared/graphics/exceptions/ISoundException.hpp new file mode 100644 index 0000000..2a2e534 --- /dev/null +++ b/shared/graphics/exceptions/ISoundException.hpp @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2024 +** arcade-shared +** File description: +** ISoundException.hpp +*/ + +#pragma once + +#include "IGraphicsException.hpp" + +namespace shared::graphics::exceptions { + class ISoundException; +} + +class shared::graphics::exceptions::ISoundException : public virtual IGraphicsException { +public: + virtual ~ISoundException() = default; +}; diff --git a/shared/graphics/exceptions/ITextureException.hpp b/shared/graphics/exceptions/ITextureException.hpp new file mode 100644 index 0000000..e894b71 --- /dev/null +++ b/shared/graphics/exceptions/ITextureException.hpp @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2024 +** arcade-shared +** File description: +** ITextureException.hpp +*/ + +#pragma once + +#include "IGraphicsException.hpp" + +namespace shared::graphics::exceptions { + class ITextureException; +} + +class shared::graphics::exceptions::ITextureException : public virtual IGraphicsException { +public: + virtual ~ITextureException() = default; +}; diff --git a/shared/graphics/exceptions/IWindowException.hpp b/shared/graphics/exceptions/IWindowException.hpp new file mode 100644 index 0000000..ae030f3 --- /dev/null +++ b/shared/graphics/exceptions/IWindowException.hpp @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2024 +** arcade-shared +** File description: +** IWindowException.hpp +*/ + +#pragma once + +#include "IGraphicsException.hpp" + +namespace shared::graphics::exceptions { + class IWindowException; +} + +class shared::graphics::exceptions::IWindowException : public virtual IGraphicsException { +public: + virtual ~IWindowException() = default; +}; From 193b45b332a4138d634d1fc35abb5c80e6ee06f8 Mon Sep 17 00:00:00 2001 From: Yann Date: Fri, 29 Mar 2024 18:52:36 +0100 Subject: [PATCH 08/13] feat(core): render props --- core/main.cpp | 3 + core/src/CMakeLists.txt | 1 + core/src/core/CMakeLists.txt | 1 + core/src/core/Core.cpp | 104 +++++++++++++----- core/src/core/Core.hpp | 50 +++++++-- core/src/exception/CMakeLists.txt | 1 + core/src/loader/CMakeLists.txt | 1 + core/src/loader/Loader.cpp | 4 +- core/src/loader/Loader.hpp | 4 +- core/src/types/CMakeLists.txt | 3 + shared/games/IGame.hpp | 3 +- .../games/components/IKeyboardComponent.hpp | 8 +- shared/games/components/ITextComponent.hpp | 6 +- shared/graphics/events/IKeyEvent.hpp | 2 +- shared/graphics/events/IMouseButtonEvent.hpp | 2 +- shared/graphics/events/IMouseEvent.hpp | 2 +- 16 files changed, 138 insertions(+), 57 deletions(-) create mode 100644 core/src/types/CMakeLists.txt diff --git a/core/main.cpp b/core/main.cpp index 4c9f92b..5a3dfe3 100644 --- a/core/main.cpp +++ b/core/main.cpp @@ -5,6 +5,7 @@ ** main */ +#include "core/Core.hpp" #include "loader/Loader.hpp" int main(void) @@ -15,6 +16,8 @@ int main(void) loader.loadLibraries("./lib"); std::cout << "Games libraries:" << loader.getGamesLibraries().size() << std::endl; std::cout << "Graphics libraries:" << loader.getGraphicsLibraries().size() << std::endl; + Core core(loader.getGamesLibraries(), loader.getGraphicsLibraries()); + core.run(); } catch (const std::exception &e) { std::cerr << e.what() << std::endl; } diff --git a/core/src/CMakeLists.txt b/core/src/CMakeLists.txt index ea744bc..b777fc8 100644 --- a/core/src/CMakeLists.txt +++ b/core/src/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(exception) add_subdirectory(loader) add_subdirectory(utils) +add_subdirectory(core) diff --git a/core/src/core/CMakeLists.txt b/core/src/core/CMakeLists.txt index 481f307..fded451 100644 --- a/core/src/core/CMakeLists.txt +++ b/core/src/core/CMakeLists.txt @@ -1,3 +1,4 @@ target_sources(${CMAKE_PROJECT_NAME} PRIVATE Core.cpp + Core.hpp ) diff --git a/core/src/core/Core.cpp b/core/src/core/Core.cpp index 864fcfb..5c4b2aa 100644 --- a/core/src/core/Core.cpp +++ b/core/src/core/Core.cpp @@ -10,13 +10,9 @@ #include "Core.hpp" #include "shared/games/components/IComponent.hpp" -Core::Core(GameProviders gameProviders, GraphicsProviders graphicsProviders) -{ - this->_gameProviders = gameProviders; - this->_graphicsProviders = graphicsProviders; - this->_gameProvider = this->_gameProviders.at(0); - this->_graphicsProvider = this->_graphicsProviders.at(0); -} +Core::Core(GameProviders &gameProviders, GraphicsProviders &graphicsProviders) : + _gameProviders(gameProviders), _graphicsProviders(graphicsProviders), + _gameProvider(gameProviders.at(0)), _graphicsProvider(graphicsProviders.at(0)) {} Core::~Core() {} @@ -28,9 +24,9 @@ void Core::_initGame() void Core::_initWindow() { auto gameManifest = this->_game.get()->getManifest(); - shared::graphics::IWindow::WindowInitProps windowInitProps { + IWindow::WindowInitProps windowInitProps { this->_game.get()->getSize(), - shared::graphics::IWindow::WINDOWED, + IWindow::WINDOWED, this->_game.get()->getFps(), gameManifest.name, gameManifest.iconPath @@ -39,24 +35,24 @@ void Core::_initWindow() this->_window = this->_graphicsProvider.get()->createWindow(windowInitProps); } -std::shared_ptr Core::_getTexture(std::string bin, std::string ascii) +std::shared_ptr Core::_getTexture(std::string bin, std::string ascii) { if (this->_textures.find(bin + ascii) == this->_textures.end()) this->_textures[bin + ascii] = this->_graphicsProvider.get()->createTexture(bin, ascii); return this->_textures[bin + ascii]; } -std::shared_ptr Core::_getFont(std::string path) +std::shared_ptr Core::_getFont(std::string path) { if (this->_fonts.find(path) == this->_fonts.end()) this->_fonts[path] = this->_graphicsProvider.get()->createFont(path); return this->_fonts[path]; } -shared::graphics::TextureProps Core::_getTextureEntity(std::shared_ptr texture) +TextureProps Core::_getTextureEntity(std::shared_ptr texture) { auto textureProps = texture.get()->getTextureProps(); - shared::graphics::TextureProps entityTextureProps { + TextureProps entityTextureProps { this->_getTexture(textureProps.sources.bin, textureProps.sources.ascii), textureProps.sources.binTileSize, textureProps.origin, @@ -67,10 +63,66 @@ shared::graphics::TextureProps Core::_getTextureEntity(std::shared_ptr text) +{ + auto textProps = text->getTextProps(); + TextProps entityTextProps { + this->_getFont(textProps.font.path), + textProps.font.size, + textProps.content, + static_cast(textProps.align), + static_cast(textProps.verticalAlign), + textProps.color, + text->getSize(), + text->getPosition() + }; + + return entityTextProps; +} + +void Core::_renderTextureProps(std::map> &textures, std::map>::iterator &texturePropsIt) +{ + if (texturePropsIt == textures.end()) + return; + for (auto &textureProps : texturePropsIt->second) + this->_window.get()->render(textureProps); + texturePropsIt++; +} + +void Core::_renderTextProps(std::map> &texts, std::map>::iterator &textPropsIt) +{ + if (textPropsIt == texts.end()) + return; + for (auto &textureProps : textPropsIt->second) + this->_window.get()->render(textureProps); + textPropsIt++; +} + +void Core::_renderProps(std::map> &textures, std::map> &texts) +{ + auto textPropsIt = texts.begin(); + auto texturePropsIt = textures.begin(); + + while (texturePropsIt != textures.end() || textPropsIt != texts.end()) { + if (textPropsIt != texts.end()) { + if (texturePropsIt->first <= textPropsIt->first) + this->_renderTextureProps(textures, texturePropsIt); + } else { + this->_renderTextureProps(textures, texturePropsIt); + } + if (texturePropsIt != textures.end()) { + if (textPropsIt->first <= texturePropsIt->first) + this->_renderTextProps(texts, textPropsIt); + } else { + this->_renderTextProps(texts, textPropsIt); + } + } +} + void Core::_renderEntities() { - std::map> entitiesTextureProps; - std::map> entitiesTextProps; + std::map> entitiesTextureProps; + std::map> entitiesTextProps; for (auto &entity : this->_gameEntities) { auto components = entity.get()->getComponents(); @@ -80,23 +132,15 @@ void Core::_renderEntities() unsigned int index = texture.get()->getZIndex(); entitiesTextureProps[index].push_back(this->_getTextureEntity(texture)); } + if (component.get()->getType() == shared::games::components::TEXT) { + auto texture = std::dynamic_pointer_cast(component); + unsigned int index = texture.get()->getZIndex(); + entitiesTextProps[index].push_back(this->_getTextEntity(texture)); + } } } this->_window.get()->clear(); - auto textPropsIt = entitiesTextProps.begin(); - auto texturePropsIt = entitiesTextureProps.begin(); - while (texturePropsIt != entitiesTextureProps.end() || textPropsIt != entitiesTextProps.end()) { - if (texturePropsIt != entitiesTextureProps.end()) { - for (auto &textureProps : texturePropsIt->second) - this->_window.get()->render(textureProps); - texturePropsIt++; - } - if (textPropsIt != entitiesTextProps.end()) { - for (auto &textProps : textPropsIt->second) - this->_window.get()->render(textProps); - textPropsIt++; - } - } + this->_renderProps(entitiesTextureProps, entitiesTextProps); this->_window.get()->display(); } @@ -108,7 +152,7 @@ void Core::run() this->_initWindow(); while (this->_window.get()->isOpen()) { auto currentTime = std::chrono::high_resolution_clock::now(); - auto deltaTime = std::chrono::duration_cast(previousTime - currentTime).count(); + auto deltaTime = std::chrono::duration_cast(previousTime - currentTime); previousTime = currentTime; this->_game.get()->compute(deltaTime); diff --git a/core/src/core/Core.hpp b/core/src/core/Core.hpp index 3ecc8b9..b530fd1 100644 --- a/core/src/core/Core.hpp +++ b/core/src/core/Core.hpp @@ -13,9 +13,11 @@ #include "shared/games/components/ITextureComponent.hpp" #include "shared/games/components/IDisplayableComponent.hpp" +using namespace shared::graphics; + class Core { public: - Core(GameProviders gameProviders, GraphicsProviders graphicsProviders); + Core(GameProviders &gameProviders, GraphicsProviders &graphicsProviders); ~Core(); /** @@ -27,13 +29,13 @@ class Core { protected: private: std::shared_ptr _game; - std::shared_ptr _window; - std::shared_ptr _gameProvider; - std::shared_ptr _graphicsProvider; - std::map> _fonts; - std::map> _textures; - GameProviders _gameProviders; - GraphicsProviders _graphicsProviders; + std::shared_ptr _window; + std::unique_ptr &_gameProvider; + std::unique_ptr &_graphicsProvider; + std::map> _fonts; + std::map> _textures; + const GameProviders &_gameProviders; + const GraphicsProviders &_graphicsProviders; shared::games::entity::EntitiesMap _gameEntities; /** @@ -61,7 +63,7 @@ class Core { * @param ascii Path to the ascii file * @return The correct texture */ - std::shared_ptr _getTexture(std::string bin, std::string ascii); + std::shared_ptr _getTexture(std::string bin, std::string ascii); /** * @brief Get a font @@ -69,7 +71,7 @@ class Core { * @param path Path to the font file * @return The correct font */ - std::shared_ptr _getFont(std::string path); + std::shared_ptr _getFont(std::string path); /** * @brief Get the texture entity @@ -77,7 +79,7 @@ class Core { * @param texture The texture component * @return The texture entity */ - shared::graphics::TextureProps _getTextureEntity(std::shared_ptr texture); + TextureProps _getTextureEntity(std::shared_ptr texture); /** * @brief Get the text entity @@ -85,5 +87,29 @@ class Core { * @param text The text component * @return The text entity */ - shared::graphics::TextProps _getTextEntity(std::shared_ptr text); + TextProps _getTextEntity(std::shared_ptr text); + + /** + * @brief Render the props + * + * @param textures The textures + * @param texts The texts + */ + void _renderProps(std::map> &textures, std::map> &texts); + + /** + * @brief Render the texture props + * + * @param textures The textures + * @param texturePropsIt The iterator + */ + void _renderTextureProps(std::map> &textures, std::map>::iterator &texturePropsIt); + + /** + * @brief Render the text props + * + * @param texts The texts + * @param textPropsIt The iterator + */ + void _renderTextProps(std::map> &texts, std::map>::iterator &textPropsIt); }; diff --git a/core/src/exception/CMakeLists.txt b/core/src/exception/CMakeLists.txt index 7f3f3dc..a39fab8 100644 --- a/core/src/exception/CMakeLists.txt +++ b/core/src/exception/CMakeLists.txt @@ -1,3 +1,4 @@ target_sources(${CMAKE_PROJECT_NAME} PRIVATE ArcadeError.cpp + ArcadeError.hpp ) diff --git a/core/src/loader/CMakeLists.txt b/core/src/loader/CMakeLists.txt index 99592b3..9cb0bf9 100644 --- a/core/src/loader/CMakeLists.txt +++ b/core/src/loader/CMakeLists.txt @@ -1,3 +1,4 @@ target_sources(${CMAKE_PROJECT_NAME} PRIVATE Loader.cpp + Loader.hpp ) diff --git a/core/src/loader/Loader.cpp b/core/src/loader/Loader.cpp index 997b504..e4d5b58 100644 --- a/core/src/loader/Loader.cpp +++ b/core/src/loader/Loader.cpp @@ -55,10 +55,10 @@ void Loader::loadLibraries(std::string path) { } } -const GameProviders &Loader::getGamesLibraries() const { +GameProviders &Loader::getGamesLibraries() { return this->_gamesLibraries; } -const GraphicsProviders &Loader::getGraphicsLibraries() const { +GraphicsProviders &Loader::getGraphicsLibraries() { return this->_graphicsLibraries; } diff --git a/core/src/loader/Loader.hpp b/core/src/loader/Loader.hpp index 809c326..fee43ed 100644 --- a/core/src/loader/Loader.hpp +++ b/core/src/loader/Loader.hpp @@ -40,13 +40,13 @@ class Loader { * @brief Get all games libraries * @return Loaded games libraries */ - const GameProviders &getGamesLibraries() const; + GameProviders &getGamesLibraries(); /** * @brief Get all graphics libraries * @return Loaded graphics libraries */ - const GraphicsProviders &getGraphicsLibraries() const; + GraphicsProviders &getGraphicsLibraries(); private: const std::string _path; diff --git a/core/src/types/CMakeLists.txt b/core/src/types/CMakeLists.txt new file mode 100644 index 0000000..39bf466 --- /dev/null +++ b/core/src/types/CMakeLists.txt @@ -0,0 +1,3 @@ +target_sources(${CMAKE_PROJECT_NAME} PRIVATE + Providers.hpp +) diff --git a/shared/games/IGame.hpp b/shared/games/IGame.hpp index bc8353d..bd684c8 100644 --- a/shared/games/IGame.hpp +++ b/shared/games/IGame.hpp @@ -8,6 +8,7 @@ #pragma once #include +#include #include "IEntity.hpp" #include "../types/Vector.hpp" #include "types/GameManifest.hpp" @@ -18,7 +19,7 @@ namespace shared::games { class IGame; - typedef unsigned long DeltaTime; + typedef std::chrono::duration DeltaTime; } class shared::games::IGame diff --git a/shared/games/components/IKeyboardComponent.hpp b/shared/games/components/IKeyboardComponent.hpp index 097b293..f2f474a 100644 --- a/shared/games/components/IKeyboardComponent.hpp +++ b/shared/games/components/IKeyboardComponent.hpp @@ -12,7 +12,11 @@ namespace shared::games::components { class IKeyboardComponent; +} +class shared::games::components::IKeyboardComponent: public virtual IComponent +{ +public: typedef enum { CONTROL, // Control key (`Ctrl`, `Shift`, `Alt`) @@ -50,11 +54,7 @@ namespace shared::games::components { KeyCode code; // Key code. Interpretation depends on the type KeyType type; // Type of the key } KeyData; -} -class shared::games::components::IKeyboardComponent: public virtual IComponent -{ -public: virtual ~IKeyboardComponent() = default; /** diff --git a/shared/games/components/ITextComponent.hpp b/shared/games/components/ITextComponent.hpp index 655d45e..64dd5cf 100644 --- a/shared/games/components/ITextComponent.hpp +++ b/shared/games/components/ITextComponent.hpp @@ -13,7 +13,10 @@ namespace shared::games::components { class ITextComponent; +} +class shared::games::components::ITextComponent : public virtual IDisplayableComponent { +public: typedef enum { LEFT, CENTER, @@ -38,10 +41,7 @@ namespace shared::games::components { TextFontProps font; // Font of the text types::Color color; // Color of the text } TextProps; -} -class shared::games::components::ITextComponent : public virtual IDisplayableComponent { -public: virtual ~ITextComponent() = default; /** diff --git a/shared/graphics/events/IKeyEvent.hpp b/shared/graphics/events/IKeyEvent.hpp index db6cab4..81fe453 100644 --- a/shared/graphics/events/IKeyEvent.hpp +++ b/shared/graphics/events/IKeyEvent.hpp @@ -13,7 +13,7 @@ namespace shared::graphics::events { class IKeyEvent; } -class shared::graphics::events::IKeyEvent : public IEvent { +class shared::graphics::events::IKeyEvent : public virtual IEvent { public: virtual ~IKeyEvent() = default; diff --git a/shared/graphics/events/IMouseButtonEvent.hpp b/shared/graphics/events/IMouseButtonEvent.hpp index b70ab17..bac5eba 100644 --- a/shared/graphics/events/IMouseButtonEvent.hpp +++ b/shared/graphics/events/IMouseButtonEvent.hpp @@ -13,7 +13,7 @@ namespace shared::graphics::events{ class IMouseButtonEvent; } -class shared::graphics::events::IMouseButtonEvent : public IMouseEvent { +class shared::graphics::events::IMouseButtonEvent : public virtual IMouseEvent { public: virtual ~IMouseButtonEvent() = default; diff --git a/shared/graphics/events/IMouseEvent.hpp b/shared/graphics/events/IMouseEvent.hpp index a85ae4b..618e5fe 100644 --- a/shared/graphics/events/IMouseEvent.hpp +++ b/shared/graphics/events/IMouseEvent.hpp @@ -14,7 +14,7 @@ namespace shared::graphics::events { class IMouseEvent; } -class shared::graphics::events::IMouseEvent : public IEvent { +class shared::graphics::events::IMouseEvent : public virtual IEvent { public: virtual ~IMouseEvent() = default; From b67ea5df5732597d4eda98dadc87e6a983305604 Mon Sep 17 00:00:00 2001 From: Yann Date: Fri, 29 Mar 2024 20:49:12 +0100 Subject: [PATCH 09/13] feat(core): handle keyboard and mouse events --- core/main.cpp | 2 +- core/src/CMakeLists.txt | 6 +- core/src/Core.cpp | 463 +++++++++++++++++++++++++++++++++++ core/src/Core.hpp | 254 +++++++++++++++++++ core/src/core/CMakeLists.txt | 4 - core/src/core/Core.cpp | 162 ------------ core/src/core/Core.hpp | 115 --------- 7 files changed, 723 insertions(+), 283 deletions(-) create mode 100644 core/src/Core.cpp create mode 100644 core/src/Core.hpp delete mode 100644 core/src/core/CMakeLists.txt delete mode 100644 core/src/core/Core.cpp delete mode 100644 core/src/core/Core.hpp diff --git a/core/main.cpp b/core/main.cpp index 5a3dfe3..db25d09 100644 --- a/core/main.cpp +++ b/core/main.cpp @@ -5,7 +5,7 @@ ** main */ -#include "core/Core.hpp" +#include "Core.hpp" #include "loader/Loader.hpp" int main(void) diff --git a/core/src/CMakeLists.txt b/core/src/CMakeLists.txt index b777fc8..910a685 100644 --- a/core/src/CMakeLists.txt +++ b/core/src/CMakeLists.txt @@ -1,4 +1,8 @@ +target_sources(${CMAKE_PROJECT_NAME} PRIVATE + Core.cpp + Core.hpp +) + add_subdirectory(exception) add_subdirectory(loader) add_subdirectory(utils) -add_subdirectory(core) diff --git a/core/src/Core.cpp b/core/src/Core.cpp new file mode 100644 index 0000000..3f78d30 --- /dev/null +++ b/core/src/Core.cpp @@ -0,0 +1,463 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** Core +*/ + +#include + +#include +#include +#include "Core.hpp" +#include "shared/games/components/IComponent.hpp" + +Core::Core(GameProviders &gameProviders, GraphicsProviders &graphicsProviders) : + _gameProviders(gameProviders), _graphicsProviders(graphicsProviders), + _gameProvider(gameProviders.at(0)), _graphicsProvider(graphicsProviders.at(0)) {} + +Core::~Core() {} + +void Core::_initGame() +{ + this->_game = this->_gameProvider->createInstance(); +} + +void Core::_initWindow() +{ + auto gameManifest = this->_game->getManifest(); + IWindow::WindowInitProps windowInitProps { + this->_game->getSize(), + IWindow::WINDOWED, + this->_game->getFps(), + gameManifest.name, + gameManifest.iconPath + }; + + this->_window = this->_graphicsProvider->createWindow(windowInitProps); +} + +std::shared_ptr Core::_getTexture(std::string bin, std::string ascii) +{ + if (this->_textures.find(bin + ascii) == this->_textures.end()) + this->_textures[bin + ascii] = this->_graphicsProvider->createTexture(bin, ascii); + return this->_textures[bin + ascii]; +} + +std::shared_ptr Core::_getFont(std::string path) +{ + if (this->_fonts.find(path) == this->_fonts.end()) + this->_fonts[path] = this->_graphicsProvider->createFont(path); + return this->_fonts[path]; +} + +TextureProps Core::_getTextureEntity(std::shared_ptr texture) +{ + auto textureProps = texture->getTextureProps(); + TextureProps entityTextureProps { + this->_getTexture(textureProps.sources.bin, textureProps.sources.ascii), + textureProps.sources.binTileSize, + textureProps.origin, + texture->getSize(), + texture->getPosition() + }; + + return entityTextureProps; +} + +TextProps Core::_getTextEntity(std::shared_ptr text) +{ + auto textProps = text->getTextProps(); + TextProps entityTextProps { + this->_getFont(textProps.font.path), + textProps.font.size, + textProps.content, + static_cast(textProps.align), + static_cast(textProps.verticalAlign), + textProps.color, + text->getSize(), + text->getPosition() + }; + + return entityTextProps; +} + +void Core::_renderTextureProps(std::map> &textures, std::map>::iterator &texturePropsIt) +{ + if (texturePropsIt == textures.end()) + return; + for (auto &textureProps : texturePropsIt->second) + this->_window->render(textureProps); + texturePropsIt++; +} + +void Core::_renderTextProps(std::map> &texts, std::map>::iterator &textPropsIt) +{ + if (textPropsIt == texts.end()) + return; + for (auto &textureProps : textPropsIt->second) + this->_window->render(textureProps); + textPropsIt++; +} + +void Core::_renderProps(std::map> &textures, std::map> &texts) +{ + auto textPropsIt = texts.begin(); + auto texturePropsIt = textures.begin(); + + while (texturePropsIt != textures.end() || textPropsIt != texts.end()) { + if (textPropsIt != texts.end()) { + if (texturePropsIt->first <= textPropsIt->first) + this->_renderTextureProps(textures, texturePropsIt); + } else { + this->_renderTextureProps(textures, texturePropsIt); + } + if (texturePropsIt != textures.end()) { + if (textPropsIt->first <= texturePropsIt->first) + this->_renderTextProps(texts, textPropsIt); + } else { + this->_renderTextProps(texts, textPropsIt); + } + } +} + +void Core::_renderEntities() +{ + std::map> entitiesTextureProps; + std::map> entitiesTextProps; + + for (auto &entity : this->_gameEntities) { + auto components = entity->getComponents(); + for (auto &component : components) { + if (component->getType() == components::TEXTURE) { + auto texture = std::dynamic_pointer_cast(component); + unsigned int index = texture->getZIndex(); + entitiesTextureProps[index].push_back(this->_getTextureEntity(texture)); + } + if (component->getType() == components::TEXT) { + auto texture = std::dynamic_pointer_cast(component); + unsigned int index = texture->getZIndex(); + entitiesTextProps[index].push_back(this->_getTextEntity(texture)); + } + } + } + this->_window->clear(); + this->_renderProps(entitiesTextureProps, entitiesTextProps); + this->_window->display(); +} + + +components::IKeyboardComponent::KeyData Core::_convertKeyPressData(events::IKeyEvent::KeyType type, events::IKeyEvent::KeyCode code) +{ + components::IKeyboardComponent::KeyData keyCodeData; + + if (type == events::IKeyEvent::CHAR) { + keyCodeData.code.character = code.character; + keyCodeData.type = components::IKeyboardComponent::CHAR; + } else if (type == events::IKeyEvent::CONTROL) { + keyCodeData.code.control = static_cast(code.control); + keyCodeData.type = components::IKeyboardComponent::CONTROL; + } else if (type == events::IKeyEvent::ARROW) { + keyCodeData.code.arrow = static_cast(code.arrow); + keyCodeData.type = components::IKeyboardComponent::ARROW; + } else if (type == events::IKeyEvent::FUNC) { + keyCodeData.code.func = code.func; + keyCodeData.type = components::IKeyboardComponent::FUNC; + } + return keyCodeData; +} + +void Core::_preventWindowClose(std::vector events) +{ + for (auto &event : events) { + auto type = event->getType(); + if (type == events::WINDOW_CLOSE) + this->_handleWindowClose(); + } +} + +void Core::_handleKeyPress(std::shared_ptr &keyEvent, std::shared_ptr &keyboard) +{ + auto keyCode = keyEvent->getKeyCode(); + auto keyType = keyEvent->getKeyType(); + auto keyCodeData = this->_convertKeyPressData(keyType, keyCode); + + keyboard->onKeyPress(this->_game, keyCodeData); +} + +void Core::_handleKeyRelease(std::shared_ptr &keyEvent, std::shared_ptr &keyboard) +{ + auto keyCode = keyEvent->getKeyCode(); + auto keyType = keyEvent->getKeyType(); + auto keyCodeData = this->_convertKeyPressData(keyType, keyCode); + + keyboard->onKeyRelease(this->_game, keyCodeData); +} + +void Core::_handleMouseButtonPress(std::shared_ptr &event, std::shared_ptr &component) +{ + auto button = event->getButton(); + auto mousePosition = event->getPosition(); + auto entityPosition = component->getPosition(); + auto entitySize = component->getSize(); + + if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + entitySize.x && + mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + entitySize.y) + component->onMousePress(this->_game); +} + +void Core::_handleMouseButtonRelease(std::shared_ptr &event, std::shared_ptr &component) +{ + auto button = event->getButton(); + auto mousePosition = event->getPosition(); + auto entityPosition = component->getPosition(); + auto entitySize = component->getSize(); + + if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + entitySize.x && + mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + entitySize.y) + component->onMouseRelease(this->_game); +} + +void Core::_handleMouseMove(std::shared_ptr &event, std::shared_ptr &component) +{ + auto mousePosition = event->getPosition(); + auto entityPosition = component->getPosition(); + auto entitySize = component->getSize(); + + if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + entitySize.x && + mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + entitySize.y) + component->onMouseHover(this->_game); +} + +void Core::_handleWindowClose() +{ + this->_window->close(); +} + +void Core::_handleWindowResize() +{ + std::cout << "Window resized" << std::endl; +} + +void Core::_handleKeyBoardEvents(std::vector &events, std::shared_ptr &component) +{ + for (auto &event : events) { + auto type = event->getType(); + if (type == events::KEY_PRESS) { + auto keyEvent = std::dynamic_pointer_cast(event); + this->_handleKeyPress(keyEvent, component); + } + if (type == events::KEY_RELEASE) { + auto keyEvent = std::dynamic_pointer_cast(event); + this->_handleKeyRelease(keyEvent, component); + } + } +} + +void Core::_handleDisplayableEvents(std::vector &events, std::shared_ptr &component) +{ + for (auto &event : events) { + auto type = event->getType(); + if (type == events::MOUSE_BTN_PRESS) { + auto mouseButtonEvent = std::dynamic_pointer_cast(event); + this->_handleMouseButtonPress(mouseButtonEvent, component); + } + if (type == events::MOUSE_BTN_RELEASE) { + auto mouseButtonEvent = std::dynamic_pointer_cast(event); + this->_handleMouseButtonRelease(mouseButtonEvent, component); + } + if (type == events::MOUSE_MOVE) { + auto mouseEvent = std::dynamic_pointer_cast(event); + this->_handleMouseMove(mouseEvent, component); + } + } +} + +// void Core::_handleCollisions(std::shared_ptr &component, std::shared_ptr &target) +// { +// auto componentPosition = component->getPosition(); +// auto componentSize = component->getSize(); +// auto targetPosition = target->getPosition(); +// auto targetSize = target->getSize(); + +// if (componentPosition.x < targetPosition.x + targetSize.x && +// componentPosition.x + componentSize.x > targetPosition.x && +// componentPosition.y < targetPosition.y + targetSize.y && +// componentPosition.y + componentSize.y > targetPosition.y) +// this->_handleCollidableComponents(component); +// } + +// void Core::_handleCollidableComponents(std::shared_ptr &component) +// { +// for (auto &entity : this->_gameEntities) { +// auto components = entity->getComponents(); +// for (auto &entityComponent : components) { +// if (entityComponent->getType() == components::COLLIDABLE) { +// auto collidable = std::dynamic_pointer_cast(entityComponent); +// this->_handleCollisions(component, collidable); +// } +// } +// } +// } + +void Core::_handleComponentEvents(std::vector &events, std::shared_ptr &component) +{ + auto type = component->getType(); + + if (type == components::KEYBOARD) { + auto keyboard = std::dynamic_pointer_cast(component); + this->_handleKeyBoardEvents(events, keyboard); + } + if (type == components::TEXTURE || type == components::TEXT) { + auto displayable = std::dynamic_pointer_cast(component); + this->_handleDisplayableEvents(events, displayable); + } +} + +void Core::_handleEvents() +{ + auto gameEvents = this->_window->getEvents(); + + this->_preventWindowClose(gameEvents); + for (auto &entity : this->_gameEntities) { + auto components = entity->getComponents(); + for (auto &component : components) { + this->_handleComponentEvents(gameEvents, component); + } + } +} + +// void Core::_handleKeyPress(std::shared_ptr &keyEvent) +// { +// auto keyCode = keyEvent->getKeyCode(); +// auto keyType = keyEvent->getKeyType(); + +// for (auto &entity : this->_gameEntities) { +// auto components = entity->getComponents(); +// for (auto &component : components) { +// if (component->getType() == components::KEYBOARD) { +// auto keyboard = std::dynamic_pointer_cast(component); +// auto keyCodeData = this->_convertKeyPressData(keyType, keyCode); +// keyboard->onKeyPress(this->_game, keyCodeData); +// } +// } +// } +// } + +// void Core::_handleKeyRelease(std::shared_ptr &keyEvent) +// { +// auto keyCode = keyEvent->getKeyCode(); +// auto keyType = keyEvent->getKeyType(); + +// for (auto &entity : this->_gameEntities) { +// auto components = entity->getComponents(); +// for (auto &component : components) { +// if (component->getType() == components::KEYBOARD) { +// auto keyboard = std::dynamic_pointer_cast(component); +// auto keyCodeData = this->_convertKeyPressData(keyType, keyCode); +// keyboard->onKeyRelease(this->_game, keyCodeData); +// } +// } +// } +// } + +// void Core::_handleMouseButtonPress(std::shared_ptr &event) +// { +// auto button = event->getButton(); +// auto mousePosition = event->getPosition(); + +// for (auto &entity : this->_gameEntities) { +// auto components = entity->getComponents(); +// for (auto &component : components) { +// auto type = component->getType(); +// if (type == components::TEXTURE || type == components::TEXT) { +// auto displayable = std::dynamic_pointer_cast(component); +// auto entityPosition = displayable->getPosition(); +// auto entitySize = displayable->getSize(); +// if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + entitySize.x && +// mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + entitySize.y) +// displayable->onMousePress(this->_game); +// } +// } +// } +// } + +// void Core::_handleMouseMove(std::shared_ptr &event) +// { +// auto mousePosition = event->getPosition(); + +// for (auto &entity : this->_gameEntities) { +// auto components = entity->getComponents(); +// for (auto &component : components) { +// auto type = component->getType(); +// if (type == components::TEXTURE || type == components::TEXT) { +// auto displayable = std::dynamic_pointer_cast(component); +// auto entityPosition = displayable->getPosition(); +// auto entitySize = displayable->getSize(); +// if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + entitySize.x && +// mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + entitySize.y) +// displayable->onMouseHover(this->_game); +// } +// } +// } +// } + +// void Core::_handleMouseButtonRelease(std::shared_ptr &event) +// { +// auto button = event->getButton(); +// auto mousePosition = event->getPosition(); + +// for (auto &entity : this->_gameEntities) { +// auto components = entity->getComponents(); +// for (auto &component : components) { +// auto type = component->getType(); +// if (type == components::TEXTURE || type == components::TEXT) { +// auto displayable = std::dynamic_pointer_cast(component); +// auto entityPosition = displayable->getPosition(); +// auto entitySize = displayable->getSize(); +// if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + entitySize.x && +// mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + entitySize.y) +// displayable->onMouseRelease(this->_game); +// } +// } +// } +// } + +// void Core::_handleEvents() +// { +// auto gameEvents = this->_window->getEvents(); + +// for (auto &event : gameEvents) { +// auto type = event->getType(); +// if (type == events::WINDOW_CLOSE) +// this->_window->close(); +// if (type == events::WINDOW_RESIZE) +// std::cout << "Window resized" << std::endl; +// if (type == events::KEY_PRESS) { +// auto keyEvent = std::dynamic_pointer_cast(event); +// this->_handleKeyPress(keyEvent); +// } +// if (type == events::KEY_RELEASE) { +// auto keyEvent = std::dynamic_pointer_cast(event); +// this->_handleKeyRelease(keyEvent); +// } +// } +// } + +void Core::run() +{ + auto previousTime = std::chrono::high_resolution_clock::now(); + + this->_initGame(); + this->_initWindow(); + while (this->_window->isOpen()) { + auto currentTime = std::chrono::high_resolution_clock::now(); + auto deltaTime = std::chrono::duration_cast(previousTime - currentTime); + previousTime = currentTime; + + this->_game->compute(deltaTime); + this->_gameEntities = this->_game->getEntities(); + this->_handleEvents(); + this->_renderEntities(); + } +} diff --git a/core/src/Core.hpp b/core/src/Core.hpp new file mode 100644 index 0000000..daf7c91 --- /dev/null +++ b/core/src/Core.hpp @@ -0,0 +1,254 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** Core +*/ + +#pragma once + +#include +#include "types/Providers.hpp" +#include "shared/graphics/events/IKeyEvent.hpp" +#include "shared/graphics/events/IMouseEvent.hpp" +#include "shared/graphics/events/IMouseButtonEvent.hpp" +#include "shared/games/components/ITextComponent.hpp" +#include "shared/games/components/ITextureComponent.hpp" +#include "shared/games/components/IKeyboardComponent.hpp" +#include "shared/games/components/IDisplayableComponent.hpp" +#include "shared/games/components/ICollidableComponent.hpp" + +using namespace shared::graphics; +using namespace shared::games; + +class Core { + public: + Core(GameProviders &gameProviders, GraphicsProviders &graphicsProviders); + ~Core(); + + /** + * @brief Run the core + * + */ + void run(); + + protected: + private: + std::shared_ptr _game; + std::shared_ptr _window; + std::unique_ptr &_gameProvider; + std::unique_ptr &_graphicsProvider; + std::map> _fonts; + std::map> _textures; + const GameProviders &_gameProviders; + const GraphicsProviders &_graphicsProviders; + entity::EntitiesMap _gameEntities; + + /** + * @brief Initialize the window + * + */ + void _initWindow(); + + /** + * @brief Initialize the game + * + */ + void _initGame(); + + /** + * @brief Render all entities + * + */ + void _renderEntities(); + + /** + * @brief Get a texture + * + * @param bin Path to the binary file + * @param ascii Path to the ascii file + * @return The correct texture + */ + std::shared_ptr _getTexture(std::string bin, std::string ascii); + + /** + * @brief Get a font + * + * @param path Path to the font file + * @return The correct font + */ + std::shared_ptr _getFont(std::string path); + + /** + * @brief Get the texture entity + * + * @param texture The texture component + * @return The texture entity + */ + TextureProps _getTextureEntity(std::shared_ptr texture); + + /** + * @brief Get the text entity + * + * @param text The text component + * @return The text entity + */ + TextProps _getTextEntity(std::shared_ptr text); + + /** + * @brief Render the props + * + * @param textures The textures + * @param texts The texts + */ + void _renderProps(std::map> &textures, std::map> &texts); + + /** + * @brief Render the texture props + * + * @param textures The textures + * @param texturePropsIt The iterator + */ + void _renderTextureProps(std::map> &textures, std::map>::iterator &texturePropsIt); + + /** + * @brief Render the text props + * + * @param texts The texts + * @param textPropsIt The iterator + */ + void _renderTextProps(std::map> &texts, std::map>::iterator &textPropsIt); + + /** + * @brief Handle the events + * + */ + void _handleEvents(); + + // void _handleComponentEvent(std::shared_ptr &event, std::shared_ptr &component); + + // void _handleComponentEvent(std::shared_ptr &event, std::shared_ptr &component); + + // void _handleComponentEvent(std::shared_ptr &event, std::shared_ptr &component); + + /** + * @brief Handle the component events + * + * @param events Events to handle + * @param component The component + */ + void _handleComponentEvents(std::vector &events, std::shared_ptr &component); + + /** + * @brief Handle the keyboard events + * + * @param events Events to handle + * @param component The keyboard component + */ + void _handleKeyBoardEvents(std::vector &events, std::shared_ptr &component); + + /** + * @brief Handle the displayable events + * + * @param events Events to handle + * @param component The displayable component + */ + void _handleDisplayableEvents(std::vector &events, std::shared_ptr &component); + + // ======================================== PENDING ======================================== + + // void _handleCollidableComponents(std::shared_ptr &component); + + // void _handleCollisions(std::shared_ptr &component, std::shared_ptr &target); + + // ========================================================================================= + + /** + * @brief Handle the key press event + * + * @param event The key event + * @param component The keyboard component + */ + void _handleKeyPress(std::shared_ptr &event, std::shared_ptr &component); + + /** + * @brief Handle the key release event + * + * @param event The key event + * @param component The keyboard component + */ + void _handleKeyRelease(std::shared_ptr &event, std::shared_ptr &component); + + /** + * @brief Handle the mouse button press event + * + * @param event The mouse button event + * @param component The displayable component + */ + void _handleMouseButtonPress(std::shared_ptr &event, std::shared_ptr &component); + + /** + * @brief Handle the mouse button release event + * + * @param event The mouse button event + * @param component The displayable component + */ + void _handleMouseButtonRelease(std::shared_ptr &event, std::shared_ptr &component); + + /** + * @brief Handle the mouse move event + * + * @param event The mouse event + * @param component The displayable component + */ + void _handleMouseMove(std::shared_ptr &event, std::shared_ptr &component); + + /** + * @brief Handle the window close event + * + */ + void _handleWindowClose(); + + /** + * @brief Handle the window resize event + * + */ + void _handleWindowResize(); + + /** + * @brief Prevent the window from closing + * + * @param events The events + */ + void _preventWindowClose(std::vector events); + + /** + * @brief Handle the key press event + * + * @param event The key event + */ + void _handleKeyPress(std::shared_ptr &event); + + /** + * @brief Handle the key release event + * + * @param event The key event + */ + void _handleKeyRelease(std::shared_ptr &event); + + /** + * @brief Convert the key press data + * + * @param type The type of the key + * @param code The code of the key + * @return The converted key press data + */ + components::IKeyboardComponent::KeyData _convertKeyPressData(events::IKeyEvent::KeyType type, events::IKeyEvent::KeyCode code); + + // void _handleMouseButtonPress(std::shared_ptr &event); + + // void _handleMouseButtonRelease(std::shared_ptr &event); + + // void _handleMouseMove(std::shared_ptr &event); + +}; diff --git a/core/src/core/CMakeLists.txt b/core/src/core/CMakeLists.txt deleted file mode 100644 index fded451..0000000 --- a/core/src/core/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -target_sources(${CMAKE_PROJECT_NAME} PRIVATE - Core.cpp - Core.hpp -) diff --git a/core/src/core/Core.cpp b/core/src/core/Core.cpp deleted file mode 100644 index 5c4b2aa..0000000 --- a/core/src/core/Core.cpp +++ /dev/null @@ -1,162 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade -** File description: -** Core -*/ - -#include -#include -#include "Core.hpp" -#include "shared/games/components/IComponent.hpp" - -Core::Core(GameProviders &gameProviders, GraphicsProviders &graphicsProviders) : - _gameProviders(gameProviders), _graphicsProviders(graphicsProviders), - _gameProvider(gameProviders.at(0)), _graphicsProvider(graphicsProviders.at(0)) {} - -Core::~Core() {} - -void Core::_initGame() -{ - this->_game = this->_gameProvider.get()->createInstance(); -} - -void Core::_initWindow() -{ - auto gameManifest = this->_game.get()->getManifest(); - IWindow::WindowInitProps windowInitProps { - this->_game.get()->getSize(), - IWindow::WINDOWED, - this->_game.get()->getFps(), - gameManifest.name, - gameManifest.iconPath - }; - - this->_window = this->_graphicsProvider.get()->createWindow(windowInitProps); -} - -std::shared_ptr Core::_getTexture(std::string bin, std::string ascii) -{ - if (this->_textures.find(bin + ascii) == this->_textures.end()) - this->_textures[bin + ascii] = this->_graphicsProvider.get()->createTexture(bin, ascii); - return this->_textures[bin + ascii]; -} - -std::shared_ptr Core::_getFont(std::string path) -{ - if (this->_fonts.find(path) == this->_fonts.end()) - this->_fonts[path] = this->_graphicsProvider.get()->createFont(path); - return this->_fonts[path]; -} - -TextureProps Core::_getTextureEntity(std::shared_ptr texture) -{ - auto textureProps = texture.get()->getTextureProps(); - TextureProps entityTextureProps { - this->_getTexture(textureProps.sources.bin, textureProps.sources.ascii), - textureProps.sources.binTileSize, - textureProps.origin, - texture.get()->getSize(), - texture.get()->getPosition() - }; - - return entityTextureProps; -} - -TextProps Core::_getTextEntity(std::shared_ptr text) -{ - auto textProps = text->getTextProps(); - TextProps entityTextProps { - this->_getFont(textProps.font.path), - textProps.font.size, - textProps.content, - static_cast(textProps.align), - static_cast(textProps.verticalAlign), - textProps.color, - text->getSize(), - text->getPosition() - }; - - return entityTextProps; -} - -void Core::_renderTextureProps(std::map> &textures, std::map>::iterator &texturePropsIt) -{ - if (texturePropsIt == textures.end()) - return; - for (auto &textureProps : texturePropsIt->second) - this->_window.get()->render(textureProps); - texturePropsIt++; -} - -void Core::_renderTextProps(std::map> &texts, std::map>::iterator &textPropsIt) -{ - if (textPropsIt == texts.end()) - return; - for (auto &textureProps : textPropsIt->second) - this->_window.get()->render(textureProps); - textPropsIt++; -} - -void Core::_renderProps(std::map> &textures, std::map> &texts) -{ - auto textPropsIt = texts.begin(); - auto texturePropsIt = textures.begin(); - - while (texturePropsIt != textures.end() || textPropsIt != texts.end()) { - if (textPropsIt != texts.end()) { - if (texturePropsIt->first <= textPropsIt->first) - this->_renderTextureProps(textures, texturePropsIt); - } else { - this->_renderTextureProps(textures, texturePropsIt); - } - if (texturePropsIt != textures.end()) { - if (textPropsIt->first <= texturePropsIt->first) - this->_renderTextProps(texts, textPropsIt); - } else { - this->_renderTextProps(texts, textPropsIt); - } - } -} - -void Core::_renderEntities() -{ - std::map> entitiesTextureProps; - std::map> entitiesTextProps; - - for (auto &entity : this->_gameEntities) { - auto components = entity.get()->getComponents(); - for (auto &component : components) { - if (component.get()->getType() == shared::games::components::TEXTURE) { - auto texture = std::dynamic_pointer_cast(component); - unsigned int index = texture.get()->getZIndex(); - entitiesTextureProps[index].push_back(this->_getTextureEntity(texture)); - } - if (component.get()->getType() == shared::games::components::TEXT) { - auto texture = std::dynamic_pointer_cast(component); - unsigned int index = texture.get()->getZIndex(); - entitiesTextProps[index].push_back(this->_getTextEntity(texture)); - } - } - } - this->_window.get()->clear(); - this->_renderProps(entitiesTextureProps, entitiesTextProps); - this->_window.get()->display(); -} - -void Core::run() -{ - auto previousTime = std::chrono::high_resolution_clock::now(); - - this->_initGame(); - this->_initWindow(); - while (this->_window.get()->isOpen()) { - auto currentTime = std::chrono::high_resolution_clock::now(); - auto deltaTime = std::chrono::duration_cast(previousTime - currentTime); - previousTime = currentTime; - - this->_game.get()->compute(deltaTime); - this->_gameEntities = this->_game.get()->getEntities(); - this->_renderEntities(); - } -} diff --git a/core/src/core/Core.hpp b/core/src/core/Core.hpp deleted file mode 100644 index b530fd1..0000000 --- a/core/src/core/Core.hpp +++ /dev/null @@ -1,115 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade -** File description: -** Core -*/ - -#pragma once - -#include -#include "types/Providers.hpp" -#include "shared/games/components/ITextComponent.hpp" -#include "shared/games/components/ITextureComponent.hpp" -#include "shared/games/components/IDisplayableComponent.hpp" - -using namespace shared::graphics; - -class Core { - public: - Core(GameProviders &gameProviders, GraphicsProviders &graphicsProviders); - ~Core(); - - /** - * @brief Run the core - * - */ - void run(); - - protected: - private: - std::shared_ptr _game; - std::shared_ptr _window; - std::unique_ptr &_gameProvider; - std::unique_ptr &_graphicsProvider; - std::map> _fonts; - std::map> _textures; - const GameProviders &_gameProviders; - const GraphicsProviders &_graphicsProviders; - shared::games::entity::EntitiesMap _gameEntities; - - /** - * @brief Initialize the window - * - */ - void _initWindow(); - - /** - * @brief Initialize the game - * - */ - void _initGame(); - - /** - * @brief Render all entities - * - */ - void _renderEntities(); - - /** - * @brief Get a texture - * - * @param bin Path to the binary file - * @param ascii Path to the ascii file - * @return The correct texture - */ - std::shared_ptr _getTexture(std::string bin, std::string ascii); - - /** - * @brief Get a font - * - * @param path Path to the font file - * @return The correct font - */ - std::shared_ptr _getFont(std::string path); - - /** - * @brief Get the texture entity - * - * @param texture The texture component - * @return The texture entity - */ - TextureProps _getTextureEntity(std::shared_ptr texture); - - /** - * @brief Get the text entity - * - * @param text The text component - * @return The text entity - */ - TextProps _getTextEntity(std::shared_ptr text); - - /** - * @brief Render the props - * - * @param textures The textures - * @param texts The texts - */ - void _renderProps(std::map> &textures, std::map> &texts); - - /** - * @brief Render the texture props - * - * @param textures The textures - * @param texturePropsIt The iterator - */ - void _renderTextureProps(std::map> &textures, std::map>::iterator &texturePropsIt); - - /** - * @brief Render the text props - * - * @param texts The texts - * @param textPropsIt The iterator - */ - void _renderTextProps(std::map> &texts, std::map>::iterator &textPropsIt); -}; From 3d9fb17e02e57152ce86ce0c7d3176d0e7a5503a Mon Sep 17 00:00:00 2001 From: Yann Date: Sat, 30 Mar 2024 11:55:08 +0100 Subject: [PATCH 10/13] feat(core): add collisions between components --- core/src/Core.cpp | 176 +++--------------- core/src/Core.hpp | 33 ++-- .../games/components/ICollidableComponent.hpp | 4 +- shared/games/components/IComponent.hpp | 3 +- .../components/IDisplayableComponent.hpp | 10 +- ...mponent.hpp => IPositionableComponent.hpp} | 14 +- 6 files changed, 61 insertions(+), 179 deletions(-) rename shared/games/components/{IPositionComponent.hpp => IPositionableComponent.hpp} (51%) diff --git a/core/src/Core.cpp b/core/src/Core.cpp index 3f78d30..b27750f 100644 --- a/core/src/Core.cpp +++ b/core/src/Core.cpp @@ -146,7 +146,6 @@ void Core::_renderEntities() this->_window->display(); } - components::IKeyboardComponent::KeyData Core::_convertKeyPressData(events::IKeyEvent::KeyType type, events::IKeyEvent::KeyCode code) { components::IKeyboardComponent::KeyData keyCodeData; @@ -273,32 +272,32 @@ void Core::_handleDisplayableEvents(std::vector &events, std:: } } -// void Core::_handleCollisions(std::shared_ptr &component, std::shared_ptr &target) -// { -// auto componentPosition = component->getPosition(); -// auto componentSize = component->getSize(); -// auto targetPosition = target->getPosition(); -// auto targetSize = target->getSize(); - -// if (componentPosition.x < targetPosition.x + targetSize.x && -// componentPosition.x + componentSize.x > targetPosition.x && -// componentPosition.y < targetPosition.y + targetSize.y && -// componentPosition.y + componentSize.y > targetPosition.y) -// this->_handleCollidableComponents(component); -// } - -// void Core::_handleCollidableComponents(std::shared_ptr &component) -// { -// for (auto &entity : this->_gameEntities) { -// auto components = entity->getComponents(); -// for (auto &entityComponent : components) { -// if (entityComponent->getType() == components::COLLIDABLE) { -// auto collidable = std::dynamic_pointer_cast(entityComponent); -// this->_handleCollisions(component, collidable); -// } -// } -// } -// } +void Core::_handleCollisions(std::shared_ptr &component, std::shared_ptr &target) +{ + auto componentPosition = component->getPosition(); + auto componentSize = component->getSize(); + auto targetPosition = target->getPosition(); + auto targetSize = target->getSize(); + + if (componentPosition.x < targetPosition.x + targetSize.x && + componentPosition.x + componentSize.x > targetPosition.x && + componentPosition.y < targetPosition.y + targetSize.y && + componentPosition.y + componentSize.y > targetPosition.y) + component->onCollide(this->_game, target); +} + +void Core::_handleCollidableComponents(std::shared_ptr &component) +{ + for (auto &entity : this->_gameEntities) { + auto components = entity->getComponents(); + for (auto &entityComponent : components) { + if (entityComponent->getType() == components::COLLIDABLE) { + auto collidable = std::dynamic_pointer_cast(entityComponent); + this->_handleCollisions(component, collidable); + } + } + } +} void Core::_handleComponentEvents(std::vector &events, std::shared_ptr &component) { @@ -308,10 +307,14 @@ void Core::_handleComponentEvents(std::vector &events, std::sh auto keyboard = std::dynamic_pointer_cast(component); this->_handleKeyBoardEvents(events, keyboard); } - if (type == components::TEXTURE || type == components::TEXT) { + if (type == components::DISPLAYABLE) { auto displayable = std::dynamic_pointer_cast(component); this->_handleDisplayableEvents(events, displayable); } + if (type == components::COLLIDABLE) { + auto collidable = std::dynamic_pointer_cast(component); + this->_handleCollidableComponents(collidable); + } } void Core::_handleEvents() @@ -327,123 +330,6 @@ void Core::_handleEvents() } } -// void Core::_handleKeyPress(std::shared_ptr &keyEvent) -// { -// auto keyCode = keyEvent->getKeyCode(); -// auto keyType = keyEvent->getKeyType(); - -// for (auto &entity : this->_gameEntities) { -// auto components = entity->getComponents(); -// for (auto &component : components) { -// if (component->getType() == components::KEYBOARD) { -// auto keyboard = std::dynamic_pointer_cast(component); -// auto keyCodeData = this->_convertKeyPressData(keyType, keyCode); -// keyboard->onKeyPress(this->_game, keyCodeData); -// } -// } -// } -// } - -// void Core::_handleKeyRelease(std::shared_ptr &keyEvent) -// { -// auto keyCode = keyEvent->getKeyCode(); -// auto keyType = keyEvent->getKeyType(); - -// for (auto &entity : this->_gameEntities) { -// auto components = entity->getComponents(); -// for (auto &component : components) { -// if (component->getType() == components::KEYBOARD) { -// auto keyboard = std::dynamic_pointer_cast(component); -// auto keyCodeData = this->_convertKeyPressData(keyType, keyCode); -// keyboard->onKeyRelease(this->_game, keyCodeData); -// } -// } -// } -// } - -// void Core::_handleMouseButtonPress(std::shared_ptr &event) -// { -// auto button = event->getButton(); -// auto mousePosition = event->getPosition(); - -// for (auto &entity : this->_gameEntities) { -// auto components = entity->getComponents(); -// for (auto &component : components) { -// auto type = component->getType(); -// if (type == components::TEXTURE || type == components::TEXT) { -// auto displayable = std::dynamic_pointer_cast(component); -// auto entityPosition = displayable->getPosition(); -// auto entitySize = displayable->getSize(); -// if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + entitySize.x && -// mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + entitySize.y) -// displayable->onMousePress(this->_game); -// } -// } -// } -// } - -// void Core::_handleMouseMove(std::shared_ptr &event) -// { -// auto mousePosition = event->getPosition(); - -// for (auto &entity : this->_gameEntities) { -// auto components = entity->getComponents(); -// for (auto &component : components) { -// auto type = component->getType(); -// if (type == components::TEXTURE || type == components::TEXT) { -// auto displayable = std::dynamic_pointer_cast(component); -// auto entityPosition = displayable->getPosition(); -// auto entitySize = displayable->getSize(); -// if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + entitySize.x && -// mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + entitySize.y) -// displayable->onMouseHover(this->_game); -// } -// } -// } -// } - -// void Core::_handleMouseButtonRelease(std::shared_ptr &event) -// { -// auto button = event->getButton(); -// auto mousePosition = event->getPosition(); - -// for (auto &entity : this->_gameEntities) { -// auto components = entity->getComponents(); -// for (auto &component : components) { -// auto type = component->getType(); -// if (type == components::TEXTURE || type == components::TEXT) { -// auto displayable = std::dynamic_pointer_cast(component); -// auto entityPosition = displayable->getPosition(); -// auto entitySize = displayable->getSize(); -// if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + entitySize.x && -// mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + entitySize.y) -// displayable->onMouseRelease(this->_game); -// } -// } -// } -// } - -// void Core::_handleEvents() -// { -// auto gameEvents = this->_window->getEvents(); - -// for (auto &event : gameEvents) { -// auto type = event->getType(); -// if (type == events::WINDOW_CLOSE) -// this->_window->close(); -// if (type == events::WINDOW_RESIZE) -// std::cout << "Window resized" << std::endl; -// if (type == events::KEY_PRESS) { -// auto keyEvent = std::dynamic_pointer_cast(event); -// this->_handleKeyPress(keyEvent); -// } -// if (type == events::KEY_RELEASE) { -// auto keyEvent = std::dynamic_pointer_cast(event); -// this->_handleKeyRelease(keyEvent); -// } -// } -// } - void Core::run() { auto previousTime = std::chrono::high_resolution_clock::now(); diff --git a/core/src/Core.hpp b/core/src/Core.hpp index daf7c91..1983a12 100644 --- a/core/src/Core.hpp +++ b/core/src/Core.hpp @@ -125,12 +125,6 @@ class Core { */ void _handleEvents(); - // void _handleComponentEvent(std::shared_ptr &event, std::shared_ptr &component); - - // void _handleComponentEvent(std::shared_ptr &event, std::shared_ptr &component); - - // void _handleComponentEvent(std::shared_ptr &event, std::shared_ptr &component); - /** * @brief Handle the component events * @@ -155,13 +149,21 @@ class Core { */ void _handleDisplayableEvents(std::vector &events, std::shared_ptr &component); - // ======================================== PENDING ======================================== - - // void _handleCollidableComponents(std::shared_ptr &component); - - // void _handleCollisions(std::shared_ptr &component, std::shared_ptr &target); + /** + * @brief Handle the collidable events + * + * @param events Events to handle + * @param component The collidable component + */ + void _handleCollidableComponents(std::shared_ptr &component); - // ========================================================================================= + /** + * @brief Handle the collisions + * + * @param component The collidable component + * @param target The target collidable component + */ + void _handleCollisions(std::shared_ptr &component, std::shared_ptr &target); /** * @brief Handle the key press event @@ -244,11 +246,4 @@ class Core { * @return The converted key press data */ components::IKeyboardComponent::KeyData _convertKeyPressData(events::IKeyEvent::KeyType type, events::IKeyEvent::KeyCode code); - - // void _handleMouseButtonPress(std::shared_ptr &event); - - // void _handleMouseButtonRelease(std::shared_ptr &event); - - // void _handleMouseMove(std::shared_ptr &event); - }; diff --git a/shared/games/components/ICollidableComponent.hpp b/shared/games/components/ICollidableComponent.hpp index f6ce1c3..ba65ba0 100644 --- a/shared/games/components/ICollidableComponent.hpp +++ b/shared/games/components/ICollidableComponent.hpp @@ -8,14 +8,14 @@ #pragma once #include "../IGame.hpp" -#include "IPositionComponent.hpp" +#include "IPositionableComponent.hpp" #include "../../types/Vector.hpp" namespace shared::games::components { class ICollidableComponent; } -class shared::games::components::ICollidableComponent: public virtual IPositionComponent +class shared::games::components::ICollidableComponent: public virtual IPositionableComponent { public: virtual ~ICollidableComponent() = default; diff --git a/shared/games/components/IComponent.hpp b/shared/games/components/IComponent.hpp index e03cc51..db6c260 100644 --- a/shared/games/components/IComponent.hpp +++ b/shared/games/components/IComponent.hpp @@ -13,9 +13,10 @@ namespace shared::games::components { typedef enum { TEXTURE, TEXT, + DISPLAYABLE, SOUND, COLLIDABLE, - POSITION, + POSITIONABLE, KEYBOARD } ComponentType; diff --git a/shared/games/components/IDisplayableComponent.hpp b/shared/games/components/IDisplayableComponent.hpp index 0a60cec..8732895 100644 --- a/shared/games/components/IDisplayableComponent.hpp +++ b/shared/games/components/IDisplayableComponent.hpp @@ -7,7 +7,7 @@ #pragma once -#include "IPositionComponent.hpp" +#include "IPositionableComponent.hpp" #include "../IGame.hpp" #include "../../types/Vector.hpp" @@ -15,16 +15,10 @@ namespace shared::games::components { class IDisplayableComponent; } -class shared::games::components::IDisplayableComponent : public virtual IPositionComponent { +class shared::games::components::IDisplayableComponent : public virtual IPositionableComponent { public: virtual ~IDisplayableComponent() = default; - /** - * @brief Get size of the entity (tiles) - * - */ - virtual Vector2u &getSize() noexcept = 0; - /** * @brief Get Z index that is usefull for display prioroty * diff --git a/shared/games/components/IPositionComponent.hpp b/shared/games/components/IPositionableComponent.hpp similarity index 51% rename from shared/games/components/IPositionComponent.hpp rename to shared/games/components/IPositionableComponent.hpp index 4e508df..bc51501 100644 --- a/shared/games/components/IPositionComponent.hpp +++ b/shared/games/components/IPositionableComponent.hpp @@ -2,7 +2,7 @@ ** EPITECH PROJECT, 2024 ** arcade-shared ** File description: -** IPositionComponent +** IPositionableComponent */ #pragma once @@ -11,17 +11,23 @@ #include "../../types/Vector.hpp" namespace shared::games::components { - class IPositionComponent; + class IPositionableComponent; } -class shared::games::components::IPositionComponent: public virtual IComponent +class shared::games::components::IPositionableComponent: public virtual IComponent { public: - virtual ~IPositionComponent() = default; + virtual ~IPositionableComponent() = default; /** * @brief Get position of the entity (tiles) * */ virtual types::Vector2i &getPosition(void) noexcept = 0; + + /** + * @brief Get size of the entity (tiles) + * + */ + virtual types::Vector2u &getSize(void) noexcept = 0; }; From 835685ffa942bf768f50aaa7583cb95be72e04d8 Mon Sep 17 00:00:00 2001 From: Yann Date: Tue, 2 Apr 2024 10:16:24 +0200 Subject: [PATCH 11/13] feat(core): add sound management --- core/src/Core.cpp | 53 ++++++++++++++++++++++++++++++++++++ core/src/Core.hpp | 28 ++++++++++++++++--- core/src/types/Providers.hpp | 4 +-- 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/core/src/Core.cpp b/core/src/Core.cpp index b27750f..f2f078a 100644 --- a/core/src/Core.cpp +++ b/core/src/Core.cpp @@ -51,6 +51,20 @@ std::shared_ptr Core::_getFont(std::string path) return this->_fonts[path]; } +Core::SoundProps Core::_getSound(std::string path) +{ + if (this->_sounds.find(path) == this->_sounds.end()) { + SoundProps soundProps { + this->_graphicsProvider->createSound(path), + ISound::SoundState::STOP, + components::STOP + }; + soundProps.sound->setState(ISound::SoundState::STOP); + this->_sounds[path] = soundProps; + } + return this->_sounds[path]; +} + TextureProps Core::_getTextureEntity(std::shared_ptr texture) { auto textureProps = texture->getTextureProps(); @@ -299,6 +313,41 @@ void Core::_handleCollidableComponents(std::shared_ptr &gameSound) +{ + auto gameSoundPath = gameSound->getPath(); + auto sound = this->_getSound(gameSoundPath); + + auto gameSoundState = gameSound->getState(); + auto gameSoundVolume = gameSound->getVolume(); + auto gameSoundLoop = gameSound->getLoop(); + auto graphicSoundState = sound.sound->getState(); + auto graphicSoundVolume = sound.sound->getVolume(); + auto graphicSoundLoop = sound.sound->getLoopState(); + + if (gameSoundState != sound.previousGameState) { + if (gameSoundState == components::PLAY) + sound.sound->setState(ISound::SoundState::PLAY); + if (gameSoundState == components::PAUSE) + sound.sound->setState(ISound::SoundState::PAUSE); + if (gameSoundState == components::STOP) + sound.sound->setState(ISound::SoundState::STOP); + sound.previousGameState = gameSoundState; + } + if (graphicSoundState != sound.previousGraphicState) { + if (graphicSoundState == ISound::SoundState::PLAY) + gameSound->onStateChange(this->_game, components::PLAY); + if (graphicSoundState == ISound::SoundState::PAUSE) + gameSound->onStateChange(this->_game, components::PAUSE); + if (graphicSoundState == ISound::SoundState::STOP) + gameSound->onStateChange(this->_game, components::STOP); + } + if (gameSoundVolume != graphicSoundVolume) + sound.sound->setVolume(gameSoundVolume); + if (gameSoundLoop != graphicSoundLoop) + sound.sound->setLoopState(gameSoundLoop); +} + void Core::_handleComponentEvents(std::vector &events, std::shared_ptr &component) { auto type = component->getType(); @@ -315,6 +364,10 @@ void Core::_handleComponentEvents(std::vector &events, std::sh auto collidable = std::dynamic_pointer_cast(component); this->_handleCollidableComponents(collidable); } + if (type == components::SOUND) { + auto sound = std::dynamic_pointer_cast(component); + this->_handleSoundComponent(sound); + } } void Core::_handleEvents() diff --git a/core/src/Core.hpp b/core/src/Core.hpp index 1983a12..fa62d10 100644 --- a/core/src/Core.hpp +++ b/core/src/Core.hpp @@ -9,6 +9,7 @@ #include #include "types/Providers.hpp" +#include "shared/graphics/ISound.hpp" #include "shared/graphics/events/IKeyEvent.hpp" #include "shared/graphics/events/IMouseEvent.hpp" #include "shared/graphics/events/IMouseButtonEvent.hpp" @@ -17,6 +18,7 @@ #include "shared/games/components/IKeyboardComponent.hpp" #include "shared/games/components/IDisplayableComponent.hpp" #include "shared/games/components/ICollidableComponent.hpp" +#include "shared/games/components/ISoundComponent.hpp" using namespace shared::graphics; using namespace shared::games; @@ -34,14 +36,22 @@ class Core { protected: private: + + typedef struct { + std::shared_ptr sound; + ISound::SoundState previousGraphicState; + components::SoundState previousGameState; + } SoundProps; + std::shared_ptr _game; std::shared_ptr _window; - std::unique_ptr &_gameProvider; - std::unique_ptr &_graphicsProvider; + std::shared_ptr &_gameProvider; + std::shared_ptr &_graphicsProvider; std::map> _fonts; std::map> _textures; - const GameProviders &_gameProviders; - const GraphicsProviders &_graphicsProviders; + std::map _sounds; + GameProviders &_gameProviders; + GraphicsProviders &_graphicsProviders; entity::EntitiesMap _gameEntities; /** @@ -79,6 +89,14 @@ class Core { */ std::shared_ptr _getFont(std::string path); + /** + * @brief Get a sound + * + * @param path Path to the sound file + * @return The correct sound + */ + SoundProps _getSound(std::string path); + /** * @brief Get the texture entity * @@ -246,4 +264,6 @@ class Core { * @return The converted key press data */ components::IKeyboardComponent::KeyData _convertKeyPressData(events::IKeyEvent::KeyType type, events::IKeyEvent::KeyCode code); + + void _handleSoundComponent(std::shared_ptr &component); }; diff --git a/core/src/types/Providers.hpp b/core/src/types/Providers.hpp index c40fb9e..518f976 100644 --- a/core/src/types/Providers.hpp +++ b/core/src/types/Providers.hpp @@ -9,5 +9,5 @@ #include "shared/types/Libraries.hpp" -typedef std::vector> GameProviders; -typedef std::vector> GraphicsProviders; +typedef std::vector> GameProviders; +typedef std::vector> GraphicsProviders; From accd757aeccee582aca06e02b6df5a5a34fbe2ec Mon Sep 17 00:00:00 2001 From: Yann Date: Tue, 2 Apr 2024 15:45:22 +0200 Subject: [PATCH 12/13] fix(core): component event type --- core/src/Core.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/Core.cpp b/core/src/Core.cpp index f2f078a..fcfa3f0 100644 --- a/core/src/Core.cpp +++ b/core/src/Core.cpp @@ -356,7 +356,7 @@ void Core::_handleComponentEvents(std::vector &events, std::sh auto keyboard = std::dynamic_pointer_cast(component); this->_handleKeyBoardEvents(events, keyboard); } - if (type == components::DISPLAYABLE) { + if (type == components::TEXT || type == components::TEXTURE) { auto displayable = std::dynamic_pointer_cast(component); this->_handleDisplayableEvents(events, displayable); } From fd43875fc608bbba47952ab5700d70a542dd279b Mon Sep 17 00:00:00 2001 From: Yann Date: Tue, 2 Apr 2024 16:05:44 +0200 Subject: [PATCH 13/13] refactor(core): just for satify @flavien-chenu --- core/CMakeLists.txt | 6 +++--- core/src/CMakeLists.txt | 2 +- core/src/Core.cpp | 4 ++-- core/src/Core.hpp | 8 ++++++-- core/src/exception/CMakeLists.txt | 2 +- core/src/loader/CMakeLists.txt | 2 +- core/src/types/CMakeLists.txt | 2 +- core/src/utils/DLLoader/CMakeLists.txt | 2 +- 8 files changed, 16 insertions(+), 12 deletions(-) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 2b63a86..95198b9 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -1,8 +1,8 @@ -add_executable(${PROJECT_NAME} +add_executable(arcade main.cpp ) -target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/..) -target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src) +target_include_directories(arcade PUBLIC ${CMAKE_CURRENT_LIST_DIR}/..) +target_include_directories(arcade PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src) add_subdirectory(src) diff --git a/core/src/CMakeLists.txt b/core/src/CMakeLists.txt index 910a685..e2576e8 100644 --- a/core/src/CMakeLists.txt +++ b/core/src/CMakeLists.txt @@ -1,4 +1,4 @@ -target_sources(${CMAKE_PROJECT_NAME} PRIVATE +target_sources(arcade PRIVATE Core.cpp Core.hpp ) diff --git a/core/src/Core.cpp b/core/src/Core.cpp index fcfa3f0..48f4526 100644 --- a/core/src/Core.cpp +++ b/core/src/Core.cpp @@ -252,7 +252,7 @@ void Core::_handleWindowResize() std::cout << "Window resized" << std::endl; } -void Core::_handleKeyBoardEvents(std::vector &events, std::shared_ptr &component) +void Core::_handleKeyboardEvents(std::vector &events, std::shared_ptr &component) { for (auto &event : events) { auto type = event->getType(); @@ -354,7 +354,7 @@ void Core::_handleComponentEvents(std::vector &events, std::sh if (type == components::KEYBOARD) { auto keyboard = std::dynamic_pointer_cast(component); - this->_handleKeyBoardEvents(events, keyboard); + this->_handleKeyboardEvents(events, keyboard); } if (type == components::TEXT || type == components::TEXTURE) { auto displayable = std::dynamic_pointer_cast(component); diff --git a/core/src/Core.hpp b/core/src/Core.hpp index fa62d10..bcab8cc 100644 --- a/core/src/Core.hpp +++ b/core/src/Core.hpp @@ -34,7 +34,6 @@ class Core { */ void run(); - protected: private: typedef struct { @@ -157,7 +156,7 @@ class Core { * @param events Events to handle * @param component The keyboard component */ - void _handleKeyBoardEvents(std::vector &events, std::shared_ptr &component); + void _handleKeyboardEvents(std::vector &events, std::shared_ptr &component); /** * @brief Handle the displayable events @@ -265,5 +264,10 @@ class Core { */ components::IKeyboardComponent::KeyData _convertKeyPressData(events::IKeyEvent::KeyType type, events::IKeyEvent::KeyCode code); + /** + * @brief Handle the mouse button press event + * + * @param event The mouse button event + */ void _handleSoundComponent(std::shared_ptr &component); }; diff --git a/core/src/exception/CMakeLists.txt b/core/src/exception/CMakeLists.txt index a39fab8..36668bd 100644 --- a/core/src/exception/CMakeLists.txt +++ b/core/src/exception/CMakeLists.txt @@ -1,4 +1,4 @@ -target_sources(${CMAKE_PROJECT_NAME} PRIVATE +target_sources(arcade PRIVATE ArcadeError.cpp ArcadeError.hpp ) diff --git a/core/src/loader/CMakeLists.txt b/core/src/loader/CMakeLists.txt index 9cb0bf9..65d16a2 100644 --- a/core/src/loader/CMakeLists.txt +++ b/core/src/loader/CMakeLists.txt @@ -1,4 +1,4 @@ -target_sources(${CMAKE_PROJECT_NAME} PRIVATE +target_sources(arcade PRIVATE Loader.cpp Loader.hpp ) diff --git a/core/src/types/CMakeLists.txt b/core/src/types/CMakeLists.txt index 39bf466..3904d4b 100644 --- a/core/src/types/CMakeLists.txt +++ b/core/src/types/CMakeLists.txt @@ -1,3 +1,3 @@ -target_sources(${CMAKE_PROJECT_NAME} PRIVATE +target_sources(arcade PRIVATE Providers.hpp ) diff --git a/core/src/utils/DLLoader/CMakeLists.txt b/core/src/utils/DLLoader/CMakeLists.txt index 00db786..7b85e37 100644 --- a/core/src/utils/DLLoader/CMakeLists.txt +++ b/core/src/utils/DLLoader/CMakeLists.txt @@ -1,3 +1,3 @@ -target_sources(${CMAKE_PROJECT_NAME} PRIVATE +target_sources(arcade PRIVATE DLLoader.cpp )