Skip to content

Commit

Permalink
CLIENT-GAME: Fix raylib comp unload
Browse files Browse the repository at this point in the history
PATCH
  • Loading branch information
TTENSHII committed Oct 4, 2023
1 parent 2ad6834 commit 8e6e565
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ECS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 4 additions & 0 deletions src/ECS/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ std::size_t Registry::addEntity()

void Registry::removeEntity(std::size_t id)
{
unloadRaylibComponents(id);
for (auto function : _removeComponentFunctions) {
function(*this, id);
}
}

void Registry::clear()
{
for (std::size_t i = 0; i < _entitiesNb; i++) {
unloadRaylibComponents(i);
}
_data.clear();
_addComponentPlaceFunctions.clear();
_removeComponentFunctions.clear();
Expand Down
2 changes: 2 additions & 0 deletions src/ECS/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class Registry {

Clock &getClock();

void unloadRaylibComponents(std::size_t id);

private:
Registry();

Expand Down
56 changes: 56 additions & 0 deletions src/ECS/UnloadRaylib.cpp
Original file line number Diff line number Diff line change
@@ -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<Raylib::Sound> arrSound =
Registry::getInstance().getComponents<Raylib::Sound>();

if (arrSound.exist(id)) {
arrSound[id].unload();
}
}

static void unloadMusic(std::size_t id)
{
Registry::components<Raylib::Music> arrMusic =
Registry::getInstance().getComponents<Raylib::Music>();

if (arrMusic.exist(id)) {
arrMusic[id].unload();
}
}

static void unloadSprite(std::size_t id)
{
Registry::components<Raylib::Sprite> arrSprite =
Registry::getInstance().getComponents<Raylib::Sprite>();

if (arrSprite.exist(id)) {
arrSprite[id].unloadSprite();
}
}

static void unloadImage(std::size_t id)
{
Registry::components<Raylib::Image> arrImage =
Registry::getInstance().getComponents<Raylib::Image>();

if (arrImage.exist(id)) {
arrImage[id].unloadImage();
}
}

void Registry::unloadRaylibComponents(std::size_t id)
{
unloadSounds(id);
unloadMusic(id);
unloadSprite(id);
}

0 comments on commit 8e6e565

Please sign in to comment.