Esempio n. 1
0
// used only when parsing the map. useless after the map was parsed
// since lot's of assumptions are no longer true
Sprite * TMXLayer::appendTileForGID(int gid, const Point& pos)
{
    if (gid != 0 && (static_cast<int>((gid & kFlippedMask)) - _tileSet->_firstGid) >= 0)
    {
        Rect rect = _tileSet->rectForGID(gid);
        rect = CC_RECT_PIXELS_TO_POINTS(rect);
        
        intptr_t z = (intptr_t)(pos.x + pos.y * _layerSize.width);
        
        Sprite *tile = reusedTileWithRect(rect);
        
        setupTileSprite(tile ,pos ,gid);
        
        // optimization:
        // The difference between appendTileForGID and insertTileforGID is that append is faster, since
        // it appends the tile at the end of the texture atlas
        ssize_t indexForZ = _atlasIndexArray->num;
        
        // don't add it using the "standard" way.
        insertQuadFromSprite(tile, indexForZ);
        
        // append should be after addQuadFromSprite since it modifies the quantity values
        ccCArrayInsertValueAtIndex(_atlasIndexArray, (void*)z, indexForZ);
        
        return tile;
    }
    
    return nullptr;
}
Esempio n. 2
0
	// CCTMXLayer - adding helper methods
	CCSprite * CCTMXLayer::insertTileForGID(unsigned int gid, const CCPoint& pos)
	{
		CCRect rect = m_pTileSet->rectForGID(gid);
                rect = CCRectMake(rect.origin.x / m_fContentScaleFactor, rect.origin.y / m_fContentScaleFactor, rect.size.width/ m_fContentScaleFactor, rect.size.height/ m_fContentScaleFactor);

		int z = (int)(pos.x + pos.y * m_tLayerSize.width);

		if( ! m_pReusedTile )
		{
			m_pReusedTile = new CCSprite();
			m_pReusedTile->initWithBatchNode(this, rect);
		}
		else
		{
			m_pReusedTile->initWithBatchNode(this, rect);
		}
		m_pReusedTile->setPositionInPixels(positionAt(pos));
		m_pReusedTile->setVertexZ((float)vertexZForPos(pos));
		m_pReusedTile->setAnchorPoint(CCPointZero);
		m_pReusedTile->setOpacity(m_cOpacity);

		// get atlas index
		unsigned int indexForZ = atlasIndexForNewZ(z);

		// Optimization: add the quad without adding a child
		this->addQuadFromSprite(m_pReusedTile, indexForZ);

		// insert it into the local atlasindex array
		ccCArrayInsertValueAtIndex(m_pAtlasIndexArray, (void*)z, indexForZ);

		// update possible children
		if (m_pChildren && m_pChildren->count()>0)
		{
            CCObject* pObject = NULL;
            CCARRAY_FOREACH(m_pChildren, pObject)
            {
                CCSprite* pChild = (CCSprite*) pObject;
                if (pChild)
                {
                    unsigned int ai = pChild->getAtlasIndex();
                    if ( ai >= indexForZ )
                    {
                        pChild->setAtlasIndex(ai+1);
                    }
                }
            }
Esempio n. 3
0
// CCTMXLayer - adding helper methods
CCSprite * CCTMXLayer::insertTileForGID(unsigned int gid, const CCPoint& pos)
{
    CCRect rect = m_pTileSet->rectForGID(gid);
    rect = CC_RECT_PIXELS_TO_POINTS(rect);

    intptr_t z = (intptr_t)(pos.x + pos.y * m_tLayerSize.width);

	// if quad optimization is used
	CCSprite *tile;
	if(m_pAtlasIndexArray)
	{
		tile = reusedTileWithRect(rect);

		setupTileSprite(tile, pos, gid);

		// get atlas index
		unsigned int indexForZ = atlasIndexForNewZ(z);

		// Optimization: add the quad without adding a child
		this->insertQuadFromSprite(tile, indexForZ);

		// insert it into the local atlasindex array
		ccCArrayInsertValueAtIndex(m_pAtlasIndexArray, (void*)z, indexForZ);

		// update possible children
		if (m_pChildren && m_pChildren->count()>0)
		{
			CCObject* pObject = NULL;
			CCARRAY_FOREACH(m_pChildren, pObject)
			{
				CCSprite* pChild = (CCSprite*) pObject;
				if (pChild)
				{
					unsigned int ai = pChild->getAtlasIndex();
					if ( ai >= indexForZ )
					{
						pChild->setAtlasIndex(ai+1);
					}
				}
			}
		}
Esempio n. 4
0
// TMXLayer - adding helper methods
Sprite * TMXLayer::insertTileForGID(int gid, const Point& pos)
{
    if (gid != 0 && (static_cast<int>((gid & kFlippedMask)) - _tileSet->_firstGid) >= 0)
    {
        Rect rect = _tileSet->rectForGID(gid);
        rect = CC_RECT_PIXELS_TO_POINTS(rect);
        
        intptr_t z = (intptr_t)(pos.x + pos.y * _layerSize.width);
        
        Sprite *tile = reusedTileWithRect(rect);
        
        setupTileSprite(tile, pos, gid);
        
        // get atlas index
        ssize_t indexForZ = atlasIndexForNewZ(static_cast<int>(z));
        
        // Optimization: add the quad without adding a child
        this->insertQuadFromSprite(tile, indexForZ);
        
        // insert it into the local atlasindex array
        ccCArrayInsertValueAtIndex(_atlasIndexArray, (void*)z, indexForZ);
        
        // update possible children
        
        for(const auto &child : _children) {
            Sprite* sp = static_cast<Sprite*>(child);
            ssize_t ai = sp->getAtlasIndex();
            if ( ai >= indexForZ )
            {
                sp->setAtlasIndex(ai+1);
            }
        }
        
        _tiles[z] = gid;
        return tile;
    }
    
    return nullptr;
}
Esempio n. 5
0
// used only when parsing the map. useless after the map was parsed
// since lot's of assumptions are no longer true
Sprite * TMXLayer::appendTileForGID(uint32_t gid, const Vec2& pos)
{
    if (gid != 0 && (static_cast<int>((gid & kTMXFlippedMask)) - _tileSet->_firstGid) >= 0)
    {
        Rect rect = _tileSet->getRectForGID(gid);
        rect = CC_RECT_PIXELS_TO_POINTS(rect);

        // Z could be just an integer that gets incremented each time it is called.
        // but that wouldn't work on layers with empty tiles.
        // and it is IMPORTANT that Z returns an unique and bigger number than the previous one.
        // since _atlasIndexArray must be ordered because `bsearch` is used to find the GID for
        // a given Z. (github issue #16512)
        intptr_t z = getZForPos(pos);

        Sprite *tile = reusedTileWithRect(rect);
        
        setupTileSprite(tile ,pos ,gid);
        
        // optimization:
        // The difference between appendTileForGID and insertTileforGID is that append is faster, since
        // it appends the tile at the end of the texture atlas
        ssize_t indexForZ = _atlasIndexArray->num;
        
        // don't add it using the "standard" way.
        insertQuadFromSprite(tile, indexForZ);
        
        // append should be after addQuadFromSprite since it modifies the quantity values
        ccCArrayInsertValueAtIndex(_atlasIndexArray, (void*)z, indexForZ);

        // Validation for issue #16512
        CCASSERT(_atlasIndexArray->num == 1 ||
                 _atlasIndexArray->arr[_atlasIndexArray->num-1] > _atlasIndexArray->arr[_atlasIndexArray->num-2], "Invalid z for _atlasIndexArray");

        return tile;
    }
    
    return nullptr;
}
// TMXLayer - adding helper methods
Sprite * TMXLayer::insertTileForGID(unsigned int gid, const Point& pos)
{
    Rect rect = m_pTileSet->rectForGID(gid);
    rect = CC_RECT_PIXELS_TO_POINTS(rect);

    KDintptr z = (KDintptr)(pos.x + pos.y * m_tLayerSize.width);

    Sprite *tile = reusedTileWithRect(rect);

    setupTileSprite(tile, pos, gid);

    // get atlas index
    unsigned int indexForZ = atlasIndexForNewZ(z);

    // Optimization: add the quad without adding a child
    this->insertQuadFromSprite(tile, indexForZ);

    // insert it into the local atlasindex array
    ccCArrayInsertValueAtIndex(m_pAtlasIndexArray, (void*)z, indexForZ);

    // update possible children
    if (m_pChildren && m_pChildren->count()>0)
    {
        Object* pObject = nullptr;
        CCARRAY_FOREACH(m_pChildren, pObject)
        {
            Sprite* child = static_cast<Sprite*>(pObject);
            if (child)
            {
                unsigned int ai = child->getAtlasIndex();
                if ( ai >= indexForZ )
                {
                    child->setAtlasIndex(ai+1);
                }
            }
        }