CCTexture2D* getTexture(std::string theImageFullPath, CCRect& theTextureRect){
	// try to load from sprite sheet
	std::string anImageFileName;
	int aLastSlashIndex = MAX((int)theImageFullPath.find_last_of('/'), (int)theImageFullPath.find_last_of('\\'));
	if (aLastSlashIndex != std::string::npos) {
		anImageFileName = theImageFullPath.substr(aLastSlashIndex + 1);
	} else {
		anImageFileName = theImageFullPath;
	}
	CCSpriteFrame *aSpriteFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(anImageFileName.c_str());
	if (aSpriteFrame) {
		theTextureRect = aSpriteFrame->getRect();
		return aSpriteFrame->getTexture();
	}
	
	CCTexture2D* aTexture = CCTextureCache::sharedTextureCache()->addImage(theImageFullPath.c_str());
	theTextureRect.origin = CCPointZero;
	theTextureRect.size = aTexture->getContentSize();
	return aTexture;
}
Example #2
0
 // Sets the tile's texture according to the given ID. If this is the first time the tile is set, also create the cocos sprite
 void CocosTile::setID(std::string tileID)
 {
     Tile::setID(tileID);
     
     // Load texture according to given ID
     CCSpriteFrame *frame = loadSpriteFrame(_tileName + "_" + tileID + "." + _tileExt);
     if (frame)
     {
         // Set texture to sprite
         if (_sprite)
         {
             _sprite->setTexture(frame->getTexture());
             _sprite->setTextureRect(frame->getRect());
         }
         else
         {
             // Sprite not created yet, create it using this texture
             _sprite = CCSprite::spriteWithSpriteFrame(frame);
         }
     }
 }
bool CScale9SpriteObject::createScale9Sprite(const string& name)
{
    string _name = name;
    string::size_type pos = name.find(".png", 0);
    if (pos != string::npos)
    {
        _name = name.substr(0, pos);
    }
    
    CCSpriteFrame *pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(_name.c_str());
    if (NULL == pFrame)
    {
        return false;
    }
//    OrigBarSize_ = pFrame->getRect().size;
    m_pSprite = CCScale9Sprite::createWithSpriteFrameName(_name.c_str(), pFrame->getRect());
    if (NULL == m_pSprite)
    {
        return false;
    }
    
    return true;
}
//the frames should have the same size in texture so shouldn't trim the frame
//I wish this limit will be removed in next moment
void CCParticleSystemFrameQuad::setTextureWithFrames(CCTexture2D* texture, CCArray* frames)
{
    // Only update the texture if is different from the current one
    if( !m_pTexture || texture->getName() != m_pTexture->getName() )
    {
        CCParticleSystem::setTexture(texture);
    }

	clearFrameSetting();
	unsigned int totalFrames = frames->count();
	m_pFrameQuads = new float *[totalFrames];
	for(unsigned int i=0; i<totalFrames;i++){
		CCSpriteFrame *frame = dynamic_cast<CCSpriteFrame*>(frames->objectAtIndex(i));
		if(frame==NULL || frame->getTexture()->getName()!= m_pTexture->getName())
			continue;
		
		CCRect rect = frame->getRect();

		float* m_sQuad = new float[4];
		m_pFrameQuads[m_uTotalFrames] = m_sQuad;
		m_uTotalFrames++;

		float atlasWidth = (float)m_pTexture->getPixelsWide();
		float atlasHeight = (float)m_pTexture->getPixelsHigh();

		float left, right, top, bottom;

		if (frame->isRotated())
		{
	#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
			left    = (2*rect.origin.x+1)/(2*atlasWidth);
			right    = left+(rect.size.height*2-2)/(2*atlasWidth);
			top        = (2*rect.origin.y+1)/(2*atlasHeight);
			bottom    = top+(rect.size.width*2-2)/(2*atlasHeight);
	#else
			left    = rect.origin.x/atlasWidth;
			right    = (rect.origin.x+rect.size.height) / atlasWidth;
			top        = rect.origin.y/atlasHeight;
			bottom    = (rect.origin.y+rect.size.width) / atlasHeight;
	#endif // CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
		}
		else
		{
	#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
			left    = (2*rect.origin.x+1)/(2*atlasWidth);
			right    = left + (rect.size.width*2-2)/(2*atlasWidth);
			top        = (2*rect.origin.y+1)/(2*atlasHeight);
			bottom    = top + (rect.size.height*2-2)/(2*atlasHeight);
	#else
			left    = rect.origin.x/atlasWidth;
			right    = (rect.origin.x + rect.size.width) / atlasWidth;
			top        = rect.origin.y/atlasHeight;
			bottom    = (rect.origin.y + rect.size.height) / atlasHeight;
	#endif // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
		}
		m_sQuad[0] = left;
		m_sQuad[1] = right;
		m_sQuad[2] = top;
		m_sQuad[3] = bottom;
	}

    GLfloat wide = (GLfloat)m_pTexture->getPixelsWide();
    GLfloat high = (GLfloat)m_pTexture->getPixelsHigh();

    ccV3F_C4B_T2F_Quad *quads = NULL;
    unsigned int start = 0, end = 0;
    if (m_pBatchNode)
    {
        quads = m_pBatchNode->getTextureAtlas()->getQuads();
        start = m_uAtlasIndex;
        end = m_uAtlasIndex + m_uTotalParticles;
    }
    else
    {
        quads = m_pQuads;
        start = 0;
        end = m_uTotalParticles;
    }

    for(unsigned int i=start; i<end; i++) 
    {
		int fid = rand()%m_uTotalFrames;
		float* m_sQuad = m_pFrameQuads[fid];
        // bottom-left vertex:
        quads[i].bl.texCoords.u = m_sQuad[0];
        quads[i].bl.texCoords.v = m_sQuad[3];
        // bottom-right vertex:
        quads[i].br.texCoords.u = m_sQuad[1];
        quads[i].br.texCoords.v = m_sQuad[3];
        // top-left vertex:
        quads[i].tl.texCoords.u = m_sQuad[0];
        quads[i].tl.texCoords.v = m_sQuad[2];
        // top-right vertex:
        quads[i].tr.texCoords.u = m_sQuad[1];
        quads[i].tr.texCoords.v = m_sQuad[2];
    }
}