加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d 3.x 纹理缓存

发布时间:2020-12-14 19:45:31 所属栏目:百科 来源:网络整理
导读:官方的开发文档就是坑 http://cn.cocos2d-x.org/article/index?type=cocos2d-xurl=/doc/cocos-docs-master/manual/framework/native/v2/memory/texture-cache/zh.md 就不能提供个版本号? 3.x中纹理缓存的实列获取改为了 Director::getInstance()-getTexture

官方的开发文档就是坑

http://cn.cocos2d-x.org/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/framework/native/v2/memory/texture-cache/zh.md

就不能提供个版本号?

3.x中纹理缓存的实列获取改为了

Director::getInstance()->getTextureCache()

Director头文件中把TextureCache当成了一个保护成员变量

//texture cache belongs to this director
TextureCache *_textureCache;

Director关于TextureCache的函数有四个

<span style="color:#330000;">TextureCache* Director::getTextureCache() const
{
    return _textureCache;//返回TextureCache实列对象
}

void Director::initTextureCache()//这个是初始化TextureCache,在Director::init()里调用
{
#ifdef EMSCRIPTEN
    _textureCache = new (std::nothrow) TextureCacheEmscripten();
#else
    _textureCache = new (std::nothrow) TextureCache();
#endif // EMSCRIPTEN
}

void Director::destroyTextureCache()//这个应该是销毁函数,在purgeDirector()里调用,我感觉怎么是向线程的延时,没看太懂
{                                   //purgeDirector()是在DisplayLinkDirector里调用,这个类继承于Director
    if (_textureCache)
    {
        _textureCache->waitForQuit();
        CC_SAFE_RELEASE_NULL(_textureCache);
    }
}

void Director::purgeCachedData(void)//这个是清空无用的缓存
{
 FontFNT::purgeCachedData();//1
 FontAtlasCache::purgeCachedData();//2

 if (s_SharedDirector->getOpenGLView())
 {
 SpriteFrameCache::getInstance()->removeUnusedSpriteFrames();//3
 _textureCache->removeUnusedTextures();//4

 // Note: some tests such as ActionsTest are leaking refcounted textures
 // There should be no test textures left in the cache
 log("%sn",_textureCache->getCachedTextureInfo().c_str());
 }
 FileUtils::getInstance()->purgeCachedEntries();//5
}</span>
真正用到的应该是Director::getInstance()->getTextureCache()


关于TextureCache类的方法

<span style="color:#660000;">void TextureCache::addImageAsync(const std::string &path,const std::function<void(Texture2D*)>& callback)//异步加载方式,第一个是文件路径,第二个是回调函数
{
    Texture2D *texture = nullptr;

    std::string fullpath = FileUtils::getInstance()->fullPathForFilename(path);

    auto it = _textures.find(fullpath);
    if( it != _textures.end() )
        texture = it->second;

    if (texture != nullptr)
    {
        callback(texture);
        return;
    }

    // lazy init
    if (_asyncStructQueue == nullptr)
    {             
        _asyncStructQueue = new queue<AsyncStruct*>();
        _imageInfoQueue   = new deque<ImageInfo*>();        

        // create a new thread to load images
        _loadingThread = new std::thread(&TextureCache::loadImage,this);

        _needQuit = false;
    }

    if (0 == _asyncRefCount)
    {
        Director::getInstance()->getScheduler()->schedule(CC_SCHEDULE_SELECTOR(TextureCache::addImageAsyncCallBack),this,false);
    }

    ++_asyncRefCount;

    // generate async struct
    AsyncStruct *data = new (std::nothrow) AsyncStruct(fullpath,callback);

    // add async struct into queue
    _asyncStructQueueMutex.lock();
    _asyncStructQueue->push(data);
    _asyncStructQueueMutex.unlock();

    _sleepCondition.notify_one();
}
关于addImage
</span>
a:Texture2D * TextureCache::addImage(const std::string &path)

b:Texture2D* TextureCache::addImage(Image *image,const std::string &key)

对a来说默认的key是文件名称
对b来说可以自定义key
a和b这两种方式都返回加载好的纹理,所以要进行用Texture2D类型的变量进行赋值。
回收可以用void TextureCache::removeTexture(Texture2D* texture)或
void TextureCache::removeTextureForKey(const std::string &textureKeyName)来进行
获取可以用Texture2D* TextureCache::getTextureForKey(const std::string &textureKeyName) const



SpriteFrame的用法基本没变。

技术比较水,努力中

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读