From dea483b6ef9e830a13970575b083155bab4cf6f0 Mon Sep 17 00:00:00 2001 From: minggo Date: Tue, 23 Dec 2014 18:07:22 +0800 Subject: [PATCH] performance improve --- cocos/2d/CCNode.cpp | 132 +++++++++++++++++++++----------------- cocos/base/CCDirector.cpp | 2 +- 2 files changed, 75 insertions(+), 59 deletions(-) diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index e756ae32ba..95f98f9127 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -457,6 +457,7 @@ void Node::setScale(float scaleX,float scaleY) #if CC_USE_PHYSICS if(g_physicsSceneCount == 0) return; + auto scene = getScene(); if (!scene || scene->getPhysicsWorld()) { @@ -477,6 +478,7 @@ void Node::setScaleX(float scaleX) #if CC_USE_PHYSICS if(g_physicsSceneCount == 0) return; + auto scene = getScene(); if (!scene || scene->getPhysicsWorld()) { @@ -524,8 +526,9 @@ void Node::setScaleY(float scaleY) _transformUpdated = _transformDirty = _inverseDirty = true; #if CC_USE_PHYSICS - if(g_physicsSceneCount == 0) + if (g_physicsSceneCount == 0) return; + auto scene = getScene(); if (!scene || scene->getPhysicsWorld()) { @@ -580,11 +583,7 @@ void Node::setPosition3D(const Vec3& position) Vec3 Node::getPosition3D() const { - Vec3 ret; - ret.x = _position.x; - ret.y = _position.y; - ret.z = _positionZ; - return ret; + return Vec3(_position.x, _position.y, _positionZ); } float Node::getPositionX() const @@ -661,7 +660,8 @@ void Node::setVisible(bool visible) if(visible != _visible) { _visible = visible; - if(_visible) _transformUpdated = _transformDirty = _inverseDirty = true; + if(_visible) + _transformUpdated = _transformDirty = _inverseDirty = true; } } @@ -686,10 +686,10 @@ void Node::setAnchorPoint(const Vec2& point) } #endif - if( ! point.equals(_anchorPoint)) + if (! point.equals(_anchorPoint)) { _anchorPoint = point; - _anchorPointInPoints = Vec2(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y ); + _anchorPointInPoints = Vec2(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y); _transformUpdated = _transformDirty = _inverseDirty = true; } } @@ -702,11 +702,11 @@ const Size& Node::getContentSize() const void Node::setContentSize(const Size & size) { - if ( ! size.equals(_contentSize)) + if (! size.equals(_contentSize)) { _contentSize = size; - _anchorPointInPoints = Vec2(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y ); + _anchorPointInPoints = Vec2(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y); _transformUpdated = _transformDirty = _inverseDirty = _contentSizeDirty = true; } } @@ -794,7 +794,8 @@ GLProgramState* Node::getGLProgramState() const void Node::setGLProgramState(cocos2d::GLProgramState *glProgramState) { - if(glProgramState != _glProgramState) { + if (glProgramState != _glProgramState) + { CC_SAFE_RELEASE(_glProgramState); _glProgramState = glProgramState; CC_SAFE_RETAIN(_glProgramState); @@ -818,7 +819,7 @@ GLProgram * Node::getGLProgram() const Scene* Node::getScene() const { - if(!_parent) + if (!_parent) return nullptr; auto sceneNode = _parent; @@ -832,7 +833,7 @@ Scene* Node::getScene() const Rect Node::getBoundingBox() const { - Rect rect = Rect(0, 0, _contentSize.width, _contentSize.height); + Rect rect(0, 0, _contentSize.width, _contentSize.height); return RectApplyAffineTransform(rect, getNodeToParentAffineTransform()); } @@ -848,7 +849,7 @@ Node* Node::getChildByTag(int tag) const { CCASSERT( tag != Node::INVALID_TAG, "Invalid tag"); - for (auto& child : _children) + for (const auto& child : _children) { if(child && child->_tag == tag) return child; @@ -1038,7 +1039,8 @@ void Node::addChildHelper(Node* child, int localZOrder, int tag, const std::stri { child->onEnter(); // prevent onEnterTransitionDidFinish to be called twice when a node is added in onEnter - if (_isTransitionFinished) { + if (_isTransitionFinished) + { child->onEnterTransitionDidFinish(); } } @@ -1151,7 +1153,7 @@ void Node::removeFromPhysicsWorld() void Node::removeAllChildrenWithCleanup(bool cleanup) { // not using detachChild improves speed here - for (auto& child : _children) + for (const auto& child : _children) { // IMPORTANT: // -1st do onExit @@ -1225,8 +1227,9 @@ void Node::reorderChild(Node *child, int zOrder) void Node::sortAllChildren() { - if( _reorderChildDirty ) { - std::sort( std::begin(_children), std::end(_children), nodeComparisonLess ); + if (_reorderChildDirty) + { + std::sort(std::begin(_children), std::end(_children), nodeComparisonLess); _reorderChildDirty = false; } } @@ -1246,16 +1249,18 @@ void Node::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags) void Node::visit() { auto renderer = Director::getInstance()->getRenderer(); - Mat4 parentTransform = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); + auto& parentTransform = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); visit(renderer, parentTransform, true); } uint32_t Node::processParentFlags(const Mat4& parentTransform, uint32_t parentFlags) { - if(_usingNormalizedPosition) { + if(_usingNormalizedPosition) + { CCASSERT(_parent, "setNormalizedPosition() doesn't work with orphan nodes"); - if ((parentFlags & FLAGS_CONTENT_SIZE_DIRTY) || _normalizedPositionDirty) { - auto s = _parent->getContentSize(); + if ((parentFlags & FLAGS_CONTENT_SIZE_DIRTY) || _normalizedPositionDirty) + { + auto& s = _parent->getContentSize(); _position.x = _normalizedPosition.x * s.width; _position.y = _normalizedPosition.y * s.height; _transformUpdated = _transformDirty = _inverseDirty = true; @@ -1313,7 +1318,7 @@ void Node::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t paren { auto node = _children.at(i); - if ( node && node->_localZOrder < 0 ) + if (node && node->_localZOrder < 0) node->visit(renderer, _modelViewTransform, flags); else break; @@ -1340,9 +1345,7 @@ void Node::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t paren Mat4 Node::transform(const Mat4& parentTransform) { - Mat4 ret = this->getNodeToParentTransform(); - ret = parentTransform * ret; - return ret; + return parentTransform * this->getNodeToParentTransform(); } // MARK: events @@ -1467,7 +1470,8 @@ void Node::setEventDispatcher(EventDispatcher* dispatcher) void Node::setActionManager(ActionManager* actionManager) { - if( actionManager != _actionManager ) { + if( actionManager != _actionManager ) + { this->stopAllActions(); CC_SAFE_RETAIN(actionManager); CC_SAFE_RELEASE(_actionManager); @@ -1521,7 +1525,8 @@ ssize_t Node::getNumberOfRunningActions() const void Node::setScheduler(Scheduler* scheduler) { - if( scheduler != _scheduler ) { + if( scheduler != _scheduler ) + { this->unscheduleAllCallbacks(); CC_SAFE_RETAIN(scheduler); CC_SAFE_RELEASE(_scheduler); @@ -1719,9 +1724,7 @@ const Mat4& Node::getNodeToParentTransform() const bool needsSkewMatrix = ( _skewX || _skewY ); - Vec2 anchorPoint; - anchorPoint.x = _anchorPointInPoints.x * _scaleX; - anchorPoint.y = _anchorPointInPoints.y * _scaleY; + Vec2 anchorPoint(_anchorPointInPoints.x * _scaleX, _anchorPointInPoints.y * _scaleY); // optimization: // inline anchor point calculation if skew is not needed @@ -1750,12 +1753,14 @@ const Mat4& Node::getNodeToParentTransform() const // FIXME: // FIX ME: Expensive operation. // FIX ME: It should be done together with the rotationZ - if(_rotationY) { + if(_rotationY) + { Mat4 rotY; Mat4::createRotationY(CC_DEGREES_TO_RADIANS(_rotationY), &rotY); _transform = _transform * rotY; } - if(_rotationX) { + if(_rotationX) + { Mat4 rotX; Mat4::createRotationX(CC_DEGREES_TO_RADIANS(_rotationX), &rotX); _transform = _transform * rotX; @@ -1818,9 +1823,12 @@ void Node::setAdditionalTransform(const AffineTransform& additionalTransform) void Node::setAdditionalTransform(Mat4* additionalTransform) { - if(additionalTransform == nullptr) { + if (additionalTransform == nullptr) + { _useAdditionalTransform = false; - } else { + } + else + { _additionalTransform = *additionalTransform; _useAdditionalTransform = true; } @@ -1831,15 +1839,15 @@ void Node::setAdditionalTransform(Mat4* additionalTransform) AffineTransform Node::getParentToNodeAffineTransform() const { AffineTransform ret; - Mat4 ret4 = getParentToNodeTransform(); - GLToCGAffine(ret4.m,&ret); + GLToCGAffine(getParentToNodeTransform().m,&ret); return ret; } const Mat4& Node::getParentToNodeTransform() const { - if ( _inverseDirty ) { + if ( _inverseDirty ) + { _inverse = _transform.getInversed(); _inverseDirty = false; } @@ -1850,7 +1858,7 @@ const Mat4& Node::getParentToNodeTransform() const AffineTransform Node::getNodeToWorldAffineTransform() const { - AffineTransform t = this->getNodeToParentAffineTransform(); + AffineTransform t(this->getNodeToParentAffineTransform()); for (Node *p = _parent; p != nullptr; p = p->getParent()) t = AffineTransformConcat(t, p->getNodeToParentAffineTransform()); @@ -1860,7 +1868,7 @@ AffineTransform Node::getNodeToWorldAffineTransform() const Mat4 Node::getNodeToWorldTransform() const { - Mat4 t = this->getNodeToParentTransform(); + Mat4 t(this->getNodeToParentTransform()); for (Node *p = _parent; p != nullptr; p = p->getParent()) { @@ -1902,27 +1910,25 @@ Vec2 Node::convertToWorldSpace(const Vec2& nodePoint) const Vec2 Node::convertToNodeSpaceAR(const Vec2& worldPoint) const { - Vec2 nodePoint = convertToNodeSpace(worldPoint); + Vec2 nodePoint(convertToNodeSpace(worldPoint)); return nodePoint - _anchorPointInPoints; } Vec2 Node::convertToWorldSpaceAR(const Vec2& nodePoint) const { - Vec2 pt = nodePoint + _anchorPointInPoints; - return convertToWorldSpace(pt); + return convertToWorldSpace(nodePoint + _anchorPointInPoints); } Vec2 Node::convertToWindowSpace(const Vec2& nodePoint) const { - Vec2 worldPoint = this->convertToWorldSpace(nodePoint); + Vec2 worldPoint(this->convertToWorldSpace(nodePoint)); return Director::getInstance()->convertToUI(worldPoint); } // convenience methods which take a Touch instead of Vec2 Vec2 Node::convertTouchToNodeSpace(Touch *touch) const { - Vec2 point = touch->getLocation(); - return this->convertToNodeSpace(point); + return this->convertToNodeSpace(touch->getLocation()); } Vec2 Node::convertTouchToNodeSpaceAR(Touch *touch) const @@ -1942,37 +1948,42 @@ void Node::updateTransform() Component* Node::getComponent(const std::string& name) { - if( _componentContainer ) + if (_componentContainer) return _componentContainer->get(name); + return nullptr; } bool Node::addComponent(Component *component) { // lazy alloc - if( !_componentContainer ) + if (!_componentContainer) _componentContainer = new (std::nothrow) ComponentContainer(this); + return _componentContainer->add(component); } bool Node::removeComponent(const std::string& name) { - if( _componentContainer ) + if (_componentContainer) return _componentContainer->remove(name); + return false; } bool Node::removeComponent(Component *component) { - if (_componentContainer) { + if (_componentContainer) + { return _componentContainer->remove(component); } + return false; } void Node::removeAllComponents() { - if( _componentContainer ) + if (_componentContainer) _componentContainer->removeAll(); } @@ -2055,7 +2066,7 @@ void Node::updatePhysicsBodyScale(Scene* scene) } } - for (auto child : _children) + for (const auto& child : _children) { child->updatePhysicsBodyScale(scene); } @@ -2159,7 +2170,8 @@ void Node::updateDisplayedOpacity(GLubyte parentOpacity) if (_cascadeOpacityEnabled) { - for(auto child : _children){ + for(const auto& child : _children) + { child->updateDisplayedOpacity(_displayedOpacity); } } @@ -2205,7 +2217,8 @@ void Node::disableCascadeOpacity() { _displayedOpacity = _realOpacity; - for(auto child : _children){ + for(const auto& child : _children) + { child->updateDisplayedOpacity(255); } } @@ -2236,7 +2249,8 @@ void Node::updateDisplayedColor(const Color3B& parentColor) if (_cascadeColorEnabled) { - for(const auto &child : _children){ + for(const auto &child : _children) + { child->updateDisplayedColor(_displayedColor); } } @@ -2279,7 +2293,8 @@ void Node::updateCascadeColor() void Node::disableCascadeColor() { - for(auto child : _children){ + for(const auto& child : _children) + { child->updateDisplayedColor(Color3B::WHITE); } } @@ -2290,7 +2305,8 @@ void Node::setCameraMask(unsigned short mask, bool applyChildren) _cameraMask = mask; if (applyChildren) { - for (auto child : _children) { + for (const auto& child : _children) + { child->setCameraMask(mask, applyChildren); } } diff --git a/cocos/base/CCDirector.cpp b/cocos/base/CCDirector.cpp index 4d5d7a9317..b24877abad 100644 --- a/cocos/base/CCDirector.cpp +++ b/cocos/base/CCDirector.cpp @@ -1114,7 +1114,7 @@ void Director::showStats() prevVerts = currentVerts; } - Mat4 identity = Mat4::IDENTITY; + const Mat4& identity = Mat4::IDENTITY; _drawnVerticesLabel->visit(_renderer, identity, 0); _drawnBatchesLabel->visit(_renderer, identity, 0); _FPSLabel->visit(_renderer, identity, 0);