惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - SeanLin

Cocos2d-html5资源预加载列表自动扫描脚本 JSDoc参考注释模板 [转]Git常用操作命令 Cocos2d-html5 HelloWorld模板和引擎使用简介 Javascript API Document生成工具JSDoc HTML5游戏开发涉及到的安全问题 Javascript开发的HTML5游戏的知识产权保护 引擎demo任务 引擎初始化过程 引擎开发数据记录 游戏引擎消息循环机制 渲染部分 几个需要思考的问题 【转】使用Jasob混淆javascript代码 【转】html5游戏开发引擎大全 【转】V8 Javascript 引擎设计理念 android 字体位置信息 【转】蓝牙协议中HCI层的研究与开发 【转】蓝牙协议的命令和事件
CCTextureCache的多线程加载原理和使用
SeanLin · 2012-03-15 · via 博客园 - SeanLin

做引擎的时候,遇到一个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,用这个纹理贴出来。

关键词:cocos2d-html5,cocos2d-x,游戏引擎