From 71f7e241fae718e6733fdb7fe2be069d8a945559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 17 Aug 2012 21:55:03 +0200 Subject: [PATCH] SceneGraph: removed fugly hack for detecting scene. Previously the scene was its own parent, which was good sometimes back then, but it became so ugly and complicated hack ("my friend has access to my privates"-style) that it needed to be replaced with something less ugly. Now there is an function isScene() which is overloaded in Scene. Yes, it's possible to overload it anywhere else, but you better shouldn't even think about it. --- src/SceneGraph/Object.cpp | 15 ++++++++------- src/SceneGraph/Object.h | 8 ++++---- src/SceneGraph/Scene.h | 3 +-- src/SceneGraph/Test/SceneTest.cpp | 5 ++--- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/SceneGraph/Object.cpp b/src/SceneGraph/Object.cpp index f74602bb1..d0b56a1f6 100644 --- a/src/SceneGraph/Object.cpp +++ b/src/SceneGraph/Object.cpp @@ -27,14 +27,14 @@ namespace Magnum { namespace SceneGraph { template ObjectType* Object::setParent(ObjectType* parent) { /* Skip if nothing to do or this is scene */ - if(_parent == parent || _parent == this) return static_cast(this); + if(_parent == parent || isScene()) return static_cast(this); /* Add the object to children list of new parent */ if(parent != nullptr) { /* Only Fry can be his own grandfather */ ObjectType* p = parent; - while(p != nullptr && p->parent() != p) { + while(p != nullptr) { if(p == this) return static_cast(this); p = p->parent(); } @@ -64,7 +64,7 @@ templatetransformation()*t; /* We got to the scene, multiply with camera matrix */ - if(p->parent() == p) { + if(p->isScene()) { if(camera) { CORRADE_ASSERT(camera->scene() == scene(), "Object::absoluteTransformation(): the camera is not part of the same scene as object!", t); t = camera->cameraMatrix()*t; @@ -95,7 +95,7 @@ templateparent() == p) return static_cast(p); + if(p->isScene()) return static_cast(p); p = p->parent(); } @@ -103,7 +103,8 @@ template ObjectType* Object::setTransformation(const MatrixType& transformation) { - if(_parent == this) return static_cast(this); + /* Setting transformation is forbidden for the scene */ + if(isScene()) return static_cast(this); _transformation = transformation; setDirty(); @@ -131,8 +132,8 @@ templateparent() == nullptr || p->parent() == p || !p->parent()->isDirty()) + /* Stop on root object / clean object */ + if(p->parent() == nullptr || !p->parent()->isDirty()) break; p = p->parent(); diff --git a/src/SceneGraph/Object.h b/src/SceneGraph/Object.h index 691baf688..6a1ccd02d 100644 --- a/src/SceneGraph/Object.h +++ b/src/SceneGraph/Object.h @@ -27,8 +27,6 @@ namespace Magnum { namespace SceneGraph { -template class Scene; - /** @todo User-specified Object implementation: - for front-to-back sorting, LoD changes etc. @@ -53,8 +51,6 @@ template& operator=(Object&& other) = delete; #endif - friend class Scene; - public: /** * @brief Constructor @@ -76,6 +72,9 @@ template class Scene; typedef Scene Scene2D; typedef Scene Scene3D; diff --git a/src/SceneGraph/Scene.h b/src/SceneGraph/Scene.h index 73e3793f0..58cc94fd1 100644 --- a/src/SceneGraph/Scene.h +++ b/src/SceneGraph/Scene.h @@ -26,8 +26,7 @@ namespace Magnum { namespace SceneGraph { /** @brief %Scene */ template class SCENEGRAPH_EXPORT Scene: public ObjectType { public: - /** @brief Constructor */ - inline Scene() { this->_parent = this; } + inline bool isScene() const { return true; } #ifndef DOXYGEN_GENERATING_OUTPUT void setParent(ObjectType* parent) = delete; diff --git a/src/SceneGraph/Test/SceneTest.cpp b/src/SceneGraph/Test/SceneTest.cpp index e2df35aa1..a3f859aa6 100644 --- a/src/SceneGraph/Test/SceneTest.cpp +++ b/src/SceneGraph/Test/SceneTest.cpp @@ -36,14 +36,13 @@ void SceneTest::transformation() { void SceneTest::parent() { Scene3D scene; - - CORRADE_VERIFY(scene.parent() == &scene); + CORRADE_VERIFY(scene.isScene()); /* Scene parent cannot be changed */ Object3D* scenePointer = &scene; Object3D object; scenePointer->setParent(&object); - CORRADE_VERIFY(scene.parent() == &scene); + CORRADE_VERIFY(scene.parent() == nullptr); CORRADE_VERIFY(scene.children().empty()); CORRADE_VERIFY(object.children().empty()); }