// Starts playing a given animation (or nothing if this animation is already playing) void CocosSpriteActorView::playAnimation(const std::string &name) { if (name != _currentAnimation) { SpriteActorView::playAnimation(name); if (_currentAnimation != "") { // Sprite wasn't created yet. Create and set parameters if (!_ccSprite) { _ccSprite = CCSprite::spriteWithSpriteFrame(((CocosAnimation *)_animations[_currentAnimation].get())->getFirstFrame()); if (_ccSprite) { setPosition(getPosition()); setAngle(getAngle()); setScale(getScale()); setZOrder(getZOrder()); setVisible(isVisible()); setAlpha(getAlpha()); } } // The sprite was created, change texture if (_ccSprite) { bool isAnimation = _animations[_currentAnimation]->getFrameCount() > 1; CCSpriteFrame *frame = ((CocosAnimation *)_animations[_currentAnimation].get())->getFirstFrame(); _ccSprite->setTexture(frame->getTexture()); CCRect frameRect = frame->getRectInPixels(); if (isAnimation) { frameRect.origin = frame->getOffsetInPixels(); } _ccSprite->setTextureRect(frameRect); _ccSprite->setDisplayFrame(frame); // If this animation contains more than one frame, play the animation if (isAnimation) { _ccSprite->runAction(((CocosAnimation *)_animations[_currentAnimation].get())->getAnimationAction()); } } } } }
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; }
// 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); } } }
void CCSpriteFrameCache::removeSpriteFramesFromTexture(CCTexture2D* texture) { vector<string> keysToRemove; m_pSpriteFrames->begin(); std::string key = ""; CCDictionary<std::string, CCObject*> *frameDict = NULL; while( (frameDict = (CCDictionary<std::string, CCObject*>*)m_pSpriteFrames->next(&key)) ) { CCSpriteFrame *frame = m_pSpriteFrames->objectForKey(key); if (frame && (frame->getTexture() == texture)) { keysToRemove.push_back(key); } } m_pSpriteFrames->end(); vector<string>::iterator iter; for (iter = keysToRemove.begin(); iter != keysToRemove.end(); ++iter) { m_pSpriteFrames->removeObjectForKey(*iter); } }
//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]; } }