cocos2d 3.x 纹理缓存

cocos2d 3.x 纹理缓存,第1张

概述官方的开发文档就是坑 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()->ge

官方的开发文档就是坑

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 >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 >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来说可以自定义keya和b这两种方式都返回加载好的纹理,所以要进行用Texture2D类型的变量进行赋值。回收可以用voID TextureCache::removeTexture(Texture2D* texture)或voID TextureCache::removeTextureForKey(const std::string &textureKeyname)来进行获取可以用Texture2D* TextureCache::getTextureForKey(const std::string &textureKeyname) const

SpriteFrame的用法基本没变。

技术比较水,努力中

总结

以上是内存溢出为你收集整理的cocos2d 3.x 纹理缓存全部内容,希望文章能够帮你解决cocos2d 3.x 纹理缓存所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1021215.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存