Reducing unnecessary function call

This commit is contained in:
Dhilan007 2014-11-24 15:27:20 +08:00
parent ac0fb68989
commit 4bc4f43b28
3 changed files with 53 additions and 23 deletions

View File

@ -60,6 +60,8 @@ THE SOFTWARE.
#define RENDER_IN_SUBPIXEL(__ARGS__) (ceil(__ARGS__)) #define RENDER_IN_SUBPIXEL(__ARGS__) (ceil(__ARGS__))
#endif #endif
extern int g_physicsSceneCount;
NS_CC_BEGIN NS_CC_BEGIN
bool nodeComparisonLess(Node* n1, Node* n2) bool nodeComparisonLess(Node* n1, Node* n2)
@ -425,7 +427,13 @@ void Node::setScale(float scale)
_transformUpdated = _transformDirty = _inverseDirty = true; _transformUpdated = _transformDirty = _inverseDirty = true;
#if CC_USE_PHYSICS #if CC_USE_PHYSICS
updatePhysicsBodyTransform(getScene()); if(g_physicsSceneCount == 0)
return;
auto scene = getScene();
if (!scene || scene->getPhysicsWorld())
{
updatePhysicsBodyTransform(scene);
}
#endif #endif
} }
@ -446,7 +454,13 @@ void Node::setScale(float scaleX,float scaleY)
_transformUpdated = _transformDirty = _inverseDirty = true; _transformUpdated = _transformDirty = _inverseDirty = true;
#if CC_USE_PHYSICS #if CC_USE_PHYSICS
updatePhysicsBodyTransform(getScene()); if(g_physicsSceneCount == 0)
return;
auto scene = getScene();
if (!scene || scene->getPhysicsWorld())
{
updatePhysicsBodyTransform(scene);
}
#endif #endif
} }
@ -460,7 +474,13 @@ void Node::setScaleX(float scaleX)
_transformUpdated = _transformDirty = _inverseDirty = true; _transformUpdated = _transformDirty = _inverseDirty = true;
#if CC_USE_PHYSICS #if CC_USE_PHYSICS
updatePhysicsBodyTransform(getScene()); if(g_physicsSceneCount == 0)
return;
auto scene = getScene();
if (!scene || scene->getPhysicsWorld())
{
updatePhysicsBodyTransform(scene);
}
#endif #endif
} }
@ -503,7 +523,13 @@ void Node::setScaleY(float scaleY)
_transformUpdated = _transformDirty = _inverseDirty = true; _transformUpdated = _transformDirty = _inverseDirty = true;
#if CC_USE_PHYSICS #if CC_USE_PHYSICS
updatePhysicsBodyTransform(getScene()); if(g_physicsSceneCount == 0)
return;
auto scene = getScene();
if (!scene || scene->getPhysicsWorld())
{
updatePhysicsBodyTransform(scene);
}
#endif #endif
} }
@ -794,7 +820,13 @@ Scene* Node::getScene() const
if(!_parent) if(!_parent)
return nullptr; return nullptr;
return _parent->getScene(); auto sceneNode = _parent;
while (sceneNode->_parent)
{
sceneNode = sceneNode->_parent;
}
return dynamic_cast<Scene*>(sceneNode);
} }
Rect Node::getBoundingBox() const Rect Node::getBoundingBox() const
@ -993,8 +1025,8 @@ void Node::addChildHelper(Node* child, int localZOrder, int tag, const std::stri
#if CC_USE_PHYSICS #if CC_USE_PHYSICS
// Recursive add children with which have physics body. // Recursive add children with which have physics body.
Scene* scene = this->getScene(); auto scene = this->getScene();
if (scene != nullptr && scene->getPhysicsWorld() != nullptr) if (scene && scene->getPhysicsWorld())
{ {
child->updatePhysicsBodyTransform(scene); child->updatePhysicsBodyTransform(scene);
scene->addChildToPhysicsWorld(child); scene->addChildToPhysicsWorld(child);
@ -1958,14 +1990,14 @@ void Node::updatePhysicsBodyPosition(Scene* scene)
{ {
if (_physicsBody != nullptr) 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); _physicsBody->setPosition(pos);
} }
else else
{ {
_physicsBody->setPosition(getPosition()); _physicsBody->setPosition(_position);
} }
} }
@ -1982,7 +2014,7 @@ void Node::updatePhysicsBodyRotation(Scene* scene)
if (scene != nullptr && scene->getPhysicsWorld() != nullptr) if (scene != nullptr && scene->getPhysicsWorld() != nullptr)
{ {
float rotation = _rotationZ_X; 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(); rotation += parent->getRotation();
} }
@ -2009,10 +2041,10 @@ void Node::updatePhysicsBodyScale(Scene* scene)
{ {
float scaleX = _scaleX / _physicsScaleStartX; float scaleX = _scaleX / _physicsScaleStartX;
float scaleY = _scaleY / _physicsScaleStartY; 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(); scaleX *= parent->_scaleX;
scaleY *= parent->getScaleY(); scaleY *= parent->_scaleY;
} }
_physicsBody->setScale(scaleX, scaleY); _physicsBody->setScale(scaleX, scaleY);
} }

View File

@ -37,6 +37,8 @@ THE SOFTWARE.
#include "physics/CCPhysicsWorld.h" #include "physics/CCPhysicsWorld.h"
#endif #endif
extern int g_physicsSceneCount = 0;
NS_CC_BEGIN NS_CC_BEGIN
Scene::Scene() Scene::Scene()
@ -58,6 +60,10 @@ Scene::Scene()
Scene::~Scene() Scene::~Scene()
{ {
#if CC_USE_PHYSICS #if CC_USE_PHYSICS
if (_physicsWorld)
{
g_physicsSceneCount--;
}
CC_SAFE_DELETE(_physicsWorld); CC_SAFE_DELETE(_physicsWorld);
#endif #endif
Director::getInstance()->getEventDispatcher()->removeEventListener(_event); Director::getInstance()->getEventDispatcher()->removeEventListener(_event);
@ -111,12 +117,6 @@ std::string Scene::getDescription() const
return StringUtils::format("<Scene | tag = %d>", _tag); return StringUtils::format("<Scene | tag = %d>", _tag);
} }
Scene* Scene::getScene() const
{
// FIX ME: should use const_case<> to fix compiling error
return const_cast<Scene*>(this);
}
void Scene::onProjectionChanged(EventCustom* event) void Scene::onProjectionChanged(EventCustom* event)
{ {
if (_defaultCamera) if (_defaultCamera)
@ -214,6 +214,7 @@ bool Scene::initWithPhysics()
this->scheduleUpdate(); this->scheduleUpdate();
// success // success
g_physicsSceneCount += 1;
ret = true; ret = true;
} while (0); } while (0);
return ret; return ret;

View File

@ -67,9 +67,6 @@ public:
/** creates a new Scene object with a predefined Size */ /** creates a new Scene object with a predefined Size */
static Scene *createWithSize(const Size& size); static Scene *createWithSize(const Size& size);
// Overrides
virtual Scene *getScene() const override;
using Node::addChild; using Node::addChild;
virtual std::string getDescription() const override; virtual std::string getDescription() const override;