做引擎的時候,遇到一個texture的異步加載,這里將具體的原理和使用方法貼出來,后面根據瀏覽器的特性做修改移植。
voidCCTextureCache::addImageAsync(constchar *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(&s_asyncStructQueueMutex, NULL);
sem_init(&s_sem, 0, 0);
pthread_mutex_init(&s_ImageInfoMutex, NULL);
pthread_create(&s_loadingThread, NULL, loadImage, NULL); //創建新的線程,用於后台加載圖片
//創建調度隊列,用來根據已加載的圖片進行紋理轉換
CCScheduler::sharedScheduler()->scheduleSelector(schedule_selector(CCTextureCache::addImageAsyncCallBack), this, 0, false);
need_quit = false;
firstRun = false;
}
// generate async struct
AsyncStruct *data = new AsyncStruct();
data->filename = fullpath.c_str();
data->target = target;
data->selector = selector;
// add async struct into queue
pthread_mutex_lock(&s_asyncStructQueueMutex);
s_pAsyncStructQueue->push(data); //將需要加載的圖片放入隊列中
pthread_mutex_unlock(&s_asyncStructQueueMutex);
sem_post(&s_sem);
}
從上述代碼分析可以看出其過程:
1.創建線程,用於后台加載
2. 將對於需要加載的圖片放入隊列中
3. callback函數設定,用於將加載完成的圖片轉為紋理,等待使用其調用是由CCTimer::update調用的。
4. addImageAsyncCallBack函數在處理完紋理轉換,還會調用addImageAsync傳入的SEL_CallFuncO selector,實現用戶加載圖片紋理之后的具體處理。
使用例子:
CCTextureCache::sharedTextureCache()->addImageAsync("Images/blocks.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack));
loadingCallBack函數就是使用異步加載完之后的用戶可以自處理結果,比如初始化一個sprite,用這個紋理貼出來。