diff --git a/src/ECS/CMakeLists.txt b/src/ECS/CMakeLists.txt index 148889f0..92476784 100644 --- a/src/ECS/CMakeLists.txt +++ b/src/ECS/CMakeLists.txt @@ -13,4 +13,5 @@ target_sources( PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Registry.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Clock.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/UnloadRaylib.cpp ) diff --git a/src/ECS/Registry.cpp b/src/ECS/Registry.cpp index 63a58dd9..f72ab76d 100644 --- a/src/ECS/Registry.cpp +++ b/src/ECS/Registry.cpp @@ -29,6 +29,7 @@ std::size_t Registry::addEntity() void Registry::removeEntity(std::size_t id) { + unloadRaylibComponents(id); for (auto function : _removeComponentFunctions) { function(*this, id); } @@ -36,6 +37,9 @@ void Registry::removeEntity(std::size_t id) void Registry::clear() { + for (std::size_t i = 0; i < _entitiesNb; i++) { + unloadRaylibComponents(i); + } _data.clear(); _addComponentPlaceFunctions.clear(); _removeComponentFunctions.clear(); diff --git a/src/ECS/Registry.hpp b/src/ECS/Registry.hpp index e2114355..54b9edf8 100644 --- a/src/ECS/Registry.hpp +++ b/src/ECS/Registry.hpp @@ -76,6 +76,8 @@ class Registry { Clock &getClock(); + void unloadRaylibComponents(std::size_t id); + private: Registry(); diff --git a/src/ECS/UnloadRaylib.cpp b/src/ECS/UnloadRaylib.cpp new file mode 100644 index 00000000..9010e4da --- /dev/null +++ b/src/ECS/UnloadRaylib.cpp @@ -0,0 +1,56 @@ +/* +** EPITECH PROJECT, 2023 +** R-Bus +** File description: +** unloadRaylib +*/ + +#include "Registry.hpp" +#include "Raylib.hpp" + +static void unloadSounds(std::size_t id) +{ + Registry::components arrSound = + Registry::getInstance().getComponents(); + + if (arrSound.exist(id)) { + arrSound[id].unload(); + } +} + +static void unloadMusic(std::size_t id) +{ + Registry::components arrMusic = + Registry::getInstance().getComponents(); + + if (arrMusic.exist(id)) { + arrMusic[id].unload(); + } +} + +static void unloadSprite(std::size_t id) +{ + Registry::components arrSprite = + Registry::getInstance().getComponents(); + + if (arrSprite.exist(id)) { + arrSprite[id].unloadSprite(); + } +} + +static void unloadImage(std::size_t id) +{ + Registry::components arrImage = + Registry::getInstance().getComponents(); + + if (arrImage.exist(id)) { + arrImage[id].unloadImage(); + } +} + +void Registry::unloadRaylibComponents(std::size_t id) +{ + unloadSounds(id); + unloadMusic(id); + unloadSprite(id); +}