cocos2d 3.x 纹理缓存

前端之家收集整理的这篇文章主要介绍了cocos2d 3.x 纹理缓存前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

官方的开发文档就是坑

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("%s\n",_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的用法基本没变。

技术比较水,努力中

原文链接:https://www.f2er.com/cocos2dx/345591.html

猜你在找的Cocos2d-x相关文章