From f31715eb38932170135dbfe1a79cb5de083265f8 Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Thu, 28 Sep 2023 22:04:59 +0200 Subject: [PATCH 01/15] ECS: Fix, reable systems to auto delete + refactor systemManager + refactor Registry custom sparseArrays MINOR --- src/Client/SceneManager.cpp | 35 ++++-------- src/Client/SceneManager.hpp | 13 +++-- src/Client/Systems/ClientSystems.cpp | 2 +- src/Client/Systems/ClientSystems.hpp | 2 +- src/Client/Systems/Events/EventsSystems.cpp | 23 ++++++-- src/Client/Systems/Events/EventsSystems.hpp | 6 ++- src/Client/Systems/Graphic/GraphicSystems.cpp | 19 ++++--- src/Client/Systems/Graphic/GraphicSystems.hpp | 13 ++--- src/ECS/Registry.cpp | 29 ++++++++-- src/ECS/Registry.hpp | 48 ++++++++++------- src/ECS/Systems/Managers/SystemManager.cpp | 53 ++++++++++++++++--- src/ECS/Systems/Managers/SystemManager.hpp | 24 +++++++-- .../Managers/SystemManagersDirector.cpp | 11 +++- .../Managers/SystemManagersDirector.hpp | 7 +-- src/ECS/Systems/Systems.cpp | 32 +++++++++-- src/ECS/Systems/Systems.hpp | 5 +- 16 files changed, 230 insertions(+), 92 deletions(-) diff --git a/src/Client/SceneManager.cpp b/src/Client/SceneManager.cpp index 47aa8647..f0c9b189 100644 --- a/src/Client/SceneManager.cpp +++ b/src/Client/SceneManager.cpp @@ -6,21 +6,16 @@ */ #include "SceneManager.hpp" +#include #include "raylib.h" #include "ClientSystems.hpp" #include "Registry.hpp" #include "SystemManagersDirector.hpp" -// to suppr -#include "CustomTypes.hpp" - constexpr int screenWidth = 1920; constexpr int screenHeight = 1080; constexpr int fps = 60; -// to suppr -constexpr int playerData = 10; - // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) bool SceneManager::_init = false; SceneManager SceneManager::_instance = SceneManager(); @@ -31,22 +26,6 @@ static void initRaylib() InitWindow(screenWidth, screenHeight, "R-Bus"); SetTargetFPS(fps); InitAudioDevice(); - Registry::getInstance().addEntity(); - Registry::getInstance().getComponents().back() = { - playerData, - playerData}; - Registry::getInstance().getComponents().back() = { - playerData, - playerData}; - Registry::getInstance().getComponents().back() = { - playerData, - playerData}; - SparseArray &playerId = - Registry::getInstance().getCustomSparseArray( - CustomIndex::PLAYER); - playerId.add(); - playerId.back() = - std::optional(Registry::getInstance().getEntitiesNb() - 1); } static void initSystemManagers() @@ -68,7 +47,7 @@ SceneManager &SceneManager::getInstance() return _instance; } -SceneManager::SceneManager() : _currentScene(Scene::MAIN_GAME), _stop(false) +SceneManager::SceneManager() : _currentScene(Scene::MENU), _stop(false) { } @@ -83,6 +62,8 @@ int SceneManager::run() auto &director = Systems::SystemManagersDirector::getInstance(); try { + Registry::getInstance().initCustomSparseArrays( + _scenesCustomIndexes.at(_currentScene)); while (!_stop && !WindowShouldClose()) { BeginDrawing(); ClearBackground(RAYWHITE); @@ -103,7 +84,13 @@ int SceneManager::run() void SceneManager::changeScene(Scene scene) { _currentScene = scene; - Registry::getInstance().clear(); + Registry::getInstance().clear(_scenesCustomIndexes.at(_currentScene)); + Systems::SystemManagersDirector::getInstance().resetChanges(); +} + +Scene SceneManager::getCurrentScene() +{ + return _currentScene; } void SceneManager::stop() diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index 44a6978a..ec7e244a 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -13,17 +13,20 @@ #include #include +enum CustomIndex { BULLET, PLAYER, ENNEMY }; + enum ReturnValue { OK = 0, ERROR = 84 }; -enum Scene { MAIN_GAME, MENU, SCENE_MAX }; +enum Scene { MENU, MAIN_GAME, SCENE_MAX }; -enum SystemManagers { GAME, EVENTS, DISPLAY, MANAGER_MAX }; +enum SystemManagers { GAME, EVENTS, DISPLAY }; class SceneManager { public: static SceneManager &getInstance(); int run(); void changeScene(Scene scene); + Scene getCurrentScene(); void stop(); private: @@ -33,7 +36,11 @@ class SceneManager { bool _stop; const std::array, 2> _scenes = { std::vector {EVENTS, GAME, DISPLAY}, - std::vector {EVENTS, DISPLAY } + std::vector {EVENTS, GAME, DISPLAY} + }; + const std::array, 2> _scenesCustomIndexes = { + std::vector {PLAYER}, + std::vector {PLAYER, BULLET, ENNEMY} }; // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) diff --git a/src/Client/Systems/ClientSystems.cpp b/src/Client/Systems/ClientSystems.cpp index 21e89c3b..115584f4 100644 --- a/src/Client/Systems/ClientSystems.cpp +++ b/src/Client/Systems/ClientSystems.cpp @@ -8,7 +8,7 @@ #include "ClientSystems.hpp" namespace Systems { - std::array>, 3> + std::array>, 3> getSystemsGroups() { return { diff --git a/src/Client/Systems/ClientSystems.hpp b/src/Client/Systems/ClientSystems.hpp index dd79b9e0..aeca2770 100644 --- a/src/Client/Systems/ClientSystems.hpp +++ b/src/Client/Systems/ClientSystems.hpp @@ -13,6 +13,6 @@ #include "Systems.hpp" namespace Systems { - std::array>, 3> + std::array>, 3> getSystemsGroups(); } // namespace Systems diff --git a/src/Client/Systems/Events/EventsSystems.cpp b/src/Client/Systems/Events/EventsSystems.cpp index 481f4a2d..56a3727f 100644 --- a/src/Client/Systems/Events/EventsSystems.cpp +++ b/src/Client/Systems/Events/EventsSystems.cpp @@ -8,9 +8,12 @@ #include "EventsSystems.hpp" #include "CustomTypes.hpp" #include "Registry.hpp" +#include "SceneManager.hpp" namespace Systems { - void EventsSystems::playerMovement(std::size_t /*unused*/) + void EventsSystems::playerMovement( + std::size_t /*unused*/, + std::size_t /*unused*/) { Registry::components arrPosition = Registry::getInstance().getComponents(); @@ -36,9 +39,23 @@ namespace Systems { } } } - std::vector> + + void + EventsSystems::changeScene(std::size_t /*unused*/, std::size_t /*unused*/) + { + if (IsKeyDown(KEY_J)) { + SceneManager &sceneManager = SceneManager::getInstance(); + if (sceneManager.getCurrentScene() == MAIN_GAME) { + sceneManager.changeScene(MENU); + } else { + sceneManager.changeScene(MAIN_GAME); + } + } + } + + std::vector> EventsSystems::getEventSystems() { - return {playerMovement}; + return {playerMovement, changeScene}; } } // namespace Systems diff --git a/src/Client/Systems/Events/EventsSystems.hpp b/src/Client/Systems/Events/EventsSystems.hpp index 457731ac..fd2edf2f 100644 --- a/src/Client/Systems/Events/EventsSystems.hpp +++ b/src/Client/Systems/Events/EventsSystems.hpp @@ -13,7 +13,9 @@ namespace Systems { namespace EventsSystems { - void playerMovement(std::size_t /*unused*/); - std::vector> getEventSystems(); + void playerMovement(std::size_t /*unused*/, std::size_t /*unused*/); + void changeScene(std::size_t /*unused*/, std::size_t /*unused*/); + std::vector> + getEventSystems(); } // namespace EventsSystems } // namespace Systems diff --git a/src/Client/Systems/Graphic/GraphicSystems.cpp b/src/Client/Systems/Graphic/GraphicSystems.cpp index dabc83ee..86e3a594 100644 --- a/src/Client/Systems/Graphic/GraphicSystems.cpp +++ b/src/Client/Systems/Graphic/GraphicSystems.cpp @@ -11,7 +11,8 @@ #include "Registry.hpp" namespace Systems { - void GraphicSystems::rectRenderer(std::size_t /*unused*/) + void + GraphicSystems::rectRenderer(std::size_t /*unused*/, std::size_t /*unused*/) { Registry::components arrPosition = Registry::getInstance().getComponents(); @@ -102,7 +103,9 @@ namespace Systems { tint); } - void GraphicSystems::spriteRenderer(std::size_t /*unused*/) + void GraphicSystems::spriteRenderer( + std::size_t /*unused*/, + std::size_t /*unused*/) { Registry::components arrSprite = Registry::getInstance().getComponents(); @@ -131,7 +134,9 @@ namespace Systems { } } - void GraphicSystems::soundEffectPlayer(std::size_t /*unused*/) + void GraphicSystems::soundEffectPlayer( + std::size_t /*unused*/, + std::size_t /*unused*/) { Registry::components arrSoundEffect = Registry::getInstance().getComponents(); @@ -147,7 +152,8 @@ namespace Systems { } } - void GraphicSystems::musicPlayer(std::size_t /*unused*/) + void + GraphicSystems::musicPlayer(std::size_t /*unused*/, std::size_t /*unused*/) { Registry::components arrMusics = Registry::getInstance().getComponents(); @@ -188,7 +194,8 @@ namespace Systems { text.color); } - void GraphicSystems::textRenderer(std::size_t /*unused*/) + void + GraphicSystems::textRenderer(std::size_t /*unused*/, std::size_t /*unused*/) { Registry::components arrText = Registry::getInstance().getComponents(); @@ -206,7 +213,7 @@ namespace Systems { textIt++; } } - std::vector> + std::vector> GraphicSystems::getGraphicsSystems() { return { diff --git a/src/Client/Systems/Graphic/GraphicSystems.hpp b/src/Client/Systems/Graphic/GraphicSystems.hpp index 3de515c8..da5f39e0 100644 --- a/src/Client/Systems/Graphic/GraphicSystems.hpp +++ b/src/Client/Systems/Graphic/GraphicSystems.hpp @@ -13,11 +13,12 @@ namespace Systems { namespace GraphicSystems { - void rectRenderer(std::size_t /*unused*/); - void spriteRenderer(std::size_t /*unused*/); - void textRenderer(std::size_t /*unused*/); - void soundEffectPlayer(std::size_t /*unused*/); - void musicPlayer(std::size_t /*unused*/); - std::vector> getGraphicsSystems(); + void rectRenderer(std::size_t /*unused*/, std::size_t /*unused*/); + void spriteRenderer(std::size_t /*unused*/, std::size_t /*unused*/); + void textRenderer(std::size_t /*unused*/, std::size_t /*unused*/); + void soundEffectPlayer(std::size_t /*unused*/, std::size_t /*unused*/); + void musicPlayer(std::size_t /*unused*/, std::size_t /*unused*/); + std::vector> + getGraphicsSystems(); } // namespace GraphicSystems } // namespace Systems diff --git a/src/ECS/Registry.cpp b/src/ECS/Registry.cpp index 6c381858..6eb34ba5 100644 --- a/src/ECS/Registry.cpp +++ b/src/ECS/Registry.cpp @@ -6,6 +6,7 @@ */ #include "Registry.hpp" +#include // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) Registry Registry::_instance = Registry(); @@ -31,22 +32,40 @@ void Registry::removeEntity(std::size_t id) } } -void Registry::clear() +void Registry::clear(std::vector indexes) { _data.clear(); _addComponentPlaceFunctions.clear(); _removeComponentFunctions.clear(); _entitiesNb = 0; + _customSparseArrays.clear(); + initCustomSparseArrays(indexes); } -std::size_t Registry::getEntitiesNb() +std::size_t Registry::getEntitiesNb() const { return (_entitiesNb); } -Registry::Registry() : _entitiesNb(0) +void Registry::initCustomSparseArrays(std::vector indexes) { - for (std::size_t i = 0; i < MAX; i++) { - addCustomSparseArray(); + for (auto index : indexes) { + addCustomSparseIndex(static_cast(index)); + } +} + +Registry::Registry() : _entitiesNb(0), _maxCustomId(0) +{ +} + +void Registry::addCustomSparseIndex(std::size_t id) +{ + if (_customSparseArrays.find(id) != _customSparseArrays.end()) { + throw std::runtime_error( + "addCustomSparseIndex: id" + std::to_string(id) + " already exist"); + }; + _customSparseArrays[id] = SparseArray(); + if (id > _maxCustomId) { + _maxCustomId = id; } } diff --git a/src/ECS/Registry.hpp b/src/ECS/Registry.hpp index e5310062..6786e7c2 100644 --- a/src/ECS/Registry.hpp +++ b/src/ECS/Registry.hpp @@ -9,18 +9,17 @@ #include #include -#include #include #include +#include #include #include #include #include #include "raylib.h" +#include "SceneManager.hpp" #include "SparseArray.hpp" -enum CustomIndex { PLAYER, BULLET, ENNEMY, MAX }; - class Registry { public: template @@ -39,13 +38,14 @@ class Registry { void removeEntity(std::size_t /*id*/); - void clear(); + void clear(std::vector); template components getCustomSparseArray(std::size_t id) { - if (id > _customSparseArrays.size()) { - throw std::runtime_error("ID not in "); + if (_customSparseArrays.find(id) == _customSparseArrays.end()) { + throw std::runtime_error( + "getCustomSparseArray ID not in :" + std::to_string(id)); } try { components castedComponent = @@ -53,12 +53,26 @@ class Registry { _customSparseArrays[id]); return castedComponent; - } catch (const std::bad_any_cast &e) { - throw std::runtime_error("Bad any cast"); + } catch (const std::bad_any_cast e) { + throw std::runtime_error( + "getCustomSparseArray: Bad any cast. ID: " + + std::to_string(id)); } } - std::size_t getEntitiesNb(); + template + std::size_t addCustomSparseArray() + { + _maxCustomId++; + std::size_t id = _maxCustomId; + + _customSparseArrays[id] = SparseArray(); + return (id); + } + + std::size_t getEntitiesNb() const; + + void initCustomSparseArrays(std::vector indexes); Registry &operator=(const Registry &) = delete; Registry(const Registry &) = delete; @@ -68,15 +82,6 @@ class Registry { private: Registry(); - template - std::size_t addCustomSparseArray() - { - std::size_t id = _customSparseArrays.size(); - - _customSparseArrays.push_back(SparseArray()); - return (id); - } - template void checkAddSparseArray() { @@ -112,6 +117,8 @@ class Registry { _data[typeid(Component)]); } + void addCustomSparseIndex(std::size_t id); + // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) static Registry _instance; // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) @@ -121,6 +128,9 @@ class Registry { std::vector> _removeComponentFunctions; std::unordered_map _data; - std::vector _customSparseArrays; + + std::unordered_map _customSparseArrays; + std::size_t _maxCustomId; + std::size_t _entitiesNb; }; diff --git a/src/ECS/Systems/Managers/SystemManager.cpp b/src/ECS/Systems/Managers/SystemManager.cpp index df21c3b4..7c12f9b1 100644 --- a/src/ECS/Systems/Managers/SystemManager.cpp +++ b/src/ECS/Systems/Managers/SystemManager.cpp @@ -8,31 +8,68 @@ #include "SystemManager.hpp" namespace Systems { + + // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) + std::size_t SystemManager::_managerNb = 0; + // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) + + SystemManager::SystemManager() : _id(_managerNb), _modified(false) + { + _managerNb += 1; + } + SystemManager::SystemManager( - std::vector> systems) - : _systems(std::move(systems)) + std::vector> systems) + : _id(_managerNb), + _originalSystems(std::move(systems)), + _modified(false) { + _managerNb += 1; } void SystemManager::updateSystems() { std::size_t i = 0; - for (auto &system : _systems) { - system(i); + for (auto &system : getSystems()) { + system(_id, i); i++; } } - void SystemManager::addSystem(std::function sys) + void + SystemManager::addSystem(std::function sys) { - _systems.push_back(sys); + setModifiedSystems(); + _modifiedSystems.push_back(sys); } void SystemManager::removeSystem(std::size_t id) { - auto it = _systems.begin(); + setModifiedSystems(); + auto it = _modifiedSystems.begin(); std::advance(it, id); - _systems.erase(it); + _modifiedSystems.erase(it); + } + + void SystemManager::resetChanges() + { + _modified = false; + _modifiedSystems.clear(); + } + + std::vector> & + SystemManager::getSystems() + { + if (_modified) { + return _modifiedSystems; + } + return _originalSystems; + } + + void SystemManager::setModifiedSystems() + { + _modified = true; + _modifiedSystems = _originalSystems; } } // namespace Systems diff --git a/src/ECS/Systems/Managers/SystemManager.hpp b/src/ECS/Systems/Managers/SystemManager.hpp index 76cc3cd0..796b903c 100644 --- a/src/ECS/Systems/Managers/SystemManager.hpp +++ b/src/ECS/Systems/Managers/SystemManager.hpp @@ -13,14 +13,30 @@ namespace Systems { class SystemManager { public: - SystemManager() = default; + SystemManager(); SystemManager( - std::vector> systems); + std::vector> + systems); void updateSystems(); - void addSystem(std::function /*sys*/); + void addSystem( + std::function /*sys*/); void removeSystem(std::size_t /*id*/); + void resetChanges(); private: - std::vector> _systems; + std::vector> & + getSystems(); + void setModifiedSystems(); + + // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) + static std::size_t _managerNb; + // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) + + std::size_t _id; + std::vector> + _originalSystems; + std::vector> + _modifiedSystems; + bool _modified; }; } // namespace Systems diff --git a/src/ECS/Systems/Managers/SystemManagersDirector.cpp b/src/ECS/Systems/Managers/SystemManagersDirector.cpp index 13a20d57..d16fad01 100644 --- a/src/ECS/Systems/Managers/SystemManagersDirector.cpp +++ b/src/ECS/Systems/Managers/SystemManagersDirector.cpp @@ -32,16 +32,23 @@ namespace Systems { } std::size_t SystemManagersDirector::addSystemManager( - std::vector> systems) + std::vector> systems) { _systemManagers.emplace_back(systems); return _systemManagers.size() - 1; } - void SystemManagersDirector::removeSystem(std::size_t id) + void SystemManagersDirector::removeSystemManager(std::size_t id) { auto it = _systemManagers.begin(); std::advance(it, id); _systemManagers.erase(it); } + + void SystemManagersDirector::resetChanges() + { + for (auto &manager : _systemManagers) { + manager.resetChanges(); + } + } } // namespace Systems diff --git a/src/ECS/Systems/Managers/SystemManagersDirector.hpp b/src/ECS/Systems/Managers/SystemManagersDirector.hpp index 1cb714e4..bf521b5a 100644 --- a/src/ECS/Systems/Managers/SystemManagersDirector.hpp +++ b/src/ECS/Systems/Managers/SystemManagersDirector.hpp @@ -16,9 +16,10 @@ namespace Systems { static SystemManagersDirector &getInstance(); SystemManager &getSystemManager(std::size_t); std::size_t addSystemManager(); - std::size_t - addSystemManager(std::vector>); - void removeSystem(std::size_t); + std::size_t addSystemManager( + std::vector>); + void removeSystemManager(std::size_t); + void resetChanges(); private: std::vector _systemManagers; diff --git a/src/ECS/Systems/Systems.cpp b/src/ECS/Systems/Systems.cpp index 77898526..af94820c 100644 --- a/src/ECS/Systems/Systems.cpp +++ b/src/ECS/Systems/Systems.cpp @@ -10,8 +10,10 @@ #include "CustomTypes.hpp" #include "Registry.hpp" +#include "SystemManagersDirector.hpp" + namespace Systems { - void windowCollision(std::size_t /*unused*/) + void windowCollision(std::size_t /*unused*/, std::size_t /*unused*/) { Registry::components arrPosition = Registry::getInstance().getComponents(); @@ -48,8 +50,32 @@ namespace Systems { } } - std::vector> getECSSystems() + constexpr int playerData = 10; + + void initPlayer(std::size_t managerId, std::size_t systemId) + { + Registry::getInstance().addEntity(); + Registry::getInstance().getComponents().back() = { + playerData, + playerData}; + Registry::getInstance().getComponents().back() = + {playerData, playerData}; + Registry::getInstance().getComponents().back() = { + playerData, + playerData}; + SparseArray &playerId = + Registry::getInstance().getCustomSparseArray( + CustomIndex::PLAYER); + playerId.add(); + playerId.back() = std::optional( + Registry::getInstance().getEntitiesNb() - 1); + SystemManagersDirector::getInstance() + .getSystemManager(managerId) + .removeSystem(systemId); + } + + std::vector> getECSSystems() { - return {windowCollision}; + return {windowCollision, initPlayer}; } } // namespace Systems diff --git a/src/ECS/Systems/Systems.hpp b/src/ECS/Systems/Systems.hpp index 07b8ff5c..33764ef5 100644 --- a/src/ECS/Systems/Systems.hpp +++ b/src/ECS/Systems/Systems.hpp @@ -12,6 +12,7 @@ #include namespace Systems { - void windowCollision(std::size_t); - std::vector> getECSSystems(); + void windowCollision(std::size_t, std::size_t); + void initPlayer(std::size_t, std::size_t); + std::vector> getECSSystems(); } // namespace Systems From db6865910c13c246a02ec8990dcba1a6c2efb9fe Mon Sep 17 00:00:00 2001 From: Github Actions Date: Thu, 28 Sep 2023 20:05:37 +0000 Subject: [PATCH 02/15] FORMAT-AUTO: automatic format on pull request #31 --- src/Client/SceneManager.hpp | 4 ++-- src/ECS/Systems/Managers/SystemManager.hpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index ec7e244a..727d91de 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -39,8 +39,8 @@ class SceneManager { std::vector {EVENTS, GAME, DISPLAY} }; const std::array, 2> _scenesCustomIndexes = { - std::vector {PLAYER}, - std::vector {PLAYER, BULLET, ENNEMY} + std::vector {PLAYER }, + std::vector { PLAYER, BULLET, ENNEMY} }; // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) diff --git a/src/ECS/Systems/Managers/SystemManager.hpp b/src/ECS/Systems/Managers/SystemManager.hpp index 796b903c..5cbc0969 100644 --- a/src/ECS/Systems/Managers/SystemManager.hpp +++ b/src/ECS/Systems/Managers/SystemManager.hpp @@ -24,7 +24,7 @@ namespace Systems { void resetChanges(); private: - std::vector> & + std::vector>& getSystems(); void setModifiedSystems(); From bc78ba89c1720ec1f6caf2fcc0ffb7052aa283b1 Mon Sep 17 00:00:00 2001 From: Github Actions Date: Thu, 28 Sep 2023 21:48:01 +0000 Subject: [PATCH 03/15] FORMAT-AUTO: automatic format on pull request #31 --- src/Client/SceneManager.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index ec7e244a..727d91de 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -39,8 +39,8 @@ class SceneManager { std::vector {EVENTS, GAME, DISPLAY} }; const std::array, 2> _scenesCustomIndexes = { - std::vector {PLAYER}, - std::vector {PLAYER, BULLET, ENNEMY} + std::vector {PLAYER }, + std::vector { PLAYER, BULLET, ENNEMY} }; // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) From daf2b9b1b034695609c5ff6c0b768f06390897c1 Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Thu, 28 Sep 2023 23:52:21 +0200 Subject: [PATCH 04/15] ECS: Fix method to const PATCH --- src/Client/SceneManager.cpp | 2 +- src/Client/SceneManager.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Client/SceneManager.cpp b/src/Client/SceneManager.cpp index f65fe36f..9e73f6ec 100644 --- a/src/Client/SceneManager.cpp +++ b/src/Client/SceneManager.cpp @@ -91,7 +91,7 @@ void SceneManager::changeScene(Scene scene) Systems::SystemManagersDirector::getInstance().resetChanges(); } -Scene SceneManager::getCurrentScene() +Scene SceneManager::getCurrentScene() const { return _currentScene; } diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index ec7e244a..226cf8bd 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -26,7 +26,7 @@ class SceneManager { static SceneManager &getInstance(); int run(); void changeScene(Scene scene); - Scene getCurrentScene(); + Scene getCurrentScene() const; void stop(); private: From 6e74e5a8e12ed5fa5600b86ce69a03ff62dcd944 Mon Sep 17 00:00:00 2001 From: guillaume abel <91884836+guillaumeAbel@users.noreply.github.com> Date: Fri, 29 Sep 2023 22:02:11 +0200 Subject: [PATCH 05/15] Update src/Client/Systems/Events/EventsSystems.cpp --- src/Client/Systems/Events/EventsSystems.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client/Systems/Events/EventsSystems.cpp b/src/Client/Systems/Events/EventsSystems.cpp index 84514e1d..cabf007b 100644 --- a/src/Client/Systems/Events/EventsSystems.cpp +++ b/src/Client/Systems/Events/EventsSystems.cpp @@ -44,7 +44,7 @@ namespace Systems { void EventsSystems::changeScene(std::size_t /*unused*/, std::size_t /*unused*/) { - if (IsKeyDown(KEY_J)) { + if (IsKeyDown(Raylib::KEY_J)) { SceneManager &sceneManager = SceneManager::getInstance(); if (sceneManager.getCurrentScene() == MAIN_GAME) { sceneManager.changeScene(MENU); From 1d681d03f959c4b5029fe2a5350fd861d64d66e0 Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Fri, 29 Sep 2023 22:41:33 +0200 Subject: [PATCH 06/15] ECS: Fix remaining call to raylib outside encapsulation PATCH --- src/Client/Systems/Events/EventsSystems.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client/Systems/Events/EventsSystems.cpp b/src/Client/Systems/Events/EventsSystems.cpp index cabf007b..75081b15 100644 --- a/src/Client/Systems/Events/EventsSystems.cpp +++ b/src/Client/Systems/Events/EventsSystems.cpp @@ -44,7 +44,7 @@ namespace Systems { void EventsSystems::changeScene(std::size_t /*unused*/, std::size_t /*unused*/) { - if (IsKeyDown(Raylib::KEY_J)) { + if (Raylib::IsKeyDown(Raylib::KeyboardKey::KB_J)) { SceneManager &sceneManager = SceneManager::getInstance(); if (sceneManager.getCurrentScene() == MAIN_GAME) { sceneManager.changeScene(MENU); From d718a20d6c2cec9c7c5df6fdad5140bc0d22687c Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Fri, 29 Sep 2023 22:47:48 +0200 Subject: [PATCH 07/15] ECS: Fix Raylib call PATCH --- src/Client/Systems/Events/EventsSystems.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client/Systems/Events/EventsSystems.cpp b/src/Client/Systems/Events/EventsSystems.cpp index 75081b15..c663d127 100644 --- a/src/Client/Systems/Events/EventsSystems.cpp +++ b/src/Client/Systems/Events/EventsSystems.cpp @@ -44,7 +44,7 @@ namespace Systems { void EventsSystems::changeScene(std::size_t /*unused*/, std::size_t /*unused*/) { - if (Raylib::IsKeyDown(Raylib::KeyboardKey::KB_J)) { + if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_J)) { SceneManager &sceneManager = SceneManager::getInstance(); if (sceneManager.getCurrentScene() == MAIN_GAME) { sceneManager.changeScene(MENU); From b7ac899730d5f5d3e538fdfd30ad6ca022e332f2 Mon Sep 17 00:00:00 2001 From: Xavier Mitault Date: Sat, 30 Sep 2023 00:16:05 +0200 Subject: [PATCH 08/15] DOCUMENTAION: fix typo missing @ PATCH Co-authored-by: TTENSHII <87119012+TTENSHII@users.noreply.github.com> --- docs/contributors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributors.md b/docs/contributors.md index 8541624d..7dbcf649 100644 --- a/docs/contributors.md +++ b/docs/contributors.md @@ -5,5 +5,5 @@ Thanks to this amazing contributors that made this project possible! - [@Saverio976](https://github.com/Saverio976) - [@TTENSHII](https://github.com/TTENSHII) - [@guillaumeAbel](https://github.com/guillaumeAbel) -- [KitetsuK](https://github.com/KitetsuK) +- [@KitetsuK](https://github.com/KitetsuK) - [@romainpanno](https://github.com/romainpanno) From 0c1f89ab0bc542445266926db134bd89a23e1cd4 Mon Sep 17 00:00:00 2001 From: Xavier Mitault Date: Sat, 30 Sep 2023 00:31:36 +0200 Subject: [PATCH 09/15] CICD: Fix source pack creation PATCH --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index e0b76374..b3bfae13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -180,6 +180,7 @@ include(InstallRequiredSystemLibraries) set(CPACK_PACKAGE_NAME "R-Type") set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CMAKE_SYSTEM_NAME}") +set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CMAKE_SYSTEM_NAME}") set(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}") set(CPACK_PACKAGE_VERSION_MINOR "${PROJECT_VERSION_MINOR}") set(CPACK_PACKAGE_VERSION_PATCH "${PROJECT_VERSION_PATCH}") From 443988aa008bdffed3b0482e778c520b3df71d27 Mon Sep 17 00:00:00 2001 From: Xavier Mitault Date: Sat, 30 Sep 2023 01:04:18 +0200 Subject: [PATCH 10/15] CICD: Fix comments from romainpanno --- .gitignore | 2 ++ CMakeLists.txt | 1 + deps/boost/CMakeLists.txt | 5 ----- scripts/bundle-windows.ps1 | 2 +- scripts/format.ps1 | 14 -------------- scripts/format.sh | 6 ------ 6 files changed, 4 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 1ec8a51a..6f20a46f 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,5 @@ r-type-windows.zip r-type-macos.zip *~ + +vgcore* diff --git a/CMakeLists.txt b/CMakeLists.txt index b3bfae13..d1b43b6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ set(PROJECT_NAME r-type) if(NOT DEFINED CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() +SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING "" FORCE) option(BUILD_SHARED_LIBS "Build using shared libraries" OFF) option(WITH_PIC "Build with position independent code" ON) diff --git a/deps/boost/CMakeLists.txt b/deps/boost/CMakeLists.txt index 303ef765..5dd075f5 100644 --- a/deps/boost/CMakeLists.txt +++ b/deps/boost/CMakeLists.txt @@ -69,9 +69,4 @@ foreach(lib ${BOOST_INCLUDE_LIBRARIES}) PRIVATE Boost::${lib} ) -# if(WIN32) -# install(FILES Boost::${lib} DESTINATION ${CMAKE_INSTALL_BINDIR}) -# else() -# install(FILES Boost::${lib} DESTINATION ${CMAKE_INSTALL_LIBDIR}) -# endif() endforeach() diff --git a/scripts/bundle-windows.ps1 b/scripts/bundle-windows.ps1 index e947bd67..4f764a40 100644 --- a/scripts/bundle-windows.ps1 +++ b/scripts/bundle-windows.ps1 @@ -5,7 +5,7 @@ $ErrorActionPreference = "SilentlyContinue" ./scripts/compil.ps1 -cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CONFIGURATION_TYPES="Release;Release" -DCONFIG=Release +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CONFIGURATION_TYPES="Release" cmake --build build diff --git a/scripts/format.ps1 b/scripts/format.ps1 index 7cd06436..64ec520f 100644 --- a/scripts/format.ps1 +++ b/scripts/format.ps1 @@ -10,17 +10,3 @@ if ($args.Count -gt 0 -and $args[0] -eq '--dry-run') { $files = Get-Childitem -Path src -Include *.cpp,*.hpp -Recurse -ErrorAction SilentlyContinue clang-format --style=file $argClangFormat -i $files - -# $found = Select-String -Path $files -Pattern '"raylib.h"' -CaseSensitive -# -# if ($found) { -# echo $found -# exit 1 -# } -# -# $found = Select-String -Path $files -Pattern '"raylib.hpp"' -CaseSensitive -# -# if ($found) { -# echo $found -# exit 1 -# } diff --git a/scripts/format.sh b/scripts/format.sh index e0d3755f..10c2d53c 100755 --- a/scripts/format.sh +++ b/scripts/format.sh @@ -10,9 +10,3 @@ if [ "$1" == "--dry-run" ]; then fi find src/ \( -name '*.cpp' -o -name '*.hpp' \) -print0 | xargs -0 clang-format --style=file $ARGS -i - -# found=$(find src/ \( -name '*.cpp' -o -name '*.hpp' \) -print0 | xargs -0 grep -e '"raylib.h"' -e '"raylib.hpp"') -# if [ -n "$found" ]; then -# echo "$found" -# exit 1 -# fi From 057f3d46ccc9659c8a21a5c3631ca8cf2068ec61 Mon Sep 17 00:00:00 2001 From: Xavier Mitault Date: Sat, 30 Sep 2023 12:48:01 +0200 Subject: [PATCH 11/15] CICD: Fix build release --- scripts/bundle-linux.sh | 4 +++- scripts/bundle-macos.sh | 2 ++ scripts/bundle-windows.ps1 | 8 ++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/bundle-linux.sh b/scripts/bundle-linux.sh index 60d1ec25..26f06479 100755 --- a/scripts/bundle-linux.sh +++ b/scripts/bundle-linux.sh @@ -3,6 +3,8 @@ set -ex +rm -rf build + cmake -S . -B build -DCMAKE_BUILD_TYPE=Release cmake --build build @@ -15,4 +17,4 @@ mv R-Type-Linux.sh ../r-type-linux.sh cpack --config CPackSourceConfig.cmake -G TGZ -mv R-Type-Source.tar.gz ../r-type-linux.tar.gz +mv R-Type-Linux.tar.gz ../r-type-linux.tar.gz diff --git a/scripts/bundle-macos.sh b/scripts/bundle-macos.sh index 11ef05d6..eca2a0c7 100755 --- a/scripts/bundle-macos.sh +++ b/scripts/bundle-macos.sh @@ -3,6 +3,8 @@ set -ex +rm -rf build + cmake -S . -B build -DCMAKE_BUILD_TYPE=Release cmake --build build diff --git a/scripts/bundle-windows.ps1 b/scripts/bundle-windows.ps1 index 4f764a40..bcdb8e1f 100644 --- a/scripts/bundle-windows.ps1 +++ b/scripts/bundle-windows.ps1 @@ -3,11 +3,11 @@ $ErrorActionPreference = "SilentlyContinue" -./scripts/compil.ps1 +rm -Recurse build cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CONFIGURATION_TYPES="Release" -cmake --build build +cmake --build build --config Release cd build @@ -17,10 +17,10 @@ if ($LASTEXITCODE -ne 0) { type _CPack_Packages/win64/NSIS/NSISOutput.log } -Copy-Item R-Type-win64.exe ../r-type-windows.exe +Copy-Item R-Type-Windows.exe ../r-type-windows.exe cpack --config CPackSourceConfig.cmake -G ZIP -Copy-Item R-Type-Source.zip ../r-type-windows.zip +Copy-Item R-Type-Windows.zip ../r-type-windows.zip cd .. From f2cd9b38bbe8c0bcaa1a95f536a778bef67db8f5 Mon Sep 17 00:00:00 2001 From: Xavier Mitault Date: Sat, 30 Sep 2023 13:17:39 +0200 Subject: [PATCH 12/15] CICD: Fix macos release and compil for windows PATCH --- .github/workflows/release.yml | 22 ++++++++++------------ scripts/bundle-macos.sh | 4 +++- scripts/compil.ps1 | 11 ++--------- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 887198de..12f6a2ce 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -54,9 +54,6 @@ jobs: - name: Build run: ./scripts/bundle-windows.ps1 - - name: LIST ALL - run: Get-ChildItem build -Recurse - - name: Upload To Release if: github.ref == 'refs/heads/main' env: @@ -93,9 +90,6 @@ jobs: - name: Build run: ./scripts/bundle-linux.sh - - name: LIST ALL - run: ls -Rl build - - name: Upload To Release if: github.ref == 'refs/heads/main' env: @@ -132,23 +126,27 @@ jobs: - name: Build run: ./scripts/bundle-macos.sh - - name: LIST ALL - run: ls -Rl build - - name: Upload To Release if: github.ref == 'refs/heads/main' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | gh release upload ${{ needs.release-create.outputs.release }} ./r-type-macos.zip - # gh release upload ${{ needs.release-create.outputs.release }} ./r-type-linux.sh + gh release upload ${{ needs.release-create.outputs.release }} ./r-type-macos.dmg + + - name: Upload To Artifact + if: github.ref != 'refs/heads/main' + uses: actions/upload-artifact@v3 + with: + name: r-type-macos.dmg + path: ./r-type-macos.dmg - name: Upload To Artifact if: github.ref != 'refs/heads/main' uses: actions/upload-artifact@v3 with: - name: r-type-macos.sh - path: ./r-type-macos.sh + name: r-type-macos.zip + path: ./r-type-macos.zip raylib-to-tar: runs-on: ubuntu-latest diff --git a/scripts/bundle-macos.sh b/scripts/bundle-macos.sh index eca2a0c7..58792505 100755 --- a/scripts/bundle-macos.sh +++ b/scripts/bundle-macos.sh @@ -13,6 +13,8 @@ cd build || exit 14 cpack --config CPackConfig.cmake -G DragNDrop || (cat "./_CPack_Packages/Darwin/DragNDrop/PreinstallOutput.log" && exit 1) +cp R-Type-Darwin.dmg ../r-type-macos.dmg + cpack --config CPackSourceConfig.cmake -G ZIP -ls -l +cp R-Type-Darwin.zip ../r-type-macos.zip diff --git a/scripts/compil.ps1 b/scripts/compil.ps1 index 9c410ce7..ef539cf0 100644 --- a/scripts/compil.ps1 +++ b/scripts/compil.ps1 @@ -3,13 +3,6 @@ $ErrorActionPreference = 'Stop' -cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON +cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -cmake --build build - -$file = "build/_deps/raylib-build/raylib/Release/raylib.lib" - -if (-not(Test-Path -Path $file -PathType Leaf)) { - New-Item -Name "build/_deps/raylib-build/raylib/Release" -Type Directory -Force - Copy-Item "build/_deps/raylib-build/raylib/Debug/raylib.lib" $file -} +cmake --build build -- /m From bb6a978848c9733c6a2dff2bc016ac5c78ac5c09 Mon Sep 17 00:00:00 2001 From: Xavier Mitault Date: Sat, 30 Sep 2023 16:34:50 +0200 Subject: [PATCH 13/15] DOCUMENTATION: Add status badge --- docs/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/README.md b/docs/README.md index 50be7b39..9c9c1a5b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,5 +1,8 @@ # R-Bus +[![Release](https://github.com/X-R-G-B/R-Bus/actions/workflows/release.yml/badge.svg?branch=main)](https://github.com/X-R-G-B/R-Bus/actions/workflows/release.yml) + +[![Compil](https://github.com/X-R-G-B/R-Bus/actions/workflows/compil.yml/badge.svg?branch=main)](https://github.com/X-R-G-B/R-Bus/actions/workflows/compil.yml) ``` R-Type[a] is a horizontally scrolling shooter arcade video game developed From 1fa1c4ca0b530bdd71477fae23636aa948b87d83 Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Sat, 30 Sep 2023 17:59:18 +0200 Subject: [PATCH 14/15] ECS: Add current progress dense SparseArrays --- src/Client/SceneManager.hpp | 1 - src/Client/Systems/CustomTypes.hpp | 16 +-- src/Client/Systems/Events/EventsSystems.cpp | 30 ++--- src/Client/Systems/Graphic/GraphicSystems.cpp | 125 +++++++----------- src/ECS/ECSCustomTypes.hpp | 3 + src/ECS/SparseArray.hpp | 67 ++++++++-- src/ECS/Systems/Systems.cpp | 78 +++++------ 7 files changed, 153 insertions(+), 167 deletions(-) diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index 133352f2..f39135c0 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -10,7 +10,6 @@ #include #include #include -#include #include enum CustomIndex { BULLET, PLAYER, ENNEMY }; diff --git a/src/Client/Systems/CustomTypes.hpp b/src/Client/Systems/CustomTypes.hpp index 8bfe60c5..5d6637c9 100644 --- a/src/Client/Systems/CustomTypes.hpp +++ b/src/Client/Systems/CustomTypes.hpp @@ -8,6 +8,7 @@ #pragma once #include +#include "ECSCustomTypes.hpp" namespace Types { @@ -18,19 +19,4 @@ namespace Types { float height; }; - struct Position { - float x; - float y; - }; - - struct RectangleShape { - float width; - float height; - }; - - struct CollisionRect { - float width; - float height; - }; - } // namespace Types diff --git a/src/Client/Systems/Events/EventsSystems.cpp b/src/Client/Systems/Events/EventsSystems.cpp index c663d127..0a8591d9 100644 --- a/src/Client/Systems/Events/EventsSystems.cpp +++ b/src/Client/Systems/Events/EventsSystems.cpp @@ -19,24 +19,20 @@ namespace Systems { Registry::components arrPosition = Registry::getInstance().getComponents(); - SparseArray &playerId = - Registry::getInstance().getCustomSparseArray( - CustomIndex::PLAYER); + std::vector playerId = Registry::getInstance().getCustomSparseArray().getExistingsId(); - for (std::optional id : playerId) { - if (id.has_value() && arrPosition[id.value()].has_value()) { - if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_RIGHT)) { - arrPosition[id.value()].value().x += 1; - } - if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_LEFT)) { - arrPosition[id.value()].value().x -= 1; - } - if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_UP)) { - arrPosition[id.value()].value().y -= 1; - } - if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_DOWN)) { - arrPosition[id.value()].value().y += 1; - } + for (std::size_t id : playerId) { + if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_RIGHT)) { + arrPosition[id].x += 1; + } + if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_LEFT)) { + arrPosition[id].x -= 1; + } + if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_UP)) { + arrPosition[id].y -= 1; + } + if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_DOWN)) { + arrPosition[id].y += 1; } } } diff --git a/src/Client/Systems/Graphic/GraphicSystems.cpp b/src/Client/Systems/Graphic/GraphicSystems.cpp index 17d511ab..b0f0a751 100644 --- a/src/Client/Systems/Graphic/GraphicSystems.cpp +++ b/src/Client/Systems/Graphic/GraphicSystems.cpp @@ -18,40 +18,37 @@ namespace Systems { Registry::getInstance().getComponents(); Registry::components arrRect = Registry::getInstance().getComponents(); + std::vector rectShapeIndexes = arrRect.getExistingsId(); const float denominator = 100.0; - auto positionIt = arrPosition.begin(); - auto rectIt = arrRect.begin(); - - while (positionIt != arrPosition.end() && rectIt != arrRect.end()) { - if (positionIt->has_value() && rectIt->has_value()) { - Types::Position &position = positionIt->value(); - Types::RectangleShape &rectangle = rectIt->value(); - - float x = - (position.x * static_cast(Raylib::getScreenWidth())) - / denominator; - float y = - (position.y * static_cast(Raylib::getScreenHeight())) - / denominator; - - float width = (rectangle.width - * static_cast(Raylib::getScreenWidth())) - / denominator; - float height = (rectangle.height - * static_cast(Raylib::getScreenHeight())) - / denominator; - - DrawRectangle( - static_cast(x), - static_cast(y), - static_cast(width), - static_cast(height), - PURPLE); + for (auto id : rectShapeIndexes) { + if (!arrPosition.exist(id)) { + continue; } - positionIt++; - rectIt++; + Types::Position &position = arrPosition[id]; + Types::RectangleShape &rectangle = arrRect[id]; + + float x = + (position.x * static_cast(Raylib::getScreenWidth())) + / denominator; + float y = + (position.y * static_cast(Raylib::getScreenHeight())) + / denominator; + + float width = (rectangle.width + * static_cast(Raylib::getScreenWidth())) + / denominator; + float height = (rectangle.height + * static_cast(Raylib::getScreenHeight())) + / denominator; + + DrawRectangle( + static_cast(x), + static_cast(y), + static_cast(width), + static_cast(height), + PURPLE); } } @@ -117,24 +114,17 @@ namespace Systems { Registry::getInstance().getComponents(); Registry::components arrPosition = Registry::getInstance().getComponents(); + std::vector spriteIndexes = arrSprite.getExistingsId(); - auto positionIt = arrPosition.begin(); - auto spriteIt = arrSprite.begin(); - auto rectIt = arrRect.begin(); - - while (positionIt != arrPosition.end() && spriteIt != arrSprite.end()) { - if (positionIt->has_value() && spriteIt->has_value() - && rectIt->has_value()) { + for (auto id : spriteIndexes) { + if (arrRect.exist(id) && arrPosition.exist(id)) { drawSpriteWithRect( - positionIt->value(), - spriteIt->value(), - rectIt->value()); - } else if (positionIt->has_value() && spriteIt->has_value()) { - drawSpriteWithoutRect(positionIt->value(), spriteIt->value()); + arrPosition[id], + arrSprite[id], + arrRect[id]); + } else if (arrPosition.exist(id)) { + drawSpriteWithoutRect(arrPosition[id], arrSprite[id]); } - positionIt++; - spriteIt++; - rectIt++; } } @@ -146,12 +136,9 @@ namespace Systems { Registry::getInstance().getComponents(); for (auto &soundEffect : arrSoundEffect) { - if (!soundEffect.has_value()) { - continue; - } - if (soundEffect.value().NeedToPlay()) { - soundEffect.value().play(); - soundEffect.value().setNeedToPlay(false); + if (soundEffect.NeedToPlay()) { + soundEffect.play(); + soundEffect.setNeedToPlay(false); } } } @@ -163,15 +150,12 @@ namespace Systems { Registry::getInstance().getComponents(); for (auto &music : arrMusics) { - if (!music.has_value()) { - continue; - } - if (music.value().NeedToPlay()) { - music.value().play(); - music.value().setNeedToPlay(false); + if (music.NeedToPlay()) { + music.play(); + music.setNeedToPlay(false); } - if (music.value().isPlaying()) { - music.value().update(); + if (music.isPlaying()) { + music.update(); } } } @@ -198,13 +182,8 @@ namespace Systems { Registry::components arrText = Registry::getInstance().getComponents(); - auto textIt = arrText.begin(); - - while (textIt != arrText.end()) { - if (textIt->has_value()) { - drawTextResponsive(textIt->value()); - } - textIt++; + for (auto &textIt: arrText) { + drawTextResponsive(textIt); } } @@ -225,21 +204,15 @@ namespace Systems { Registry::getInstance().getComponents(); for (auto &music : arrMusics) { - if (!music.has_value()) { - continue; - } - if (music.value().getPath() == musicPath + if (music.getPath() == musicPath && Raylib::isKeyPressed(Raylib::KeyboardKey::KB_SPACE)) { - music.value().setNeedToPlay(true); + music.setNeedToPlay(true); } } for (auto &sound : arrSounds) { - if (!sound.has_value()) { - continue; - } - if (sound.value().getPath() == soundPath + if (sound.getPath() == soundPath && Raylib::isKeyPressed(Raylib::KeyboardKey::KB_ENTER)) { - sound.value().setNeedToPlay(true); + sound.setNeedToPlay(true); } } } diff --git a/src/ECS/ECSCustomTypes.hpp b/src/ECS/ECSCustomTypes.hpp index 7c15c4a5..2d38457f 100644 --- a/src/ECS/ECSCustomTypes.hpp +++ b/src/ECS/ECSCustomTypes.hpp @@ -26,4 +26,7 @@ namespace Types { float y; }; + struct Player { + }; + } // namespace Types diff --git a/src/ECS/SparseArray.hpp b/src/ECS/SparseArray.hpp index fa368e21..bb236d6f 100644 --- a/src/ECS/SparseArray.hpp +++ b/src/ECS/SparseArray.hpp @@ -16,37 +16,76 @@ class SparseArray { public: void add() { - _components.push_back(std::nullopt); + _sparse.push_back(-1); + } + void insertBack(const Component &value) + { + insert(_sparse.size() - 1, value); + } + void insert(size_t id, const Component &value) { + if (id >= sparse.size()) { + throw std::runtime_error("SparseArrays::insert: ID out of bounds!"); + } + + if (_sparse[id] > -1) { + _dense[_sparse[id]] = value; + _revSparse[_sparse[id]] = id; + return; + } + + _sparse[id] = _dense.size(); + _dense.push_back(value); + _revSparse.push_back(id); } void erase(std::size_t id) { - auto it = _components.begin(); + if (id >= sparse.size()) { + throw std::runtime_error("SparseArrays::erase: ID out of bounds!"); + } + if (_sparse[id] != -1) { + auto it = _dense.begin(); + std::advance(it, _sparse[id]); + _dense.erase(it); + auto revIt = _revSparse.begin(); + std::advance(revIt, _sparse[id]); + _revSparse.erase(revIt); + } + auto it = _sparse.begin(); std::advance(it, id); - _components.erase(it); + _sparse.erase(it); }; - std::optional &operator[](size_t idx) + Component &operator[](size_t id) { - return _components[idx]; + throwIfDontExist(id); + return _dense[_sparse[id]]; } - typename std::vector>::iterator begin() + typename std::vector::iterator begin() { - return _components.begin(); + return _dense.begin(); } typename std::vector>::iterator end() { - return _components.end(); + return _dense.end(); } - std::optional &back() - { - return _components.back(); + bool exist(std::size_t id) { + return id < _sparse.size() && _sparse[id] != -1; } - std::size_t size() + std::vector getExistingsId() { - return _components.size(); + return _revSparse; } private: - std::vector> _components; + void throwIfDontExist(std::size_t id) + { + if (!exist(id)) { + throw std::runtime_error("SparseArrays: ID out of bounds!"); + } + } + + std::vector _dense; + std::vector _sparse; + std::vector _revSparse; }; diff --git a/src/ECS/Systems/Systems.cpp b/src/ECS/Systems/Systems.cpp index 3196e88a..f63a50fe 100644 --- a/src/ECS/Systems/Systems.cpp +++ b/src/ECS/Systems/Systems.cpp @@ -20,32 +20,27 @@ namespace Systems { Registry::components arrCollisionRect = Registry::getInstance().getComponents(); - SparseArray &playerId = - Registry::getInstance().getCustomSparseArray( - CustomIndex::PLAYER); + std::vector playerIdx = Registry::getInstance().getCustomSparseArray().getExistingsId(); const float maxPercent = 100.0F; - for (std::optional id : playerId) { - if (id.has_value() && arrPosition[id.value()].has_value() - && arrCollisionRect[id.value()].has_value()) { - if (arrPosition[id.value()].value().x < 0) { - arrPosition[id.value()].value().x = 0; - } - if (arrPosition[id.value()].value().y < 0) { - arrPosition[id.value()].value().y = 0; - } - if (arrPosition[id.value()].value().x - + arrCollisionRect[id.value()].value().width - > maxPercent) { - arrPosition[id.value()].value().x = - maxPercent - arrCollisionRect[id.value()].value().width; - } - if (arrPosition[id.value()].value().y - + arrCollisionRect[id.value()].value().height - > maxPercent) { - arrPosition[id.value()].value().y = maxPercent - - arrCollisionRect[id.value()].value().height; - } + for (std::size_t id : playerIdx) { + if (arrPosition[id].x < 0) { + arrPosition[id].x = 0; + } + if (arrPosition[id].y < 0) { + arrPosition[id].y = 0; + } + if (arrPosition[id].x + + arrCollisionRect[id].width + > maxPercent) { + arrPosition[id].x = + maxPercent - arrCollisionRect[id].width; + } + if (arrPosition[id].y + + arrCollisionRect[id].height + > maxPercent) { + arrPosition[id].y = maxPercent + - arrCollisionRect[id].height; } } } @@ -66,34 +61,29 @@ namespace Systems { void init(std::size_t managerId, std::size_t systemId) { Registry::getInstance().addEntity(); - Registry::getInstance().getComponents().back() = { + Registry::getInstance().getComponents().insertBack({ playerData, - playerData}; - Registry::getInstance().getComponents().back() = { + playerData}); + Registry::getInstance().getComponents().insertBack({ playerPath, playerWidth, - playerHeight}; - Registry::getInstance().getComponents().back() = - spriteRect; - Registry::getInstance().getComponents().back() = - collisionRect; - SparseArray &playerId = - Registry::getInstance().getCustomSparseArray( - CustomIndex::PLAYER); - playerId.add(); - playerId.back() = std::optional( - Registry::getInstance().getEntitiesNb() - 1); - Registry::getInstance().getComponents().back() = { + playerHeight}); + Registry::getInstance().getComponents().insertBack( + spriteRect); + Registry::getInstance().getComponents().insertBack( + collisionRect); + Registry::getInstance().getComponents().insertBack(); + Registry::getInstance().getComponents().insertBack({ musicPath, - musicVolume}; - Registry::getInstance().getComponents().back() = { + musicVolume}); + Registry::getInstance().getComponents().insertBack({ soundPath, - soundVolume}; - Registry::getInstance().getComponents().back() = { + soundVolume}); + Registry::getInstance().getComponents().insertBack({ "Press space to play music, enter to play sound", textPos, fontScale, - Raylib::DarkBlue}; + Raylib::DarkBlue}); SystemManagersDirector::getInstance() .getSystemManager(managerId) .removeSystem(systemId); From 058c46c9342598e6883b61038a25b691093871cc Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Sat, 30 Sep 2023 18:23:17 +0200 Subject: [PATCH 15/15] ECS: Add function dense SparseArrays MINOR --- src/Client/SceneManager.cpp | 4 +- src/Client/SceneManager.hpp | 6 -- src/Client/Systems/Events/EventsSystems.cpp | 4 +- src/Client/Systems/Graphic/GraphicSystems.cpp | 11 ++-- src/ECS/ECSCustomTypes.hpp | 3 +- src/ECS/Registry.cpp | 30 +--------- src/ECS/Registry.hpp | 39 +------------ src/ECS/SparseArray.hpp | 29 ++++++---- src/ECS/Systems/Systems.cpp | 57 ++++++++----------- 9 files changed, 56 insertions(+), 127 deletions(-) diff --git a/src/Client/SceneManager.cpp b/src/Client/SceneManager.cpp index 9e73f6ec..5cfcbeba 100644 --- a/src/Client/SceneManager.cpp +++ b/src/Client/SceneManager.cpp @@ -65,8 +65,6 @@ int SceneManager::run() auto &director = Systems::SystemManagersDirector::getInstance(); try { - Registry::getInstance().initCustomSparseArrays( - _scenesCustomIndexes.at(_currentScene)); while (!_stop && !Raylib::windowShouldClose()) { Raylib::beginDrawing(); Raylib::clearBackground(Raylib::DarkGray); @@ -87,7 +85,7 @@ int SceneManager::run() void SceneManager::changeScene(Scene scene) { _currentScene = scene; - Registry::getInstance().clear(_scenesCustomIndexes.at(_currentScene)); + Registry::getInstance().clear(); Systems::SystemManagersDirector::getInstance().resetChanges(); } diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index f39135c0..cbc95f83 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -12,8 +12,6 @@ #include #include -enum CustomIndex { BULLET, PLAYER, ENNEMY }; - enum ReturnValue { OK = 0, ERROR = 84 }; enum Scene { MENU, MAIN_GAME, SCENE_MAX }; @@ -37,10 +35,6 @@ class SceneManager { std::vector {EVENTS, GAME, DISPLAY}, std::vector {EVENTS, GAME, DISPLAY} }; - const std::array, 2> _scenesCustomIndexes = { - std::vector {PLAYER }, - std::vector { PLAYER, BULLET, ENNEMY} - }; // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) static bool _init; diff --git a/src/Client/Systems/Events/EventsSystems.cpp b/src/Client/Systems/Events/EventsSystems.cpp index 0a8591d9..293357c2 100644 --- a/src/Client/Systems/Events/EventsSystems.cpp +++ b/src/Client/Systems/Events/EventsSystems.cpp @@ -19,7 +19,9 @@ namespace Systems { Registry::components arrPosition = Registry::getInstance().getComponents(); - std::vector playerId = Registry::getInstance().getCustomSparseArray().getExistingsId(); + std::vector playerId = Registry::getInstance() + .getComponents() + .getExistingsId(); for (std::size_t id : playerId) { if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_RIGHT)) { diff --git a/src/Client/Systems/Graphic/GraphicSystems.cpp b/src/Client/Systems/Graphic/GraphicSystems.cpp index b0f0a751..756bb20b 100644 --- a/src/Client/Systems/Graphic/GraphicSystems.cpp +++ b/src/Client/Systems/Graphic/GraphicSystems.cpp @@ -36,8 +36,8 @@ namespace Systems { (position.y * static_cast(Raylib::getScreenHeight())) / denominator; - float width = (rectangle.width - * static_cast(Raylib::getScreenWidth())) + float width = + (rectangle.width * static_cast(Raylib::getScreenWidth())) / denominator; float height = (rectangle.height * static_cast(Raylib::getScreenHeight())) @@ -118,10 +118,7 @@ namespace Systems { for (auto id : spriteIndexes) { if (arrRect.exist(id) && arrPosition.exist(id)) { - drawSpriteWithRect( - arrPosition[id], - arrSprite[id], - arrRect[id]); + drawSpriteWithRect(arrPosition[id], arrSprite[id], arrRect[id]); } else if (arrPosition.exist(id)) { drawSpriteWithoutRect(arrPosition[id], arrSprite[id]); } @@ -182,7 +179,7 @@ namespace Systems { Registry::components arrText = Registry::getInstance().getComponents(); - for (auto &textIt: arrText) { + for (auto &textIt : arrText) { drawTextResponsive(textIt); } } diff --git a/src/ECS/ECSCustomTypes.hpp b/src/ECS/ECSCustomTypes.hpp index 2d38457f..0aa01b59 100644 --- a/src/ECS/ECSCustomTypes.hpp +++ b/src/ECS/ECSCustomTypes.hpp @@ -26,7 +26,6 @@ namespace Types { float y; }; - struct Player { - }; + struct Player { }; } // namespace Types diff --git a/src/ECS/Registry.cpp b/src/ECS/Registry.cpp index 6eb34ba5..008c44e6 100644 --- a/src/ECS/Registry.cpp +++ b/src/ECS/Registry.cpp @@ -32,40 +32,14 @@ void Registry::removeEntity(std::size_t id) } } -void Registry::clear(std::vector indexes) +void Registry::clear() { _data.clear(); _addComponentPlaceFunctions.clear(); _removeComponentFunctions.clear(); _entitiesNb = 0; - _customSparseArrays.clear(); - initCustomSparseArrays(indexes); } -std::size_t Registry::getEntitiesNb() const +Registry::Registry() : _entitiesNb(0) { - return (_entitiesNb); -} - -void Registry::initCustomSparseArrays(std::vector indexes) -{ - for (auto index : indexes) { - addCustomSparseIndex(static_cast(index)); - } -} - -Registry::Registry() : _entitiesNb(0), _maxCustomId(0) -{ -} - -void Registry::addCustomSparseIndex(std::size_t id) -{ - if (_customSparseArrays.find(id) != _customSparseArrays.end()) { - throw std::runtime_error( - "addCustomSparseIndex: id" + std::to_string(id) + " already exist"); - }; - _customSparseArrays[id] = SparseArray(); - if (id > _maxCustomId) { - _maxCustomId = id; - } } diff --git a/src/ECS/Registry.hpp b/src/ECS/Registry.hpp index 176e64b7..88e520ca 100644 --- a/src/ECS/Registry.hpp +++ b/src/ECS/Registry.hpp @@ -37,39 +37,7 @@ class Registry { void removeEntity(std::size_t /*id*/); - void clear(std::vector); - - template - components getCustomSparseArray(std::size_t id) - { - if (_customSparseArrays.find(id) == _customSparseArrays.end()) { - throw std::runtime_error( - "getCustomSparseArray ID not in :" + std::to_string(id)); - } - try { - components castedComponent = - std::any_cast>( - _customSparseArrays[id]); - - return castedComponent; - } catch (const std::bad_any_cast &e) { - throw std::runtime_error("Bad cast: " + std::string(e.what())); - } - } - - template - std::size_t addCustomSparseArray() - { - _maxCustomId++; - std::size_t id = _maxCustomId; - - _customSparseArrays[id] = SparseArray(); - return (id); - } - - std::size_t getEntitiesNb() const; - - void initCustomSparseArrays(std::vector indexes); + void clear(); Registry &operator=(const Registry &) = delete; Registry(const Registry &) = delete; @@ -114,8 +82,6 @@ class Registry { _data[typeid(Component)]); } - void addCustomSparseIndex(std::size_t id); - // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) static Registry _instance; // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) @@ -126,8 +92,5 @@ class Registry { _removeComponentFunctions; std::unordered_map _data; - std::unordered_map _customSparseArrays; - std::size_t _maxCustomId; - std::size_t _entitiesNb; }; diff --git a/src/ECS/SparseArray.hpp b/src/ECS/SparseArray.hpp index bb236d6f..d571451e 100644 --- a/src/ECS/SparseArray.hpp +++ b/src/ECS/SparseArray.hpp @@ -8,7 +8,6 @@ #pragma once #include -#include #include template @@ -16,19 +15,23 @@ class SparseArray { public: void add() { - _sparse.push_back(-1); + _sparse.push_back(std::size_t(-1)); } + void insertBack(const Component &value) { insert(_sparse.size() - 1, value); } - void insert(size_t id, const Component &value) { - if (id >= sparse.size()) { - throw std::runtime_error("SparseArrays::insert: ID out of bounds!"); + + void insert(size_t id, const Component &value) + { + if (id >= _sparse.size()) { + throw std::runtime_error( + "SparseArrays::insert: ID out of bounds!"); } if (_sparse[id] > -1) { - _dense[_sparse[id]] = value; + _dense[_sparse[id]] = value; _revSparse[_sparse[id]] = id; return; } @@ -37,10 +40,12 @@ class SparseArray { _dense.push_back(value); _revSparse.push_back(id); } + void erase(std::size_t id) { - if (id >= sparse.size()) { - throw std::runtime_error("SparseArrays::erase: ID out of bounds!"); + if (id >= _sparse.size()) { + throw std::runtime_error( + "SparseArrays::erase: ID out of bounds!"); } if (_sparse[id] != -1) { auto it = _dense.begin(); @@ -54,6 +59,7 @@ class SparseArray { std::advance(it, id); _sparse.erase(it); }; + Component &operator[](size_t id) { throwIfDontExist(id); @@ -65,13 +71,16 @@ class SparseArray { return _dense.begin(); } - typename std::vector>::iterator end() + typename std::vector::iterator end() { return _dense.end(); } - bool exist(std::size_t id) { + + bool exist(std::size_t id) + { return id < _sparse.size() && _sparse[id] != -1; } + std::vector getExistingsId() { return _revSparse; diff --git a/src/ECS/Systems/Systems.cpp b/src/ECS/Systems/Systems.cpp index f63a50fe..a55f6cb9 100644 --- a/src/ECS/Systems/Systems.cpp +++ b/src/ECS/Systems/Systems.cpp @@ -20,7 +20,9 @@ namespace Systems { Registry::components arrCollisionRect = Registry::getInstance().getComponents(); - std::vector playerIdx = Registry::getInstance().getCustomSparseArray().getExistingsId(); + std::vector playerIdx = Registry::getInstance() + .getComponents() + .getExistingsId(); const float maxPercent = 100.0F; for (std::size_t id : playerIdx) { @@ -30,17 +32,11 @@ namespace Systems { if (arrPosition[id].y < 0) { arrPosition[id].y = 0; } - if (arrPosition[id].x - + arrCollisionRect[id].width - > maxPercent) { - arrPosition[id].x = - maxPercent - arrCollisionRect[id].width; + if (arrPosition[id].x + arrCollisionRect[id].width > maxPercent) { + arrPosition[id].x = maxPercent - arrCollisionRect[id].width; } - if (arrPosition[id].y - + arrCollisionRect[id].height - > maxPercent) { - arrPosition[id].y = maxPercent - - arrCollisionRect[id].height; + if (arrPosition[id].y + arrCollisionRect[id].height > maxPercent) { + arrPosition[id].y = maxPercent - arrCollisionRect[id].height; } } } @@ -61,29 +57,26 @@ namespace Systems { void init(std::size_t managerId, std::size_t systemId) { Registry::getInstance().addEntity(); - Registry::getInstance().getComponents().insertBack({ - playerData, - playerData}); - Registry::getInstance().getComponents().insertBack({ - playerPath, - playerWidth, - playerHeight}); + Registry::getInstance().getComponents().insertBack( + {playerData, playerData}); + Registry::getInstance().getComponents().insertBack( + {playerPath, playerWidth, playerHeight}); Registry::getInstance().getComponents().insertBack( spriteRect); - Registry::getInstance().getComponents().insertBack( - collisionRect); - Registry::getInstance().getComponents().insertBack(); - Registry::getInstance().getComponents().insertBack({ - musicPath, - musicVolume}); - Registry::getInstance().getComponents().insertBack({ - soundPath, - soundVolume}); - Registry::getInstance().getComponents().insertBack({ - "Press space to play music, enter to play sound", - textPos, - fontScale, - Raylib::DarkBlue}); + Registry::getInstance() + .getComponents() + .insertBack(collisionRect); + Registry::getInstance().getComponents().insertBack({}); + Registry::getInstance().getComponents().insertBack( + {musicPath, musicVolume}); + Registry::getInstance().getComponents().insertBack( + {soundPath, soundVolume}); + Registry::getInstance().getComponents().insertBack( + {"Press SPACE to play music, ENTER to play sound, J to reset " + "scene, ARROWS to move", + textPos, + fontScale, + Raylib::DarkBlue}); SystemManagersDirector::getInstance() .getSystemManager(managerId) .removeSystem(systemId);