CCSpriteFrameCache精灵帧缓存分析(2)

来源:互联网 发布:织梦cms使用教程 编辑:程序博客网 时间:2024/06/06 09:48
CCSpriteFrameCache精灵帧缓存分析(2):CCSpriteFrameCache其他方法:1、根据plist和纹理图片名加载    /** Adds multiple Sprite Frames from a plist file. The texture will be associated with the created sprite frames.     @since v0.99.5     @js addSpriteFrames     */    void addSpriteFramesWithFile(const char* plist, const char* textureFileName);   2、// Purges the dictionary of loaded sprite frames.// 移除所有的精灵帧缓存void CCSpriteFrameCache::removeSpriteFrames(void)3、     /** Removes unused sprite frames.     * Sprite Frames that have a retain count of 1 will be deleted.     * It is convenient to call this method after when starting a new Scene.     */     //移除不使用的精灵帧缓存,什么不使用的精灵帧,就是引用计数为1,因为     //我们加入到精灵帧缓存中的精灵帧的应用计数为1,如果被其他使用,     //则引用计数就会增加,使用完后,引用计数就会减1,所以如果引用计数为1,就表明     //此时没有被其他东西引用。    void removeUnusedSpriteFrames(void);    void CCSpriteFrameCache::removeUnusedSpriteFrames(void){    bool bRemoved = false;    CCDictElement* pElement = NULL;    CCDICT_FOREACH(m_pSpriteFrames, pElement)    {        CCSpriteFrame* spriteFrame = (CCSpriteFrame*)pElement->getObject();        if( spriteFrame->retainCount() == 1 )         {            CCLOG("cocos2d: CCSpriteFrameCache: removing unused frame: %s", pElement->getStrKey());            m_pSpriteFrames->removeObjectForElememt(pElement);            bRemoved = true;        }    }    //如果有一个精灵帧被移除,那么因为我们不知道被移除的精灵帧属于那个.plist文件,所以    //需要把所有缓存的.plist文件清空。但是精灵帧缓存还是存在的。    // XXX. Since we don't know the .plist file that originated the frame, we must remove all .plist from the cache    if( bRemoved )    {        m_pLoadedFileNames->clear();    }}4、    /** Removes multiple Sprite Frames from a plist file.    * Sprite Frames stored in this file will be removed.    * It is convenient to call this method when a specific texture needs to be removed.    * @since v0.99.5    */    void removeSpriteFramesFromFile(const char* plist);   //移除plist文件中包含的所有精灵帧。   void CCSpriteFrameCache::removeSpriteFramesFromFile(const char* plist){    std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(plist);    CCDictionary* dict = CCDictionary::createWithContentsOfFileThreadSafe(fullPath.c_str());    //移除dict中包含的所有精灵帧    removeSpriteFramesFromDictionary((CCDictionary*)dict);    // remove it from the cache    set<string>::iterator ret = m_pLoadedFileNames->find(plist);    if (ret != m_pLoadedFileNames->end())    {        //把plist文件从m_pLoadedFileNames缓存文件中清楚。        m_pLoadedFileNames->erase(ret);    }    dict->release();}5、/** Removes all Sprite Frames associated with the specified textures.    * It is convenient to call this method when a specific texture needs to be removed.    * @since v0.995.    */    根据纹理移除精灵帧缓存,因为同一个plist文件中所有的精灵帧对应同一个纹理,    这里只需要void CCSpriteFrameCache::removeSpriteFramesFromTexture(CCTexture2D* texture){    CCArray* keysToRemove = CCArray::create();    CCDictElement* pElement = NULL;    CCDICT_FOREACH(m_pSpriteFrames, pElement)    {        string key = pElement->getStrKey();        CCSpriteFrame* frame = (CCSpriteFrame*)m_pSpriteFrames->objectForKey(key.c_str());        if (frame && (frame->getTexture() == texture))        {            keysToRemove->addObject(CCString::create(pElement->getStrKey()));        }    }    m_pSpriteFrames->removeObjectsForKeys(keysToRemove);}6、     /** Returns an Sprite Frame that was previously added.     If the name is not found it will return nil.     You should retain the returned copy if you are going to use it.     @js getSpriteFrame     */    CCSpriteFrame* spriteFrameByName(const char *pszName);    //通过文件名获取对应的CCSpriteFrame*。    CCSpriteFrame* CCSpriteFrameCache::spriteFrameByName(const char *pszName){    CCSpriteFrame* frame = (CCSpriteFrame*)m_pSpriteFrames->objectForKey(pszName);    if (!frame)    {        // try alias dictionary        CCString *key = (CCString*)m_pSpriteFramesAliases->objectForKey(pszName);          if (key)        {            frame = (CCSpriteFrame*)m_pSpriteFrames->objectForKey(key->getCString());            if (! frame)            {                CCLOG("cocos2d: CCSpriteFrameCache: Frame '%s' not found", pszName);            }        }    }    return frame;}
0 0