From a51604ca91a40b851635a0606a2bb7abb616fe2e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 12 Jun 2020 13:08:48 -0400 Subject: [PATCH] CCollisionMeshGroup: Make use of unique_ptr --- .../Resource/Collision/CCollisionMeshGroup.h | 30 ++++++++----------- .../Resource/Factory/CCollisionLoader.cpp | 12 ++++---- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/Core/Resource/Collision/CCollisionMeshGroup.h b/src/Core/Resource/Collision/CCollisionMeshGroup.h index 0082b726..52a989d9 100644 --- a/src/Core/Resource/Collision/CCollisionMeshGroup.h +++ b/src/Core/Resource/Collision/CCollisionMeshGroup.h @@ -5,42 +5,38 @@ #include "Core/Resource/CResource.h" #include "Core/Resource/TResPtr.h" #include +#include #include class CCollisionMeshGroup : public CResource { DECLARE_RESOURCE_TYPE(DynamicCollision) - std::vector mMeshes; + std::vector> mMeshes; public: - CCollisionMeshGroup(CResourceEntry *pEntry = 0) : CResource(pEntry) {} + explicit CCollisionMeshGroup(CResourceEntry *pEntry = nullptr) : CResource(pEntry) {} + ~CCollisionMeshGroup() = default; - ~CCollisionMeshGroup() - { - for (auto it = mMeshes.begin(); it != mMeshes.end(); it++) - delete *it; - } - - uint32 NumMeshes() const { return mMeshes.size(); } - CCollisionMesh* MeshByIndex(uint32 Index) const { return mMeshes[Index]; } - void AddMesh(CCollisionMesh *pMesh) { mMeshes.push_back(pMesh); } + uint32 NumMeshes() const { return mMeshes.size(); } + CCollisionMesh* MeshByIndex(uint32 Index) const { return mMeshes[Index].get(); } + void AddMesh(std::unique_ptr&& pMesh) { mMeshes.push_back(std::move(pMesh)); } void BuildRenderData() { - for (auto It = mMeshes.begin(); It != mMeshes.end(); It++) - (*It)->BuildRenderData(); + for (auto& mesh : mMeshes) + mesh->BuildRenderData(); } void Draw() { - for (auto it = mMeshes.begin(); it != mMeshes.end(); it++) - (*it)->GetRenderData().Render(false); + for (auto& mesh : mMeshes) + mesh->GetRenderData().Render(false); } void DrawWireframe() { - for (auto it = mMeshes.begin(); it != mMeshes.end(); it++) - (*it)->GetRenderData().Render(true); + for (auto& mesh : mMeshes) + mesh->GetRenderData().Render(true); } }; diff --git a/src/Core/Resource/Factory/CCollisionLoader.cpp b/src/Core/Resource/Factory/CCollisionLoader.cpp index c2c315e9..74208437 100644 --- a/src/Core/Resource/Factory/CCollisionLoader.cpp +++ b/src/Core/Resource/Factory/CCollisionLoader.cpp @@ -204,9 +204,11 @@ std::unique_ptr CCollisionLoader::LoadAreaCollision(IInputS return nullptr; } + auto mesh = std::make_unique(); + CCollisionLoader Loader; Loader.mVersion = GetFormatVersion(rMREA.ReadLong()); - Loader.mpMesh = new CCollisionMesh; + Loader.mpMesh = mesh.get(); // Octree - structure is known, but not coding this right now Loader.mpMesh->mAABox = CAABox(rMREA); @@ -218,7 +220,7 @@ std::unique_ptr CCollisionLoader::LoadAreaCollision(IInputS Loader.LoadCollisionIndices(rMREA, Loader.mpMesh->mIndexData); auto pOut = std::make_unique(); - pOut->AddMesh(Loader.mpMesh); + pOut->AddMesh(std::move(mesh)); return pOut; } @@ -243,9 +245,9 @@ std::unique_ptr CCollisionLoader::LoadDCLN(IInputStream& rD return nullptr; } + auto mesh = std::make_unique(); Loader.mVersion = GetFormatVersion(rDCLN.ReadLong()); - - Loader.mpMesh = new CCollidableOBBTree; + Loader.mpMesh = mesh.get(); if (Loader.mVersion == EGame::DKCReturns) Loader.mpMesh->mAABox = CAABox(rDCLN); @@ -253,7 +255,7 @@ std::unique_ptr CCollisionLoader::LoadDCLN(IInputStream& rD // Read indices and return rDCLN.Seek(0x4, SEEK_CUR); Loader.LoadCollisionIndices(rDCLN, Loader.mpMesh->mIndexData); - Loader.mpGroup->AddMesh(Loader.mpMesh); + Loader.mpGroup->AddMesh(std::move(mesh)); // Build bounding box if (Loader.mVersion != EGame::DKCReturns)