cocos2d-x基础知识(八)CCTextureCache的多线程加载原理和使用
做引擎的时候,遇到一个texture的异步加载,这里将具体的原理和使用方法贴出来,后面根据浏览器的特性做修改移植。 void CCTextureCache::addImageAsync(const char *path, CCObject *target, SEL_CallFuncO selector) { CCAssert(path != NULL, "TextureCache: fileimage MUST not be NULL"); CCTexture2D *texture = NULL; // optimization std::string pathKey = path; CCFileUtils::ccRemoveHDSuffixFromFile(pathKey); pathKey = CCFileUtils::fullPathFromRelativePath(pathKey.c_str()); texture = m_pTextures->objectForKey(pathKey); //根据pathkey查看是否纹理已经加载过,如果已经有了,则不重复加载 std::string fullpath = pathKey; if (texture != NULL) { if (target && selector) { (target->*selector)(texture); } return; } if (target) { target->retain(); } // lazy init static bool firstRun = true; if (firstRun) { s_pAsyncStructQueue = new queue<asyncstruct>(); s_pImageQueue = new queue<imageinfo>(); pthread_mutex_init(&amp;s_asyncStructQueueMutex, NULL); sem_init(&amp;s_sem, 0, 0); pthread_mutex_init(&amp;s_ImageInfoMutex, NULL); pthread_create(&amp;s_loadingThread, NULL, loadImage, NULL); //创建新的线程,用于后台加载图片 //创建调度队列,用来根据已加载的图片进行纹理转换 CCScheduler::sharedScheduler()-&gt;scheduleSelector(schedule_selector(CCTextureCache::addImageAsyncCallBack), this, 0, false); need_quit = false; firstRun = false; } // generate async struct AsyncStruct *data = new AsyncStruct(); data-&gt;filename = fullpath.c_str(); data-&gt;target = target; data-&gt;selector = selector; // add async struct into queue pthread_mutex_lock(&amp;s_asyncStructQueueMutex); s_pAsyncStructQueue-&gt;push(data); //将需要加载的图片放入队列中 pthread_mutex_unlock(&amp;s_asyncStructQueueMutex); sem_post(&amp;s_sem); } 从上述代码分析可以看出其过程: 1.创建线程,用于后台加载 2. 将对于需要加载的图片放入队列中 3. callback函数设定,用于将加载完成的图片转为纹理,等待使用其调用是由CCTimer::update调用的。 4. addImageAsyncCallBack函数在处理完纹理转换,还会调用addImageAsync传入的SEL_CallFuncO selector,实现用户加载图片纹理之后的具体处理。 使用例子: CCTextureCache::sharedTextureCache()-&gt;addImageAsync("Images/blocks.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); loadingCallBack函数就是使用异步加载完之后的用户可以自处理结果,比如初始化一个sprite,用这个纹理贴出来。
摘自:http://www.cnblogs.com/SeanLin/archive/2012/03/15/2398190.html</imageinfo></asyncstruct>
相关推荐
瓜牛呱呱 2020-11-12
starinshy 2020-11-10
farewellpoem 2020-11-09
huichen 2020-10-13
WenSenli 2020-10-12
lhtzbj 2020-08-13
cuiweisaidelike 2020-08-02
comeonxueRong 2020-08-02
learnpy 2020-07-19
kyelu 2020-07-09
yunfeitian 2020-07-05
zhoujiyu 2020-06-28
TreasureZ 2020-06-25
三动 2020-06-21
jvm 2020-06-14
举 2020-06-14
这些年来 2020-06-12