mirror of
https://github.com/AxioDL/PrimeWorldEditor.git
synced 2025-12-18 09:25:31 +00:00
CGameArea: Simplify allocation handling
Eliminates manual new/delete and also makes the lifecycle of allocations enforced within the interface.
This commit is contained in:
@@ -18,9 +18,6 @@ CGameArea::CGameArea(CResourceEntry *pEntry /*= 0*/)
|
||||
CGameArea::~CGameArea()
|
||||
{
|
||||
ClearTerrain();
|
||||
|
||||
for (uint32 iSCLY = 0; iSCLY < mScriptLayers.size(); iSCLY++)
|
||||
delete mScriptLayers[iSCLY];
|
||||
}
|
||||
|
||||
std::unique_ptr<CDependencyTree> CGameArea::BuildDependencyTree() const
|
||||
@@ -52,18 +49,19 @@ std::unique_ptr<CDependencyTree> CGameArea::BuildDependencyTree() const
|
||||
for (uint32 iLayer = 0; iLayer < mScriptLayers.size(); iLayer++)
|
||||
{
|
||||
const std::vector<CAssetID>& rkExtras = (mExtraLayerDeps.size() > iLayer ? mExtraLayerDeps[iLayer] : DummyDeps);
|
||||
pTree->AddScriptLayer(mScriptLayers[iLayer], rkExtras);
|
||||
pTree->AddScriptLayer(mScriptLayers[iLayer].get(), rkExtras);
|
||||
}
|
||||
|
||||
return pTree;
|
||||
}
|
||||
|
||||
void CGameArea::AddWorldModel(CModel *pModel)
|
||||
void CGameArea::AddWorldModel(std::unique_ptr<CModel>&& pModel)
|
||||
{
|
||||
mWorldModels.push_back(pModel);
|
||||
mVertexCount += pModel->GetVertexCount();
|
||||
mTriangleCount += pModel->GetTriangleCount();
|
||||
mAABox.ExpandBounds(pModel->AABox());
|
||||
|
||||
mWorldModels.push_back(std::move(pModel));
|
||||
}
|
||||
|
||||
void CGameArea::MergeTerrain()
|
||||
@@ -73,7 +71,7 @@ void CGameArea::MergeTerrain()
|
||||
// Nothing really complicated here - iterate through every terrain submesh, add each to a static model
|
||||
for (uint32 iMdl = 0; iMdl < mWorldModels.size(); iMdl++)
|
||||
{
|
||||
CModel *pMdl = mWorldModels[iMdl];
|
||||
auto& pMdl = mWorldModels[iMdl];
|
||||
uint32 SubmeshCount = pMdl->GetSurfaceCount();
|
||||
|
||||
for (uint32 iSurf = 0; iSurf < SubmeshCount; iSurf++)
|
||||
@@ -82,7 +80,7 @@ void CGameArea::MergeTerrain()
|
||||
CMaterial *pMat = mpMaterialSet->MaterialByIndex(pSurf->MaterialID, false);
|
||||
|
||||
bool NewMat = true;
|
||||
for (std::vector<CStaticModel*>::iterator it = mStaticWorldModels.begin(); it != mStaticWorldModels.end(); it++)
|
||||
for (auto it = mStaticWorldModels.begin(); it != mStaticWorldModels.end(); it++)
|
||||
{
|
||||
if ((*it)->GetMaterial() == pMat)
|
||||
{
|
||||
@@ -91,10 +89,10 @@ void CGameArea::MergeTerrain()
|
||||
// (particularly with multi-layered transparent meshes)
|
||||
// so we need to at least try to maintain it.
|
||||
// This is maybe not the most efficient way to do this, but it works.
|
||||
CStaticModel *pStatic = *it;
|
||||
auto pStatic = std::move(*it);
|
||||
pStatic->AddSurface(pSurf);
|
||||
mStaticWorldModels.erase(it);
|
||||
mStaticWorldModels.push_back(pStatic);
|
||||
mStaticWorldModels.push_back(std::move(pStatic));
|
||||
NewMat = false;
|
||||
break;
|
||||
}
|
||||
@@ -102,9 +100,9 @@ void CGameArea::MergeTerrain()
|
||||
|
||||
if (NewMat)
|
||||
{
|
||||
CStaticModel *pStatic = new CStaticModel(pMat);
|
||||
auto pStatic = std::make_unique<CStaticModel>(pMat);
|
||||
pStatic->AddSurface(pSurf);
|
||||
mStaticWorldModels.push_back(pStatic);
|
||||
mStaticWorldModels.push_back(std::move(pStatic));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -112,15 +110,11 @@ void CGameArea::MergeTerrain()
|
||||
|
||||
void CGameArea::ClearTerrain()
|
||||
{
|
||||
for (uint32 iModel = 0; iModel < mWorldModels.size(); iModel++)
|
||||
delete mWorldModels[iModel];
|
||||
mWorldModels.clear();
|
||||
|
||||
for (uint32 iStatic = 0; iStatic < mStaticWorldModels.size(); iStatic++)
|
||||
delete mStaticWorldModels[iStatic];
|
||||
mStaticWorldModels.clear();
|
||||
|
||||
if (mpMaterialSet) delete mpMaterialSet;
|
||||
if (mpMaterialSet)
|
||||
delete mpMaterialSet;
|
||||
|
||||
mVertexCount = 0;
|
||||
mTriangleCount = 0;
|
||||
@@ -130,8 +124,6 @@ void CGameArea::ClearTerrain()
|
||||
|
||||
void CGameArea::ClearScriptLayers()
|
||||
{
|
||||
for (auto it = mScriptLayers.begin(); it != mScriptLayers.end(); it++)
|
||||
delete *it;
|
||||
mScriptLayers.clear();
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,9 @@
|
||||
#include <Common/Math/CQuaternion.h>
|
||||
#include <Common/Math/CTransform4f.h>
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
class CScriptLayer;
|
||||
class CScriptObject;
|
||||
@@ -45,10 +47,10 @@ class CGameArea : public CResource
|
||||
|
||||
// Geometry
|
||||
CMaterialSet *mpMaterialSet;
|
||||
std::vector<CModel*> mWorldModels; // TerrainModels is the original version of each model; this is currently mainly used in the POI map editor
|
||||
std::vector<CStaticModel*> mStaticWorldModels; // StaticTerrainModels is the merged terrain for faster rendering in the world editor
|
||||
std::vector<std::unique_ptr<CModel>> mWorldModels; // TerrainModels is the original version of each model; this is currently mainly used in the POI map editor
|
||||
std::vector<std::unique_ptr<CStaticModel>> mStaticWorldModels; // StaticTerrainModels is the merged terrain for faster rendering in the world editor
|
||||
// Script
|
||||
std::vector<CScriptLayer*> mScriptLayers;
|
||||
std::vector<std::unique_ptr<CScriptLayer>> mScriptLayers;
|
||||
std::unordered_map<uint32, CScriptObject*> mObjectMap;
|
||||
// Collision
|
||||
std::unique_ptr<CCollisionMeshGroup> mpCollision;
|
||||
@@ -69,7 +71,7 @@ public:
|
||||
~CGameArea();
|
||||
std::unique_ptr<CDependencyTree> BuildDependencyTree() const override;
|
||||
|
||||
void AddWorldModel(CModel *pModel);
|
||||
void AddWorldModel(std::unique_ptr<CModel>&& pModel);
|
||||
void MergeTerrain();
|
||||
void ClearTerrain();
|
||||
void ClearScriptLayers();
|
||||
@@ -91,11 +93,11 @@ public:
|
||||
CMaterialSet* Materials() const { return mpMaterialSet; }
|
||||
uint32 NumWorldModels() const { return mWorldModels.size(); }
|
||||
uint32 NumStaticModels() const { return mStaticWorldModels.size(); }
|
||||
CModel* TerrainModel(uint32 iMdl) const { return mWorldModels[iMdl]; }
|
||||
CStaticModel* StaticModel(uint32 iMdl) const { return mStaticWorldModels[iMdl]; }
|
||||
CModel* TerrainModel(uint32 iMdl) const { return mWorldModels[iMdl].get(); }
|
||||
CStaticModel* StaticModel(uint32 iMdl) const { return mStaticWorldModels[iMdl].get(); }
|
||||
CCollisionMeshGroup* Collision() const { return mpCollision.get(); }
|
||||
uint32 NumScriptLayers() const { return mScriptLayers.size(); }
|
||||
CScriptLayer* ScriptLayer(uint32 Index) const { return mScriptLayers[Index]; }
|
||||
CScriptLayer* ScriptLayer(uint32 Index) const { return mScriptLayers[Index].get(); }
|
||||
uint32 NumLightLayers() const { return mLightLayers.size(); }
|
||||
uint32 NumLights(uint32 LayerIndex) const { return (LayerIndex < mLightLayers.size() ? mLightLayers[LayerIndex].size() : 0); }
|
||||
CLight* Light(uint32 LayerIndex, uint32 LightIndex) { return &mLightLayers[LayerIndex][LightIndex]; }
|
||||
|
||||
Reference in New Issue
Block a user