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__))
#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<Scene*>(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);
}

View File

@ -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("<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)
{
if (_defaultCamera)
@ -214,6 +214,7 @@ bool Scene::initWithPhysics()
this->scheduleUpdate();
// success
g_physicsSceneCount += 1;
ret = true;
} while (0);
return ret;

View File

@ -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;