From 492a09c1cd8a20a14a7d77ff4d37bed95408cfbb Mon Sep 17 00:00:00 2001 From: Matheo Coquet Date: Mon, 25 Mar 2024 18:57:18 +0100 Subject: [PATCH 01/38] feat: init sfml provider --- graphics/CMakeLists.txt | 1 + graphics/sfml/CMakeLists.txt | 7 +++++++ graphics/sfml/GraphicsProvider.cpp | 8 ++++++++ graphics/sfml/GraphicsProvider.hpp | 14 ++++++++++++++ graphics/sfml/export.cpp | 22 ++++++++++++++++++++++ shared/games/IGame.hpp | 6 ------ shared/graphics/IGraphicsProvider.hpp | 8 -------- shared/graphics/window/IWindow.hpp | 5 ++--- shared/graphics/window/IWindowIcon.hpp | 17 ----------------- 9 files changed, 54 insertions(+), 34 deletions(-) create mode 100644 graphics/sfml/CMakeLists.txt create mode 100644 graphics/sfml/GraphicsProvider.cpp create mode 100644 graphics/sfml/GraphicsProvider.hpp create mode 100644 graphics/sfml/export.cpp delete mode 100644 shared/graphics/window/IWindowIcon.hpp diff --git a/graphics/CMakeLists.txt b/graphics/CMakeLists.txt index c166dc2..1d54559 100644 --- a/graphics/CMakeLists.txt +++ b/graphics/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(ncurses) +add_subdirectory(sfml) diff --git a/graphics/sfml/CMakeLists.txt b/graphics/sfml/CMakeLists.txt new file mode 100644 index 0000000..bce39a5 --- /dev/null +++ b/graphics/sfml/CMakeLists.txt @@ -0,0 +1,7 @@ +add_library(sfml SHARED + export.cpp + GraphicsProvider.cpp + GraphicsProvider.hpp +) +target_include_directories(sfml PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src) +target_include_directories(sfml PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../..) diff --git a/graphics/sfml/GraphicsProvider.cpp b/graphics/sfml/GraphicsProvider.cpp new file mode 100644 index 0000000..aa3f23f --- /dev/null +++ b/graphics/sfml/GraphicsProvider.cpp @@ -0,0 +1,8 @@ +/* +** EPITECH PROJECT, 2024 +** GraphicsProvider.cpp +** File description: +** GraphicsProvider class +*/ + +#include "GraphicsProvider.hpp" diff --git a/graphics/sfml/GraphicsProvider.hpp b/graphics/sfml/GraphicsProvider.hpp new file mode 100644 index 0000000..dc49733 --- /dev/null +++ b/graphics/sfml/GraphicsProvider.hpp @@ -0,0 +1,14 @@ +/* +** EPITECH PROJECT, 2024 +** GraphicsProvider.hpp +** File description: +** GraphicsProvider class +*/ + +#pragma once + +#include "shared/graphics/IGraphicsProvider.hpp" + +class GraphicsProvider : public shared::graphics::IGraphicsProvider { + +}; diff --git a/graphics/sfml/export.cpp b/graphics/sfml/export.cpp new file mode 100644 index 0000000..8ba688b --- /dev/null +++ b/graphics/sfml/export.cpp @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2024 +** arcade-shared +** File description: +** export +*/ + +#include "shared/games/IGameProvider.hpp" +#include "shared/types/Libraries.hpp" + +extern "C" { + shared::types::LibraryType SHARED_LIBRARY_TYPE_GETTER_NAME(void) + { + return shared::types::LibraryType::GRAPHIC; + } + +/* std::shared_ptr SHARED_GRAPHICS_PROVIDER_LOADER_NAME(void) + { + return std::make_shared(...); + }*/ +} + diff --git a/shared/games/IGame.hpp b/shared/games/IGame.hpp index d888a3f..291ac21 100644 --- a/shared/games/IGame.hpp +++ b/shared/games/IGame.hpp @@ -39,12 +39,6 @@ class shared::games::IGame */ virtual const GameManifest &getManifest(void) const noexcept = 0; - /** - * @brief The minimum window size required for the game (pixels) - * - */ - virtual const Vector2u getWindowMinSize(void) const noexcept = 0; - /** * @brief Number of tiles that represent the game * Tile size is managed by the renderer diff --git a/shared/graphics/IGraphicsProvider.hpp b/shared/graphics/IGraphicsProvider.hpp index eab2ca9..f907c64 100644 --- a/shared/graphics/IGraphicsProvider.hpp +++ b/shared/graphics/IGraphicsProvider.hpp @@ -53,12 +53,4 @@ class shared::graphics::IGraphicsProvider { * @return Created texture object */ virtual std::shared_ptr createTexture(const std::string &path) = 0; - - /** - * @brief Create a window icon object - * - * @param path Path of the window icon file - * @return Created window icon object - */ - virtual std::unique_ptr createWindowIcon(const std::string &path) = 0; }; diff --git a/shared/graphics/window/IWindow.hpp b/shared/graphics/window/IWindow.hpp index 95fac0f..22f3fc3 100644 --- a/shared/graphics/window/IWindow.hpp +++ b/shared/graphics/window/IWindow.hpp @@ -10,7 +10,6 @@ #include #include -#include "IWindowIcon.hpp" #include "../events/IEvent.hpp" #include "../../types/types.hpp" #include "../types/EntityProps.hpp" @@ -101,14 +100,14 @@ class shared::graphics::IWindow { * * @param icon Icon to use */ - virtual void setIcon(std::unique_ptr icon) = 0; + virtual void setIcon(const std::string &icon) = 0; /** * @brief Get the icon of the window * * @return Icon object of the window */ - virtual const IWindowIcon &getIcon(void) const = 0; + virtual const std::string &getIcon(void) const = 0; /** * @brief Render the entity with given properties diff --git a/shared/graphics/window/IWindowIcon.hpp b/shared/graphics/window/IWindowIcon.hpp deleted file mode 100644 index 9dfdb83..0000000 --- a/shared/graphics/window/IWindowIcon.hpp +++ /dev/null @@ -1,17 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** arcade-shared -** File description: -** IWindowIcon -*/ - -#pragma once - -namespace shared::graphics { - class IWindowIcon; -} - -class shared::graphics::IWindowIcon { - public: - virtual ~IWindowIcon() = default; -}; From 7066796faea5138bb2b03cd33c1d2574a6f03c3d Mon Sep 17 00:00:00 2001 From: Matheo Coquet Date: Tue, 26 Mar 2024 13:45:57 +0100 Subject: [PATCH 02/38] feat(sfml): add window class --- graphics/sfml/CMakeLists.txt | 6 +- graphics/sfml/GraphicsProvider.cpp | 8 -- graphics/sfml/GraphicsProvider.hpp | 14 --- graphics/sfml/export.cpp | 7 +- graphics/sfml/src/GraphicsProvider.cpp | 32 ++++++ graphics/sfml/src/GraphicsProvider.hpp | 57 +++++++++++ graphics/sfml/src/window/Window.cpp | 8 ++ graphics/sfml/src/window/Window.hpp | 134 +++++++++++++++++++++++++ shared/graphics/IGraphicsProvider.hpp | 5 +- 9 files changed, 242 insertions(+), 29 deletions(-) delete mode 100644 graphics/sfml/GraphicsProvider.cpp delete mode 100644 graphics/sfml/GraphicsProvider.hpp create mode 100644 graphics/sfml/src/GraphicsProvider.cpp create mode 100644 graphics/sfml/src/GraphicsProvider.hpp create mode 100644 graphics/sfml/src/window/Window.cpp create mode 100644 graphics/sfml/src/window/Window.hpp diff --git a/graphics/sfml/CMakeLists.txt b/graphics/sfml/CMakeLists.txt index bce39a5..bb469b6 100644 --- a/graphics/sfml/CMakeLists.txt +++ b/graphics/sfml/CMakeLists.txt @@ -1,7 +1,9 @@ add_library(sfml SHARED export.cpp - GraphicsProvider.cpp - GraphicsProvider.hpp +#[[ src/GraphicsProvider.cpp + src/GraphicsProvider.hpp + src/window/Window.cpp + src/window/Window.hpp]] ) target_include_directories(sfml PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src) target_include_directories(sfml PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../..) diff --git a/graphics/sfml/GraphicsProvider.cpp b/graphics/sfml/GraphicsProvider.cpp deleted file mode 100644 index aa3f23f..0000000 --- a/graphics/sfml/GraphicsProvider.cpp +++ /dev/null @@ -1,8 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** GraphicsProvider.cpp -** File description: -** GraphicsProvider class -*/ - -#include "GraphicsProvider.hpp" diff --git a/graphics/sfml/GraphicsProvider.hpp b/graphics/sfml/GraphicsProvider.hpp deleted file mode 100644 index dc49733..0000000 --- a/graphics/sfml/GraphicsProvider.hpp +++ /dev/null @@ -1,14 +0,0 @@ -/* -** EPITECH PROJECT, 2024 -** GraphicsProvider.hpp -** File description: -** GraphicsProvider class -*/ - -#pragma once - -#include "shared/graphics/IGraphicsProvider.hpp" - -class GraphicsProvider : public shared::graphics::IGraphicsProvider { - -}; diff --git a/graphics/sfml/export.cpp b/graphics/sfml/export.cpp index 8ba688b..d137fff 100644 --- a/graphics/sfml/export.cpp +++ b/graphics/sfml/export.cpp @@ -5,10 +5,12 @@ ** export */ +#include #include "shared/games/IGameProvider.hpp" #include "shared/types/Libraries.hpp" +#include "src/GraphicsProvider.hpp" -extern "C" { +extern "C++" { shared::types::LibraryType SHARED_LIBRARY_TYPE_GETTER_NAME(void) { return shared::types::LibraryType::GRAPHIC; @@ -16,7 +18,6 @@ extern "C" { /* std::shared_ptr SHARED_GRAPHICS_PROVIDER_LOADER_NAME(void) { - return std::make_shared(...); + return std::make_shared(); }*/ } - diff --git a/graphics/sfml/src/GraphicsProvider.cpp b/graphics/sfml/src/GraphicsProvider.cpp new file mode 100644 index 0000000..fe6547b --- /dev/null +++ b/graphics/sfml/src/GraphicsProvider.cpp @@ -0,0 +1,32 @@ +/* +** EPITECH PROJECT, 2024 +** GraphicsProvider.cpp +** File description: +** GraphicsProvider class +*/ + +#include "GraphicsProvider.hpp" +#include "window/Window.hpp" + +const shared::graphics::GraphicsManifest sfml::GraphicsProvider::_manifest = { + .name = "sfml", + .description = "SFML Library", + .version = "1.0", + .authors = {{ + .name = "tekmath", + .email = "matheo.coquet@epitech.eu", + .website = "thismath.com" + }} +}; + +const shared::graphics::GraphicsManifest &sfml::GraphicsProvider::getManifest() { + return sfml::GraphicsProvider::_manifest; +} + +std::unique_ptr +sfml::GraphicsProvider::createWindow(const shared::graphics::WindowInitProps &windowProps) { + std::unique_ptr WindowObject = std::make_unique(windowProps.title, + windowProps.size); + + return WindowObject; +} diff --git a/graphics/sfml/src/GraphicsProvider.hpp b/graphics/sfml/src/GraphicsProvider.hpp new file mode 100644 index 0000000..e58013c --- /dev/null +++ b/graphics/sfml/src/GraphicsProvider.hpp @@ -0,0 +1,57 @@ +/* +** EPITECH PROJECT, 2024 +** GraphicsProvider.hpp +** File description: +** GraphicsProvider class +*/ + +#pragma once + +#include "shared/graphics/IGraphicsProvider.hpp" + +namespace sfml +{ + class GraphicsProvider; +} + +class sfml::GraphicsProvider : public shared::graphics::IGraphicsProvider +{ +public: + GraphicsProvider() = default; + ~GraphicsProvider() override = default; + + /** + * @brief Get the manifest of the graphics library + * + * @return Manifest of the graphics library + */ + const shared::graphics::GraphicsManifest &getManifest(); + + /** + * @brief Create a renderer object + * + * @param windowProps Properties to use to init the window + * @return Created renderer object + */ + std::unique_ptr createWindow(const shared::graphics::WindowInitProps &windowProps) override; + + /** + * @brief Create a sound object + * + * @param path Path of the sound file + * @return Created sound object + */ + std::shared_ptr createSound(const std::string &path) override; + + /** + * @brief Create a texture object + * + * @param bin Path of the binary texture file + * @param ascii Path of the ascii texture file + * @return Created texture object + */ + std::shared_ptr createTexture(const std::string &bin, const std::string &ascii) override; + +protected: + static const shared::graphics::GraphicsManifest _manifest; +}; diff --git a/graphics/sfml/src/window/Window.cpp b/graphics/sfml/src/window/Window.cpp new file mode 100644 index 0000000..ef7ab1a --- /dev/null +++ b/graphics/sfml/src/window/Window.cpp @@ -0,0 +1,8 @@ +/* +** EPITECH PROJECT, 2024 +** Window.cpp +** File description: +** Window class +*/ + +#include "Window.hpp" diff --git a/graphics/sfml/src/window/Window.hpp b/graphics/sfml/src/window/Window.hpp new file mode 100644 index 0000000..afd5f85 --- /dev/null +++ b/graphics/sfml/src/window/Window.hpp @@ -0,0 +1,134 @@ +/* +** EPITECH PROJECT, 2024 +** Window.hpp +** File description: +** Window class +*/ + +#pragma once + +#include "shared/graphics/window/IWindow.hpp" + +using namespace shared::graphics; +namespace sfml { + class Window; +} + +class sfml::Window : public IWindow { +public: + Window(std::string title, Vector2u size); + ~Window() override = default; + + /** + * @brief Set the title of current window + * + * @param title Title of the window + */ + void setTitle(const std::string &title); + + /** + * @brief Get the title of current window + * + * @return Title of the window + */ + std::string getTitle() const; + + /** + * @brief Set the size of the window + * + * @param size Size of the window + */ + void setSize(Vector2u size); + + /** + * @brief Get the size of the window + * + * @return Size of the window + */ + Vector2u getSize() const; + + /** + * @brief Set the framerate Limit of the window + * + * @param fps Frame per seconds + */ + void setFramerateLimit(unsigned int fps); + + /** + * @brief Get the framerate Limit of the window + * + * @return Frame per seconds + */ + unsigned int getFramerateLimit() const; + + /** + * @brief Set the mode of the window + * + * @param mode Mode to apply to the window + */ + void setMode(shared::graphics::WindowMode mode); + + /** + * @brief Get the mode of the window + * + * @return Mode of the window + */ + WindowMode getMode() const; + + /** + * @brief Set the icon of the window + * + * @param icon Icon to use + */ + void setIcon(const std::string &icon); + + /** + * @brief Get the icon of the window + * + * @return Icon object of the window + */ + const std::string &getIcon() const; + + /** + * @brief Render the entity with given properties + * + * @param props Properties of the entity to render + */ + void render(const EntityProps &props); + + /** + * @brief Clear the content of the window + * + */ + void clear(); + + /** + * @brief Display the content of the window + * + */ + void display(); + + /** + * @brief Close the window + * + */ + void close(); + + /** + * @brief Check if the window is open + * + * @return Open status of the window + */ + bool isOpen() const; + + /** + * @brief Get the events object + * + * @return Last events occured + * @warning Call successively this method will result in losing events + * @note Call `A` return `eventsA` containing 2 events, + * but make another call `B` (directly after call `A`) `eventsB` + * will result to an empty vector + */ + std::vector getEvents() = 0; +}; diff --git a/shared/graphics/IGraphicsProvider.hpp b/shared/graphics/IGraphicsProvider.hpp index f907c64..2d2b7cf 100644 --- a/shared/graphics/IGraphicsProvider.hpp +++ b/shared/graphics/IGraphicsProvider.hpp @@ -49,8 +49,9 @@ class shared::graphics::IGraphicsProvider { /** * @brief Create a texture object * - * @param path Path of the texture file + * @param bin Path of the binary texture file + * @param ascii Path of the ascii texture file * @return Created texture object */ - virtual std::shared_ptr createTexture(const std::string &path) = 0; + virtual std::shared_ptr createTexture(const std::string &bin, const std::string &ascii) = 0; }; From 233aca0328186c61bebabe553b1039281b221324 Mon Sep 17 00:00:00 2001 From: Yann Date: Tue, 26 Mar 2024 18:45:21 +0100 Subject: [PATCH 03/38] 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 04/38] 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 bf17c89dc2b4bc20fd7b1b7ccc49c2319dd0751c Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Tue, 26 Mar 2024 22:39:14 +0100 Subject: [PATCH 05/38] build: update shared library --- shared/games/IEntity.hpp | 16 +--- shared/games/IGame.hpp | 20 +++-- shared/games/components/IComponent.hpp | 8 -- shared/games/types/GameManifest.hpp | 29 +++--- shared/graphics/IGraphicsProvider.hpp | 9 +- shared/graphics/ISound.hpp | 17 ++-- shared/graphics/ITexture.hpp | 2 +- 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, 185 insertions(+), 598 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/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 291ac21..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,27 +36,29 @@ class shared::games::IGame /** * @brief Manifest with informations of the game * + * @return Manifest 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 * + * @return The number of tiles of the game */ - virtual const Vector2u getSize(void) const noexcept = 0; + virtual const Vector2u getSize() const noexcept = 0; /** - * @brief Get map of entities + * @brief Get fps of the game * + * @return The number of frame per seconds of the game */ - virtual const entity::EntitiesMap &getEntities(void) const = 0; + virtual const unsigned int getFps() const noexcept = 0; /** - * @brief Get entity by id + * @brief Get map of entities * - * @param id Id of the entity - * @return The specific entity + * @return Entities map of the game */ - virtual std::shared_ptr getEntityById(const UUId &id) const = 0; + virtual const entity::EntitiesMap &getEntities(void) 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/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/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/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; }; 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 3e2349e2c7def1cbca975f611e55f38b4d5244a7 Mon Sep 17 00:00:00 2001 From: Yann Date: Wed, 27 Mar 2024 10:36:59 +0100 Subject: [PATCH 06/38] 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 07/38] 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 08/38] 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 09/38] 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 3aedf86d078c1baa23bc2d1c714cb65b55c1c144 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Wed, 27 Mar 2024 19:53:13 +0100 Subject: [PATCH 10/38] feat(graphics): add SFML Window --- CMakeLists.txt | 2 +- core/main.cpp | 32 ++++-- core/src/loader/DLLoader.cpp | 18 ++- core/src/loader/DLLoader.hpp | 2 + core/src/types/Providers.hpp | 4 +- graphics/CMakeLists.txt | 2 +- graphics/sfml/CMakeLists.txt | 5 +- graphics/sfml/export.cpp | 17 +-- graphics/sfml/src/GraphicsProvider.cpp | 53 ++++++--- graphics/sfml/src/GraphicsProvider.hpp | 25 +++-- graphics/sfml/src/window/Window.cpp | 103 ++++++++++++++++++ graphics/sfml/src/window/Window.hpp | 59 +++++----- 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 +++++ shared/types/Libraries.hpp | 8 +- 28 files changed, 533 insertions(+), 213 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/CMakeLists.txt b/CMakeLists.txt index b23028f..bf775be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,5 +12,5 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ARCADE_LIB_DIR}) set(CMAKE_SHARED_LIBRARY_PREFIX "arcade_") add_subdirectory(core) -add_subdirectory(games) +#add_subdirectory(games) add_subdirectory(graphics) diff --git a/core/main.cpp b/core/main.cpp index 6e160ea..dc87911 100644 --- a/core/main.cpp +++ b/core/main.cpp @@ -7,16 +7,26 @@ #include "loader/DLLoader.hpp" -int main(void) -{ - DLLoader loader; +int main(void) { + DLLoader loader; - try { - loader.loadLibraries("./lib"); - std::cout << "Games libraries:" << loader.getGamesLibraries().size() << std::endl; - std::cout << "Graphics libraries:" << loader.getGraphicsLibraries().size() << std::endl; - } catch (const std::exception &e) { - std::cerr << e.what() << std::endl; - } - return 0; + try { + loader.loadLibraries("./lib"); + std::cout << "Games libraries: " << loader.getGamesLibraries().size() << std::endl; + std::cout << "Graphics libraries: " << loader.getGraphicsLibraries().size() << std::endl; + } catch (const std::exception &e) { + std::cerr << e.what() << std::endl; + } + auto graphics = loader.getGraphicsLibraries(); + auto provider = graphics[0]; + auto window = provider->createWindow({ + {1920, 1080}, + shared::graphics::IWindow::FULLSCREEN, + 60, + "Notre putain de snake" + }); + while (window->isOpen()) { + window->getEvents(); + } + return 0; } diff --git a/core/src/loader/DLLoader.cpp b/core/src/loader/DLLoader.cpp index a31a2bd..d107459 100644 --- a/core/src/loader/DLLoader.cpp +++ b/core/src/loader/DLLoader.cpp @@ -12,6 +12,15 @@ #include "DLLoader.hpp" #include "exception/ArcadeError.hpp" +DLLoader::DLLoader() = default; + +DLLoader::~DLLoader() { + for (auto &game : this->_gamesLibraries) + delete game; + for (auto &graphics : this->_graphicsLibraries) + delete graphics; +} + shared::types::LibraryType DLLoader::_getLibraryGetter(const std::string &filepath, void *handle) { shared::types::LibraryTypeGetter getter = nullptr; @@ -22,18 +31,15 @@ shared::types::LibraryType DLLoader::_getLibraryGetter(const std::string &filepa } void DLLoader::_loadGameLibrary(const std::string &filepath, void *handle) { - shared::types::GameProvider game = nullptr; + auto game = reinterpret_cast(dlsym(handle, SHARED_STRINGIFY(SHARED_GAME_PROVIDER_GETTER_NAME))); - game = reinterpret_cast(dlsym(handle, SHARED_STRINGIFY(SHARED_GAME_PROVIDER_LOADER_NAME))); if (!game) throw ArcadeError("Cannot find game provider in library: " + filepath); this->_gamesLibraries.push_back(game()); } void DLLoader::_loadGraphicsLibrary(const std::string &filepath, void *handle) { - shared::types::GraphicsProvider graphics = nullptr; - - graphics = reinterpret_cast(dlsym(handle, SHARED_STRINGIFY(SHARED_GRAPHICS_PROVIDER_LOADER_NAME))); + auto graphics = reinterpret_cast(dlsym(handle, SHARED_STRINGIFY(SHARED_GRAPHICS_PROVIDER_GETTER_NAME))); if (!graphics) throw ArcadeError("Cannot find graphics provider in library: " + filepath); this->_graphicsLibraries.push_back(graphics()); @@ -44,7 +50,7 @@ void DLLoader::registerLibrary(const std::string &filepath) { shared::types::LibraryType type; if (!handle) - throw ArcadeError("Cannot load library: " + filepath); + throw ArcadeError("Cannot load library: " + filepath + ": " + dlerror()); type = this->_getLibraryGetter(filepath, handle); if (type == shared::types::LibraryType::GAME) this->_loadGameLibrary(filepath, handle); diff --git a/core/src/loader/DLLoader.hpp b/core/src/loader/DLLoader.hpp index ec7b363..9ced32d 100644 --- a/core/src/loader/DLLoader.hpp +++ b/core/src/loader/DLLoader.hpp @@ -12,6 +12,8 @@ class DLLoader { public: + DLLoader(); + ~DLLoader(); /** * @brief Register a library diff --git a/core/src/types/Providers.hpp b/core/src/types/Providers.hpp index 518f976..7780708 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; diff --git a/graphics/CMakeLists.txt b/graphics/CMakeLists.txt index 1d54559..2715642 100644 --- a/graphics/CMakeLists.txt +++ b/graphics/CMakeLists.txt @@ -1,2 +1,2 @@ -add_subdirectory(ncurses) +#add_subdirectory(ncurses) add_subdirectory(sfml) diff --git a/graphics/sfml/CMakeLists.txt b/graphics/sfml/CMakeLists.txt index bb469b6..43687fc 100644 --- a/graphics/sfml/CMakeLists.txt +++ b/graphics/sfml/CMakeLists.txt @@ -1,9 +1,10 @@ add_library(sfml SHARED export.cpp -#[[ src/GraphicsProvider.cpp + src/GraphicsProvider.cpp src/GraphicsProvider.hpp src/window/Window.cpp - src/window/Window.hpp]] + src/window/Window.hpp ) target_include_directories(sfml PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src) target_include_directories(sfml PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../..) +target_link_libraries(sfml sfml-graphics sfml-window sfml-system) diff --git a/graphics/sfml/export.cpp b/graphics/sfml/export.cpp index d137fff..4373075 100644 --- a/graphics/sfml/export.cpp +++ b/graphics/sfml/export.cpp @@ -5,19 +5,22 @@ ** export */ -#include #include "shared/games/IGameProvider.hpp" #include "shared/types/Libraries.hpp" #include "src/GraphicsProvider.hpp" -extern "C++" { - shared::types::LibraryType SHARED_LIBRARY_TYPE_GETTER_NAME(void) +using namespace arcade::graphics; +using namespace shared::graphics; +using namespace shared::types; + +extern "C" { + 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_GETTER_NAME(void) { - return std::make_shared(); - }*/ + return new sfml::GraphicsProvider(); + } } diff --git a/graphics/sfml/src/GraphicsProvider.cpp b/graphics/sfml/src/GraphicsProvider.cpp index fe6547b..79aada4 100644 --- a/graphics/sfml/src/GraphicsProvider.cpp +++ b/graphics/sfml/src/GraphicsProvider.cpp @@ -5,28 +5,49 @@ ** GraphicsProvider class */ +#include #include "GraphicsProvider.hpp" #include "window/Window.hpp" -const shared::graphics::GraphicsManifest sfml::GraphicsProvider::_manifest = { - .name = "sfml", - .description = "SFML Library", - .version = "1.0", - .authors = {{ - .name = "tekmath", - .email = "matheo.coquet@epitech.eu", - .website = "thismath.com" - }} +using namespace shared::graphics; +using namespace arcade::graphics::sfml; + +const shared::graphics::GraphicsManifest GraphicsProvider::_manifest = { + .name = "sfml", + .description = "SFML Library", + .version = "1.0", + .authors = { + { + .name = "tekmath", + .email = "matheo.coquet@epitech.eu", + .website = "thismath.com" + }, + { + .name = "Flavien Chenu", + .email = "flavien.chenu@epitech.eu", + .website = "https://github.com/flavien-chenu" + } + } }; -const shared::graphics::GraphicsManifest &sfml::GraphicsProvider::getManifest() { - return sfml::GraphicsProvider::_manifest; +GraphicsProvider::GraphicsProvider() = default; + +const GraphicsManifest &GraphicsProvider::getManifest() const noexcept { + return GraphicsProvider::_manifest; +} + +std::unique_ptr GraphicsProvider::createWindow(const IWindow::WindowInitProps &props) { + return std::make_unique(props); } -std::unique_ptr -sfml::GraphicsProvider::createWindow(const shared::graphics::WindowInitProps &windowProps) { - std::unique_ptr WindowObject = std::make_unique(windowProps.title, - windowProps.size); +std::shared_ptr GraphicsProvider::createSound(const std::string &path) { + return nullptr; +} + +std::shared_ptr GraphicsProvider::createTexture(const std::string &bin, const std::string &ascii) { + return nullptr; +} - return WindowObject; +std::shared_ptr GraphicsProvider::createFont(const std::string &path) { + return nullptr; } diff --git a/graphics/sfml/src/GraphicsProvider.hpp b/graphics/sfml/src/GraphicsProvider.hpp index e58013c..e9ff58d 100644 --- a/graphics/sfml/src/GraphicsProvider.hpp +++ b/graphics/sfml/src/GraphicsProvider.hpp @@ -7,17 +7,20 @@ #pragma once +#include "shared/graphics/ITexture.hpp" #include "shared/graphics/IGraphicsProvider.hpp" -namespace sfml +using namespace shared::graphics; + +namespace arcade::graphics::sfml { class GraphicsProvider; } -class sfml::GraphicsProvider : public shared::graphics::IGraphicsProvider +class arcade::graphics::sfml::GraphicsProvider : public shared::graphics::IGraphicsProvider { public: - GraphicsProvider() = default; + GraphicsProvider(); ~GraphicsProvider() override = default; /** @@ -25,7 +28,7 @@ class sfml::GraphicsProvider : public shared::graphics::IGraphicsProvider * * @return Manifest of the graphics library */ - const shared::graphics::GraphicsManifest &getManifest(); + const shared::graphics::GraphicsManifest &getManifest() const noexcept override; /** * @brief Create a renderer object @@ -33,7 +36,7 @@ class sfml::GraphicsProvider : public shared::graphics::IGraphicsProvider * @param windowProps Properties to use to init the window * @return Created renderer object */ - std::unique_ptr createWindow(const shared::graphics::WindowInitProps &windowProps) override; + std::unique_ptr createWindow(const IWindow::WindowInitProps &windowProps) override; /** * @brief Create a sound object @@ -41,7 +44,7 @@ class sfml::GraphicsProvider : public shared::graphics::IGraphicsProvider * @param path Path of the sound file * @return Created sound object */ - std::shared_ptr createSound(const std::string &path) override; + std::shared_ptr createSound(const std::string &path) override; /** * @brief Create a texture object @@ -50,7 +53,15 @@ class sfml::GraphicsProvider : public shared::graphics::IGraphicsProvider * @param ascii Path of the ascii texture file * @return Created texture object */ - std::shared_ptr createTexture(const std::string &bin, const std::string &ascii) override; + std::shared_ptr createTexture(const std::string &bin, const std::string &ascii) override; + + /** + * @brief Create a font object + * + * @param path Path of the font file + * @return Created font object + */ + std::shared_ptr createFont(const std::string &path) override; protected: static const shared::graphics::GraphicsManifest _manifest; diff --git a/graphics/sfml/src/window/Window.cpp b/graphics/sfml/src/window/Window.cpp index ef7ab1a..f049c16 100644 --- a/graphics/sfml/src/window/Window.cpp +++ b/graphics/sfml/src/window/Window.cpp @@ -5,4 +5,107 @@ ** Window class */ +#include #include "Window.hpp" + +using namespace arcade::graphics::sfml; + +Window::Window(const IWindow::WindowInitProps &props) +{ + this->_mode = props.mode; + this->_fps = props.fps; + this->_window = std::make_unique( + sf::VideoMode(props.size.x, props.size.y), + props.title + ); +} + +Window::~Window() +{ + this->_window->close(); +} + +void Window::setTitle(const std::string &title) { + this->_window->setTitle(title); +} + +void Window::setSize(shared::types::Vector2u size) { + (void) size; + //this->_window->setSize(sf::Vector2u(size.x, size.y)); +} + +shared::types::Vector2u Window::getSize() const { + return {12, 12}; + //return shared::types::Vector2u(this->_window->getSize().x, this->_window->getSize().y); +} + +void Window::setFramerateLimit(unsigned int fps) { + this->_window->setFramerateLimit(fps); + this->_fps = fps; +} + +unsigned int Window::getFramerateLimit() const { + return this->_fps; +} + +void Window::setMode(IWindow::WindowMode mode) { + this->_mode = mode; + if (mode == FULLSCREEN) { + this->_window->create( + sf::VideoMode(1920, 1080), + this->_title, + sf::Style::Fullscreen + ); + } else { + this->_window->create( + sf::VideoMode(1920, 1080), + this->_title, + sf::Style::Default + ); + } +} + +IWindow::WindowMode Window::getMode() const { + return this->_mode; +} + +bool Window::isOpen() const { + return this->_window->isOpen(); +} + +void Window::setIcon(const std::string &icon) { + (void) icon; +} +void Window::render(const TextProps &props) { + (void) props; +} + +void Window::render(const TextureProps &props) { + (void) props; +} + +void Window::clear() { + this->_window->clear(); +} + +void Window::display() { + this->_window->display(); +} + +void Window::close() { + this->_window->close(); +} + +std::vector Window::getEvents() { + sf::Event event{}; + std::vector events; + + while (this->_window->pollEvent(event)) { + if (event.type == sf::Event::Closed) { + this->_window->close(); + return events; + } + std::cout << "Event received" << std::endl; + } + return events; +} diff --git a/graphics/sfml/src/window/Window.hpp b/graphics/sfml/src/window/Window.hpp index afd5f85..d0cbfd6 100644 --- a/graphics/sfml/src/window/Window.hpp +++ b/graphics/sfml/src/window/Window.hpp @@ -7,119 +7,114 @@ #pragma once -#include "shared/graphics/window/IWindow.hpp" +#include +#include "shared/graphics/IWindow.hpp" using namespace shared::graphics; -namespace sfml { + +namespace arcade::graphics::sfml{ class Window; } -class sfml::Window : public IWindow { +class arcade::graphics::sfml::Window : public IWindow { public: - Window(std::string title, Vector2u size); - ~Window() override = default; + explicit Window(const IWindow::WindowInitProps &props); + ~Window() override; /** * @brief Set the title of current window * * @param title Title of the window */ - void setTitle(const std::string &title); - - /** - * @brief Get the title of current window - * - * @return Title of the window - */ - std::string getTitle() const; + void setTitle(const std::string &title) override; /** * @brief Set the size of the window * * @param size Size of the window */ - void setSize(Vector2u size); + void setSize(Vector2u size) override; /** * @brief Get the size of the window * * @return Size of the window */ - Vector2u getSize() const; + Vector2u getSize() const override; /** * @brief Set the framerate Limit of the window * * @param fps Frame per seconds */ - void setFramerateLimit(unsigned int fps); + void setFramerateLimit(unsigned int fps) override; /** * @brief Get the framerate Limit of the window * * @return Frame per seconds */ - unsigned int getFramerateLimit() const; + unsigned int getFramerateLimit() const override; /** * @brief Set the mode of the window * * @param mode Mode to apply to the window */ - void setMode(shared::graphics::WindowMode mode); + void setMode(IWindow::WindowMode mode) override; /** * @brief Get the mode of the window * * @return Mode of the window */ - WindowMode getMode() const; + WindowMode getMode() const override; /** * @brief Set the icon of the window * * @param icon Icon to use */ - void setIcon(const std::string &icon); + void setIcon(const std::string &icon) override; /** - * @brief Get the icon of the window + * @brief Render the text with given properties * - * @return Icon object of the window + * @param props Properties of the entity to render */ - const std::string &getIcon() const; + void render(const TextProps &props) override; /** * @brief Render the entity with given properties * * @param props Properties of the entity to render */ - void render(const EntityProps &props); + void render(const TextureProps &props) override; /** * @brief Clear the content of the window * */ - void clear(); + void clear() override; /** * @brief Display the content of the window * */ - void display(); + void display() override; /** * @brief Close the window * */ - void close(); + void close() override; /** * @brief Check if the window is open * * @return Open status of the window */ - bool isOpen() const; + bool isOpen() const override; /** * @brief Get the events object @@ -130,5 +125,11 @@ class sfml::Window : public IWindow { * but make another call `B` (directly after call `A`) `eventsB` * will result to an empty vector */ - std::vector getEvents() = 0; + std::vector getEvents() override; + +private: + std::unique_ptr _window; + std::string _title; + unsigned int _fps; + WindowMode _mode; }; 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; +} diff --git a/shared/types/Libraries.hpp b/shared/types/Libraries.hpp index 9711994..1d2629f 100644 --- a/shared/types/Libraries.hpp +++ b/shared/types/Libraries.hpp @@ -10,8 +10,8 @@ #include "../games/IGameProvider.hpp" #include "../graphics/IGraphicsProvider.hpp" -#define SHARED_GAME_PROVIDER_LOADER_NAME arcadeLibGetGameProvider -#define SHARED_GRAPHICS_PROVIDER_LOADER_NAME arcadeLibGetGraphicsProvider +#define SHARED_GAME_PROVIDER_GETTER_NAME arcadeLibGetGameProvider +#define SHARED_GRAPHICS_PROVIDER_GETTER_NAME arcadeLibGetGraphicsProvider #define SHARED_LIBRARY_TYPE_GETTER_NAME arcadeLibGetType #define STRINGIFY(x) #x #define SHARED_STRINGIFY(x) STRINGIFY(x) @@ -23,7 +23,7 @@ namespace shared::types GRAPHIC, } LibraryType; - typedef std::shared_ptr (*GameProvider)(void); - typedef std::shared_ptr (*GraphicsProvider)(void); + typedef games::IGameProvider* (*GameProviderGetter)(void); + typedef graphics::IGraphicsProvider* (*GraphicsProviderGetter)(void); typedef LibraryType (*LibraryTypeGetter)(void); } From fb681bf8c32ca94771033d7d5114d746e3f065da Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Wed, 27 Mar 2024 21:45:18 +0100 Subject: [PATCH 11/38] feat(graphics): add common events classes --- graphics/common/CMakeLists.txt | 2 + graphics/common/events/CMakeLists.txt | 2 + graphics/common/events/key/AKeyEvent.hpp | 39 ++++++++++++++ graphics/common/events/key/CMakeLists.txt | 6 +++ graphics/common/events/key/KeyPressEvent.hpp | 19 +++++++ .../common/events/key/KeyReleaseEvent.hpp | 19 +++++++ graphics/common/events/key/key.hpp | 11 ++++ .../common/events/mouse/AMouseButtonEvent.hpp | 52 +++++++++++++++++++ graphics/common/events/mouse/AMouseEvent.hpp | 43 +++++++++++++++ graphics/common/events/mouse/CMakeLists.txt | 8 +++ .../events/mouse/MouseButtonPressEvent.hpp | 23 ++++++++ .../events/mouse/MouseButtonReleaseEvent.hpp | 23 ++++++++ .../common/events/mouse/MouseMoveEvent.hpp | 25 +++++++++ graphics/common/events/mouse/mouse.hpp | 12 +++++ graphics/sfml/CMakeLists.txt | 11 ++-- graphics/sfml/src/window/Window.cpp | 8 ++- 16 files changed, 299 insertions(+), 4 deletions(-) create mode 100644 graphics/common/CMakeLists.txt create mode 100644 graphics/common/events/CMakeLists.txt create mode 100644 graphics/common/events/key/AKeyEvent.hpp create mode 100644 graphics/common/events/key/CMakeLists.txt create mode 100644 graphics/common/events/key/KeyPressEvent.hpp create mode 100644 graphics/common/events/key/KeyReleaseEvent.hpp create mode 100644 graphics/common/events/key/key.hpp create mode 100644 graphics/common/events/mouse/AMouseButtonEvent.hpp create mode 100644 graphics/common/events/mouse/AMouseEvent.hpp create mode 100644 graphics/common/events/mouse/CMakeLists.txt create mode 100644 graphics/common/events/mouse/MouseButtonPressEvent.hpp create mode 100644 graphics/common/events/mouse/MouseButtonReleaseEvent.hpp create mode 100644 graphics/common/events/mouse/MouseMoveEvent.hpp create mode 100644 graphics/common/events/mouse/mouse.hpp diff --git a/graphics/common/CMakeLists.txt b/graphics/common/CMakeLists.txt new file mode 100644 index 0000000..ec53431 --- /dev/null +++ b/graphics/common/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(events) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../..) diff --git a/graphics/common/events/CMakeLists.txt b/graphics/common/events/CMakeLists.txt new file mode 100644 index 0000000..9fa6de9 --- /dev/null +++ b/graphics/common/events/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(mouse) +add_subdirectory(key) diff --git a/graphics/common/events/key/AKeyEvent.hpp b/graphics/common/events/key/AKeyEvent.hpp new file mode 100644 index 0000000..8896d59 --- /dev/null +++ b/graphics/common/events/key/AKeyEvent.hpp @@ -0,0 +1,39 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** AKeyEvent.hpp +*/ + +#pragma once + +#include "shared/graphics/events/IKeyEvent.hpp" + +using namespace shared::graphics::events; + +namespace arcade::graphics::common::events { + template + concept KeyEventType = T == EventType::KEY_PRESS || T == EventType::KEY_RELEASE; + + template requires KeyEventType + class AKeyEvent; +} + +using namespace arcade::graphics::common::events; + +template requires KeyEventType +class arcade::graphics::common::events::AKeyEvent: public IKeyEvent { +public: + ~AKeyEvent() override = default; + + EventType getType() const noexcept override { + return _type; + } + +protected: + AKeyEvent(const KeyType keyType, const KeyCode code) : _keyType(keyType), _code(code) {}; + + const EventType _type = T; + const KeyType _keyType; + const KeyCode _code; +}; diff --git a/graphics/common/events/key/CMakeLists.txt b/graphics/common/events/key/CMakeLists.txt new file mode 100644 index 0000000..ea02f33 --- /dev/null +++ b/graphics/common/events/key/CMakeLists.txt @@ -0,0 +1,6 @@ +target_sources(${PROJECT_NAME} PUBLIC + AKeyEvent.hpp + KeyPressEvent.hpp + KeyReleaseEvent.hpp + key.hpp +) diff --git a/graphics/common/events/key/KeyPressEvent.hpp b/graphics/common/events/key/KeyPressEvent.hpp new file mode 100644 index 0000000..5a79aac --- /dev/null +++ b/graphics/common/events/key/KeyPressEvent.hpp @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** KeyPressEvent.hpp +*/ + +#pragma once + +#include "AKeyEvent.hpp" + +namespace arcade::graphics::common::events { + class KeyPressEvent; +} + +class arcade::graphics::common::events::KeyPressEvent: public AKeyEvent { +public: + using AKeyEvent::AKeyEvent; +}; diff --git a/graphics/common/events/key/KeyReleaseEvent.hpp b/graphics/common/events/key/KeyReleaseEvent.hpp new file mode 100644 index 0000000..b4c0c0d --- /dev/null +++ b/graphics/common/events/key/KeyReleaseEvent.hpp @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** KeyPressEvent.hpp +*/ + +#pragma once + +#include "AKeyEvent.hpp" + +namespace arcade::graphics::common::events { + class KeyReleaseEvent; +} + +class arcade::graphics::common::events::KeyReleaseEvent: public AKeyEvent { +public: + using AKeyEvent::AKeyEvent; +}; diff --git a/graphics/common/events/key/key.hpp b/graphics/common/events/key/key.hpp new file mode 100644 index 0000000..4702d1c --- /dev/null +++ b/graphics/common/events/key/key.hpp @@ -0,0 +1,11 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** mouse.hpp +*/ + +#pragma once + +#include "KeyPressEvent.hpp" +#include "KeyReleaseEvent.hpp" diff --git a/graphics/common/events/mouse/AMouseButtonEvent.hpp b/graphics/common/events/mouse/AMouseButtonEvent.hpp new file mode 100644 index 0000000..0f7eb8c --- /dev/null +++ b/graphics/common/events/mouse/AMouseButtonEvent.hpp @@ -0,0 +1,52 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** AMouseEvent.hpp +*/ + +#pragma once + +#include "AMouseEvent.hpp" +#include "shared/graphics/events/IMouseButtonEvent.hpp" + +using namespace shared::graphics::events; +using namespace shared::types; +using namespace arcade::graphics::common::events; + +namespace arcade::graphics::common::events { + template + concept MouseButtonEventType = T == EventType::MOUSE_BTN_PRESS || T == EventType::MOUSE_BTN_RELEASE; + + template requires MouseButtonEventType + class AMouseButtonEvent; +} + +template requires MouseButtonEventType +class arcade::graphics::common::events::AMouseButtonEvent: public AMouseEvent, public IMouseButtonEvent { +public: + /** + * @brief Construct a new AMouseButtonEvent object + * + * @param pos Position of the mouse + * @param button Targeted button + */ + explicit AMouseButtonEvent( + Vector2i pos, + MouseButton button + ): AMouseEvent(pos), _button(button) {}; + + ~AMouseButtonEvent() override = default; + + /** + * @brief Get the button object + * + * @return Target button type + */ + const MouseButton getButton() const noexcept override { + return _button; + } + +protected: + const MouseButton _button; +}; diff --git a/graphics/common/events/mouse/AMouseEvent.hpp b/graphics/common/events/mouse/AMouseEvent.hpp new file mode 100644 index 0000000..1e77f70 --- /dev/null +++ b/graphics/common/events/mouse/AMouseEvent.hpp @@ -0,0 +1,43 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** AMouseEvent.hpp +*/ + +#pragma once + +#include "shared/graphics/events/IMouseEvent.hpp" + +using namespace shared::graphics::events; +using namespace shared::types; + +namespace arcade::graphics::common::events { + template + class AMouseEvent; +} + +template +class arcade::graphics::common::events::AMouseEvent: public IMouseEvent { +public: + ~AMouseEvent() override = default; + + EventType getType() const noexcept override { + return _type; + } + + const Vector2i getPosition() const noexcept override { + return _pos; + } + +protected: + /** + * @brief Construct a new AMouseEvent object + * + * @param pos Position of the mouse + */ + explicit AMouseEvent(Vector2i pos): _pos(pos){}; + + const EventType _type = T; + Vector2i _pos; +}; diff --git a/graphics/common/events/mouse/CMakeLists.txt b/graphics/common/events/mouse/CMakeLists.txt new file mode 100644 index 0000000..23a780a --- /dev/null +++ b/graphics/common/events/mouse/CMakeLists.txt @@ -0,0 +1,8 @@ +target_sources(${PROJECT_NAME} PUBLIC + AMouseEvent.hpp + MouseButtonPressEvent.hpp + MouseButtonReleaseEvent.hpp + AMouseButtonEvent.hpp + MouseMoveEvent.hpp + mouse.hpp +) diff --git a/graphics/common/events/mouse/MouseButtonPressEvent.hpp b/graphics/common/events/mouse/MouseButtonPressEvent.hpp new file mode 100644 index 0000000..94e08a5 --- /dev/null +++ b/graphics/common/events/mouse/MouseButtonPressEvent.hpp @@ -0,0 +1,23 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** MouseButtonPressEvent.hpp +*/ + +#pragma once + +#include "AMouseButtonEvent.hpp" + +using namespace shared::graphics::events; +using namespace arcade::graphics::common::events; + +namespace arcade::graphics::common::events { + class MouseButtonPressEvent; +}; + +class arcade::graphics::common::events::MouseButtonPressEvent : + public AMouseButtonEvent { +public: + using AMouseButtonEvent::AMouseButtonEvent; +}; diff --git a/graphics/common/events/mouse/MouseButtonReleaseEvent.hpp b/graphics/common/events/mouse/MouseButtonReleaseEvent.hpp new file mode 100644 index 0000000..98638e6 --- /dev/null +++ b/graphics/common/events/mouse/MouseButtonReleaseEvent.hpp @@ -0,0 +1,23 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** MouseButtonPressEvent.hpp +*/ + +#pragma once + +#include "AMouseButtonEvent.hpp" + +using namespace shared::graphics::events; +using namespace arcade::graphics::common::events; + +namespace arcade::graphics::common::events { + class MouseButtonReleaseEvent; +}; + +class arcade::graphics::common::events::MouseButtonReleaseEvent : + public AMouseButtonEvent { +public: + using AMouseButtonEvent::AMouseButtonEvent; +}; diff --git a/graphics/common/events/mouse/MouseMoveEvent.hpp b/graphics/common/events/mouse/MouseMoveEvent.hpp new file mode 100644 index 0000000..3dc3131 --- /dev/null +++ b/graphics/common/events/mouse/MouseMoveEvent.hpp @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** AMouseEvent.hpp +*/ + +#pragma once + +#include "AMouseEvent.hpp" + +using namespace shared::graphics::events; +using namespace shared::types; + +namespace arcade::graphics::common::events { + class MouseMoveEvent; +} + +class arcade::graphics::common::events::MouseMoveEvent: public AMouseEvent { +public: + + explicit MouseMoveEvent(Vector2i pos): AMouseEvent(pos) {} + + ~MouseMoveEvent() override = default; +}; diff --git a/graphics/common/events/mouse/mouse.hpp b/graphics/common/events/mouse/mouse.hpp new file mode 100644 index 0000000..662df31 --- /dev/null +++ b/graphics/common/events/mouse/mouse.hpp @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** mouse.hpp +*/ + +#pragma once + +#include "MouseButtonPressEvent.hpp" +#include "MouseButtonReleaseEvent.hpp" +#include "MouseMoveEvent.hpp" diff --git a/graphics/sfml/CMakeLists.txt b/graphics/sfml/CMakeLists.txt index 43687fc..805d974 100644 --- a/graphics/sfml/CMakeLists.txt +++ b/graphics/sfml/CMakeLists.txt @@ -1,10 +1,15 @@ -add_library(sfml SHARED +project(sfml) +add_library(${PROJECT_NAME} SHARED export.cpp src/GraphicsProvider.cpp src/GraphicsProvider.hpp src/window/Window.cpp src/window/Window.hpp ) -target_include_directories(sfml PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src) -target_include_directories(sfml PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../..) +add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../common PRIVATE) + +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../..) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/..) + target_link_libraries(sfml sfml-graphics sfml-window sfml-system) diff --git a/graphics/sfml/src/window/Window.cpp b/graphics/sfml/src/window/Window.cpp index f049c16..eb6418d 100644 --- a/graphics/sfml/src/window/Window.cpp +++ b/graphics/sfml/src/window/Window.cpp @@ -7,6 +7,7 @@ #include #include "Window.hpp" +#include "common/events/mouse/mouse.hpp" using namespace arcade::graphics::sfml; @@ -105,7 +106,12 @@ std::vector Window::getEvents() { this->_window->close(); return events; } - std::cout << "Event received" << std::endl; + if (event.type == sf::Event::MouseMoved) { + auto mouseEvent = std::make_shared( + Vector2i(event.mouseMove.x, event.mouseMove.y) + ); + events.push_back(mouseEvent); + } } return events; } From b3380f2f4147e100f5fcb3c7674b3e835993da09 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Wed, 27 Mar 2024 21:52:22 +0100 Subject: [PATCH 12/38] feat(graphics): add common window events --- graphics/common/events/CMakeLists.txt | 1 + graphics/common/events/key/AKeyEvent.hpp | 3 +-- graphics/common/events/mouse/AMouseEvent.hpp | 3 +-- graphics/common/events/window/CMakeLists.txt | 4 +++ .../common/events/window/WindowCloseEvent.hpp | 27 +++++++++++++++++++ .../events/window/WindowResizeEvent.hpp | 27 +++++++++++++++++++ 6 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 graphics/common/events/window/CMakeLists.txt create mode 100644 graphics/common/events/window/WindowCloseEvent.hpp create mode 100644 graphics/common/events/window/WindowResizeEvent.hpp diff --git a/graphics/common/events/CMakeLists.txt b/graphics/common/events/CMakeLists.txt index 9fa6de9..b6abb48 100644 --- a/graphics/common/events/CMakeLists.txt +++ b/graphics/common/events/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(mouse) add_subdirectory(key) +add_subdirectory(window) diff --git a/graphics/common/events/key/AKeyEvent.hpp b/graphics/common/events/key/AKeyEvent.hpp index 8896d59..735e9a0 100644 --- a/graphics/common/events/key/AKeyEvent.hpp +++ b/graphics/common/events/key/AKeyEvent.hpp @@ -27,13 +27,12 @@ class arcade::graphics::common::events::AKeyEvent: public IKeyEvent { ~AKeyEvent() override = default; EventType getType() const noexcept override { - return _type; + return T; } protected: AKeyEvent(const KeyType keyType, const KeyCode code) : _keyType(keyType), _code(code) {}; - const EventType _type = T; const KeyType _keyType; const KeyCode _code; }; diff --git a/graphics/common/events/mouse/AMouseEvent.hpp b/graphics/common/events/mouse/AMouseEvent.hpp index 1e77f70..b77ed90 100644 --- a/graphics/common/events/mouse/AMouseEvent.hpp +++ b/graphics/common/events/mouse/AMouseEvent.hpp @@ -23,7 +23,7 @@ class arcade::graphics::common::events::AMouseEvent: public IMouseEvent { ~AMouseEvent() override = default; EventType getType() const noexcept override { - return _type; + return T; } const Vector2i getPosition() const noexcept override { @@ -38,6 +38,5 @@ class arcade::graphics::common::events::AMouseEvent: public IMouseEvent { */ explicit AMouseEvent(Vector2i pos): _pos(pos){}; - const EventType _type = T; Vector2i _pos; }; diff --git a/graphics/common/events/window/CMakeLists.txt b/graphics/common/events/window/CMakeLists.txt new file mode 100644 index 0000000..a208362 --- /dev/null +++ b/graphics/common/events/window/CMakeLists.txt @@ -0,0 +1,4 @@ +target_sources(${PROJECT_NAME} PUBLIC + WindowCloseEvent.hpp + WindowResizeEvent.hpp +) diff --git a/graphics/common/events/window/WindowCloseEvent.hpp b/graphics/common/events/window/WindowCloseEvent.hpp new file mode 100644 index 0000000..1ad3100 --- /dev/null +++ b/graphics/common/events/window/WindowCloseEvent.hpp @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** WindowCloseEvent.hpp +*/ + +#pragma once + +#include "shared/graphics/events/IEvent.hpp" + +using namespace shared::graphics::events; + +namespace arcade::graphics::common::events { + class WindowCloseEvent; +} + + +class arcade::graphics::common::events::WindowCloseEvent: public IEvent { +public: + WindowCloseEvent() = default; + ~WindowCloseEvent() override = default; + + EventType getType() const noexcept override { + return WINDOW_CLOSE; + } +}; diff --git a/graphics/common/events/window/WindowResizeEvent.hpp b/graphics/common/events/window/WindowResizeEvent.hpp new file mode 100644 index 0000000..da75768 --- /dev/null +++ b/graphics/common/events/window/WindowResizeEvent.hpp @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** WindowCloseEvent.hpp +*/ + +#pragma once + +#include "shared/graphics/events/IEvent.hpp" + +using namespace shared::graphics::events; + +namespace arcade::graphics::common::events { + class WindowResizeEvent; +} + + +class arcade::graphics::common::events::WindowResizeEvent: public IEvent { +public: + WindowResizeEvent() = default; + ~WindowResizeEvent() override = default; + + EventType getType() const noexcept override { + return WINDOW_RESIZE; + } +}; From dcca351f24bb86f7a8f5a48c13c48108be4870e7 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Thu, 28 Mar 2024 13:13:44 +0100 Subject: [PATCH 13/38] feat(graphics): add events handling --- graphics/common/events/key/AKeyEvent.hpp | 8 + graphics/common/events/key/KeyPressEvent.hpp | 4 +- .../common/events/key/KeyReleaseEvent.hpp | 3 +- .../common/events/mouse/AMouseButtonEvent.hpp | 2 +- graphics/common/events/mouse/AMouseEvent.hpp | 2 +- .../events/mouse/MouseButtonPressEvent.hpp | 3 +- .../common/events/mouse/MouseMoveEvent.hpp | 3 +- graphics/common/events/window/CMakeLists.txt | 1 + graphics/common/events/window/window.hpp | 11 + graphics/sfml/CMakeLists.txt | 2 + graphics/sfml/src/window/EventsHandler.cpp | 228 ++++++++++++++++++ graphics/sfml/src/window/EventsHandler.hpp | 126 ++++++++++ graphics/sfml/src/window/Window.cpp | 22 +- utils/compiler.hpp | 10 + 14 files changed, 401 insertions(+), 24 deletions(-) create mode 100644 graphics/common/events/window/window.hpp create mode 100644 graphics/sfml/src/window/EventsHandler.cpp create mode 100644 graphics/sfml/src/window/EventsHandler.hpp create mode 100644 utils/compiler.hpp diff --git a/graphics/common/events/key/AKeyEvent.hpp b/graphics/common/events/key/AKeyEvent.hpp index 735e9a0..b428723 100644 --- a/graphics/common/events/key/AKeyEvent.hpp +++ b/graphics/common/events/key/AKeyEvent.hpp @@ -30,6 +30,14 @@ class arcade::graphics::common::events::AKeyEvent: public IKeyEvent { return T; } + const KeyCode getKeyCode() const noexcept override { + return _code; + } + + const KeyType getKeyType() const noexcept override { + return _keyType; + } + protected: AKeyEvent(const KeyType keyType, const KeyCode code) : _keyType(keyType), _code(code) {}; diff --git a/graphics/common/events/key/KeyPressEvent.hpp b/graphics/common/events/key/KeyPressEvent.hpp index 5a79aac..f1655a0 100644 --- a/graphics/common/events/key/KeyPressEvent.hpp +++ b/graphics/common/events/key/KeyPressEvent.hpp @@ -15,5 +15,7 @@ namespace arcade::graphics::common::events { class arcade::graphics::common::events::KeyPressEvent: public AKeyEvent { public: - using AKeyEvent::AKeyEvent; + explicit KeyPressEvent(const KeyType type, const KeyCode code = { .character = 0 }) + : AKeyEvent(type, code) {}; + ~KeyPressEvent() override = default; }; diff --git a/graphics/common/events/key/KeyReleaseEvent.hpp b/graphics/common/events/key/KeyReleaseEvent.hpp index b4c0c0d..7b868b5 100644 --- a/graphics/common/events/key/KeyReleaseEvent.hpp +++ b/graphics/common/events/key/KeyReleaseEvent.hpp @@ -15,5 +15,6 @@ namespace arcade::graphics::common::events { class arcade::graphics::common::events::KeyReleaseEvent: public AKeyEvent { public: - using AKeyEvent::AKeyEvent; + KeyReleaseEvent(const KeyType type, const KeyCode code) : AKeyEvent(type, code) {}; + ~KeyReleaseEvent() override = default; }; diff --git a/graphics/common/events/mouse/AMouseButtonEvent.hpp b/graphics/common/events/mouse/AMouseButtonEvent.hpp index 0f7eb8c..0071cb0 100644 --- a/graphics/common/events/mouse/AMouseButtonEvent.hpp +++ b/graphics/common/events/mouse/AMouseButtonEvent.hpp @@ -23,7 +23,7 @@ namespace arcade::graphics::common::events { } template requires MouseButtonEventType -class arcade::graphics::common::events::AMouseButtonEvent: public AMouseEvent, public IMouseButtonEvent { +class arcade::graphics::common::events::AMouseButtonEvent: public AMouseEvent, public virtual IMouseButtonEvent { public: /** * @brief Construct a new AMouseButtonEvent object diff --git a/graphics/common/events/mouse/AMouseEvent.hpp b/graphics/common/events/mouse/AMouseEvent.hpp index b77ed90..2bf413a 100644 --- a/graphics/common/events/mouse/AMouseEvent.hpp +++ b/graphics/common/events/mouse/AMouseEvent.hpp @@ -18,7 +18,7 @@ namespace arcade::graphics::common::events { } template -class arcade::graphics::common::events::AMouseEvent: public IMouseEvent { +class arcade::graphics::common::events::AMouseEvent: public virtual IMouseEvent { public: ~AMouseEvent() override = default; diff --git a/graphics/common/events/mouse/MouseButtonPressEvent.hpp b/graphics/common/events/mouse/MouseButtonPressEvent.hpp index 94e08a5..f2865c2 100644 --- a/graphics/common/events/mouse/MouseButtonPressEvent.hpp +++ b/graphics/common/events/mouse/MouseButtonPressEvent.hpp @@ -19,5 +19,6 @@ namespace arcade::graphics::common::events { class arcade::graphics::common::events::MouseButtonPressEvent : public AMouseButtonEvent { public: - using AMouseButtonEvent::AMouseButtonEvent; + MouseButtonPressEvent(Vector2i pos, MouseButton button) : AMouseButtonEvent(pos, button){}; + ~MouseButtonPressEvent() override = default; }; diff --git a/graphics/common/events/mouse/MouseMoveEvent.hpp b/graphics/common/events/mouse/MouseMoveEvent.hpp index 3dc3131..76cefef 100644 --- a/graphics/common/events/mouse/MouseMoveEvent.hpp +++ b/graphics/common/events/mouse/MouseMoveEvent.hpp @@ -18,8 +18,7 @@ namespace arcade::graphics::common::events { class arcade::graphics::common::events::MouseMoveEvent: public AMouseEvent { public: - - explicit MouseMoveEvent(Vector2i pos): AMouseEvent(pos) {} + explicit MouseMoveEvent(const Vector2i pos): AMouseEvent(pos) {} ~MouseMoveEvent() override = default; }; diff --git a/graphics/common/events/window/CMakeLists.txt b/graphics/common/events/window/CMakeLists.txt index a208362..103c201 100644 --- a/graphics/common/events/window/CMakeLists.txt +++ b/graphics/common/events/window/CMakeLists.txt @@ -1,4 +1,5 @@ target_sources(${PROJECT_NAME} PUBLIC WindowCloseEvent.hpp WindowResizeEvent.hpp + window.hpp ) diff --git a/graphics/common/events/window/window.hpp b/graphics/common/events/window/window.hpp new file mode 100644 index 0000000..7edf622 --- /dev/null +++ b/graphics/common/events/window/window.hpp @@ -0,0 +1,11 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** window.hpp +*/ + +#pragma once + +#include "WindowCloseEvent.hpp" +#include "WindowResizeEvent.hpp" diff --git a/graphics/sfml/CMakeLists.txt b/graphics/sfml/CMakeLists.txt index 805d974..33a7690 100644 --- a/graphics/sfml/CMakeLists.txt +++ b/graphics/sfml/CMakeLists.txt @@ -5,6 +5,8 @@ add_library(${PROJECT_NAME} SHARED src/GraphicsProvider.hpp src/window/Window.cpp src/window/Window.hpp + src/window/EventsHandler.cpp + src/window/EventsHandler.hpp ) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../common PRIVATE) diff --git a/graphics/sfml/src/window/EventsHandler.cpp b/graphics/sfml/src/window/EventsHandler.cpp new file mode 100644 index 0000000..c3aa556 --- /dev/null +++ b/graphics/sfml/src/window/EventsHandler.cpp @@ -0,0 +1,228 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** EventHandler.cpp +*/ + +#include + +#include "EventsHandler.hpp" +#include "utils/compiler.hpp" + +using namespace arcade::graphics::sfml::events; + +EventsHandler::EventHandler EventsHandler::_getHandler(sf::Event::EventType type) { + static std::map handlers = { + {sf::Event::KeyPressed, _handleKeyPressEvent}, + {sf::Event::KeyReleased, _handleKeyReleaseEvent}, + {sf::Event::MouseButtonPressed, _handleMouseButtonPressEvent}, + {sf::Event::MouseButtonReleased, _handleMouseBtnReleaseEvent}, + {sf::Event::MouseMoved, _handleMouseMoveEvent}, + {sf::Event::Closed, _handleWindowCloseEvent}, + {sf::Event::Resized, _handleWindowResizeEvent} + }; + auto handler = handlers.find(type); + + return handler != handlers.end() ? handler->second : nullptr; +} + +std::vector EventsHandler::handleEvents(sf::RenderWindow &window) { + std::vector events; + sf::Event SFMLEvent{}; + + while (window.pollEvent(SFMLEvent)) { + auto handler = _getHandler(SFMLEvent.type); + auto event = handler ? handler(SFMLEvent, window) : nullptr; + + if (event) + events.push_back(event); + } + return events; +} + +bool EventsHandler::_handleControlKey( + sf::Event::KeyEvent event, + IKeyEvent::KeyCode &code +) { + switch (event.code) { + case sf::Keyboard::LControl: + case sf::Keyboard::RControl: + case sf::Keyboard::LShift: + case sf::Keyboard::RShift: + case sf::Keyboard::LAlt: + case sf::Keyboard::RAlt: + return true; + + default: + return false; + } +} + +bool EventsHandler::_handleArrowKey( + sf::Event::KeyEvent event, + IKeyEvent::KeyCode &code +) { + switch (event.code) { + case sf::Keyboard::Left: + code.arrow = IKeyEvent::ArrowCode::LEFT; + return true; + case sf::Keyboard::Right: + code.arrow = IKeyEvent::ArrowCode::RIGHT; + return true; + case sf::Keyboard::Up: + code.arrow = IKeyEvent::ArrowCode::UP; + return true; + case sf::Keyboard::Down: + code.arrow = IKeyEvent::ArrowCode::DOWN; + return true; + default: + return false; + } +} + +bool EventsHandler::_handleFunctionKey( + sf::Event::KeyEvent event, + IKeyEvent::KeyCode &code +) +{ + if (event.code >= sf::Keyboard::F1 && event.code <= sf::Keyboard::F12) { + code.func = event.code - sf::Keyboard::F1 + 1; + return true; + } else { + return false; + } +} + +bool EventsHandler::_handleCharKey( + sf::Event::KeyEvent event, + IKeyEvent::KeyCode &code +) { + static const std::map specials = { + {sf::Keyboard::Space, ' '}, + {sf::Keyboard::LBracket, '['}, + {sf::Keyboard::RBracket, ']'}, + {sf::Keyboard::Semicolon, ';'}, + {sf::Keyboard::Comma, ','}, + {sf::Keyboard::Period, '.'}, + {sf::Keyboard::Quote, '\''}, + {sf::Keyboard::Slash, '/'}, + {sf::Keyboard::Backslash, '\\'}, + {sf::Keyboard::Tilde, '~'}, + {sf::Keyboard::Equal, '='}, + {sf::Keyboard::Hyphen, '-'}, + {sf::Keyboard::Enter, '\n'}, + {sf::Keyboard::Backspace, '\b'}, + {sf::Keyboard::Tab, '\t'}, + {sf::Keyboard::Escape, 0x1B}, + {sf::Keyboard::Add, '+'}, + {sf::Keyboard::Subtract, '-'}, + {sf::Keyboard::Multiply, '*'}, + {sf::Keyboard::Divide, '/'} + }; + + if (event.code >= sf::Keyboard::A && event.code <= sf::Keyboard::Z) { + code.character = static_cast(event.code - sf::Keyboard::A + 'a'); + } else if (event.code >= sf::Keyboard::Num0 && event.code <= sf::Keyboard::Num9) { + code.character = static_cast(event.code - sf::Keyboard::Num0 + '0'); + } else if (event.code >= sf::Keyboard::Numpad0 && event.code <= sf::Keyboard::Numpad9) { + code.character = static_cast(event.code - sf::Keyboard::Numpad0 + '0'); + } else { + auto special = specials.find(event.code); + + if (special != specials.end()) { + code.character = special->second; + } else { + return false; + } + } + return true; +} + +EventPtr EventsHandler::_handleKeyPressEvent( + sf::Event &event, + unused sf::RenderWindow &window +) { + IKeyEvent::KeyType type = IKeyEvent::KeyType::UNKNOWN; + IKeyEvent::KeyCode code; + + if (_handleControlKey(event.key, code)) + type = IKeyEvent::KeyType::CONTROL; + else if (_handleArrowKey(event.key, code)) + type = IKeyEvent::KeyType::ARROW; + else if (_handleFunctionKey(event.key, code)) + type = IKeyEvent::KeyType::FUNC; + else if (_handleCharKey(event.key, code)) + type = IKeyEvent::KeyType::CHAR; + return std::make_shared(type, code); +} + +EventPtr EventsHandler::_handleKeyReleaseEvent( + sf::Event &event, + unused sf::RenderWindow &window +) { + IKeyEvent::KeyType type = IKeyEvent::KeyType::UNKNOWN; + IKeyEvent::KeyCode code; + + if (_handleControlKey(event.key, code)) + type = IKeyEvent::KeyType::CONTROL; + else if (_handleArrowKey(event.key, code)) + type = IKeyEvent::KeyType::ARROW; + else if (_handleFunctionKey(event.key, code)) + type = IKeyEvent::KeyType::FUNC; + else if (_handleCharKey(event.key, code)) + type = IKeyEvent::KeyType::CHAR; + return std::make_shared(type, code); +} + +EventPtr EventsHandler::_handleWindowCloseEvent( + unused sf::Event &event, + unused sf::RenderWindow &window +) { + return std::make_shared(); +} + +EventPtr EventsHandler::_handleWindowResizeEvent( + unused sf::Event &event, + unused sf::RenderWindow &window +) { + return std::make_shared(); +} + +EventPtr EventsHandler::_handleMouseMoveEvent( + sf::Event &event, + unused sf::RenderWindow &window +) { + return std::make_shared(Vector2i( + event.mouseMove.x, + event.mouseMove.y + )); +} + +EventPtr EventsHandler::_handleMouseButtonPressEvent( + sf::Event &event, + unused sf::RenderWindow &window +) { + Vector2i pos(event.mouseButton.x, event.mouseButton.y); + + if (event.mouseButton.button == sf::Mouse::Button::Left) + return std::make_shared(pos, IMouseButtonEvent::MouseButton::LEFT); + else if (event.mouseButton.button == sf::Mouse::Button::Right) + return std::make_shared(pos, IMouseButtonEvent::MouseButton::RIGHT); + else + return nullptr; +} + +EventPtr EventsHandler::_handleMouseBtnReleaseEvent( + sf::Event &event, + unused sf::RenderWindow &window +) { + Vector2i pos(event.mouseButton.x, event.mouseButton.y); + + if (event.mouseButton.button == sf::Mouse::Button::Left) + return std::make_shared(pos, IMouseButtonEvent::MouseButton::LEFT); + else if (event.mouseButton.button == sf::Mouse::Button::Right) + return std::make_shared(pos, IMouseButtonEvent::MouseButton::RIGHT); + else + return nullptr; +} diff --git a/graphics/sfml/src/window/EventsHandler.hpp b/graphics/sfml/src/window/EventsHandler.hpp new file mode 100644 index 0000000..b2d072a --- /dev/null +++ b/graphics/sfml/src/window/EventsHandler.hpp @@ -0,0 +1,126 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** EventHandler.hpp +*/ + +#pragma once + +#include +#include + +#include "common/events/window/window.hpp" +#include "common/events/mouse/mouse.hpp" +#include "common/events/key/key.hpp" + +namespace arcade::graphics::sfml::events { + class EventsHandler; +} + +class arcade::graphics::sfml::events::EventsHandler { +public: + EventsHandler() = delete; + ~EventsHandler() = delete; + + typedef EventPtr (*EventHandler)(sf::Event &event, sf::RenderWindow &window); + + /** + * @brief Handle events from SFML + * @param window Window object + * @return Vector of events + */ + static std::vector handleEvents(sf::RenderWindow &window); + +private: + + static EventHandler _getHandler(sf::Event::EventType type); + + /** + * @brief Handle control key event if it's a control key + * @param event Event from SFML + * @param code Code of the key to set + * @return Status of handling + */ + static bool _handleControlKey(sf::Event::KeyEvent event, IKeyEvent::KeyCode &code); + + /** + * @brief Handle control key event if it's an arrow key + * @param event Event from SFML + * @param code Code of the key to set + * @return Status of handling + */ + static bool _handleArrowKey(sf::Event::KeyEvent event, IKeyEvent::KeyCode &code); + + /** + * @brief Handle control key event if it's a function key + * @param event Event from SFML + * @param code Code of the key to set + * @return Status of handling + */ + static bool _handleFunctionKey(sf::Event::KeyEvent event, IKeyEvent::KeyCode &code); + + /** + * @brief Handle control key event if it's a char key + * @param event Event from SFML + * @param code Code of the key to set + * @return Status of handling + */ + static bool _handleCharKey(sf::Event::KeyEvent event, IKeyEvent::KeyCode &code); + + /** + * @brief Handle key press event + * @param event Event from SFML + * @param window Window object + * @return Pointer to created event or null if not handled + */ + static EventPtr _handleKeyPressEvent(sf::Event &event, sf::RenderWindow &window); + + /** + * @brief Handle key release event + * @param event Event from SFML + * @param window Window object + * @return Pointer to created event or null if not handled + */ + static EventPtr _handleKeyReleaseEvent(sf::Event &event, sf::RenderWindow &window); + + /** + * @brief Handle mouse button press event + * @param event Event from SFML + * @param window Window object + * @return Pointer to created event or null if not handled + */ + static EventPtr _handleMouseButtonPressEvent(sf::Event &event, sf::RenderWindow &window); + + /** + * @brief Handle mouse button release event + * @param event Event from SFML + * @param window Window object + * @return Pointer to created event or null if not handled + */ + static EventPtr _handleMouseBtnReleaseEvent(sf::Event &event, sf::RenderWindow &window); + + /** + * @brief Handle mouse move event + * @param event Event from SFML + * @param window Window object + * @return Pointer to created event or null if not handled + */ + static EventPtr _handleMouseMoveEvent(sf::Event &event, sf::RenderWindow &window); + + /** + * @brief Handle window close event + * @param event Event from SFML + * @param window Window object + * @return Pointer to created event or null if not handled + */ + static EventPtr _handleWindowCloseEvent(sf::Event &event, sf::RenderWindow &window); + + /** + * @brief Handle window resize event + * @param event Event from SFML + * @param window Window object + * @return Pointer to created event or null if not handled + */ + static EventPtr _handleWindowResizeEvent(sf::Event &event, sf::RenderWindow &window); +}; diff --git a/graphics/sfml/src/window/Window.cpp b/graphics/sfml/src/window/Window.cpp index eb6418d..0333c08 100644 --- a/graphics/sfml/src/window/Window.cpp +++ b/graphics/sfml/src/window/Window.cpp @@ -7,9 +7,12 @@ #include #include "Window.hpp" +#include "EventsHandler.hpp" #include "common/events/mouse/mouse.hpp" using namespace arcade::graphics::sfml; +using namespace shared::graphics::events; +using namespace arcade::graphics::sfml::events; Window::Window(const IWindow::WindowInitProps &props) { @@ -97,21 +100,6 @@ void Window::close() { this->_window->close(); } -std::vector Window::getEvents() { - sf::Event event{}; - std::vector events; - - while (this->_window->pollEvent(event)) { - if (event.type == sf::Event::Closed) { - this->_window->close(); - return events; - } - if (event.type == sf::Event::MouseMoved) { - auto mouseEvent = std::make_shared( - Vector2i(event.mouseMove.x, event.mouseMove.y) - ); - events.push_back(mouseEvent); - } - } - return events; +std::vector Window::getEvents() { + return EventsHandler::handleEvents(*this->_window); } diff --git a/utils/compiler.hpp b/utils/compiler.hpp new file mode 100644 index 0000000..5a59f81 --- /dev/null +++ b/utils/compiler.hpp @@ -0,0 +1,10 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** compiler.hpp +*/ + +#pragma once + +#define unused __attribute__((unused)) From 9cbb6ee86ad93e4ec5490b4f9fbe81b4e85794da Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Thu, 28 Mar 2024 13:45:44 +0100 Subject: [PATCH 14/38] refactor(shared): update shared library --- shared/games/components/ITextComponent.hpp | 2 +- shared/games/export.cpp.example | 2 +- shared/graphics/events/IKeyEvent.hpp | 2 +- shared/graphics/events/IMouseButtonEvent.hpp | 2 +- shared/graphics/events/IMouseEvent.hpp | 2 +- shared/graphics/export.cpp.example | 2 +- shared/graphics/types/TextProps.hpp | 9 +++------ 7 files changed, 9 insertions(+), 12 deletions(-) diff --git a/shared/games/components/ITextComponent.hpp b/shared/games/components/ITextComponent.hpp index c0a269c..655d45e 100644 --- a/shared/games/components/ITextComponent.hpp +++ b/shared/games/components/ITextComponent.hpp @@ -28,7 +28,7 @@ namespace shared::games::components { typedef struct { std::string path; // Path of the font - types::Vector2u size; // Font size + unsigned int size; // Font size } TextFontProps; typedef struct { diff --git a/shared/games/export.cpp.example b/shared/games/export.cpp.example index 5dd7c4a..938943b 100644 --- a/shared/games/export.cpp.example +++ b/shared/games/export.cpp.example @@ -17,7 +17,7 @@ extern "C" { return LibraryType::GAME; } - IGameProvider* SHARED_GAME_PROVIDER_LOADER_NAME(void) + IGameProvider* SHARED_GAME_PROVIDER_GETTER_NAME(void) { return new YOUR_CLASS(); } 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; diff --git a/shared/graphics/export.cpp.example b/shared/graphics/export.cpp.example index 2a6ebf5..d8e1b45 100644 --- a/shared/graphics/export.cpp.example +++ b/shared/graphics/export.cpp.example @@ -17,7 +17,7 @@ extern "C" { return LibraryType::GRAPHIC; } - IGraphicsProvider *SHARED_GRAPHICS_PROVIDER_LOADER_NAME(void) + IGraphicsProvider *SHARED_GRAPHICS_PROVIDER_GETTER_NAME(void) { return new YOUR_CLASS(); } diff --git a/shared/graphics/types/TextProps.hpp b/shared/graphics/types/TextProps.hpp index 2d7187a..9f01dc9 100644 --- a/shared/graphics/types/TextProps.hpp +++ b/shared/graphics/types/TextProps.hpp @@ -7,6 +7,7 @@ #pragma once +#include #include #include "../IFont.hpp" @@ -29,12 +30,8 @@ namespace shared::graphics { } 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::shared_ptr font; // Font of the text + unsigned int fontSize; // Font size std::string content; // Content of the text TextAlign align; // Alignment of the text TextVerticalAlign verticalAlign; // Vertical alignment of the text From b547d8642cdf85da07a239151974fe1d82f8a44f Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Thu, 28 Mar 2024 16:04:34 +0100 Subject: [PATCH 15/38] refactor: update global CMakeLists.txt and restore main.cpp --- CMakeLists.txt | 2 +- core/main.cpp | 32 +++++++++++--------------------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bf775be..b23028f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,5 +12,5 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ARCADE_LIB_DIR}) set(CMAKE_SHARED_LIBRARY_PREFIX "arcade_") add_subdirectory(core) -#add_subdirectory(games) +add_subdirectory(games) add_subdirectory(graphics) diff --git a/core/main.cpp b/core/main.cpp index dc87911..6e160ea 100644 --- a/core/main.cpp +++ b/core/main.cpp @@ -7,26 +7,16 @@ #include "loader/DLLoader.hpp" -int main(void) { - DLLoader loader; +int main(void) +{ + DLLoader loader; - try { - loader.loadLibraries("./lib"); - std::cout << "Games libraries: " << loader.getGamesLibraries().size() << std::endl; - std::cout << "Graphics libraries: " << loader.getGraphicsLibraries().size() << std::endl; - } catch (const std::exception &e) { - std::cerr << e.what() << std::endl; - } - auto graphics = loader.getGraphicsLibraries(); - auto provider = graphics[0]; - auto window = provider->createWindow({ - {1920, 1080}, - shared::graphics::IWindow::FULLSCREEN, - 60, - "Notre putain de snake" - }); - while (window->isOpen()) { - window->getEvents(); - } - return 0; + try { + loader.loadLibraries("./lib"); + std::cout << "Games libraries:" << loader.getGamesLibraries().size() << std::endl; + std::cout << "Graphics libraries:" << loader.getGraphicsLibraries().size() << std::endl; + } catch (const std::exception &e) { + std::cerr << e.what() << std::endl; + } + return 0; } From 5966939c8a89863b2e8ce301eef2d2267970610a Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Thu, 28 Mar 2024 17:18:51 +0100 Subject: [PATCH 16/38] refactor(shared): update library to pull exceptions interfaces --- shared/graphics/exceptions/IFontException.hpp | 19 ++++++++++++ .../exceptions/IGraphicsException.hpp | 29 +++++++++++++++++++ .../graphics/exceptions/ISoundException.hpp | 19 ++++++++++++ .../graphics/exceptions/ITextureException.hpp | 19 ++++++++++++ .../graphics/exceptions/IWindowException.hpp | 19 ++++++++++++ 5 files changed, 105 insertions(+) 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/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..927d899 --- /dev/null +++ b/shared/graphics/exceptions/IGraphicsException.hpp @@ -0,0 +1,29 @@ +/* +** EPITECH PROJECT, 2024 +** arcade-shared +** File description: +** IGraphicsException.hpp +*/ + +#pragma once + +namespace shared::graphics::exceptions { + class IGraphicsException; +} + +class shared::graphics::exceptions::IGraphicsException { +public: + virtual ~IGraphicsException() = default; + + /** + * @brief Get error details + * @return String containing error details + */ + virtual const std::string &what() const noexcept = 0; + + /** + * @brief Get error location + * @return String containing error location + */ + virtual const std::string &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 99b282435e94356f670be24e20861269fe9b8df6 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Thu, 28 Mar 2024 17:54:57 +0100 Subject: [PATCH 17/38] feat(graphics): add GraphicsException --- graphics/common/CMakeLists.txt | 1 + graphics/common/exceptions/CMakeLists.txt | 4 +++ .../common/exceptions/GraphicsException.cpp | 24 +++++++++++++++ .../common/exceptions/GraphicsException.hpp | 30 +++++++++++++++++++ shared/games/IGame.hpp | 3 +- .../exceptions/IGraphicsException.hpp | 2 ++ 6 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 graphics/common/exceptions/CMakeLists.txt create mode 100644 graphics/common/exceptions/GraphicsException.cpp create mode 100644 graphics/common/exceptions/GraphicsException.hpp diff --git a/graphics/common/CMakeLists.txt b/graphics/common/CMakeLists.txt index ec53431..a655d16 100644 --- a/graphics/common/CMakeLists.txt +++ b/graphics/common/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(events) +add_subdirectory(exceptions) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../..) diff --git a/graphics/common/exceptions/CMakeLists.txt b/graphics/common/exceptions/CMakeLists.txt new file mode 100644 index 0000000..dbce0b0 --- /dev/null +++ b/graphics/common/exceptions/CMakeLists.txt @@ -0,0 +1,4 @@ +target_sources(${PROJECT_NAME} PUBLIC + GraphicsException.cpp + GraphicsException.hpp +) \ No newline at end of file diff --git a/graphics/common/exceptions/GraphicsException.cpp b/graphics/common/exceptions/GraphicsException.cpp new file mode 100644 index 0000000..b928e1f --- /dev/null +++ b/graphics/common/exceptions/GraphicsException.cpp @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** GraphicsException.cpp +*/ + +#include "GraphicsException.hpp" + +using namespace arcade::graphics::common::exceptions; + +GraphicsException::GraphicsException(const std::string &message, const std::string &where) +{ + this->_message = message; + this->_where = where; +} + +const std::string &GraphicsException::what() const noexcept { + return this->_message; +} + +const std::string &GraphicsException::where() const noexcept { + return this->_where; +} diff --git a/graphics/common/exceptions/GraphicsException.hpp b/graphics/common/exceptions/GraphicsException.hpp new file mode 100644 index 0000000..47e2d8c --- /dev/null +++ b/graphics/common/exceptions/GraphicsException.hpp @@ -0,0 +1,30 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** GraphicsException.hpp +*/ + +#pragma once + +#include "shared/graphics/exceptions/IGraphicsException.hpp" + +using namespace shared::graphics::exceptions; + +namespace arcade::graphics::common::exceptions { + class GraphicsException; +} + +class arcade::graphics::common::exceptions::GraphicsException : public IGraphicsException { +public: + GraphicsException(const std::string &message, const std::string &where); + ~GraphicsException() override = default; + + const std::string &what() const noexcept override; + + const std::string &where() const noexcept override; + +protected: + std::string _message; + std::string _where; +}; 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/graphics/exceptions/IGraphicsException.hpp b/shared/graphics/exceptions/IGraphicsException.hpp index 927d899..ee5333b 100644 --- a/shared/graphics/exceptions/IGraphicsException.hpp +++ b/shared/graphics/exceptions/IGraphicsException.hpp @@ -7,6 +7,8 @@ #pragma once +#include + namespace shared::graphics::exceptions { class IGraphicsException; } From 4b470bf79968b9bafa3463aa8be431c04dff9c22 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Thu, 28 Mar 2024 22:03:08 +0100 Subject: [PATCH 18/38] feat(graphics): add SFML sound --- graphics/common/exceptions/CMakeLists.txt | 4 ++ graphics/common/exceptions/FontException.hpp | 22 +++++++ .../common/exceptions/GraphicsException.cpp | 8 +-- .../common/exceptions/GraphicsException.hpp | 7 ++- graphics/common/exceptions/SoundException.hpp | 22 +++++++ .../common/exceptions/TextureException.hpp | 22 +++++++ .../common/exceptions/WindowException.hpp | 22 +++++++ graphics/sfml/CMakeLists.txt | 12 ++-- graphics/sfml/src/GraphicsProvider.cpp | 5 +- graphics/sfml/src/sound/Sound.cpp | 63 +++++++++++++++++++ graphics/sfml/src/sound/Sound.hpp | 36 +++++++++++ graphics/sfml/src/window/Window.cpp | 2 +- graphics/sfml/src/window/Window.hpp | 4 +- .../exceptions/IGraphicsException.hpp | 14 +---- 14 files changed, 215 insertions(+), 28 deletions(-) create mode 100644 graphics/common/exceptions/FontException.hpp create mode 100644 graphics/common/exceptions/SoundException.hpp create mode 100644 graphics/common/exceptions/TextureException.hpp create mode 100644 graphics/common/exceptions/WindowException.hpp create mode 100644 graphics/sfml/src/sound/Sound.cpp create mode 100644 graphics/sfml/src/sound/Sound.hpp diff --git a/graphics/common/exceptions/CMakeLists.txt b/graphics/common/exceptions/CMakeLists.txt index dbce0b0..ae1ba07 100644 --- a/graphics/common/exceptions/CMakeLists.txt +++ b/graphics/common/exceptions/CMakeLists.txt @@ -1,4 +1,8 @@ target_sources(${PROJECT_NAME} PUBLIC GraphicsException.cpp GraphicsException.hpp + SoundException.hpp + TextureException.hpp + WindowException.hpp + FontException.hpp ) \ No newline at end of file diff --git a/graphics/common/exceptions/FontException.hpp b/graphics/common/exceptions/FontException.hpp new file mode 100644 index 0000000..0039eb0 --- /dev/null +++ b/graphics/common/exceptions/FontException.hpp @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** FontException.hpp +*/ + +#pragma once + +#include "GraphicsException.hpp" +#include "shared/graphics/exceptions/IFontException.hpp" + +using namespace shared::graphics::exceptions; + +namespace arcade::graphics::common::exceptions { + class FontException; +} + +class arcade::graphics::common::exceptions::FontException: public GraphicsException, public IFontException { +public: + using GraphicsException::GraphicsException; +}; diff --git a/graphics/common/exceptions/GraphicsException.cpp b/graphics/common/exceptions/GraphicsException.cpp index b928e1f..5e1da56 100644 --- a/graphics/common/exceptions/GraphicsException.cpp +++ b/graphics/common/exceptions/GraphicsException.cpp @@ -15,10 +15,10 @@ GraphicsException::GraphicsException(const std::string &message, const std::stri this->_where = where; } -const std::string &GraphicsException::what() const noexcept { - return this->_message; +const char *GraphicsException::what() const noexcept { + return this->_message.c_str(); } -const std::string &GraphicsException::where() const noexcept { - return this->_where; +const char *GraphicsException::where() const noexcept { + return this->_where.c_str(); } diff --git a/graphics/common/exceptions/GraphicsException.hpp b/graphics/common/exceptions/GraphicsException.hpp index 47e2d8c..5cff65a 100644 --- a/graphics/common/exceptions/GraphicsException.hpp +++ b/graphics/common/exceptions/GraphicsException.hpp @@ -7,6 +7,7 @@ #pragma once +#include #include "shared/graphics/exceptions/IGraphicsException.hpp" using namespace shared::graphics::exceptions; @@ -15,14 +16,14 @@ namespace arcade::graphics::common::exceptions { class GraphicsException; } -class arcade::graphics::common::exceptions::GraphicsException : public IGraphicsException { +class arcade::graphics::common::exceptions::GraphicsException : public virtual IGraphicsException { public: GraphicsException(const std::string &message, const std::string &where); ~GraphicsException() override = default; - const std::string &what() const noexcept override; + const char *what() const noexcept override; - const std::string &where() const noexcept override; + const char *where() const noexcept override; protected: std::string _message; diff --git a/graphics/common/exceptions/SoundException.hpp b/graphics/common/exceptions/SoundException.hpp new file mode 100644 index 0000000..4518994 --- /dev/null +++ b/graphics/common/exceptions/SoundException.hpp @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** SoundException.hpp +*/ + +#pragma once + +#include "GraphicsException.hpp" +#include "shared/graphics/exceptions/ISoundException.hpp" + +using namespace shared::graphics::exceptions; + +namespace arcade::graphics::common::exceptions { + class SoundException; +} + +class arcade::graphics::common::exceptions::SoundException: public GraphicsException, public ISoundException { +public: + using GraphicsException::GraphicsException; +}; diff --git a/graphics/common/exceptions/TextureException.hpp b/graphics/common/exceptions/TextureException.hpp new file mode 100644 index 0000000..187fe00 --- /dev/null +++ b/graphics/common/exceptions/TextureException.hpp @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** TextureException.hpp +*/ + +#pragma once + +#include "GraphicsException.hpp" +#include "shared/graphics/exceptions/ITextureException.hpp" + +using namespace shared::graphics::exceptions; + +namespace arcade::graphics::common::exceptions { + class TextureException; +} + +class arcade::graphics::common::exceptions::TextureException: public GraphicsException, public ITextureException { +public: + using GraphicsException::GraphicsException; +}; diff --git a/graphics/common/exceptions/WindowException.hpp b/graphics/common/exceptions/WindowException.hpp new file mode 100644 index 0000000..e3ab418 --- /dev/null +++ b/graphics/common/exceptions/WindowException.hpp @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** WindowException.hpp +*/ + +#pragma once + +#include "GraphicsException.hpp" +#include "shared/graphics/exceptions/IWindowException.hpp" + +using namespace shared::graphics::exceptions; + +namespace arcade::graphics::common::exceptions { + class WindowException; +} + +class arcade::graphics::common::exceptions::WindowException: public GraphicsException, public IWindowException { +public: + using GraphicsException::GraphicsException; +}; diff --git a/graphics/sfml/CMakeLists.txt b/graphics/sfml/CMakeLists.txt index 33a7690..d3acbc0 100644 --- a/graphics/sfml/CMakeLists.txt +++ b/graphics/sfml/CMakeLists.txt @@ -1,5 +1,5 @@ project(sfml) -add_library(${PROJECT_NAME} SHARED +add_library(sfml SHARED export.cpp src/GraphicsProvider.cpp src/GraphicsProvider.hpp @@ -7,11 +7,13 @@ add_library(${PROJECT_NAME} SHARED src/window/Window.hpp src/window/EventsHandler.cpp src/window/EventsHandler.hpp + src/sound/Sound.cpp + src/sound/Sound.hpp ) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../common PRIVATE) -target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src) -target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../..) -target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/..) +target_include_directories(sfml PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src) +target_include_directories(sfml PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../..) +target_include_directories(sfml PUBLIC ${CMAKE_CURRENT_LIST_DIR}/..) -target_link_libraries(sfml sfml-graphics sfml-window sfml-system) +target_link_libraries(sfml sfml-graphics sfml-window sfml-system sfml-audio) diff --git a/graphics/sfml/src/GraphicsProvider.cpp b/graphics/sfml/src/GraphicsProvider.cpp index 79aada4..496e0ee 100644 --- a/graphics/sfml/src/GraphicsProvider.cpp +++ b/graphics/sfml/src/GraphicsProvider.cpp @@ -8,6 +8,7 @@ #include #include "GraphicsProvider.hpp" #include "window/Window.hpp" +#include "sound/Sound.hpp" using namespace shared::graphics; using namespace arcade::graphics::sfml; @@ -37,11 +38,11 @@ const GraphicsManifest &GraphicsProvider::getManifest() const noexcept { } std::unique_ptr GraphicsProvider::createWindow(const IWindow::WindowInitProps &props) { - return std::make_unique(props); + return std::make_unique(props); } std::shared_ptr GraphicsProvider::createSound(const std::string &path) { - return nullptr; + return std::make_shared(path); } std::shared_ptr GraphicsProvider::createTexture(const std::string &bin, const std::string &ascii) { diff --git a/graphics/sfml/src/sound/Sound.cpp b/graphics/sfml/src/sound/Sound.cpp new file mode 100644 index 0000000..51f79f6 --- /dev/null +++ b/graphics/sfml/src/sound/Sound.cpp @@ -0,0 +1,63 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** Sound.cpp +*/ + +#include "Sound.hpp" +#include "common/exceptions/SoundException.hpp" + +using namespace arcade::graphics::sfml::sound; +using namespace arcade::graphics::common::exceptions; + +Sound::Sound(const std::string &path, SoundState state) { + if(!_buffer.loadFromFile(path)) { + throw SoundException( + "Failed to load sound at: " + path, + "Sound constructor in SFML library" + ); + } + _sound.setBuffer(_buffer); + _sound.stop(); +} + +ISound::SoundVolume Sound::getVolume() const { + return static_cast(_sound.getVolume()); +} + +void Sound::setVolume(SoundVolume volume) { + _sound.setVolume(volume); +} + +void Sound::setState(SoundState state) { + switch (state) { + case PLAY: + return _sound.play(); + case PAUSE: + return _sound.pause(); + case STOP: + return _sound.stop(); + } +} + +ISound::SoundState Sound::getState() const { + auto state = _sound.getStatus(); + + switch (state) { + case sf::Sound::Playing: + return PLAY; + case sf::Sound::Paused: + return PAUSE; + default: + return STOP; + } +} + +void Sound::setLoopState(bool loop) { + _sound.setLoop(loop); +} + +bool Sound::getLoopState() const { + return _sound.getLoop(); +} diff --git a/graphics/sfml/src/sound/Sound.hpp b/graphics/sfml/src/sound/Sound.hpp new file mode 100644 index 0000000..458ba79 --- /dev/null +++ b/graphics/sfml/src/sound/Sound.hpp @@ -0,0 +1,36 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** Sound.hpp +*/ + +#pragma once + +#include +#include + +#include "shared/graphics/ISound.hpp" + +using namespace shared::graphics; + +namespace arcade::graphics::sfml::sound { + class Sound; +} + +class arcade::graphics::sfml::sound::Sound : public ISound { +public: + explicit Sound(const std::string &path, SoundState state = STOP); + ~Sound() override = default; + + SoundVolume getVolume() const override; + SoundState getState() const override; + void setVolume(SoundVolume volume) override; + void setState(SoundState state) override; + void setLoopState(bool loop) override; + bool getLoopState() const override; + +private: + sf::SoundBuffer _buffer; + sf::Sound _sound; +}; diff --git a/graphics/sfml/src/window/Window.cpp b/graphics/sfml/src/window/Window.cpp index 0333c08..bc3502f 100644 --- a/graphics/sfml/src/window/Window.cpp +++ b/graphics/sfml/src/window/Window.cpp @@ -10,8 +10,8 @@ #include "EventsHandler.hpp" #include "common/events/mouse/mouse.hpp" -using namespace arcade::graphics::sfml; using namespace shared::graphics::events; +using namespace arcade::graphics::sfml::window; using namespace arcade::graphics::sfml::events; Window::Window(const IWindow::WindowInitProps &props) diff --git a/graphics/sfml/src/window/Window.hpp b/graphics/sfml/src/window/Window.hpp index d0cbfd6..e9b1864 100644 --- a/graphics/sfml/src/window/Window.hpp +++ b/graphics/sfml/src/window/Window.hpp @@ -12,11 +12,11 @@ using namespace shared::graphics; -namespace arcade::graphics::sfml{ +namespace arcade::graphics::sfml::window { class Window; } -class arcade::graphics::sfml::Window : public IWindow { +class arcade::graphics::sfml::window::Window : public IWindow { public: explicit Window(const IWindow::WindowInitProps &props); ~Window() override; diff --git a/shared/graphics/exceptions/IGraphicsException.hpp b/shared/graphics/exceptions/IGraphicsException.hpp index ee5333b..a899775 100644 --- a/shared/graphics/exceptions/IGraphicsException.hpp +++ b/shared/graphics/exceptions/IGraphicsException.hpp @@ -7,25 +7,17 @@ #pragma once -#include +#include namespace shared::graphics::exceptions { class IGraphicsException; } -class shared::graphics::exceptions::IGraphicsException { +class shared::graphics::exceptions::IGraphicsException: public std::exception { public: - virtual ~IGraphicsException() = default; - - /** - * @brief Get error details - * @return String containing error details - */ - virtual const std::string &what() const noexcept = 0; - /** * @brief Get error location * @return String containing error location */ - virtual const std::string &where() const noexcept = 0; + virtual const char *where() const noexcept = 0; }; From 98e05c746e1c4c844c6bd2b96dfeed1d7406228b Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Thu, 28 Mar 2024 23:15:51 +0100 Subject: [PATCH 19/38] feat(graphics): add SFML texture --- graphics/sfml/CMakeLists.txt | 2 ++ graphics/sfml/src/GraphicsProvider.cpp | 6 +++-- graphics/sfml/src/texture/Texture.cpp | 25 +++++++++++++++++++ graphics/sfml/src/texture/Texture.hpp | 33 ++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 graphics/sfml/src/texture/Texture.cpp create mode 100644 graphics/sfml/src/texture/Texture.hpp diff --git a/graphics/sfml/CMakeLists.txt b/graphics/sfml/CMakeLists.txt index d3acbc0..67f88f6 100644 --- a/graphics/sfml/CMakeLists.txt +++ b/graphics/sfml/CMakeLists.txt @@ -9,6 +9,8 @@ add_library(sfml SHARED src/window/EventsHandler.hpp src/sound/Sound.cpp src/sound/Sound.hpp + src/texture/Texture.cpp + src/texture/Texture.hpp ) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../common PRIVATE) diff --git a/graphics/sfml/src/GraphicsProvider.cpp b/graphics/sfml/src/GraphicsProvider.cpp index 496e0ee..6abadf0 100644 --- a/graphics/sfml/src/GraphicsProvider.cpp +++ b/graphics/sfml/src/GraphicsProvider.cpp @@ -7,8 +7,10 @@ #include #include "GraphicsProvider.hpp" +#include "utils/compiler.hpp" #include "window/Window.hpp" #include "sound/Sound.hpp" +#include "texture/Texture.hpp" using namespace shared::graphics; using namespace arcade::graphics::sfml; @@ -45,8 +47,8 @@ std::shared_ptr GraphicsProvider::createSound(const std::string &path) { return std::make_shared(path); } -std::shared_ptr GraphicsProvider::createTexture(const std::string &bin, const std::string &ascii) { - return nullptr; +std::shared_ptr GraphicsProvider::createTexture(const std::string &bin, unused const std::string &ascii) { + return std::make_shared(bin); } std::shared_ptr GraphicsProvider::createFont(const std::string &path) { diff --git a/graphics/sfml/src/texture/Texture.cpp b/graphics/sfml/src/texture/Texture.cpp new file mode 100644 index 0000000..a1e4c4e --- /dev/null +++ b/graphics/sfml/src/texture/Texture.cpp @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** Texture.cpp +*/ + +#include "Texture.hpp" +#include "common/exceptions/TextureException.hpp" + +using namespace arcade::graphics::sfml::texture; +using namespace arcade::graphics::common::exceptions; + +Texture::Texture(const std::string &path) { + if (!_texture.loadFromFile(path)) + throw TextureException( + "Failed to load texture at: " + path, + "Texture constructor in SFML library" + ); +} + +sf::Texture &Texture::getInnerTexture() +{ + return _texture; +} diff --git a/graphics/sfml/src/texture/Texture.hpp b/graphics/sfml/src/texture/Texture.hpp new file mode 100644 index 0000000..856ebd9 --- /dev/null +++ b/graphics/sfml/src/texture/Texture.hpp @@ -0,0 +1,33 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** Texture.hpp +*/ + +#pragma once + +#include +#include +#include "shared/graphics/ITexture.hpp" + +using namespace shared::graphics; + +namespace arcade::graphics::sfml::texture { + class Texture; +} + +class arcade::graphics::sfml::texture::Texture: public ITexture { +public: + explicit Texture(const std::string &path); + ~Texture() override = default; + + /** + * @brief Get the inner SFML texture + * @return Reference to the inner SFML texture + */ + sf::Texture &getInnerTexture(); + +private: + sf::Texture _texture; +}; From 517d5d91d3addeed676e094ab50340b6f8ea9948 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Thu, 28 Mar 2024 23:27:04 +0100 Subject: [PATCH 20/38] feat(graphics): add SFML fonts --- graphics/sfml/CMakeLists.txt | 2 ++ graphics/sfml/src/GraphicsProvider.cpp | 3 ++- graphics/sfml/src/font/Font.cpp | 26 ++++++++++++++++++++ graphics/sfml/src/font/Font.hpp | 34 ++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 graphics/sfml/src/font/Font.cpp create mode 100644 graphics/sfml/src/font/Font.hpp diff --git a/graphics/sfml/CMakeLists.txt b/graphics/sfml/CMakeLists.txt index 67f88f6..856e225 100644 --- a/graphics/sfml/CMakeLists.txt +++ b/graphics/sfml/CMakeLists.txt @@ -11,6 +11,8 @@ add_library(sfml SHARED src/sound/Sound.hpp src/texture/Texture.cpp src/texture/Texture.hpp + src/font/Font.cpp + src/font/Font.hpp ) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../common PRIVATE) diff --git a/graphics/sfml/src/GraphicsProvider.cpp b/graphics/sfml/src/GraphicsProvider.cpp index 6abadf0..20031d4 100644 --- a/graphics/sfml/src/GraphicsProvider.cpp +++ b/graphics/sfml/src/GraphicsProvider.cpp @@ -9,6 +9,7 @@ #include "GraphicsProvider.hpp" #include "utils/compiler.hpp" #include "window/Window.hpp" +#include "font/Font.hpp" #include "sound/Sound.hpp" #include "texture/Texture.hpp" @@ -52,5 +53,5 @@ std::shared_ptr GraphicsProvider::createTexture(const std::string &bin } std::shared_ptr GraphicsProvider::createFont(const std::string &path) { - return nullptr; + return std::make_shared(path); } diff --git a/graphics/sfml/src/font/Font.cpp b/graphics/sfml/src/font/Font.cpp new file mode 100644 index 0000000..ce8b063 --- /dev/null +++ b/graphics/sfml/src/font/Font.cpp @@ -0,0 +1,26 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** Font.cpp +*/ + +#include "Font.hpp" +#include "common/exceptions/FontException.hpp" + +using namespace arcade::graphics::sfml::font; +using namespace arcade::graphics::common::exceptions; + +Font::Font(const std::string &path) +{ + if (!_font.loadFromFile(path)) + throw FontException( + "Failed to load font at: " + path, + "Font constructor in SFML library" + ); +} + +sf::Font &Font::getInnerFont() +{ + return _font; +} diff --git a/graphics/sfml/src/font/Font.hpp b/graphics/sfml/src/font/Font.hpp new file mode 100644 index 0000000..a85ca73 --- /dev/null +++ b/graphics/sfml/src/font/Font.hpp @@ -0,0 +1,34 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** Font.hpp +*/ + +#pragma once + +#include +#include +#include "shared/graphics/IFont.hpp" +#include "utils/compiler.hpp" + +using namespace shared::graphics; + +namespace arcade::graphics::sfml::font { + class Font; +} + +class arcade::graphics::sfml::font::Font : public IFont { +public: + explicit Font(const std::string &path); + ~Font() override = default; + + /** + * @brief Get the inner SFML font + * @return Reference to the inner SFML font + */ + sf::Font &getInnerFont(); + +private: + sf::Font _font; +}; From 904ad7000438448b801c5a469368dd69f9e06357 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Thu, 28 Mar 2024 23:45:44 +0100 Subject: [PATCH 21/38] refactor(graphics): update namespaces --- graphics/sfml/src/font/Font.hpp | 4 +--- graphics/sfml/src/sound/Sound.cpp | 4 ++-- graphics/sfml/src/sound/Sound.hpp | 4 +--- graphics/sfml/src/texture/Texture.hpp | 4 +--- graphics/sfml/src/window/Window.cpp | 9 ++++----- graphics/sfml/src/window/Window.hpp | 12 +++++------- 6 files changed, 14 insertions(+), 23 deletions(-) diff --git a/graphics/sfml/src/font/Font.hpp b/graphics/sfml/src/font/Font.hpp index a85ca73..1460376 100644 --- a/graphics/sfml/src/font/Font.hpp +++ b/graphics/sfml/src/font/Font.hpp @@ -12,13 +12,11 @@ #include "shared/graphics/IFont.hpp" #include "utils/compiler.hpp" -using namespace shared::graphics; - namespace arcade::graphics::sfml::font { class Font; } -class arcade::graphics::sfml::font::Font : public IFont { +class arcade::graphics::sfml::font::Font : public shared::graphics::IFont { public: explicit Font(const std::string &path); ~Font() override = default; diff --git a/graphics/sfml/src/sound/Sound.cpp b/graphics/sfml/src/sound/Sound.cpp index 51f79f6..c53a4ed 100644 --- a/graphics/sfml/src/sound/Sound.cpp +++ b/graphics/sfml/src/sound/Sound.cpp @@ -22,7 +22,7 @@ Sound::Sound(const std::string &path, SoundState state) { _sound.stop(); } -ISound::SoundVolume Sound::getVolume() const { +Sound::SoundVolume Sound::getVolume() const { return static_cast(_sound.getVolume()); } @@ -41,7 +41,7 @@ void Sound::setState(SoundState state) { } } -ISound::SoundState Sound::getState() const { +Sound::SoundState Sound::getState() const { auto state = _sound.getStatus(); switch (state) { diff --git a/graphics/sfml/src/sound/Sound.hpp b/graphics/sfml/src/sound/Sound.hpp index 458ba79..ed69710 100644 --- a/graphics/sfml/src/sound/Sound.hpp +++ b/graphics/sfml/src/sound/Sound.hpp @@ -12,13 +12,11 @@ #include "shared/graphics/ISound.hpp" -using namespace shared::graphics; - namespace arcade::graphics::sfml::sound { class Sound; } -class arcade::graphics::sfml::sound::Sound : public ISound { +class arcade::graphics::sfml::sound::Sound : public shared::graphics::ISound { public: explicit Sound(const std::string &path, SoundState state = STOP); ~Sound() override = default; diff --git a/graphics/sfml/src/texture/Texture.hpp b/graphics/sfml/src/texture/Texture.hpp index 856ebd9..3f51e42 100644 --- a/graphics/sfml/src/texture/Texture.hpp +++ b/graphics/sfml/src/texture/Texture.hpp @@ -11,13 +11,11 @@ #include #include "shared/graphics/ITexture.hpp" -using namespace shared::graphics; - namespace arcade::graphics::sfml::texture { class Texture; } -class arcade::graphics::sfml::texture::Texture: public ITexture { +class arcade::graphics::sfml::texture::Texture: public shared::graphics::ITexture { public: explicit Texture(const std::string &path); ~Texture() override = default; diff --git a/graphics/sfml/src/window/Window.cpp b/graphics/sfml/src/window/Window.cpp index bc3502f..668ffb2 100644 --- a/graphics/sfml/src/window/Window.cpp +++ b/graphics/sfml/src/window/Window.cpp @@ -10,9 +10,8 @@ #include "EventsHandler.hpp" #include "common/events/mouse/mouse.hpp" -using namespace shared::graphics::events; -using namespace arcade::graphics::sfml::window; using namespace arcade::graphics::sfml::events; +using namespace arcade::graphics::sfml::window; Window::Window(const IWindow::WindowInitProps &props) { @@ -69,7 +68,7 @@ void Window::setMode(IWindow::WindowMode mode) { } } -IWindow::WindowMode Window::getMode() const { +Window::WindowMode Window::getMode() const { return this->_mode; } @@ -80,11 +79,11 @@ bool Window::isOpen() const { void Window::setIcon(const std::string &icon) { (void) icon; } -void Window::render(const TextProps &props) { +void Window::render(const shared::graphics::TextureProps &props) { (void) props; } -void Window::render(const TextureProps &props) { +void Window::render(const shared::graphics::TextProps &props) { (void) props; } diff --git a/graphics/sfml/src/window/Window.hpp b/graphics/sfml/src/window/Window.hpp index e9b1864..0fcb416 100644 --- a/graphics/sfml/src/window/Window.hpp +++ b/graphics/sfml/src/window/Window.hpp @@ -10,15 +10,13 @@ #include #include "shared/graphics/IWindow.hpp" -using namespace shared::graphics; - namespace arcade::graphics::sfml::window { class Window; } -class arcade::graphics::sfml::window::Window : public IWindow { +class arcade::graphics::sfml::window::Window: public shared::graphics::IWindow { public: - explicit Window(const IWindow::WindowInitProps &props); + explicit Window(const WindowInitProps &props); ~Window() override; /** @@ -82,14 +80,14 @@ class arcade::graphics::sfml::window::Window : public IWindow { * * @param props Properties of the entity to render */ - void render(const TextProps &props) override; + void render(const shared::graphics::TextProps &props) override; /** * @brief Render the entity with given properties * * @param props Properties of the entity to render */ - void render(const TextureProps &props) override; + void render(const shared::graphics::TextureProps &props) override; /** * @brief Clear the content of the window @@ -125,7 +123,7 @@ class arcade::graphics::sfml::window::Window : public IWindow { * but make another call `B` (directly after call `A`) `eventsB` * will result to an empty vector */ - std::vector getEvents() override; + std::vector getEvents() override; private: std::unique_ptr _window; From f4aada40dc6abcebddfe126395337f53db760192 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Fri, 29 Mar 2024 00:16:01 +0100 Subject: [PATCH 22/38] feat(graphics): add SFML icon --- graphics/sfml/src/window/Window.cpp | 52 ++++++++++++++++++----------- graphics/sfml/src/window/Window.hpp | 11 +++--- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/graphics/sfml/src/window/Window.cpp b/graphics/sfml/src/window/Window.cpp index 668ffb2..a12c494 100644 --- a/graphics/sfml/src/window/Window.cpp +++ b/graphics/sfml/src/window/Window.cpp @@ -5,31 +5,33 @@ ** Window class */ -#include #include "Window.hpp" #include "EventsHandler.hpp" #include "common/events/mouse/mouse.hpp" +#include "common/exceptions/WindowException.hpp" using namespace arcade::graphics::sfml::events; using namespace arcade::graphics::sfml::window; +using namespace arcade::graphics::common::exceptions; Window::Window(const IWindow::WindowInitProps &props) { - this->_mode = props.mode; - this->_fps = props.fps; - this->_window = std::make_unique( + _mode = props.mode; + _fps = props.fps; + _window.create( sf::VideoMode(props.size.x, props.size.y), props.title ); + Window::setIcon(props.icon); } Window::~Window() { - this->_window->close(); + _window.close(); } void Window::setTitle(const std::string &title) { - this->_window->setTitle(title); + _window.setTitle(title); } void Window::setSize(shared::types::Vector2u size) { @@ -43,24 +45,24 @@ shared::types::Vector2u Window::getSize() const { } void Window::setFramerateLimit(unsigned int fps) { - this->_window->setFramerateLimit(fps); - this->_fps = fps; + _window.setFramerateLimit(fps); + _fps = fps; } unsigned int Window::getFramerateLimit() const { - return this->_fps; + return _fps; } void Window::setMode(IWindow::WindowMode mode) { this->_mode = mode; if (mode == FULLSCREEN) { - this->_window->create( + _window.create( sf::VideoMode(1920, 1080), this->_title, sf::Style::Fullscreen ); } else { - this->_window->create( + _window.create( sf::VideoMode(1920, 1080), this->_title, sf::Style::Default @@ -69,15 +71,27 @@ void Window::setMode(IWindow::WindowMode mode) { } Window::WindowMode Window::getMode() const { - return this->_mode; + return _mode; } bool Window::isOpen() const { - return this->_window->isOpen(); + return _window.isOpen(); } -void Window::setIcon(const std::string &icon) { - (void) icon; +void Window::setIcon(const std::string &path) { + if (path.empty()) + return; + if (!_icon.loadFromFile(path)) { + throw WindowException( + "Failed to load icon at: " + path, + "Window.setIcon in SFML library" + ); + } + _window.setIcon( + _icon.getSize().x, + _icon.getSize().y, + _icon.getPixelsPtr() + ); } void Window::render(const shared::graphics::TextureProps &props) { (void) props; @@ -88,17 +102,17 @@ void Window::render(const shared::graphics::TextProps &props) { } void Window::clear() { - this->_window->clear(); + _window.clear(); } void Window::display() { - this->_window->display(); + _window.display(); } void Window::close() { - this->_window->close(); + _window.close(); } std::vector Window::getEvents() { - return EventsHandler::handleEvents(*this->_window); + return EventsHandler::handleEvents(_window); } diff --git a/graphics/sfml/src/window/Window.hpp b/graphics/sfml/src/window/Window.hpp index 0fcb416..81f6c87 100644 --- a/graphics/sfml/src/window/Window.hpp +++ b/graphics/sfml/src/window/Window.hpp @@ -117,7 +117,7 @@ class arcade::graphics::sfml::window::Window: public shared::graphics::IWindow { /** * @brief Get the events object * - * @return Last events occured + * @return Last events occurred * @warning Call successively this method will result in losing events * @note Call `A` return `eventsA` containing 2 events, * but make another call `B` (directly after call `A`) `eventsB` @@ -126,8 +126,9 @@ class arcade::graphics::sfml::window::Window: public shared::graphics::IWindow { std::vector getEvents() override; private: - std::unique_ptr _window; - std::string _title; - unsigned int _fps; - WindowMode _mode; + sf::RenderWindow _window; + std::string _title; + unsigned int _fps; + WindowMode _mode; + sf::Image _icon; }; From 6e93f3215aadef74ff4bbe975ca9fa0f3b0db1dd Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Fri, 29 Mar 2024 15:07:31 +0100 Subject: [PATCH 23/38] refactor(graphics): add sfml auto resize --- graphics/sfml/CMakeLists.txt | 2 + graphics/sfml/src/window/EventsHandler.cpp | 26 ++++--- graphics/sfml/src/window/EventsHandler.hpp | 23 +++--- graphics/sfml/src/window/Renderer.cpp | 82 ++++++++++++++++++++++ graphics/sfml/src/window/Renderer.hpp | 76 ++++++++++++++++++++ graphics/sfml/src/window/Window.cpp | 38 ++++++++-- graphics/sfml/src/window/Window.hpp | 11 +++ 7 files changed, 228 insertions(+), 30 deletions(-) create mode 100644 graphics/sfml/src/window/Renderer.cpp create mode 100644 graphics/sfml/src/window/Renderer.hpp diff --git a/graphics/sfml/CMakeLists.txt b/graphics/sfml/CMakeLists.txt index 856e225..9a4ffd1 100644 --- a/graphics/sfml/CMakeLists.txt +++ b/graphics/sfml/CMakeLists.txt @@ -13,6 +13,8 @@ add_library(sfml SHARED src/texture/Texture.hpp src/font/Font.cpp src/font/Font.hpp + src/window/Renderer.cpp + src/window/Renderer.hpp ) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../common PRIVATE) diff --git a/graphics/sfml/src/window/EventsHandler.cpp b/graphics/sfml/src/window/EventsHandler.cpp index c3aa556..4944fd0 100644 --- a/graphics/sfml/src/window/EventsHandler.cpp +++ b/graphics/sfml/src/window/EventsHandler.cpp @@ -10,7 +10,7 @@ #include "EventsHandler.hpp" #include "utils/compiler.hpp" -using namespace arcade::graphics::sfml::events; +using namespace arcade::graphics::sfml::window; EventsHandler::EventHandler EventsHandler::_getHandler(sf::Event::EventType type) { static std::map handlers = { @@ -27,13 +27,14 @@ EventsHandler::EventHandler EventsHandler::_getHandler(sf::Event::EventType type return handler != handlers.end() ? handler->second : nullptr; } -std::vector EventsHandler::handleEvents(sf::RenderWindow &window) { + +std::vector EventsHandler::handleEvents(Window &_window) { std::vector events; sf::Event SFMLEvent{}; - while (window.pollEvent(SFMLEvent)) { + while (_window.getInnerWindow().pollEvent(SFMLEvent)) { auto handler = _getHandler(SFMLEvent.type); - auto event = handler ? handler(SFMLEvent, window) : nullptr; + auto event = handler ? handler(SFMLEvent, _window) : nullptr; if (event) events.push_back(event); @@ -139,10 +140,7 @@ bool EventsHandler::_handleCharKey( return true; } -EventPtr EventsHandler::_handleKeyPressEvent( - sf::Event &event, - unused sf::RenderWindow &window -) { +EventPtr EventsHandler::_handleKeyPressEvent(sf::Event &event, unused Window &window) { IKeyEvent::KeyType type = IKeyEvent::KeyType::UNKNOWN; IKeyEvent::KeyCode code; @@ -159,7 +157,7 @@ EventPtr EventsHandler::_handleKeyPressEvent( EventPtr EventsHandler::_handleKeyReleaseEvent( sf::Event &event, - unused sf::RenderWindow &window + unused Window &window ) { IKeyEvent::KeyType type = IKeyEvent::KeyType::UNKNOWN; IKeyEvent::KeyCode code; @@ -177,21 +175,21 @@ EventPtr EventsHandler::_handleKeyReleaseEvent( EventPtr EventsHandler::_handleWindowCloseEvent( unused sf::Event &event, - unused sf::RenderWindow &window + unused Window &window ) { return std::make_shared(); } EventPtr EventsHandler::_handleWindowResizeEvent( unused sf::Event &event, - unused sf::RenderWindow &window + Window &window ) { return std::make_shared(); } EventPtr EventsHandler::_handleMouseMoveEvent( sf::Event &event, - unused sf::RenderWindow &window + unused Window &window ) { return std::make_shared(Vector2i( event.mouseMove.x, @@ -201,7 +199,7 @@ EventPtr EventsHandler::_handleMouseMoveEvent( EventPtr EventsHandler::_handleMouseButtonPressEvent( sf::Event &event, - unused sf::RenderWindow &window + unused Window &window ) { Vector2i pos(event.mouseButton.x, event.mouseButton.y); @@ -215,7 +213,7 @@ EventPtr EventsHandler::_handleMouseButtonPressEvent( EventPtr EventsHandler::_handleMouseBtnReleaseEvent( sf::Event &event, - unused sf::RenderWindow &window + unused Window &window ) { Vector2i pos(event.mouseButton.x, event.mouseButton.y); diff --git a/graphics/sfml/src/window/EventsHandler.hpp b/graphics/sfml/src/window/EventsHandler.hpp index b2d072a..abc766f 100644 --- a/graphics/sfml/src/window/EventsHandler.hpp +++ b/graphics/sfml/src/window/EventsHandler.hpp @@ -13,24 +13,25 @@ #include "common/events/window/window.hpp" #include "common/events/mouse/mouse.hpp" #include "common/events/key/key.hpp" +#include "Window.hpp" -namespace arcade::graphics::sfml::events { +namespace arcade::graphics::sfml::window { class EventsHandler; } -class arcade::graphics::sfml::events::EventsHandler { +class arcade::graphics::sfml::window::EventsHandler { public: EventsHandler() = delete; ~EventsHandler() = delete; - typedef EventPtr (*EventHandler)(sf::Event &event, sf::RenderWindow &window); + typedef EventPtr (*EventHandler)(sf::Event &event, Window &window); /** * @brief Handle events from SFML * @param window Window object * @return Vector of events */ - static std::vector handleEvents(sf::RenderWindow &window); + static std::vector handleEvents(Window &window); private: @@ -74,7 +75,7 @@ class arcade::graphics::sfml::events::EventsHandler { * @param window Window object * @return Pointer to created event or null if not handled */ - static EventPtr _handleKeyPressEvent(sf::Event &event, sf::RenderWindow &window); + static EventPtr _handleKeyPressEvent(sf::Event &event, Window &window); /** * @brief Handle key release event @@ -82,7 +83,7 @@ class arcade::graphics::sfml::events::EventsHandler { * @param window Window object * @return Pointer to created event or null if not handled */ - static EventPtr _handleKeyReleaseEvent(sf::Event &event, sf::RenderWindow &window); + static EventPtr _handleKeyReleaseEvent(sf::Event &event, Window &window); /** * @brief Handle mouse button press event @@ -90,7 +91,7 @@ class arcade::graphics::sfml::events::EventsHandler { * @param window Window object * @return Pointer to created event or null if not handled */ - static EventPtr _handleMouseButtonPressEvent(sf::Event &event, sf::RenderWindow &window); + static EventPtr _handleMouseButtonPressEvent(sf::Event &event, Window &window); /** * @brief Handle mouse button release event @@ -98,7 +99,7 @@ class arcade::graphics::sfml::events::EventsHandler { * @param window Window object * @return Pointer to created event or null if not handled */ - static EventPtr _handleMouseBtnReleaseEvent(sf::Event &event, sf::RenderWindow &window); + static EventPtr _handleMouseBtnReleaseEvent(sf::Event &event, Window &window); /** * @brief Handle mouse move event @@ -106,7 +107,7 @@ class arcade::graphics::sfml::events::EventsHandler { * @param window Window object * @return Pointer to created event or null if not handled */ - static EventPtr _handleMouseMoveEvent(sf::Event &event, sf::RenderWindow &window); + static EventPtr _handleMouseMoveEvent(sf::Event &event, Window &window); /** * @brief Handle window close event @@ -114,7 +115,7 @@ class arcade::graphics::sfml::events::EventsHandler { * @param window Window object * @return Pointer to created event or null if not handled */ - static EventPtr _handleWindowCloseEvent(sf::Event &event, sf::RenderWindow &window); + static EventPtr _handleWindowCloseEvent(sf::Event &event, Window &window); /** * @brief Handle window resize event @@ -122,5 +123,5 @@ class arcade::graphics::sfml::events::EventsHandler { * @param window Window object * @return Pointer to created event or null if not handled */ - static EventPtr _handleWindowResizeEvent(sf::Event &event, sf::RenderWindow &window); + static EventPtr _handleWindowResizeEvent(sf::Event &event, Window &window); }; diff --git a/graphics/sfml/src/window/Renderer.cpp b/graphics/sfml/src/window/Renderer.cpp new file mode 100644 index 0000000..471d33d --- /dev/null +++ b/graphics/sfml/src/window/Renderer.cpp @@ -0,0 +1,82 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** Renderer.cpp +*/ + +#include +#include "Renderer.hpp" +#include "font/Font.hpp" +#include "common/exceptions/WindowException.hpp" + +using namespace arcade::graphics::sfml::window; +using namespace arcade::graphics::common::exceptions; + +Renderer::Renderer(sf::RenderWindow &window, const Vector2u &size): + _window(window), + _tileSize(20, 20), + size(size.x, size.y), + tileSize(_tileSize) +{ + _text.setFont(sf::Font()); + _sprite.setTexture(sf::Texture()); +} + +void Renderer::render(const shared::graphics::TextProps &props) { + std::cout << "Rendering text inner" << std::endl; + auto font = _castOrThrow(props.font); + + _reset(_text); + _text.setFont(font->getInnerFont()); + _text.setString(props.content); + _text.setCharacterSize(props.fontSize); + _text.setFillColor(sf::Color( + props.color.r, + props.color.g, + props.color.b, + props.color.a) + ); + _window.draw(_text); +} + +void Renderer::render(unused const shared::graphics::TextureProps &props) { + +} + +void Renderer::_reset(sf::Text &text) { + text.setString(""); + text.setCharacterSize(0); + text.setFillColor(sf::Color::White); + text.setStyle(sf::Text::Regular); + text.setPosition(0, 0); + text.setScale(1, 1); + text.setOrigin(0, 0); +} + +void Renderer::_reset(sf::Sprite &sprite) { + sprite.setTextureRect(sf::IntRect(0, 0, 0, 0)); + sprite.setColor(sf::Color::White); + sprite.setScale(1, 1); + sprite.setPosition(0, 0); + sprite.setOrigin(0, 0); +} + +template +std::shared_ptr Renderer::_castOrThrow(std::shared_ptr from) { + std::shared_ptr to = std::dynamic_pointer_cast(from); + if (!to) { + throw WindowException( + "Failed to cast shared pointer of:" + std::string(typeid(from).name()) + " to " + typeid(to).name(), + "SFML Library Renderer::_castOrThrow" + ); + } + return to; +} + +void Renderer::updateTileSize() { + if (size.x != 0) + _tileSize.x = static_cast(_window.getSize().x) / static_cast(size.x); + if (size.y != 0) + _tileSize.y = static_cast(_window.getSize().y) / static_cast(size.y); +} diff --git a/graphics/sfml/src/window/Renderer.hpp b/graphics/sfml/src/window/Renderer.hpp new file mode 100644 index 0000000..83f9048 --- /dev/null +++ b/graphics/sfml/src/window/Renderer.hpp @@ -0,0 +1,76 @@ +/* +** EPITECH PROJECT, 2024 +** arcade +** File description: +** Renderer.hpp +*/ + +#pragma once + +#include +#include "shared/graphics/ITexture.hpp" +#include "shared/graphics/types/TextureProps.hpp" +#include "shared/graphics/types/TextProps.hpp" + +namespace arcade::graphics::sfml::window { + class Renderer; + class Window; +} + +class arcade::graphics::sfml::window::Renderer { +public: + Renderer(sf::RenderWindow &window, const Vector2u &size); + ~Renderer() = default; + + /** + * @brief Render a texture on the window + * @param props Texture properties + */ + void render(const shared::graphics::TextureProps &props); + + /** + * @brief Render a text on the window + * @param props Text properties + */ + void render(const shared::graphics::TextProps &props); + + /** + * @brief Get the size of a tile + * @param props Texture properties + * @return Size of a tile + */ + void updateTileSize(); + + /** + * @brief Get the size of a tile + * @return Size of a tile + */ + const Vector2f &tileSize; + + /** + * @brief Get the size of the rendering area + * @return Size of the rendering area + */ + const Vector2u size; + +private: + Vector2f _tileSize; + sf::RenderWindow &_window; + sf::Text _text; + sf::Sprite _sprite; + + template + static std::shared_ptr _castOrThrow(std::shared_ptr from); + + /** + * @brief Reset the text properties + * @param text Text to reset + */ + static void _reset(sf::Text &text); + + /** + * @brief Reset the sprite properties + * @param sprite Sprite to reset + */ + static void _reset(sf::Sprite &sprite); +}; diff --git a/graphics/sfml/src/window/Window.cpp b/graphics/sfml/src/window/Window.cpp index a12c494..e8751d8 100644 --- a/graphics/sfml/src/window/Window.cpp +++ b/graphics/sfml/src/window/Window.cpp @@ -5,21 +5,23 @@ ** Window class */ +#include #include "Window.hpp" #include "EventsHandler.hpp" #include "common/events/mouse/mouse.hpp" #include "common/exceptions/WindowException.hpp" -using namespace arcade::graphics::sfml::events; using namespace arcade::graphics::sfml::window; using namespace arcade::graphics::common::exceptions; -Window::Window(const IWindow::WindowInitProps &props) +Window::Window(const IWindow::WindowInitProps &props): _renderer(_window, props.size) { + auto size = _getInitSize(props.size); + std::cout << "Size: " << size.x << " " << size.y << std::endl; _mode = props.mode; _fps = props.fps; _window.create( - sf::VideoMode(props.size.x, props.size.y), + sf::VideoMode(size.x, size.y), props.title ); Window::setIcon(props.icon); @@ -30,6 +32,10 @@ Window::~Window() _window.close(); } +sf::RenderWindow &Window::getInnerWindow() noexcept { + return _window; +} + void Window::setTitle(const std::string &title) { _window.setTitle(title); } @@ -93,12 +99,14 @@ void Window::setIcon(const std::string &path) { _icon.getPixelsPtr() ); } + void Window::render(const shared::graphics::TextureProps &props) { (void) props; } void Window::render(const shared::graphics::TextProps &props) { - (void) props; + std::cout << "Rendering text" << std::endl; + _renderer.render(props); } void Window::clear() { @@ -106,6 +114,16 @@ void Window::clear() { } void Window::display() { + sf::RectangleShape rect(sf::Vector2f(_renderer.tileSize.x, _renderer.tileSize.y)); + rect.setOutlineColor(sf::Color::Red); + rect.setFillColor(sf::Color::Transparent); + rect.setOutlineThickness(1); + for (unsigned int i = 0; i < 1920 / _renderer.tileSize.x; i++) { + for (unsigned int j = 0; j < 1080 / _renderer.tileSize.y; j++) { + rect.setPosition(i * _renderer.tileSize.x, j * _renderer.tileSize.y); + _window.draw(rect); + } + } _window.display(); } @@ -114,5 +132,15 @@ void Window::close() { } std::vector Window::getEvents() { - return EventsHandler::handleEvents(_window); + return EventsHandler::handleEvents(*this); +} + +Vector2u Window::_getInitSize(const Vector2u &requestedSize) const { + Vector2u size(1920, 1080); + + if (requestedSize.x * static_cast(_renderer.tileSize.x) < 1920) + size.x = requestedSize.x * static_cast(_renderer.tileSize.x); + if (requestedSize.y * static_cast(_renderer.tileSize.y) < 1080) + size.y = requestedSize.y * static_cast(_renderer.tileSize.y); + return size; } diff --git a/graphics/sfml/src/window/Window.hpp b/graphics/sfml/src/window/Window.hpp index 81f6c87..30ab3fc 100644 --- a/graphics/sfml/src/window/Window.hpp +++ b/graphics/sfml/src/window/Window.hpp @@ -8,7 +8,9 @@ #pragma once #include +#include "Renderer.hpp" #include "shared/graphics/IWindow.hpp" +#include "EventsHandler.hpp" namespace arcade::graphics::sfml::window { class Window; @@ -125,7 +127,16 @@ class arcade::graphics::sfml::window::Window: public shared::graphics::IWindow { */ std::vector getEvents() override; + /** + * @brief Get the window object + * @return Window object + */ + sf::RenderWindow &getInnerWindow() noexcept; + private: + Vector2u _getInitSize(const Vector2u &requestedSize) const; + + Renderer _renderer; sf::RenderWindow _window; std::string _title; unsigned int _fps; From ee03858628e684f7fe5546b684a5e8147bb888de Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Fri, 29 Mar 2024 17:57:14 +0100 Subject: [PATCH 24/38] refactor(graphics): update sfml mouse events --- graphics/sfml/src/window/EventsHandler.cpp | 33 +++++++++++--- graphics/sfml/src/window/EventsHandler.hpp | 18 ++++++-- graphics/sfml/src/window/Renderer.cpp | 12 +---- graphics/sfml/src/window/Renderer.hpp | 13 ------ graphics/sfml/src/window/Window.cpp | 52 ++++++++++------------ graphics/sfml/src/window/Window.hpp | 9 +++- 6 files changed, 73 insertions(+), 64 deletions(-) diff --git a/graphics/sfml/src/window/EventsHandler.cpp b/graphics/sfml/src/window/EventsHandler.cpp index 4944fd0..d16e617 100644 --- a/graphics/sfml/src/window/EventsHandler.cpp +++ b/graphics/sfml/src/window/EventsHandler.cpp @@ -7,6 +7,7 @@ #include +#include "Window.hpp" #include "EventsHandler.hpp" #include "utils/compiler.hpp" @@ -27,8 +28,9 @@ EventsHandler::EventHandler EventsHandler::_getHandler(sf::Event::EventType type return handler != handlers.end() ? handler->second : nullptr; } +EventsHandler::EventsHandler(Window &window): _window(window) {} -std::vector EventsHandler::handleEvents(Window &_window) { +std::vector EventsHandler::handleEvents() { std::vector events; sf::Event SFMLEvent{}; @@ -191,9 +193,9 @@ EventPtr EventsHandler::_handleMouseMoveEvent( sf::Event &event, unused Window &window ) { - return std::make_shared(Vector2i( - event.mouseMove.x, - event.mouseMove.y + return std::make_shared(_resolvePosition( + Vector2i(event.mouseMove.x, event.mouseMove.y), + window )); } @@ -201,7 +203,10 @@ EventPtr EventsHandler::_handleMouseButtonPressEvent( sf::Event &event, unused Window &window ) { - Vector2i pos(event.mouseButton.x, event.mouseButton.y); + Vector2i pos = _resolvePosition( + Vector2i(event.mouseButton.x, event.mouseButton.y), + window + ); if (event.mouseButton.button == sf::Mouse::Button::Left) return std::make_shared(pos, IMouseButtonEvent::MouseButton::LEFT); @@ -215,7 +220,10 @@ EventPtr EventsHandler::_handleMouseBtnReleaseEvent( sf::Event &event, unused Window &window ) { - Vector2i pos(event.mouseButton.x, event.mouseButton.y); + Vector2i pos = _resolvePosition( + Vector2i(event.mouseButton.x, event.mouseButton.y), + window + ); if (event.mouseButton.button == sf::Mouse::Button::Left) return std::make_shared(pos, IMouseButtonEvent::MouseButton::LEFT); @@ -224,3 +232,16 @@ EventPtr EventsHandler::_handleMouseBtnReleaseEvent( else return nullptr; } + +Vector2i EventsHandler::_resolvePosition( + Vector2i position, + Window &window +){ + auto size = window.getSize(); + auto realSize = window.getInnerWindow().getSize(); + + return { + static_cast(position.x * size.x / realSize.x), + static_cast(position.y * size.y / realSize.y) + }; +} diff --git a/graphics/sfml/src/window/EventsHandler.hpp b/graphics/sfml/src/window/EventsHandler.hpp index abc766f..e78132b 100644 --- a/graphics/sfml/src/window/EventsHandler.hpp +++ b/graphics/sfml/src/window/EventsHandler.hpp @@ -13,16 +13,16 @@ #include "common/events/window/window.hpp" #include "common/events/mouse/mouse.hpp" #include "common/events/key/key.hpp" -#include "Window.hpp" namespace arcade::graphics::sfml::window { class EventsHandler; + class Window; } class arcade::graphics::sfml::window::EventsHandler { public: - EventsHandler() = delete; - ~EventsHandler() = delete; + explicit EventsHandler(Window &window); + ~EventsHandler() = default; typedef EventPtr (*EventHandler)(sf::Event &event, Window &window); @@ -31,7 +31,7 @@ class arcade::graphics::sfml::window::EventsHandler { * @param window Window object * @return Vector of events */ - static std::vector handleEvents(Window &window); + std::vector handleEvents(); private: @@ -124,4 +124,14 @@ class arcade::graphics::sfml::window::EventsHandler { * @return Pointer to created event or null if not handled */ static EventPtr _handleWindowResizeEvent(sf::Event &event, Window &window); + + /** + * @brief Resolve position of the event to convert it in tiles unit + * @param position Position to resolve + * @param window Window object of which relate the position + * @return Resolved position + */ + static Vector2i _resolvePosition(Vector2i position, Window &window); + + window::Window &_window; }; diff --git a/graphics/sfml/src/window/Renderer.cpp b/graphics/sfml/src/window/Renderer.cpp index 471d33d..2226e8d 100644 --- a/graphics/sfml/src/window/Renderer.cpp +++ b/graphics/sfml/src/window/Renderer.cpp @@ -5,7 +5,6 @@ ** Renderer.cpp */ -#include #include "Renderer.hpp" #include "font/Font.hpp" #include "common/exceptions/WindowException.hpp" @@ -15,8 +14,7 @@ using namespace arcade::graphics::common::exceptions; Renderer::Renderer(sf::RenderWindow &window, const Vector2u &size): _window(window), - _tileSize(20, 20), - size(size.x, size.y), + _tileSize(10, 10), tileSize(_tileSize) { _text.setFont(sf::Font()); @@ -24,7 +22,6 @@ Renderer::Renderer(sf::RenderWindow &window, const Vector2u &size): } void Renderer::render(const shared::graphics::TextProps &props) { - std::cout << "Rendering text inner" << std::endl; auto font = _castOrThrow(props.font); _reset(_text); @@ -73,10 +70,3 @@ std::shared_ptr Renderer::_castOrThrow(std::shared_ptr from) { } return to; } - -void Renderer::updateTileSize() { - if (size.x != 0) - _tileSize.x = static_cast(_window.getSize().x) / static_cast(size.x); - if (size.y != 0) - _tileSize.y = static_cast(_window.getSize().y) / static_cast(size.y); -} diff --git a/graphics/sfml/src/window/Renderer.hpp b/graphics/sfml/src/window/Renderer.hpp index 83f9048..5cb3145 100644 --- a/graphics/sfml/src/window/Renderer.hpp +++ b/graphics/sfml/src/window/Renderer.hpp @@ -34,25 +34,12 @@ class arcade::graphics::sfml::window::Renderer { */ void render(const shared::graphics::TextProps &props); - /** - * @brief Get the size of a tile - * @param props Texture properties - * @return Size of a tile - */ - void updateTileSize(); - /** * @brief Get the size of a tile * @return Size of a tile */ const Vector2f &tileSize; - /** - * @brief Get the size of the rendering area - * @return Size of the rendering area - */ - const Vector2u size; - private: Vector2f _tileSize; sf::RenderWindow &_window; diff --git a/graphics/sfml/src/window/Window.cpp b/graphics/sfml/src/window/Window.cpp index e8751d8..15fb0b7 100644 --- a/graphics/sfml/src/window/Window.cpp +++ b/graphics/sfml/src/window/Window.cpp @@ -5,7 +5,6 @@ ** Window class */ -#include #include "Window.hpp" #include "EventsHandler.hpp" #include "common/events/mouse/mouse.hpp" @@ -14,10 +13,13 @@ using namespace arcade::graphics::sfml::window; using namespace arcade::graphics::common::exceptions; -Window::Window(const IWindow::WindowInitProps &props): _renderer(_window, props.size) +Window::Window(const IWindow::WindowInitProps &props): + _size(props.size), + _renderer(_window, props.size), + _eventsHandler(*this) { - auto size = _getInitSize(props.size); - std::cout << "Size: " << size.x << " " << size.y << std::endl; + auto size = _getPixelSizeFromTiles(props.size); + _mode = props.mode; _fps = props.fps; _window.create( @@ -41,13 +43,14 @@ void Window::setTitle(const std::string &title) { } void Window::setSize(shared::types::Vector2u size) { - (void) size; - //this->_window->setSize(sf::Vector2u(size.x, size.y)); + auto real = _getPixelSizeFromTiles(size); + + _size = size; + _window.setSize(sf::Vector2u(real.x, real.y)); } shared::types::Vector2u Window::getSize() const { - return {12, 12}; - //return shared::types::Vector2u(this->_window->getSize().x, this->_window->getSize().y); + return _size; } void Window::setFramerateLimit(unsigned int fps) { @@ -60,16 +63,18 @@ unsigned int Window::getFramerateLimit() const { } void Window::setMode(IWindow::WindowMode mode) { + auto size = _window.getSize(); + this->_mode = mode; if (mode == FULLSCREEN) { _window.create( - sf::VideoMode(1920, 1080), + sf::VideoMode(size.x, size.y), this->_title, sf::Style::Fullscreen ); } else { _window.create( - sf::VideoMode(1920, 1080), + sf::VideoMode(size.x, size.y), this->_title, sf::Style::Default ); @@ -105,7 +110,6 @@ void Window::render(const shared::graphics::TextureProps &props) { } void Window::render(const shared::graphics::TextProps &props) { - std::cout << "Rendering text" << std::endl; _renderer.render(props); } @@ -114,16 +118,6 @@ void Window::clear() { } void Window::display() { - sf::RectangleShape rect(sf::Vector2f(_renderer.tileSize.x, _renderer.tileSize.y)); - rect.setOutlineColor(sf::Color::Red); - rect.setFillColor(sf::Color::Transparent); - rect.setOutlineThickness(1); - for (unsigned int i = 0; i < 1920 / _renderer.tileSize.x; i++) { - for (unsigned int j = 0; j < 1080 / _renderer.tileSize.y; j++) { - rect.setPosition(i * _renderer.tileSize.x, j * _renderer.tileSize.y); - _window.draw(rect); - } - } _window.display(); } @@ -132,15 +126,15 @@ void Window::close() { } std::vector Window::getEvents() { - return EventsHandler::handleEvents(*this); + return _eventsHandler.handleEvents(); } -Vector2u Window::_getInitSize(const Vector2u &requestedSize) const { - Vector2u size(1920, 1080); +Vector2u Window::_getPixelSizeFromTiles(const Vector2u &size) const { + Vector2u real(1920, 1080); - if (requestedSize.x * static_cast(_renderer.tileSize.x) < 1920) - size.x = requestedSize.x * static_cast(_renderer.tileSize.x); - if (requestedSize.y * static_cast(_renderer.tileSize.y) < 1080) - size.y = requestedSize.y * static_cast(_renderer.tileSize.y); - return size; + if (size.x * static_cast(_renderer.tileSize.x) < 1920) + real.x = size.x * static_cast(_renderer.tileSize.x); + if (size.y * static_cast(_renderer.tileSize.y) < 1080) + real.y = size.y * static_cast(_renderer.tileSize.y); + return real; } diff --git a/graphics/sfml/src/window/Window.hpp b/graphics/sfml/src/window/Window.hpp index 30ab3fc..6eefcf1 100644 --- a/graphics/sfml/src/window/Window.hpp +++ b/graphics/sfml/src/window/Window.hpp @@ -133,13 +133,20 @@ class arcade::graphics::sfml::window::Window: public shared::graphics::IWindow { */ sf::RenderWindow &getInnerWindow() noexcept; + /** + * @brief On resize event + */ + void onResize(); + private: - Vector2u _getInitSize(const Vector2u &requestedSize) const; + Vector2u _getPixelSizeFromTiles(const Vector2u &size) const; + EventsHandler _eventsHandler; Renderer _renderer; sf::RenderWindow _window; std::string _title; unsigned int _fps; WindowMode _mode; sf::Image _icon; + Vector2u _size; }; From 38defe2c8b50938f2ec88c9c1d4706ddf968fdef Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Fri, 29 Mar 2024 18:06:07 +0100 Subject: [PATCH 25/38] refactor(graphics:sfml): move tile size relation from Renderer to Window --- graphics/sfml/src/window/Renderer.cpp | 8 +++----- graphics/sfml/src/window/Renderer.hpp | 12 +++--------- graphics/sfml/src/window/Window.cpp | 14 ++++++++------ graphics/sfml/src/window/Window.hpp | 7 ++++--- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/graphics/sfml/src/window/Renderer.cpp b/graphics/sfml/src/window/Renderer.cpp index 2226e8d..13abf5f 100644 --- a/graphics/sfml/src/window/Renderer.cpp +++ b/graphics/sfml/src/window/Renderer.cpp @@ -5,6 +5,7 @@ ** Renderer.cpp */ +#include "Window.hpp" #include "Renderer.hpp" #include "font/Font.hpp" #include "common/exceptions/WindowException.hpp" @@ -12,10 +13,7 @@ using namespace arcade::graphics::sfml::window; using namespace arcade::graphics::common::exceptions; -Renderer::Renderer(sf::RenderWindow &window, const Vector2u &size): - _window(window), - _tileSize(10, 10), - tileSize(_tileSize) +Renderer::Renderer(Window &window): _window(window), _layer(_window.getInnerWindow()) { _text.setFont(sf::Font()); _sprite.setTexture(sf::Texture()); @@ -34,7 +32,7 @@ void Renderer::render(const shared::graphics::TextProps &props) { props.color.b, props.color.a) ); - _window.draw(_text); + _layer.draw(_text); } void Renderer::render(unused const shared::graphics::TextureProps &props) { diff --git a/graphics/sfml/src/window/Renderer.hpp b/graphics/sfml/src/window/Renderer.hpp index 5cb3145..822af17 100644 --- a/graphics/sfml/src/window/Renderer.hpp +++ b/graphics/sfml/src/window/Renderer.hpp @@ -19,7 +19,7 @@ namespace arcade::graphics::sfml::window { class arcade::graphics::sfml::window::Renderer { public: - Renderer(sf::RenderWindow &window, const Vector2u &size); + explicit Renderer(Window &window); ~Renderer() = default; /** @@ -34,15 +34,9 @@ class arcade::graphics::sfml::window::Renderer { */ void render(const shared::graphics::TextProps &props); - /** - * @brief Get the size of a tile - * @return Size of a tile - */ - const Vector2f &tileSize; - private: - Vector2f _tileSize; - sf::RenderWindow &_window; + Window &_window; + sf::RenderWindow &_layer; sf::Text _text; sf::Sprite _sprite; diff --git a/graphics/sfml/src/window/Window.cpp b/graphics/sfml/src/window/Window.cpp index 15fb0b7..5c98879 100644 --- a/graphics/sfml/src/window/Window.cpp +++ b/graphics/sfml/src/window/Window.cpp @@ -13,9 +13,11 @@ using namespace arcade::graphics::sfml::window; using namespace arcade::graphics::common::exceptions; +const Vector2u Window::tileSize = { 10, 10 }; + Window::Window(const IWindow::WindowInitProps &props): _size(props.size), - _renderer(_window, props.size), + _renderer(*this), _eventsHandler(*this) { auto size = _getPixelSizeFromTiles(props.size); @@ -129,12 +131,12 @@ std::vector Window::getEvents() { return _eventsHandler.handleEvents(); } -Vector2u Window::_getPixelSizeFromTiles(const Vector2u &size) const { +Vector2u Window::_getPixelSizeFromTiles(const Vector2u &size) { Vector2u real(1920, 1080); - if (size.x * static_cast(_renderer.tileSize.x) < 1920) - real.x = size.x * static_cast(_renderer.tileSize.x); - if (size.y * static_cast(_renderer.tileSize.y) < 1080) - real.y = size.y * static_cast(_renderer.tileSize.y); + if (size.x * static_cast(tileSize.x) < 1920) + real.x = size.x * static_cast(tileSize.x); + if (size.y * static_cast(tileSize.y) < 1080) + real.y = size.y * static_cast(tileSize.y); return real; } diff --git a/graphics/sfml/src/window/Window.hpp b/graphics/sfml/src/window/Window.hpp index 6eefcf1..4f348b0 100644 --- a/graphics/sfml/src/window/Window.hpp +++ b/graphics/sfml/src/window/Window.hpp @@ -134,12 +134,13 @@ class arcade::graphics::sfml::window::Window: public shared::graphics::IWindow { sf::RenderWindow &getInnerWindow() noexcept; /** - * @brief On resize event + * @brief Get the size of a tile + * @return Size of a tile */ - void onResize(); + static const Vector2u tileSize; private: - Vector2u _getPixelSizeFromTiles(const Vector2u &size) const; + static Vector2u _getPixelSizeFromTiles(const Vector2u &size); EventsHandler _eventsHandler; Renderer _renderer; From 39bc8cbb0705f8373ef388aee06547c92238ba27 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Fri, 29 Mar 2024 18:28:05 +0100 Subject: [PATCH 26/38] refactor(graphics:sfml): move positions conversions to Window class --- graphics/sfml/src/window/EventsHandler.cpp | 34 +++++----------------- graphics/sfml/src/window/Window.cpp | 19 ++++++++++++ graphics/sfml/src/window/Window.hpp | 12 ++++++++ 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/graphics/sfml/src/window/EventsHandler.cpp b/graphics/sfml/src/window/EventsHandler.cpp index d16e617..877a4d8 100644 --- a/graphics/sfml/src/window/EventsHandler.cpp +++ b/graphics/sfml/src/window/EventsHandler.cpp @@ -191,22 +191,18 @@ EventPtr EventsHandler::_handleWindowResizeEvent( EventPtr EventsHandler::_handleMouseMoveEvent( sf::Event &event, - unused Window &window + Window &window ) { - return std::make_shared(_resolvePosition( - Vector2i(event.mouseMove.x, event.mouseMove.y), - window - )); + return std::make_shared( + window.pixelsToTiles(Vector2i(event.mouseMove.x, event.mouseMove.y)) + ); } EventPtr EventsHandler::_handleMouseButtonPressEvent( sf::Event &event, - unused Window &window + Window &window ) { - Vector2i pos = _resolvePosition( - Vector2i(event.mouseButton.x, event.mouseButton.y), - window - ); + Vector2i pos = window.pixelsToTiles(Vector2i(event.mouseButton.x, event.mouseButton.y)); if (event.mouseButton.button == sf::Mouse::Button::Left) return std::make_shared(pos, IMouseButtonEvent::MouseButton::LEFT); @@ -220,10 +216,7 @@ EventPtr EventsHandler::_handleMouseBtnReleaseEvent( sf::Event &event, unused Window &window ) { - Vector2i pos = _resolvePosition( - Vector2i(event.mouseButton.x, event.mouseButton.y), - window - ); + Vector2i pos = window.pixelsToTiles(Vector2i(event.mouseButton.x, event.mouseButton.y)); if (event.mouseButton.button == sf::Mouse::Button::Left) return std::make_shared(pos, IMouseButtonEvent::MouseButton::LEFT); @@ -232,16 +225,3 @@ EventPtr EventsHandler::_handleMouseBtnReleaseEvent( else return nullptr; } - -Vector2i EventsHandler::_resolvePosition( - Vector2i position, - Window &window -){ - auto size = window.getSize(); - auto realSize = window.getInnerWindow().getSize(); - - return { - static_cast(position.x * size.x / realSize.x), - static_cast(position.y * size.y / realSize.y) - }; -} diff --git a/graphics/sfml/src/window/Window.cpp b/graphics/sfml/src/window/Window.cpp index 5c98879..df0194f 100644 --- a/graphics/sfml/src/window/Window.cpp +++ b/graphics/sfml/src/window/Window.cpp @@ -140,3 +140,22 @@ Vector2u Window::_getPixelSizeFromTiles(const Vector2u &size) { real.y = size.y * static_cast(tileSize.y); return real; } + +Vector2i Window::pixelsToTiles(const shared::types::Vector2i &position) const { + auto realSize = _window.getSize(); + + return { + static_cast(position.x * _size.x / realSize.x), + static_cast(position.y * _size.y / realSize.y) + }; +} + +Vector2i Window::tilesToPixels(const Vector2i &position) const { + auto realSize = _window.getSize(); + + return { + static_cast(position.x * realSize.x / _size.x), + static_cast(position.y * realSize.y / _size.y) + }; +} + diff --git a/graphics/sfml/src/window/Window.hpp b/graphics/sfml/src/window/Window.hpp index 4f348b0..93ad26c 100644 --- a/graphics/sfml/src/window/Window.hpp +++ b/graphics/sfml/src/window/Window.hpp @@ -133,6 +133,18 @@ class arcade::graphics::sfml::window::Window: public shared::graphics::IWindow { */ sf::RenderWindow &getInnerWindow() noexcept; + /** + * @brief Convert a position in pixels to a position in tiles + * @return Converted position + */ + Vector2i pixelsToTiles(const Vector2i &position) const; + + /** + * @brief Convert a position in tiles to a position in pixels + * @return Converted position + */ + Vector2i tilesToPixels(const Vector2i &position) const; + /** * @brief Get the size of a tile * @return Size of a tile From 14bf683ca8f9115624c990adc0d9ec23c4403f04 Mon Sep 17 00:00:00 2001 From: Yann Date: Fri, 29 Mar 2024 18:50:08 +0100 Subject: [PATCH 27/38] 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 28/38] 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 29/38] 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 30/38] 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 929749598de714a04f336a2b09f9c5adc7da7a03 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Mon, 1 Apr 2024 15:18:18 +0200 Subject: [PATCH 31/38] feat(graphics:sfml): add text rendering --- graphics/sfml/src/window/Renderer.cpp | 51 ++++++++++++++++++- graphics/sfml/src/window/Renderer.hpp | 47 +++++++++++++++-- graphics/sfml/src/window/Window.cpp | 9 ++++ graphics/sfml/src/window/Window.hpp | 6 +++ .../games/components/ICollidableComponent.hpp | 4 +- shared/games/components/IComponent.hpp | 3 +- .../components/IDisplayableComponent.hpp | 10 +--- .../games/components/IKeyboardComponent.hpp | 8 +-- ...mponent.hpp => IPositionableComponent.hpp} | 14 +++-- shared/games/components/ITextComponent.hpp | 6 +-- 10 files changed, 130 insertions(+), 28 deletions(-) rename shared/games/components/{IPositionComponent.hpp => IPositionableComponent.hpp} (51%) diff --git a/graphics/sfml/src/window/Renderer.cpp b/graphics/sfml/src/window/Renderer.cpp index 13abf5f..2e6d56a 100644 --- a/graphics/sfml/src/window/Renderer.cpp +++ b/graphics/sfml/src/window/Renderer.cpp @@ -21,6 +21,8 @@ Renderer::Renderer(Window &window): _window(window), _layer(_window.getInnerWind void Renderer::render(const shared::graphics::TextProps &props) { auto font = _castOrThrow(props.font); + auto entityPosition = _entityPixelsPosition(props.position); + auto entitySize = _window.tilesToPixels(props.size); _reset(_text); _text.setFont(font->getInnerFont()); @@ -32,11 +34,49 @@ void Renderer::render(const shared::graphics::TextProps &props) { props.color.b, props.color.a) ); + _text.setPosition(entityPosition.x, entityPosition.y); + _textAlign(props.align, entityPosition, entitySize); + _textVerticalAlign(props.verticalAlign, entityPosition, entitySize); + _textAdjustPosition(); _layer.draw(_text); } -void Renderer::render(unused const shared::graphics::TextureProps &props) { +void Renderer::_textVerticalAlign(const shared::graphics::TextVerticalAlign &align, const shared::types::Vector2f &entityPos, + const shared::types::Vector2i &entitySize) { + auto bounds = _text.getGlobalBounds(); + auto position = _text.getPosition(); + + if (align == shared::graphics::MIDDLE) { + position.y += (static_cast(entitySize.y) - bounds.height) / 2; + } else if (align == shared::graphics::BOTTOM) { + position.y += static_cast(entitySize.y) - bounds.height; + } + _text.setPosition(position); +} +void Renderer::_textAlign(const shared::graphics::TextAlign &align, const shared::types::Vector2f &entityPos, + const shared::types::Vector2i &entitySize) { + auto bounds = _text.getGlobalBounds(); + auto position = _text.getPosition(); + + if (align == shared::graphics::CENTER) { + position.x += (static_cast(entitySize.x) - bounds.width) / 2; + } else if (align == shared::graphics::RIGHT) { + position.x += static_cast(entitySize.x) - bounds.width; + } + _text.setPosition(position); +} + +void Renderer::_textAdjustPosition() { + auto actual = _text.getPosition(); + sf::FloatRect bounds = {0, 0, 0, 0 }; + + _text.setPosition(0, 0); + bounds = _text.getGlobalBounds(); + _text.setPosition(actual.x - bounds.left, actual.y - bounds.top); +} + +void Renderer::render(unused const shared::graphics::TextureProps &props) { } void Renderer::_reset(sf::Text &text) { @@ -57,6 +97,15 @@ void Renderer::_reset(sf::Sprite &sprite) { sprite.setOrigin(0, 0); } +Vector2f Renderer::_entityPixelsPosition(const Vector2i &position) { + auto pixels = _window.tilesToPixels(position); + + return { + static_cast(pixels.x), + static_cast(pixels.y) + }; +} + template std::shared_ptr Renderer::_castOrThrow(std::shared_ptr from) { std::shared_ptr to = std::dynamic_pointer_cast(from); diff --git a/graphics/sfml/src/window/Renderer.hpp b/graphics/sfml/src/window/Renderer.hpp index 822af17..7560b7a 100644 --- a/graphics/sfml/src/window/Renderer.hpp +++ b/graphics/sfml/src/window/Renderer.hpp @@ -20,6 +20,7 @@ namespace arcade::graphics::sfml::window { class arcade::graphics::sfml::window::Renderer { public: explicit Renderer(Window &window); + ~Renderer() = default; /** @@ -35,10 +36,10 @@ class arcade::graphics::sfml::window::Renderer { void render(const shared::graphics::TextProps &props); private: - Window &_window; - sf::RenderWindow &_layer; - sf::Text _text; - sf::Sprite _sprite; + Window &_window; + sf::RenderWindow &_layer; + sf::Text _text; + sf::Sprite _sprite; template static std::shared_ptr _castOrThrow(std::shared_ptr from); @@ -54,4 +55,40 @@ class arcade::graphics::sfml::window::Renderer { * @param sprite Sprite to reset */ static void _reset(sf::Sprite &sprite); -}; + + /** + * @brief Convert a tile position to pixel position + * @param position Tile position + * @return Pixel position + */ + Vector2f _entityPixelsPosition(const Vector2i &position); + + /** + * @brief Align vertically the text + * @param align Text alignment + * @param entityPos Entity position + * @param entitySize Entity size + */ + void _textVerticalAlign( + const shared::graphics::TextVerticalAlign &align, + const shared::types::Vector2f &entityPos, + const shared::types::Vector2i &entitySize + ); + + /** + * @brief Align the text + * @param align Text alignment + * @param entityPos Entity position + * @param entitySize Entity size + */ + void _textAlign( + const shared::graphics::TextAlign &align, + const shared::types::Vector2f &entityPos, + const shared::types::Vector2i &entitySize + ); + + /** + * @brief Adjust the text position + */ + void _textAdjustPosition(); +}; \ No newline at end of file diff --git a/graphics/sfml/src/window/Window.cpp b/graphics/sfml/src/window/Window.cpp index df0194f..2bef034 100644 --- a/graphics/sfml/src/window/Window.cpp +++ b/graphics/sfml/src/window/Window.cpp @@ -159,3 +159,12 @@ Vector2i Window::tilesToPixels(const Vector2i &position) const { }; } +Vector2i Window::tilesToPixels(const Vector2u &position) const { + auto realSize = _window.getSize(); + + return { + static_cast(position.x * realSize.x / _size.x), + static_cast(position.y * realSize.y / _size.y) + }; +} + diff --git a/graphics/sfml/src/window/Window.hpp b/graphics/sfml/src/window/Window.hpp index 93ad26c..5ff6fdd 100644 --- a/graphics/sfml/src/window/Window.hpp +++ b/graphics/sfml/src/window/Window.hpp @@ -145,6 +145,12 @@ class arcade::graphics::sfml::window::Window: public shared::graphics::IWindow { */ Vector2i tilesToPixels(const Vector2i &position) const; + /** + * @brief Convert a position in tiles to a position in pixels + * @return Converted position + */ + Vector2i tilesToPixels(const Vector2u &position) const; + /** * @brief Get the size of a tile * @return Size of a tile 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/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/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; }; 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; /** From 5f927f29fd2bddffc2cc1b3c7b0d5679ee597876 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Mon, 1 Apr 2024 15:27:03 +0200 Subject: [PATCH 32/38] refactor(graphics:sfml): update CMakeLists.txt of common exceptions --- graphics/common/exceptions/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphics/common/exceptions/CMakeLists.txt b/graphics/common/exceptions/CMakeLists.txt index ae1ba07..82dcf6d 100644 --- a/graphics/common/exceptions/CMakeLists.txt +++ b/graphics/common/exceptions/CMakeLists.txt @@ -5,4 +5,4 @@ target_sources(${PROJECT_NAME} PUBLIC TextureException.hpp WindowException.hpp FontException.hpp -) \ No newline at end of file +) From 050bdb5ecf9bc2462a651682f89071791a8c0f08 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Mon, 1 Apr 2024 16:32:11 +0200 Subject: [PATCH 33/38] feat(graphics:sfml): add texture rendering --- graphics/sfml/src/window/Renderer.cpp | 48 +++++++++++++++++++++------ graphics/sfml/src/window/Renderer.hpp | 15 +++++---- graphics/sfml/src/window/Window.cpp | 11 +++--- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/graphics/sfml/src/window/Renderer.cpp b/graphics/sfml/src/window/Renderer.cpp index 2e6d56a..e73693b 100644 --- a/graphics/sfml/src/window/Renderer.cpp +++ b/graphics/sfml/src/window/Renderer.cpp @@ -8,13 +8,13 @@ #include "Window.hpp" #include "Renderer.hpp" #include "font/Font.hpp" +#include "texture/Texture.hpp" #include "common/exceptions/WindowException.hpp" using namespace arcade::graphics::sfml::window; using namespace arcade::graphics::common::exceptions; -Renderer::Renderer(Window &window): _window(window), _layer(_window.getInnerWindow()) -{ +Renderer::Renderer(Window &window) : _window(window), _layer(_window.getInnerWindow()) { _text.setFont(sf::Font()); _sprite.setTexture(sf::Texture()); } @@ -35,14 +35,15 @@ void Renderer::render(const shared::graphics::TextProps &props) { props.color.a) ); _text.setPosition(entityPosition.x, entityPosition.y); - _textAlign(props.align, entityPosition, entitySize); - _textVerticalAlign(props.verticalAlign, entityPosition, entitySize); + _textAlign(props.align, entitySize); + _textVerticalAlign(props.verticalAlign, entitySize); _textAdjustPosition(); _layer.draw(_text); } -void Renderer::_textVerticalAlign(const shared::graphics::TextVerticalAlign &align, const shared::types::Vector2f &entityPos, - const shared::types::Vector2i &entitySize) { +void +Renderer::_textVerticalAlign(const shared::graphics::TextVerticalAlign &align, const shared::types::Vector2i &entitySize +) { auto bounds = _text.getGlobalBounds(); auto position = _text.getPosition(); @@ -54,8 +55,7 @@ void Renderer::_textVerticalAlign(const shared::graphics::TextVerticalAlign &ali _text.setPosition(position); } -void Renderer::_textAlign(const shared::graphics::TextAlign &align, const shared::types::Vector2f &entityPos, - const shared::types::Vector2i &entitySize) { +void Renderer::_textAlign(const shared::graphics::TextAlign &align, const shared::types::Vector2i &entitySize) { auto bounds = _text.getGlobalBounds(); auto position = _text.getPosition(); @@ -69,14 +69,42 @@ void Renderer::_textAlign(const shared::graphics::TextAlign &align, const shared void Renderer::_textAdjustPosition() { auto actual = _text.getPosition(); - sf::FloatRect bounds = {0, 0, 0, 0 }; + sf::FloatRect bounds; _text.setPosition(0, 0); bounds = _text.getGlobalBounds(); _text.setPosition(actual.x - bounds.left, actual.y - bounds.top); } -void Renderer::render(unused const shared::graphics::TextureProps &props) { +void Renderer::render(const shared::graphics::TextureProps &props) { + auto texture = _castOrThrow(props.texture); + auto entityPosition = _entityPixelsPosition(props.position); + + _reset(_sprite); + _sprite.setTexture(texture->getInnerTexture()); + _sprite.setPosition(entityPosition.x, entityPosition.y); + _setTextureRectAndScale(props); + _layer.draw(_sprite); +} + +void Renderer::_setTextureRectAndScale(const shared::graphics::TextureProps &props) { + auto size = _window.tilesToPixels(props.size); + float width = static_cast(props.size.x) * props.binTileSize.x; + float height = static_cast(props.size.y) * props.binTileSize.y; + float left = static_cast(props.origin.x) * props.binTileSize.x; + float top = static_cast(props.origin.y) * props.binTileSize.y; + sf::IntRect rectangle = { + static_cast(left), + static_cast(top), + static_cast(width), + static_cast(height) + }; + + _sprite.setTextureRect(rectangle); + _sprite.setScale( + static_cast(size.x) / width, + static_cast(size.y) / height + ); } void Renderer::_reset(sf::Text &text) { diff --git a/graphics/sfml/src/window/Renderer.hpp b/graphics/sfml/src/window/Renderer.hpp index 7560b7a..8f6c822 100644 --- a/graphics/sfml/src/window/Renderer.hpp +++ b/graphics/sfml/src/window/Renderer.hpp @@ -14,6 +14,7 @@ namespace arcade::graphics::sfml::window { class Renderer; + class Window; } @@ -71,24 +72,24 @@ class arcade::graphics::sfml::window::Renderer { */ void _textVerticalAlign( const shared::graphics::TextVerticalAlign &align, - const shared::types::Vector2f &entityPos, const shared::types::Vector2i &entitySize ); /** * @brief Align the text * @param align Text alignment - * @param entityPos Entity position * @param entitySize Entity size */ - void _textAlign( - const shared::graphics::TextAlign &align, - const shared::types::Vector2f &entityPos, - const shared::types::Vector2i &entitySize - ); + void _textAlign(const shared::graphics::TextAlign &align, const shared::types::Vector2i &entitySize); /** * @brief Adjust the text position */ void _textAdjustPosition(); + + /** + * @brief Set texture rect depending on the texture properties + * @param props Texture properties + */ + void _setTextureRectAndScale(const shared::graphics::TextureProps &props); }; \ No newline at end of file diff --git a/graphics/sfml/src/window/Window.cpp b/graphics/sfml/src/window/Window.cpp index 2bef034..c63269e 100644 --- a/graphics/sfml/src/window/Window.cpp +++ b/graphics/sfml/src/window/Window.cpp @@ -13,7 +13,7 @@ using namespace arcade::graphics::sfml::window; using namespace arcade::graphics::common::exceptions; -const Vector2u Window::tileSize = { 10, 10 }; +const Vector2u Window::tileSize = { 12, 12 }; Window::Window(const IWindow::WindowInitProps &props): _size(props.size), @@ -108,7 +108,7 @@ void Window::setIcon(const std::string &path) { } void Window::render(const shared::graphics::TextureProps &props) { - (void) props; + _renderer.render(props); } void Window::render(const shared::graphics::TextProps &props) { @@ -132,11 +132,12 @@ std::vector Window::getEvents() { } Vector2u Window::_getPixelSizeFromTiles(const Vector2u &size) { - Vector2u real(1920, 1080); + auto mode = sf::VideoMode::getDesktopMode(); + Vector2u real(mode.width, mode.height); - if (size.x * static_cast(tileSize.x) < 1920) + if (size.x * static_cast(tileSize.x) < mode.width) real.x = size.x * static_cast(tileSize.x); - if (size.y * static_cast(tileSize.y) < 1080) + if (size.y * static_cast(tileSize.y) < mode.height) real.y = size.y * static_cast(tileSize.y); return real; } From 168b3ba6545cd09dd3925c6c478e2dcabc90c90f Mon Sep 17 00:00:00 2001 From: Yann Date: Tue, 2 Apr 2024 08:11:44 +0200 Subject: [PATCH 34/38] fix(dlloader): remove useless references --- core/src/loader/Loader.cpp | 6 +++--- core/src/loader/Loader.hpp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/loader/Loader.cpp b/core/src/loader/Loader.cpp index 97bbd9d..04c7e33 100644 --- a/core/src/loader/Loader.cpp +++ b/core/src/loader/Loader.cpp @@ -13,14 +13,14 @@ Loader::Loader() {} Loader::~Loader() {} -shared::types::LibraryType Loader::_getLibraryGetter(const std::string &filepath, std::shared_ptr &dlLoader) { +shared::types::LibraryType Loader::_getLibraryGetter(const std::string &filepath, std::shared_ptr dlLoader) { shared::types::LibraryTypeGetter getter = nullptr; getter = dlLoader->loadSymbol(SHARED_STRINGIFY(SHARED_LIBRARY_TYPE_GETTER_NAME)); return getter(); } -void Loader::_loadGameLibrary(const std::string &filepath, std::shared_ptr &dlLoader) { +void Loader::_loadGameLibrary(const std::string &filepath, std::shared_ptr dlLoader) { shared::types::GameProviderGetter game = nullptr; game = dlLoader->loadSymbol(SHARED_STRINGIFY(SHARED_GAME_PROVIDER_GETTER_NAME)); @@ -28,7 +28,7 @@ void Loader::_loadGameLibrary(const std::string &filepath, std::shared_ptr_libraries.push_back(dlLoader); } -void Loader::_loadGraphicsLibrary(const std::string &filepath, std::shared_ptr &dlLoader) { +void Loader::_loadGraphicsLibrary(const std::string &filepath, std::shared_ptr dlLoader) { shared::types::GraphicsProviderGetter graphics = nullptr; graphics = dlLoader->loadSymbol(SHARED_STRINGIFY(SHARED_GRAPHICS_PROVIDER_GETTER_NAME)); diff --git a/core/src/loader/Loader.hpp b/core/src/loader/Loader.hpp index deed716..34a4459 100644 --- a/core/src/loader/Loader.hpp +++ b/core/src/loader/Loader.hpp @@ -60,21 +60,21 @@ class Loader { * @param loader DLLoader * @return getter function */ - shared::types::LibraryType _getLibraryGetter(const std::string &filepath, std::shared_ptr &loader); + shared::types::LibraryType _getLibraryGetter(const std::string &filepath, std::shared_ptr loader); /** * @brief Load a game library * @param filepath file path of the library * @param loader DLLoader */ - void _loadGameLibrary(const std::string &filepath, std::shared_ptr &loader); + void _loadGameLibrary(const std::string &filepath, std::shared_ptr loader); /** * @brief Load a graphics library * @param filepath file path of the library * @param loader DLLoader */ - void _loadGraphicsLibrary(const std::string &filepath, std::shared_ptr &loader); + void _loadGraphicsLibrary(const std::string &filepath, std::shared_ptr loader); /** * @brief Throw an error when loading a library From 835685ffa942bf768f50aaa7583cb95be72e04d8 Mon Sep 17 00:00:00 2001 From: Yann Date: Tue, 2 Apr 2024 10:16:24 +0200 Subject: [PATCH 35/38] 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 913ad316d56f1a2d2e32c3921c27ba3b7a7e20e7 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Tue, 2 Apr 2024 14:01:18 +0200 Subject: [PATCH 36/38] refactor(graphics:sfml): move templated method to header file as requested --- graphics/sfml/src/window/Renderer.cpp | 12 ---------- graphics/sfml/src/window/Renderer.hpp | 33 ++++++++++++++++++++------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/graphics/sfml/src/window/Renderer.cpp b/graphics/sfml/src/window/Renderer.cpp index e73693b..3ba9489 100644 --- a/graphics/sfml/src/window/Renderer.cpp +++ b/graphics/sfml/src/window/Renderer.cpp @@ -133,15 +133,3 @@ Vector2f Renderer::_entityPixelsPosition(const Vector2i &position) { static_cast(pixels.y) }; } - -template -std::shared_ptr Renderer::_castOrThrow(std::shared_ptr from) { - std::shared_ptr to = std::dynamic_pointer_cast(from); - if (!to) { - throw WindowException( - "Failed to cast shared pointer of:" + std::string(typeid(from).name()) + " to " + typeid(to).name(), - "SFML Library Renderer::_castOrThrow" - ); - } - return to; -} diff --git a/graphics/sfml/src/window/Renderer.hpp b/graphics/sfml/src/window/Renderer.hpp index 8f6c822..21c4998 100644 --- a/graphics/sfml/src/window/Renderer.hpp +++ b/graphics/sfml/src/window/Renderer.hpp @@ -11,6 +11,7 @@ #include "shared/graphics/ITexture.hpp" #include "shared/graphics/types/TextureProps.hpp" #include "shared/graphics/types/TextProps.hpp" +#include "common/exceptions/WindowException.hpp" namespace arcade::graphics::sfml::window { class Renderer; @@ -42,9 +43,6 @@ class arcade::graphics::sfml::window::Renderer { sf::Text _text; sf::Sprite _sprite; - template - static std::shared_ptr _castOrThrow(std::shared_ptr from); - /** * @brief Reset the text properties * @param text Text to reset @@ -76,10 +74,10 @@ class arcade::graphics::sfml::window::Renderer { ); /** - * @brief Align the text - * @param align Text alignment - * @param entitySize Entity size - */ + * @brief Align the text + * @param align Text alignment + * @param entitySize Entity size + */ void _textAlign(const shared::graphics::TextAlign &align, const shared::types::Vector2i &entitySize); /** @@ -92,4 +90,23 @@ class arcade::graphics::sfml::window::Renderer { * @param props Texture properties */ void _setTextureRectAndScale(const shared::graphics::TextureProps &props); -}; \ No newline at end of file + + /** + * @brief Cast a shared pointer from a type to another + * @tparam From Type from which to cast + * @tparam To Type to which cast + * @param from Value to cast + * @return Casted value + */ + template + static std::shared_ptr _castOrThrow(std::shared_ptr from) { + std::shared_ptr to = std::dynamic_pointer_cast(from); + if (!to) { + throw common::exceptions::WindowException( + "Failed to cast shared pointer of:" + std::string(typeid(from).name()) + " to " + typeid(to).name(), + "SFML Library Renderer::_castOrThrow" + ); + } + return to; + }; +}; From accd757aeccee582aca06e02b6df5a5a34fbe2ec Mon Sep 17 00:00:00 2001 From: Yann Date: Tue, 2 Apr 2024 15:45:22 +0200 Subject: [PATCH 37/38] 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 38/38] 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 )