Rendering mechanism of cocos2dx-ui

Source: Internet
Author: User

Rendering mechanism of cocos2dx-ui

The rendering system is one of the most important modules in the game engine. How can we traverse the UI tree, render the UI properly on the screen, and select the Rendering sequence, this is what the rendering system needs to consider most. In fact, the order of traversal determines the rendering order.

The rendering function of the Cocos2d-x is implemented through Node: visit. First, let's take a look at what this function does.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35 36 38 39 void Node::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t parentFlags) { // quick return if not visible. children won't be drawn. if (!_visible) { return; } uint32_t flags = processParentFlags(parentTransform, parentFlags); bool visibleByCamera = isVisitableByVisitingCamera(); int i = 0; if(!_children.empty()) { sortAllChildren(); // draw children zOrder < 0 for( ; i < _children.size(); i++ ) { auto node = _children.at(i); if (node && node->_localZOrder < 0) node->visit(renderer, _modelViewTransform, flags); else break; } // self draw if (visibleByCamera) this->draw(renderer, _modelViewTransform, flags); for(auto it=_children.cbegin()+i; it != _children.cend(); ++it) (*it)->visit(renderer, _modelViewTransform, flags); } else if (visibleByCamera) { this->draw(renderer, _modelViewTransform, flags); } }

Then, you can analyze the code step by step. First, you can quickly remove invisible elements without drawing their child elements. The _ visible judgment is done.

Then, determine whether the child node is empty. If the child node is empty, determine whether the child node is within the visible range of the camera. Draw only within the visible range, as shown below:

1 2 3 4 else if (visibleByCamera) { this->draw(renderer, _modelViewTransform, flags); }

Then the key point is that the subnode is not empty. When the subnode is not empty, all the subnodes are sorted first. What sort is based on? Let's take a look at the sortAllChildren function.

1 2 3 4 5 6 7 8 void Node::sortAllChildren() { if (_reorderChildDirty) { std::sort(std::begin(_children), std::end(_children), nodeComparisonLess); _reorderChildDirty = false; } }

Continue to go deep into the nodeComparisonLess Function

1 2 3 4 5 6 bool nodeComparisonLess(Node* n1, Node* n2) { return( n1->getLocalZOrder() < n2->getLocalZOrder() || ( n1->getLocalZOrder() == n2->getLocalZOrder() && n1->getOrderOfArrival() < n2->getOrderOfArrival() ) ); }

It can be seen that it is sorted by LocalZOrder. When LocalZOrder is the same, it is sorted by the order added to the UI tree. Therefore, LocalZOrder is a very important element. The sorted sub-nodes are incremented Based on LocalZOrder. Next, let's take a look at how the child nodes are drawn.

1 2 3 4 5 6 7 8 9 10 // draw children zOrder < 0 for( ; i < _children.size(); i++ ) { auto node = _children.at(i); if (node && node->_localZOrder < 0) node->visit(renderer, _modelViewTransform, flags); else break; }

First Draw _ localZOrder less than 0, and then draw its own last draw greater than or equal to 0

1 2 3 4 5 6 // self draw if (visibleByCamera) this->draw(renderer, _modelViewTransform, flags); for(auto it=_children.cbegin()+i; it != _children.cend(); ++it) (*it)->visit(renderer, _modelViewTransform, flags);

The figure below is easy to understand

Therefore, the Traversal method of the UI tree is the depth-first algorithm of the Middle-order traversal. Summary:

1. traverse the left subnode

2. traverse the root node

3. traverse the child nodes on the right

This method ensures that the UI painting sequence can be adjusted through _ localZOrder. If it is less than 0, it is first drawn, and then the parent node is drawn. Then draw a chart that is greater than 0.

Therefore, the smaller the value of _ localZOrder, the element will be drawn first.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.