void TMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flags) { updateTotalQuads(); if( flags != 0 || _dirty || _quadsDirty ) { Size s = Director::getInstance()->getWinSize(); auto rect = Rect(0, 0, s.width, s.height); Mat4 inv = transform; inv.inverse(); rect = RectApplyTransform(rect, inv); updateTiles(rect); updateIndexBuffer(); updatePrimitives(); _dirty = false; } if(_renderCommands.size() < static_cast<size_t>(_primitives.size())) { _renderCommands.resize(_primitives.size()); } _texture->prepareDraw(); int index = 0; for(const auto& iter : _primitives) { if(iter.second->getCount() > 0) { auto& cmd = _renderCommands[index++]; //修改混合模式,满足美术需求和PS一样 added by tokentong 20150327 cmd.init(iter.first, _texture->getName(), getGLProgramState(), BlendFunc::ALPHA_PREMULTIPLIED, iter.second, _modelViewTransform, flags); renderer->addCommand(&cmd); } } }
static Rect getRect(Node * pNode) { Size contentSize = pNode->getContentSize(); Rect rect = Rect(0, 0, contentSize.width, contentSize.height); return RectApplyTransform(rect, pNode->getNodeToWorldTransform()); }
void TMXLayer::updateTiles(const Rect& culledRect) { Rect visibleTiles = culledRect; Size mapTileSize = CC_SIZE_PIXELS_TO_POINTS(_mapTileSize); Size tileSize = CC_SIZE_PIXELS_TO_POINTS(_tileSet->_tileSize); Mat4 nodeToTileTransform = _tileToNodeTransform.getInversed(); //transform to tile visibleTiles = RectApplyTransform(visibleTiles, nodeToTileTransform); // tile coordinate is upside-down, so we need to make the tile coordinate use top-left for the start point. visibleTiles.origin.y += 1; // if x=0.7, width=9.5, we need to draw number 0~10 of tiles, and so is height. visibleTiles.size.width = ceil(visibleTiles.origin.x + visibleTiles.size.width) - floor(visibleTiles.origin.x); visibleTiles.size.height = ceil(visibleTiles.origin.y + visibleTiles.size.height) - floor(visibleTiles.origin.y); visibleTiles.origin.x = floor(visibleTiles.origin.x); visibleTiles.origin.y = floor(visibleTiles.origin.y); // for the bigger tiles. int tilesOverX = 0; int tilesOverY = 0; // for diagonal oriention tiles float tileSizeMax = std::max(tileSize.width, tileSize.height); if (_layerOrientation == FAST_TMX_ORIENTATION_ORTHO) { tilesOverX = ceil(tileSizeMax / mapTileSize.width) - 1; tilesOverY = ceil(tileSizeMax / mapTileSize.height) - 1; if (tilesOverX < 0) tilesOverX = 0; if (tilesOverY < 0) tilesOverY = 0; } else if(_layerOrientation == FAST_TMX_ORIENTATION_ISO) { Rect overTileRect(0, 0, tileSizeMax - mapTileSize.width, tileSizeMax - mapTileSize.height); if (overTileRect.size.width < 0) overTileRect.size.width = 0; if (overTileRect.size.height < 0) overTileRect.size.height = 0; overTileRect = RectApplyTransform(overTileRect, nodeToTileTransform); tilesOverX = ceil(overTileRect.origin.x + overTileRect.size.width) - floor(overTileRect.origin.x); tilesOverY = ceil(overTileRect.origin.y + overTileRect.size.height) - floor(overTileRect.origin.y); } else { //do nothing, do not support //CCASSERT(0, "TMX invalid value"); } _indicesVertexZNumber.clear(); for(const auto& iter : _indicesVertexZOffsets) { _indicesVertexZNumber[iter.first] = iter.second; } int yBegin = std::max(0.f,visibleTiles.origin.y - tilesOverY); int yEnd = std::min(_layerSize.height,visibleTiles.origin.y + visibleTiles.size.height + tilesOverY); int xBegin = std::max(0.f,visibleTiles.origin.x - tilesOverX); int xEnd = std::min(_layerSize.width,visibleTiles.origin.x + visibleTiles.size.width + tilesOverX); for (int y = yBegin; y < yEnd; ++y) { for (int x = xBegin; x < xEnd; ++x) { int tileIndex = getTileIndexByPos(x, y); if(_tiles[tileIndex] == 0) continue; int vertexZ = getVertexZForPos(Vec2(x,y)); auto iter = _indicesVertexZNumber.find(vertexZ); int offset = iter->second; iter->second++; int quadIndex = _tileToQuadIndex[tileIndex]; CC_ASSERT(-1 != quadIndex); (*_indices)[6 * offset + 0] = quadIndex * 4 + 0; //[CY MOD] (*_indices)[6 * offset + 1] = quadIndex * 4 + 1; //[CY MOD] (*_indices)[6 * offset + 2] = quadIndex * 4 + 2; //[CY MOD] (*_indices)[6 * offset + 3] = quadIndex * 4 + 3; //[CY MOD] (*_indices)[6 * offset + 4] = quadIndex * 4 + 2; //[CY MOD] (*_indices)[6 * offset + 5] = quadIndex * 4 + 1; //[CY MOD] } // for x } // for y for(const auto& iter : _indicesVertexZOffsets) { _indicesVertexZNumber[iter.first] -= iter.second; if(_indicesVertexZNumber[iter.first] == 0) { _indicesVertexZNumber.erase(iter.first); } } }