From 0f02df28b05832611fe12e0e9d7fd4f8eaba11e8 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 2 Oct 2023 01:34:21 -0400 Subject: [PATCH] Fixed some prefab stuff and memory leak for assimp --- Nuake/src/Resource/Model.cpp | 11 +++++--- Nuake/src/Resource/ModelLoader.cpp | 2 ++ Nuake/src/Resource/Prefab.cpp | 5 +++- Nuake/src/Resource/Prefab.h | 44 +++++++++++++++++++++++++----- 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/Nuake/src/Resource/Model.cpp b/Nuake/src/Resource/Model.cpp index 08588337..7ccba4e3 100644 --- a/Nuake/src/Resource/Model.cpp +++ b/Nuake/src/Resource/Model.cpp @@ -58,12 +58,15 @@ namespace Nuake } else { - for (auto& m : j["Meshes"]) + if (j.contains("Meshes")) { - Ref mesh = CreateRef(); - mesh->Deserialize(m); + for (auto& m : j["Meshes"]) + { + Ref mesh = CreateRef(); + mesh->Deserialize(m); - m_Meshes.push_back(mesh); + m_Meshes.push_back(mesh); + } } } diff --git a/Nuake/src/Resource/ModelLoader.cpp b/Nuake/src/Resource/ModelLoader.cpp index 634f742c..e2662343 100644 --- a/Nuake/src/Resource/ModelLoader.cpp +++ b/Nuake/src/Resource/ModelLoader.cpp @@ -47,6 +47,8 @@ namespace Nuake model->AddMesh(mesh); } + importer.FreeScene(); + return model; } diff --git a/Nuake/src/Resource/Prefab.cpp b/Nuake/src/Resource/Prefab.cpp index 5d912843..f13eed4f 100644 --- a/Nuake/src/Resource/Prefab.cpp +++ b/Nuake/src/Resource/Prefab.cpp @@ -5,8 +5,11 @@ namespace Nuake { Ref Prefab::CreatePrefabFromEntity(Entity entity) { + auto& parentC = entity.GetComponent(); + auto& nameComponent = entity.GetComponent(); + Ref prefab = CreateRef(); - ParentComponent parentC = entity.GetComponent(); + prefab->DisplayName = nameComponent.Name; prefab->EntityWalker(entity); prefab->Root = entity; return prefab; diff --git a/Nuake/src/Resource/Prefab.h b/Nuake/src/Resource/Prefab.h index c79bc2ec..b42d4a10 100644 --- a/Nuake/src/Resource/Prefab.h +++ b/Nuake/src/Resource/Prefab.h @@ -6,16 +6,20 @@ #include #include +#include + namespace Nuake { + class Prefab : ISerializable { public: + std::string DisplayName; + std::string Description; std::string Path; std::vector Entities; Entity Root; static Ref CreatePrefabFromEntity(Entity entity); - static Ref New(const std::string& path); Prefab() @@ -42,10 +46,14 @@ namespace Nuake { { BEGIN_SERIALIZE(); SERIALIZE_VAL(Path); + SERIALIZE_VAL(DisplayName); + SERIALIZE_VAL(Description); - std::vector entities = std::vector(); + auto entities = std::vector(); for (Entity e : Entities) + { entities.push_back(e.Serialize()); + } j["Root"] = Root.GetComponent().ID; SERIALIZE_VAL_LBL("Entities", entities); @@ -67,10 +75,23 @@ namespace Nuake { return false; Path = j["Path"]; + + if (j.contains("DisplayName")) + { + DisplayName = j["DisplayName"]; + } + + if (j.contains("Description")) + { + Description = j["Description"]; + } + if (j.contains("Entities")) { const auto& scene = Engine::GetCurrentScene(); + std::map newIdsLut; + for (json e : j["Entities"]) { Entity entity = { scene->m_Registry.create(), scene.get() }; @@ -81,27 +102,36 @@ namespace Nuake { if (nameComponent.ID == j["Root"]) { isRoot = true; // We found the root entity of the prefab. + auto& parentComponent = entity.GetComponent(); + parentComponent.HasParent = false; + parentComponent.ParentID = 0; } + uint32_t oldId = nameComponent.ID; + uint32_t newId = OS::GetTime(); nameComponent.Name = scene->GetUniqueEntityName(nameComponent.Name); - nameComponent.ID = OS::GetTime(); + nameComponent.ID = newId; + + newIdsLut[oldId] = newId; if (isRoot) { Root = entity; } - this->AddEntity(entity); + AddEntity(entity); } // Set reference to the parent entity to children for (auto& e : Entities) { - auto parentC = e.GetComponent(); - if (!parentC.HasParent) + if (e.GetID() == Root.GetID()) + { continue; + } - auto parent = Engine::GetCurrentScene()->GetEntityByID(parentC.ParentID); + auto& parentC = e.GetComponent(); + auto parent = Engine::GetCurrentScene()->GetEntityByID(newIdsLut[parentC.ParentID]); parent.AddChild(e); } }