CCollisionLoader: Make use of unique_ptr more

Same behavior, but makes the allocation ownership explicit
This commit is contained in:
Lioncash 2020-06-12 12:50:17 -04:00
parent d43f2dab31
commit 3449f60317
2 changed files with 19 additions and 17 deletions

View File

@ -23,31 +23,31 @@ CCollisionMesh::CCollisionOctree::SLeaf* CCollisionLoader::ParseOctreeLeaf(IInpu
} }
#endif #endif
SOBBTreeNode* CCollisionLoader::ParseOBBNode(IInputStream& DCLN) std::unique_ptr<SOBBTreeNode> CCollisionLoader::ParseOBBNode(IInputStream& DCLN) const
{ {
SOBBTreeNode* pOut = nullptr; std::unique_ptr<SOBBTreeNode> pOut;
CTransform4f Transform(DCLN); const CTransform4f Transform(DCLN);
CVector3f Radius(DCLN); const CVector3f Radius(DCLN);
bool IsLeaf = DCLN.ReadBool(); const bool IsLeaf = DCLN.ReadBool();
if (IsLeaf) if (IsLeaf)
{ {
SOBBTreeLeaf* pLeaf = new SOBBTreeLeaf; auto pLeaf = std::make_unique<SOBBTreeLeaf>();
uint NumTris = DCLN.ReadLong(); const uint NumTris = DCLN.ReadLong();
pLeaf->TriangleIndices.resize(NumTris); pLeaf->TriangleIndices.resize(NumTris);
for (uint i=0; i<NumTris; i++) for (uint i = 0; i < NumTris; i++)
pLeaf->TriangleIndices[i] = DCLN.ReadShort(); pLeaf->TriangleIndices[i] = DCLN.ReadShort();
pOut = pLeaf; pOut = std::move(pLeaf);
} }
else else
{ {
SOBBTreeBranch* pBranch = new SOBBTreeBranch; auto pBranch = std::make_unique<SOBBTreeBranch>();
pBranch->pLeft = std::unique_ptr<SOBBTreeNode>( ParseOBBNode(DCLN) ); pBranch->pLeft = ParseOBBNode(DCLN);
pBranch->pRight = std::unique_ptr<SOBBTreeNode>( ParseOBBNode(DCLN) ); pBranch->pRight = ParseOBBNode(DCLN);
pOut = pBranch; pOut = std::move(pBranch);
} }
pOut->Transform = Transform; pOut->Transform = Transform;
@ -271,7 +271,7 @@ std::unique_ptr<CCollisionMeshGroup> CCollisionLoader::LoadDCLN(IInputStream& rD
// Parse OBB tree // Parse OBB tree
CCollidableOBBTree* pOBBTree = static_cast<CCollidableOBBTree*>(Loader.mpMesh); CCollidableOBBTree* pOBBTree = static_cast<CCollidableOBBTree*>(Loader.mpMesh);
pOBBTree->mpOBBTree = std::unique_ptr<SOBBTreeNode>( Loader.ParseOBBNode(rDCLN) ); pOBBTree->mpOBBTree = Loader.ParseOBBNode(rDCLN);
} }
return ptr; return ptr;

View File

@ -6,6 +6,8 @@
#include "Core/Resource/Collision/CCollidableOBBTree.h" #include "Core/Resource/Collision/CCollidableOBBTree.h"
#include <Common/EGame.h> #include <Common/EGame.h>
#include <memory>
class CCollisionLoader class CCollisionLoader
{ {
TResPtr<CCollisionMeshGroup> mpGroup; TResPtr<CCollisionMeshGroup> mpGroup;
@ -20,9 +22,9 @@ class CCollisionLoader
CCollisionMesh::CCollisionOctree::SLeaf* ParseOctreeLeaf(IInputStream& rSrc); CCollisionMesh::CCollisionOctree::SLeaf* ParseOctreeLeaf(IInputStream& rSrc);
#endif #endif
SOBBTreeNode* ParseOBBNode(IInputStream& DCLN); std::unique_ptr<SOBBTreeNode> ParseOBBNode(IInputStream& DCLN) const;
void LoadCollisionMaterial(IInputStream& Src, CCollisionMaterial& OutMaterial); void LoadCollisionMaterial(IInputStream& Src, CCollisionMaterial& OutMaterial);
void LoadCollisionIndices(IInputStream& File, SCollisionIndexData& OutData); void LoadCollisionIndices(IInputStream& File, SCollisionIndexData& OutData);
public: public:
static std::unique_ptr<CCollisionMeshGroup> LoadAreaCollision(IInputStream& rMREA); static std::unique_ptr<CCollisionMeshGroup> LoadAreaCollision(IInputStream& rMREA);