From 4bc4f43b281444ac10259e7c913c2253d2fabbe3 Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Mon, 24 Nov 2014 15:27:20 +0800 Subject: [PATCH] Reducing unnecessary function call --- cocos/2d/CCNode.cpp | 60 +++++++++++++++++++++++++++++++++----------- cocos/2d/CCScene.cpp | 13 +++++----- cocos/2d/CCScene.h | 3 --- 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 27597bb248..dbf4ec2789 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -60,6 +60,8 @@ THE SOFTWARE. #define RENDER_IN_SUBPIXEL(__ARGS__) (ceil(__ARGS__)) #endif +extern int g_physicsSceneCount; + NS_CC_BEGIN bool nodeComparisonLess(Node* n1, Node* n2) @@ -425,7 +427,13 @@ void Node::setScale(float scale) _transformUpdated = _transformDirty = _inverseDirty = true; #if CC_USE_PHYSICS - updatePhysicsBodyTransform(getScene()); + if(g_physicsSceneCount == 0) + return; + auto scene = getScene(); + if (!scene || scene->getPhysicsWorld()) + { + updatePhysicsBodyTransform(scene); + } #endif } @@ -446,7 +454,13 @@ void Node::setScale(float scaleX,float scaleY) _transformUpdated = _transformDirty = _inverseDirty = true; #if CC_USE_PHYSICS - updatePhysicsBodyTransform(getScene()); + if(g_physicsSceneCount == 0) + return; + auto scene = getScene(); + if (!scene || scene->getPhysicsWorld()) + { + updatePhysicsBodyTransform(scene); + } #endif } @@ -460,7 +474,13 @@ void Node::setScaleX(float scaleX) _transformUpdated = _transformDirty = _inverseDirty = true; #if CC_USE_PHYSICS - updatePhysicsBodyTransform(getScene()); + if(g_physicsSceneCount == 0) + return; + auto scene = getScene(); + if (!scene || scene->getPhysicsWorld()) + { + updatePhysicsBodyTransform(scene); + } #endif } @@ -503,7 +523,13 @@ void Node::setScaleY(float scaleY) _transformUpdated = _transformDirty = _inverseDirty = true; #if CC_USE_PHYSICS - updatePhysicsBodyTransform(getScene()); + if(g_physicsSceneCount == 0) + return; + auto scene = getScene(); + if (!scene || scene->getPhysicsWorld()) + { + updatePhysicsBodyTransform(scene); + } #endif } @@ -794,7 +820,13 @@ Scene* Node::getScene() const if(!_parent) return nullptr; - return _parent->getScene(); + auto sceneNode = _parent; + while (sceneNode->_parent) + { + sceneNode = sceneNode->_parent; + } + + return dynamic_cast(sceneNode); } Rect Node::getBoundingBox() const @@ -993,8 +1025,8 @@ void Node::addChildHelper(Node* child, int localZOrder, int tag, const std::stri #if CC_USE_PHYSICS // Recursive add children with which have physics body. - Scene* scene = this->getScene(); - if (scene != nullptr && scene->getPhysicsWorld() != nullptr) + auto scene = this->getScene(); + if (scene && scene->getPhysicsWorld()) { child->updatePhysicsBodyTransform(scene); scene->addChildToPhysicsWorld(child); @@ -1958,14 +1990,14 @@ void Node::updatePhysicsBodyPosition(Scene* scene) { if (_physicsBody != nullptr) { - if (scene != nullptr && scene->getPhysicsWorld() != nullptr) + if (scene && scene->getPhysicsWorld()) { - Vec2 pos = getParent() == scene ? getPosition() : scene->convertToNodeSpace(_parent->convertToWorldSpace(getPosition())); + Vec2 pos = _parent == scene ? _position : scene->convertToNodeSpace(_parent->convertToWorldSpace(_position)); _physicsBody->setPosition(pos); } else { - _physicsBody->setPosition(getPosition()); + _physicsBody->setPosition(_position); } } @@ -1982,7 +2014,7 @@ void Node::updatePhysicsBodyRotation(Scene* scene) if (scene != nullptr && scene->getPhysicsWorld() != nullptr) { float rotation = _rotationZ_X; - for (Node* parent = _parent; parent != scene; parent = parent->getParent()) + for (Node* parent = _parent; parent != scene; parent = parent->_parent) { rotation += parent->getRotation(); } @@ -2009,10 +2041,10 @@ void Node::updatePhysicsBodyScale(Scene* scene) { float scaleX = _scaleX / _physicsScaleStartX; float scaleY = _scaleY / _physicsScaleStartY; - for (Node* parent = _parent; parent != scene; parent = parent->getParent()) + for (Node* parent = _parent; parent != scene; parent = parent->_parent) { - scaleX *= parent->getScaleX(); - scaleY *= parent->getScaleY(); + scaleX *= parent->_scaleX; + scaleY *= parent->_scaleY; } _physicsBody->setScale(scaleX, scaleY); } diff --git a/cocos/2d/CCScene.cpp b/cocos/2d/CCScene.cpp index 297ca6119d..927ae8b62f 100644 --- a/cocos/2d/CCScene.cpp +++ b/cocos/2d/CCScene.cpp @@ -37,6 +37,8 @@ THE SOFTWARE. #include "physics/CCPhysicsWorld.h" #endif +extern int g_physicsSceneCount = 0; + NS_CC_BEGIN Scene::Scene() @@ -58,6 +60,10 @@ Scene::Scene() Scene::~Scene() { #if CC_USE_PHYSICS + if (_physicsWorld) + { + g_physicsSceneCount--; + } CC_SAFE_DELETE(_physicsWorld); #endif Director::getInstance()->getEventDispatcher()->removeEventListener(_event); @@ -111,12 +117,6 @@ std::string Scene::getDescription() const return StringUtils::format("", _tag); } -Scene* Scene::getScene() const -{ - // FIX ME: should use const_case<> to fix compiling error - return const_cast(this); -} - void Scene::onProjectionChanged(EventCustom* event) { if (_defaultCamera) @@ -214,6 +214,7 @@ bool Scene::initWithPhysics() this->scheduleUpdate(); // success + g_physicsSceneCount += 1; ret = true; } while (0); return ret; diff --git a/cocos/2d/CCScene.h b/cocos/2d/CCScene.h index 4fb66846f4..058dd051b0 100644 --- a/cocos/2d/CCScene.h +++ b/cocos/2d/CCScene.h @@ -67,9 +67,6 @@ public: /** creates a new Scene object with a predefined Size */ static Scene *createWithSize(const Size& size); - // Overrides - virtual Scene *getScene() const override; - using Node::addChild; virtual std::string getDescription() const override;