CCollisionMeshGroup: Make use of unique_ptr

This commit is contained in:
Lioncash 2020-06-12 13:08:48 -04:00
parent 48d8d361b6
commit a51604ca91
2 changed files with 20 additions and 22 deletions

View File

@ -5,42 +5,38 @@
#include "Core/Resource/CResource.h" #include "Core/Resource/CResource.h"
#include "Core/Resource/TResPtr.h" #include "Core/Resource/TResPtr.h"
#include <Common/Math/CTransform4f.h> #include <Common/Math/CTransform4f.h>
#include <memory>
#include <vector> #include <vector>
class CCollisionMeshGroup : public CResource class CCollisionMeshGroup : public CResource
{ {
DECLARE_RESOURCE_TYPE(DynamicCollision) DECLARE_RESOURCE_TYPE(DynamicCollision)
std::vector<CCollisionMesh*> mMeshes; std::vector<std::unique_ptr<CCollisionMesh>> mMeshes;
public: 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(); } uint32 NumMeshes() const { return mMeshes.size(); }
CCollisionMesh* MeshByIndex(uint32 Index) const { return mMeshes[Index]; } CCollisionMesh* MeshByIndex(uint32 Index) const { return mMeshes[Index].get(); }
void AddMesh(CCollisionMesh *pMesh) { mMeshes.push_back(pMesh); } void AddMesh(std::unique_ptr<CCollisionMesh>&& pMesh) { mMeshes.push_back(std::move(pMesh)); }
void BuildRenderData() void BuildRenderData()
{ {
for (auto It = mMeshes.begin(); It != mMeshes.end(); It++) for (auto& mesh : mMeshes)
(*It)->BuildRenderData(); mesh->BuildRenderData();
} }
void Draw() void Draw()
{ {
for (auto it = mMeshes.begin(); it != mMeshes.end(); it++) for (auto& mesh : mMeshes)
(*it)->GetRenderData().Render(false); mesh->GetRenderData().Render(false);
} }
void DrawWireframe() void DrawWireframe()
{ {
for (auto it = mMeshes.begin(); it != mMeshes.end(); it++) for (auto& mesh : mMeshes)
(*it)->GetRenderData().Render(true); mesh->GetRenderData().Render(true);
} }
}; };

View File

@ -204,9 +204,11 @@ std::unique_ptr<CCollisionMeshGroup> CCollisionLoader::LoadAreaCollision(IInputS
return nullptr; return nullptr;
} }
auto mesh = std::make_unique<CCollisionMesh>();
CCollisionLoader Loader; CCollisionLoader Loader;
Loader.mVersion = GetFormatVersion(rMREA.ReadLong()); Loader.mVersion = GetFormatVersion(rMREA.ReadLong());
Loader.mpMesh = new CCollisionMesh; Loader.mpMesh = mesh.get();
// Octree - structure is known, but not coding this right now // Octree - structure is known, but not coding this right now
Loader.mpMesh->mAABox = CAABox(rMREA); Loader.mpMesh->mAABox = CAABox(rMREA);
@ -218,7 +220,7 @@ std::unique_ptr<CCollisionMeshGroup> CCollisionLoader::LoadAreaCollision(IInputS
Loader.LoadCollisionIndices(rMREA, Loader.mpMesh->mIndexData); Loader.LoadCollisionIndices(rMREA, Loader.mpMesh->mIndexData);
auto pOut = std::make_unique<CCollisionMeshGroup>(); auto pOut = std::make_unique<CCollisionMeshGroup>();
pOut->AddMesh(Loader.mpMesh); pOut->AddMesh(std::move(mesh));
return pOut; return pOut;
} }
@ -243,9 +245,9 @@ std::unique_ptr<CCollisionMeshGroup> CCollisionLoader::LoadDCLN(IInputStream& rD
return nullptr; return nullptr;
} }
auto mesh = std::make_unique<CCollidableOBBTree>();
Loader.mVersion = GetFormatVersion(rDCLN.ReadLong()); Loader.mVersion = GetFormatVersion(rDCLN.ReadLong());
Loader.mpMesh = mesh.get();
Loader.mpMesh = new CCollidableOBBTree;
if (Loader.mVersion == EGame::DKCReturns) if (Loader.mVersion == EGame::DKCReturns)
Loader.mpMesh->mAABox = CAABox(rDCLN); Loader.mpMesh->mAABox = CAABox(rDCLN);
@ -253,7 +255,7 @@ std::unique_ptr<CCollisionMeshGroup> CCollisionLoader::LoadDCLN(IInputStream& rD
// Read indices and return // Read indices and return
rDCLN.Seek(0x4, SEEK_CUR); rDCLN.Seek(0x4, SEEK_CUR);
Loader.LoadCollisionIndices(rDCLN, Loader.mpMesh->mIndexData); Loader.LoadCollisionIndices(rDCLN, Loader.mpMesh->mIndexData);
Loader.mpGroup->AddMesh(Loader.mpMesh); Loader.mpGroup->AddMesh(std::move(mesh));
// Build bounding box // Build bounding box
if (Loader.mVersion != EGame::DKCReturns) if (Loader.mVersion != EGame::DKCReturns)