From d7574a1965f63e508fb8510ee5b55c9dd74eba1c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 19 Jun 2020 19:14:56 -0400 Subject: [PATCH] CScene: General tidying up - Ranged for, sign conversion fixes, etc. --- src/Core/Scene/CScene.cpp | 140 ++++++++++++++++++++------------------ 1 file changed, 75 insertions(+), 65 deletions(-) diff --git a/src/Core/Scene/CScene.cpp b/src/Core/Scene/CScene.cpp index 2193b491..72587ec4 100644 --- a/src/Core/Scene/CScene.cpp +++ b/src/Core/Scene/CScene.cpp @@ -25,12 +25,12 @@ CScene::~CScene() bool CScene::IsNodeIDUsed(uint32 ID) const { - return (mNodeMap.find(ID) != mNodeMap.end()); + return mNodeMap.find(ID) != mNodeMap.cend(); } uint32 CScene::CreateNodeID(uint32 SuggestedID) const { - if (SuggestedID != -1) + if (SuggestedID != UINT32_MAX) { if (IsNodeIDUsed(SuggestedID)) errorf("Suggested node ID is already being used! New ID will be created."); @@ -48,51 +48,55 @@ uint32 CScene::CreateNodeID(uint32 SuggestedID) const CModelNode* CScene::CreateModelNode(CModel *pModel, uint32 NodeID) { - if (pModel == nullptr) return nullptr; + if (pModel == nullptr) + return nullptr; - uint32 ID = CreateNodeID(NodeID); - CModelNode *pNode = new CModelNode(this, ID, mpAreaRootNode, pModel); + const uint32 ID = CreateNodeID(NodeID); + auto* pNode = new CModelNode(this, ID, mpAreaRootNode, pModel); mNodes[ENodeType::Model].push_back(pNode); - mNodeMap[ID] = pNode; + mNodeMap.insert_or_assign(ID, pNode); mNumNodes++; return pNode; } CStaticNode* CScene::CreateStaticNode(CStaticModel *pModel, uint32 NodeID) { - if (pModel == nullptr) return nullptr; + if (pModel == nullptr) + return nullptr; - uint32 ID = CreateNodeID(NodeID); - CStaticNode *pNode = new CStaticNode(this, ID, mpAreaRootNode, pModel); + const uint32 ID = CreateNodeID(NodeID); + auto* pNode = new CStaticNode(this, ID, mpAreaRootNode, pModel); mNodes[ENodeType::Static].push_back(pNode); - mNodeMap[ID] = pNode; + mNodeMap.insert_or_assign(ID, pNode); mNumNodes++; return pNode; } CCollisionNode* CScene::CreateCollisionNode(CCollisionMeshGroup *pMesh, uint32 NodeID) { - if (pMesh == nullptr) return nullptr; + if (pMesh == nullptr) + return nullptr; - uint32 ID = CreateNodeID(NodeID); - CCollisionNode *pNode = new CCollisionNode(this, ID, mpAreaRootNode, pMesh); + const uint32 ID = CreateNodeID(NodeID); + auto* pNode = new CCollisionNode(this, ID, mpAreaRootNode, pMesh); mNodes[ENodeType::Collision].push_back(pNode); - mNodeMap[ID] = pNode; + mNodeMap.insert_or_assign(ID, pNode); mNumNodes++; return pNode; } CScriptNode* CScene::CreateScriptNode(CScriptObject *pObj, uint32 NodeID) { - if (pObj == nullptr) return nullptr; + if (pObj == nullptr) + return nullptr; - uint32 ID = CreateNodeID(NodeID); - uint32 InstanceID = pObj->InstanceID(); + const uint32 ID = CreateNodeID(NodeID); + const uint32 InstanceID = pObj->InstanceID(); - CScriptNode *pNode = new CScriptNode(this, ID, mpAreaRootNode, pObj); + auto *pNode = new CScriptNode(this, ID, mpAreaRootNode, pObj); mNodes[ENodeType::Script].push_back(pNode); - mNodeMap[ID] = pNode; - mScriptMap[InstanceID] = pNode; + mNodeMap.insert_or_assign(ID, pNode); + mScriptMap.insert_or_assign(InstanceID, pNode); pNode->BuildLightList(mpArea); // AreaAttributes check @@ -100,7 +104,7 @@ CScriptNode* CScene::CreateScriptNode(CScriptObject *pObj, uint32 NodeID) { case 0x4E: // MP1 AreaAttributes ID case FOURCC('REAA'): // MP2/MP3/DKCR AreaAttributes ID - mAreaAttributesObjects.emplace_back( CAreaAttributes(pObj) ); + mAreaAttributesObjects.emplace_back(pObj); break; } @@ -110,21 +114,22 @@ CScriptNode* CScene::CreateScriptNode(CScriptObject *pObj, uint32 NodeID) CLightNode* CScene::CreateLightNode(CLight *pLight, uint32 NodeID) { - if (pLight == nullptr) return nullptr; + if (pLight == nullptr) + return nullptr; - uint32 ID = CreateNodeID(NodeID); - CLightNode *pNode = new CLightNode(this, ID, mpAreaRootNode, pLight); + const uint32 ID = CreateNodeID(NodeID); + auto *pNode = new CLightNode(this, ID, mpAreaRootNode, pLight); mNodes[ENodeType::Light].push_back(pNode); - mNodeMap[ID] = pNode; + mNodeMap.insert_or_assign(ID, pNode); mNumNodes++; return pNode; } void CScene::DeleteNode(CSceneNode *pNode) { - ENodeType Type = pNode->NodeType(); + const ENodeType Type = pNode->NodeType(); - for (auto it = mNodes[Type].begin(); it != mNodes[Type].end(); it++) + for (auto it = mNodes[Type].begin(); it != mNodes[Type].end(); ++it) { if (*it == pNode) { @@ -133,23 +138,21 @@ void CScene::DeleteNode(CSceneNode *pNode) } } - auto MapIt = mNodeMap.find(pNode->ID()); - if (MapIt != mNodeMap.end()) + if (const auto MapIt = mNodeMap.find(pNode->ID()); MapIt != mNodeMap.end()) mNodeMap.erase(MapIt); if (Type == ENodeType::Script) { - CScriptNode *pScript = static_cast(pNode); + auto *pScript = static_cast(pNode); - auto it = mScriptMap.find(pScript->Instance()->InstanceID()); - if (it != mScriptMap.end()) + if (const auto it = mScriptMap.find(pScript->Instance()->InstanceID()); it != mScriptMap.end()) mScriptMap.erase(it); switch (pScript->Instance()->ObjectTypeID()) { case 0x4E: case FOURCC('REAA'): - for (auto it = mAreaAttributesObjects.begin(); it != mAreaAttributesObjects.end(); it++) + for (auto it = mAreaAttributesObjects.begin(); it != mAreaAttributesObjects.end(); ++it) { if ((*it).Instance() == pScript->Instance()) { @@ -275,8 +278,8 @@ void CScene::AddSceneToRenderer(CRenderer *pRenderer, const SViewInfo& rkViewInf PostLoad(); // Override show flags in game mode - FShowFlags ShowFlags = (rkViewInfo.GameMode ? gkGameModeShowFlags : rkViewInfo.ShowFlags); - FNodeFlags NodeFlags = NodeFlagsForShowFlags(ShowFlags); + const FShowFlags ShowFlags = rkViewInfo.GameMode ? gkGameModeShowFlags : rkViewInfo.ShowFlags; + const FNodeFlags NodeFlags = NodeFlagsForShowFlags(ShowFlags); for (CSceneIterator It(this, NodeFlags, false); It; ++It) { @@ -287,8 +290,8 @@ void CScene::AddSceneToRenderer(CRenderer *pRenderer, const SViewInfo& rkViewInf SRayIntersection CScene::SceneRayCast(const CRay& rkRay, const SViewInfo& rkViewInfo) { - FShowFlags ShowFlags = (rkViewInfo.GameMode ? gkGameModeShowFlags : rkViewInfo.ShowFlags); - FNodeFlags NodeFlags = NodeFlagsForShowFlags(ShowFlags); + const FShowFlags ShowFlags = rkViewInfo.GameMode ? gkGameModeShowFlags : rkViewInfo.ShowFlags; + const FNodeFlags NodeFlags = NodeFlagsForShowFlags(ShowFlags); CRayCollisionTester Tester(rkRay); for (CSceneIterator It(this, NodeFlags, false); It; ++It) @@ -302,18 +305,22 @@ SRayIntersection CScene::SceneRayCast(const CRay& rkRay, const SViewInfo& rkView CSceneNode* CScene::NodeByID(uint32 NodeID) { - auto it = mNodeMap.find(NodeID); + const auto it = mNodeMap.find(NodeID); - if (it != mNodeMap.end()) return it->second; - else return nullptr; + if (it != mNodeMap.cend()) + return it->second; + + return nullptr; } CScriptNode* CScene::NodeForInstanceID(uint32 InstanceID) { - auto it = mScriptMap.find(InstanceID); + const auto it = mScriptMap.find(InstanceID); - if (it != mScriptMap.end()) return it->second; - else return nullptr; + if (it != mScriptMap.cend()) + return it->second; + + return nullptr; } CScriptNode* CScene::NodeForInstance(CScriptObject *pObj) @@ -326,23 +333,23 @@ CLightNode* CScene::NodeForLight(CLight *pLight) // Slow. Is there a better way to do this? std::vector& rLights = mNodes[ENodeType::Light]; - for (auto it = rLights.begin(); it != rLights.end(); it++) - { - CLightNode *pNode = static_cast(*it); - if (pNode->Light() == pLight) return pNode; - } + const auto iter = std::find_if(rLights.begin(), rLights.end(), + [pLight](const auto* entry) { return static_cast(entry)->Light() == pLight; }); - return nullptr; + if (iter == rLights.cend()) + return nullptr; + + return static_cast(*iter); } CModel* CScene::ActiveSkybox() { bool SkyEnabled = false; - for (uint32 iAtt = 0; iAtt < mAreaAttributesObjects.size(); iAtt++) + for (auto& rkAttributes : mAreaAttributesObjects) { - const CAreaAttributes& rkAttributes = mAreaAttributesObjects[iAtt]; - if (rkAttributes.IsSkyEnabled()) SkyEnabled = true; + if (rkAttributes.IsSkyEnabled()) + SkyEnabled = true; if (rkAttributes.IsLayerEnabled()) { @@ -350,13 +357,16 @@ CModel* CScene::ActiveSkybox() { SkyEnabled = true; CModel *pSky = rkAttributes.SkyModel(); - if (pSky) return pSky; + if (pSky != nullptr) + return pSky; } } } - if (SkyEnabled) return mpWorld->DefaultSkybox(); - else return nullptr; + if (SkyEnabled) + return mpWorld->DefaultSkybox(); + + return nullptr; } CGameArea* CScene::ActiveArea() @@ -368,21 +378,21 @@ CGameArea* CScene::ActiveArea() FShowFlags CScene::ShowFlagsForNodeFlags(FNodeFlags NodeFlags) { FShowFlags Out; - if (NodeFlags & ENodeType::Model) Out |= EShowFlag::SplitWorld; - if (NodeFlags & ENodeType::Static) Out |= EShowFlag::MergedWorld; - if (NodeFlags & ENodeType::Script) Out |= EShowFlag::Objects; - if (NodeFlags & ENodeType::Collision) Out |= EShowFlag::WorldCollision; - if (NodeFlags & ENodeType::Light) Out |= EShowFlag::Lights; + if ((NodeFlags & ENodeType::Model) != 0) Out |= EShowFlag::SplitWorld; + if ((NodeFlags & ENodeType::Static) != 0) Out |= EShowFlag::MergedWorld; + if ((NodeFlags & ENodeType::Script) != 0) Out |= EShowFlag::Objects; + if ((NodeFlags & ENodeType::Collision) != 0) Out |= EShowFlag::WorldCollision; + if ((NodeFlags & ENodeType::Light) != 0) Out |= EShowFlag::Lights; return Out; } FNodeFlags CScene::NodeFlagsForShowFlags(FShowFlags ShowFlags) { FNodeFlags Out = ENodeType::Root; - if (ShowFlags & EShowFlag::SplitWorld) Out |= ENodeType::Model; - if (ShowFlags & EShowFlag::MergedWorld) Out |= ENodeType::Static; - if (ShowFlags & EShowFlag::WorldCollision) Out |= ENodeType::Collision; - if (ShowFlags & EShowFlag::Objects) Out |= ENodeType::Script | ENodeType::ScriptExtra; - if (ShowFlags & EShowFlag::Lights) Out |= ENodeType::Light; + if ((ShowFlags & EShowFlag::SplitWorld) != 0) Out |= ENodeType::Model; + if ((ShowFlags & EShowFlag::MergedWorld) != 0) Out |= ENodeType::Static; + if ((ShowFlags & EShowFlag::WorldCollision) != 0) Out |= ENodeType::Collision; + if ((ShowFlags & EShowFlag::Objects) != 0) Out |= ENodeType::Script | ENodeType::ScriptExtra; + if ((ShowFlags & EShowFlag::Lights) != 0) Out |= ENodeType::Light; return Out; }