From 492a09c1cd8a20a14a7d77ff4d37bed95408cfbb Mon Sep 17 00:00:00 2001 From: Matheo Coquet Date: Mon, 25 Mar 2024 18:57:18 +0100 Subject: [PATCH 01/24] 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/24] 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 bf17c89dc2b4bc20fd7b1b7ccc49c2319dd0751c Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Tue, 26 Mar 2024 22:39:14 +0100 Subject: [PATCH 03/24] 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 3aedf86d078c1baa23bc2d1c714cb65b55c1c144 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Wed, 27 Mar 2024 19:53:13 +0100 Subject: [PATCH 04/24] 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 05/24] 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 06/24] 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 07/24] 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 08/24] 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 09/24] 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 10/24] 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 11/24] 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 12/24] 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 13/24] 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 14/24] 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 15/24] 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 16/24] 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 17/24] 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 18/24] 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 19/24] 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 20/24] 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 929749598de714a04f336a2b09f9c5adc7da7a03 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Mon, 1 Apr 2024 15:18:18 +0200 Subject: [PATCH 21/24] 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 22/24] 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 23/24] 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 913ad316d56f1a2d2e32c3921c27ba3b7a7e20e7 Mon Sep 17 00:00:00 2001 From: Flavien Chenu Date: Tue, 2 Apr 2024 14:01:18 +0200 Subject: [PATCH 24/24] 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; + }; +};