From d3315bf84f89fa602d01d4d015894e15157eebaa Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 9 Jun 2020 22:55:23 -0400 Subject: [PATCH 1/4] CSceneNode: Make use of in-class initializers Same behavior, less code. --- src/Core/Scene/CSceneNode.cpp | 16 ++-------------- src/Core/Scene/CSceneNode.h | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/Core/Scene/CSceneNode.cpp b/src/Core/Scene/CSceneNode.cpp index d67c5197..4260a5ef 100644 --- a/src/Core/Scene/CSceneNode.cpp +++ b/src/Core/Scene/CSceneNode.cpp @@ -13,21 +13,9 @@ uint32 CSceneNode::smNumNodes = 0; CColor CSceneNode::skSelectionTint = CColor::Integral(39, 154, 167); CSceneNode::CSceneNode(CScene *pScene, uint32 NodeID, CSceneNode *pParent) - : mpScene(pScene) + : _mID(NodeID) , mpParent(pParent) - , _mID(NodeID) - , mPosition(CVector3f::skZero) - , mRotation(CQuaternion::skIdentity) - , mScale(CVector3f::skOne) - , _mTransformDirty(true) - , _mInheritsPosition(true) - , _mInheritsRotation(true) - , _mInheritsScale(true) - , mLightLayerIndex(0) - , mLightCount(0) - , mMouseHovering(false) - , mSelected(false) - , mVisible(true) + , mpScene(pScene) { smNumNodes++; diff --git a/src/Core/Scene/CSceneNode.h b/src/Core/Scene/CSceneNode.h index 802b82d9..8c062a8b 100644 --- a/src/Core/Scene/CSceneNode.h +++ b/src/Core/Scene/CSceneNode.h @@ -71,11 +71,11 @@ class CSceneNode : public IRenderable private: mutable CTransform4f _mCachedTransform; mutable CAABox _mCachedAABox; - mutable bool _mTransformDirty; + mutable bool _mTransformDirty = true; - bool _mInheritsPosition; - bool _mInheritsRotation; - bool _mInheritsScale; + bool _mInheritsPosition = true; + bool _mInheritsRotation = true; + bool _mInheritsScale = true; uint32 _mID; @@ -85,18 +85,18 @@ protected: CSceneNode *mpParent; CScene *mpScene; - CVector3f mPosition; - CQuaternion mRotation; - CVector3f mScale; + CVector3f mPosition{CVector3f::skZero}; + CQuaternion mRotation{CQuaternion::skIdentity}; + CVector3f mScale{CVector3f::skOne}; CAABox mLocalAABox; - bool mMouseHovering; - bool mSelected; - bool mVisible; + bool mMouseHovering = false; + bool mSelected = false; + bool mVisible = true; std::list mChildren; - uint32 mLightLayerIndex; - uint32 mLightCount; + uint32 mLightLayerIndex = 0; + uint32 mLightCount = 0; CLight* mLights[8]; CColor mAmbientColor; From 7065005c30393fbf362c86aa3f561274ca5321bc Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 9 Jun 2020 22:56:36 -0400 Subject: [PATCH 2/4] CSceneNode: Make use of override Same behavior, stronger enforcement of API. --- src/Core/Scene/CSceneNode.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Core/Scene/CSceneNode.h b/src/Core/Scene/CSceneNode.h index 8c062a8b..1aa487e6 100644 --- a/src/Core/Scene/CSceneNode.h +++ b/src/Core/Scene/CSceneNode.h @@ -102,12 +102,12 @@ protected: public: explicit CSceneNode(CScene *pScene, uint32 NodeID, CSceneNode *pParent = 0); - virtual ~CSceneNode(); + ~CSceneNode() override; virtual ENodeType NodeType() = 0; virtual void PostLoad() {} virtual void OnTransformed() {} - virtual void AddToRenderer(CRenderer* /*pRenderer*/, const SViewInfo& /*rkViewInfo*/) {} - virtual void DrawSelection(); + void AddToRenderer(CRenderer* /*pRenderer*/, const SViewInfo& /*rkViewInfo*/) override {} + void DrawSelection() override; virtual void RayAABoxIntersectTest(CRayCollisionTester& rTester, const SViewInfo& rkViewInfo); virtual SRayIntersection RayNodeIntersectTest(const CRay& rkRay, uint32 AssetID, const SViewInfo& rkViewInfo) = 0; virtual bool AllowsTranslate() const { return true; } @@ -179,7 +179,7 @@ public: void SetVisible(bool Visible) { mVisible = Visible; } // Static - inline static int NumNodes() { return smNumNodes; } + static int NumNodes() { return smNumNodes; } static CColor skSelectionTint; }; From 3d4f2d60eb12206b2f736f4c4acb5aef995f12ab Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 9 Jun 2020 22:57:42 -0400 Subject: [PATCH 3/4] CSceneNode: Make use of std::array Same behavior, stronger typing. --- src/Core/Scene/CSceneNode.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Core/Scene/CSceneNode.h b/src/Core/Scene/CSceneNode.h index 1aa487e6..eae48c65 100644 --- a/src/Core/Scene/CSceneNode.h +++ b/src/Core/Scene/CSceneNode.h @@ -15,6 +15,7 @@ #include #include #include +#include class CRenderer; class CScene; @@ -97,7 +98,7 @@ protected: uint32 mLightLayerIndex = 0; uint32 mLightCount = 0; - CLight* mLights[8]; + std::array mLights{}; CColor mAmbientColor; public: From c285207f24ef3e818c5b2f0f414b37306fb56305 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 9 Jun 2020 23:07:45 -0400 Subject: [PATCH 4/4] CSceneNode: Collapse loops into ranged for loops Where applicable, we can simplify some loops a little bit, migrating off the slightly more verbose iterator means of traversing standard containers. --- src/Core/Scene/CSceneNode.cpp | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Core/Scene/CSceneNode.cpp b/src/Core/Scene/CSceneNode.cpp index 4260a5ef..132c8f8c 100644 --- a/src/Core/Scene/CSceneNode.cpp +++ b/src/Core/Scene/CSceneNode.cpp @@ -26,8 +26,7 @@ CSceneNode::CSceneNode(CScene *pScene, uint32 NodeID, CSceneNode *pParent) CSceneNode::~CSceneNode() { smNumNodes--; - for (auto it = mChildren.begin(); it != mChildren.end(); it++) - delete (*it); + DeleteChildren(); } // ************ VIRTUAL ************ @@ -69,8 +68,8 @@ void CSceneNode::OnLoadFinished() { PostLoad(); - for (auto it = mChildren.begin(); it != mChildren.end(); it++) - (*it)->OnLoadFinished(); + for (auto* child : mChildren) + child->OnLoadFinished(); } void CSceneNode::Unparent() @@ -85,7 +84,7 @@ void CSceneNode::Unparent() void CSceneNode::RemoveChild(CSceneNode *pChild) { - for (auto it = mChildren.begin(); it != mChildren.end(); it++) + for (auto it = mChildren.begin(); it != mChildren.end(); ++it) { if (*it == pChild) { @@ -97,8 +96,8 @@ void CSceneNode::RemoveChild(CSceneNode *pChild) void CSceneNode::DeleteChildren() { - for (auto it = mChildren.begin(); it != mChildren.end(); it++) - delete *it; + for (auto* child : mChildren) + delete child; mChildren.clear(); } @@ -317,8 +316,8 @@ void CSceneNode::ForceRecalculateTransform() const // If so, the children will already be marked if (!_mTransformDirty) { - for (auto it = mChildren.begin(); it != mChildren.end(); it++) - (*it)->MarkTransformChanged(); + for (auto* child : mChildren) + child->MarkTransformChanged(); } _mTransformDirty = false; } @@ -327,8 +326,8 @@ void CSceneNode::MarkTransformChanged() const { if (!_mTransformDirty) { - for (auto it = mChildren.begin(); it != mChildren.end(); it++) - (*it)->MarkTransformChanged(); + for (auto* child : mChildren) + child->MarkTransformChanged(); } _mTransformDirty = true; @@ -355,7 +354,7 @@ CVector3f CSceneNode::AbsolutePosition() const { CVector3f ret = mPosition; - if ((mpParent) && (InheritsPosition())) + if (mpParent != nullptr && InheritsPosition()) ret += mpParent->AbsolutePosition(); return ret; @@ -365,7 +364,7 @@ CQuaternion CSceneNode::AbsoluteRotation() const { CQuaternion ret = mRotation; - if ((mpParent) && (InheritsRotation())) + if (mpParent != nullptr && InheritsRotation()) ret *= mpParent->AbsoluteRotation(); return ret; @@ -375,7 +374,7 @@ CVector3f CSceneNode::AbsoluteScale() const { CVector3f ret = mScale; - if ((mpParent) && (InheritsScale())) + if (mpParent != nullptr && InheritsScale()) ret *= mpParent->AbsoluteScale(); return ret;