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/TResPtr.h"
#include <Common/Math/CTransform4f.h>
#include <memory>
#include <vector>
class CCollisionMeshGroup : public CResource
{
DECLARE_RESOURCE_TYPE(DynamicCollision)
std::vector<CCollisionMesh*> mMeshes;
std::vector<std::unique_ptr<CCollisionMesh>> mMeshes;
public:
CCollisionMeshGroup(CResourceEntry *pEntry = 0) : CResource(pEntry) {}
~CCollisionMeshGroup()
{
for (auto it = mMeshes.begin(); it != mMeshes.end(); it++)
delete *it;
}
explicit CCollisionMeshGroup(CResourceEntry *pEntry = nullptr) : CResource(pEntry) {}
~CCollisionMeshGroup() = default;
uint32 NumMeshes() const { return mMeshes.size(); }
CCollisionMesh* MeshByIndex(uint32 Index) const { return mMeshes[Index]; }
void AddMesh(CCollisionMesh *pMesh) { mMeshes.push_back(pMesh); }
CCollisionMesh* MeshByIndex(uint32 Index) const { return mMeshes[Index].get(); }
void AddMesh(std::unique_ptr<CCollisionMesh>&& 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);
}
};

View File

@ -204,9 +204,11 @@ std::unique_ptr<CCollisionMeshGroup> CCollisionLoader::LoadAreaCollision(IInputS
return nullptr;
}
auto mesh = std::make_unique<CCollisionMesh>();
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<CCollisionMeshGroup> CCollisionLoader::LoadAreaCollision(IInputS
Loader.LoadCollisionIndices(rMREA, Loader.mpMesh->mIndexData);
auto pOut = std::make_unique<CCollisionMeshGroup>();
pOut->AddMesh(Loader.mpMesh);
pOut->AddMesh(std::move(mesh));
return pOut;
}
@ -243,9 +245,9 @@ std::unique_ptr<CCollisionMeshGroup> CCollisionLoader::LoadDCLN(IInputStream& rD
return nullptr;
}
auto mesh = std::make_unique<CCollidableOBBTree>();
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<CCollisionMeshGroup> 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)