From 8e6e565adc41845e897da3f2669fac34e68c9306 Mon Sep 17 00:00:00 2001 From: tenshi Date: Wed, 4 Oct 2023 16:41:14 +0200 Subject: [PATCH] CLIENT-GAME: Fix raylib comp unload PATCH --- src/ECS/CMakeLists.txt | 1 + src/ECS/Registry.cpp | 4 +++ src/ECS/Registry.hpp | 2 ++ src/ECS/UnloadRaylib.cpp | 56 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 src/ECS/UnloadRaylib.cpp 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); +}