如果没有预先加载图片,则可以通过addImageAsync()函数实现异步加载,该函数通过创建一个加载线程来加载图片,并且在主线程中通过调用回调函数来读取该图片资源纹理。其主要过程如下:
1.创建线程,用于后台加载图片
2.将对于需要加载的图片放入图片资源队列中
3.callback函数设定,用于将加载完成的图片转为纹理,等待使用其调用是由CCTimer::update调用的。
4.addImageAsyncCallBack函数在处理完纹理转换,还会调用addImageAsync传入的SEL_CallFuncO selector,实现用户加载图片纹理之后的具体处理。
void addImageAsync(const char *path, CCObject *target, SEL_CallFuncO selector);
函数实现过程如下:
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;
pathKey = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(pathKey.c_str());
texture = (CCTexture2D*)m_pTextures->objectForKey(pathKey.c_str());
std::string fullpath = pathKey;
if (texture != NULL)
{
if (target && selector)
{
(target->*selector)(texture);
}
return;
}
// lazy init
if (s_pSem == NULL)
{
#if CC_ASYNC_TEXTURE_CACHE_USE_NAMED_SEMAPHORE
s_pSem = sem_open(CC_ASYNC_TEXTURE_CACHE_SEMAPHORE, O_CREAT, 0644, 0);
if( s_pSem == SEM_FAILED )
{
CCLOG( “CCTextureCache async thread semaphore init error: %s\n”, strerror( errno ) );
s_pSem = NULL;
return;
}
#else
int semInitRet = sem_init(&s_sem, 0, 0);
if( semInitRet < 0 )
{
CCLOG( “CCTextureCache async thread semaphore init error: %s\n”, strerror( errno ) );
return;
}
s_pSem = &s_sem;
#endif
//创建一个异步信息队列
s_pAsyncStructQueue = new queue<AsyncStruct*>();
//创建一个图片资源队列
s_pImageQueue = new queue<ImageInfo*>();
pthread_mutex_init(&s_asyncStructQueueMutex, NULL);
pthread_mutex_init(&s_ImageInfoMutex, NULL);
//创建加载图片资源线程,用于加载图片
pthread_create(&s_loadingThread, NULL, loadImage, NULL);
need_quit = false;
}
if (0 == s_nAsyncRefCount)
{
//创建调度队列,用来根据已加载图片来进行纹理转换
CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(CCTextureCache::addImageAsyncCallBack), this, 0, false);
}
++s_nAsyncRefCount;
if (target)
{
target->retain();
}
// 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_pSem);
}
创建图片资源
static void* loadImage(void* data)
{
// create autorelease pool for iOS
CCThread thread;
thread.createAutoreleasePool();
AsyncStruct *pAsyncStruct = NULL;
while (true)
{
// wait for rendering thread to ask for loading if s_pAsyncStructQueue is empty
int semWaitRet = sem_wait(s_pSem);
if( semWaitRet < 0 )
{
CCLOG( “CCTextureCache async thread semaphore error: %s\n”, strerror( errno ) );
break;
}
//从异步信息队列中取出
std::queue<AsyncStruct*> *pQueue = s_pAsyncStructQueue;
pthread_mutex_lock(&s_asyncStructQueueMutex);// get async struct from queue
if (pQueue->empty())
{
pthread_mutex_unlock(&s_asyncStructQueueMutex);
if (need_quit)
break;
else
continue;
}
else
{
pAsyncStruct = pQueue->front();
pQueue->pop();
pthread_mutex_unlock(&s_asyncStructQueueMutex);
}
const char *filename = pAsyncStruct->filename.c_str();
// compute image type
CCImage::EImageFormat imageType = computeImageFormatType(pAsyncStruct->filename);
if (imageType == CCImage::kFmtUnKnown)
{
CCLOG(“unsupported format %s”,filename);
delete pAsyncStruct;
continue;
}
//创建图片资源
CCImage *pImage = new CCImage();
if (! pImage->initWithImageFileThreadSafe(filename, imageType))
{
delete pImage;
CCLOG(“can not load %s”, filename);
continue;
}
// 创建图片资源信息
ImageInfo *pImageInfo = new ImageInfo();
pImageInfo->asyncStruct = pAsyncStruct;
pImageInfo->image = pImage;
pImageInfo->imageType = imageType;
// 把图片资源加入图片资源队列
pthread_mutex_lock(&s_ImageInfoMutex);
s_pImageQueue->push(pImageInfo);
pthread_mutex_unlock(&s_ImageInfoMutex);
}
if( s_pSem != NULL )
{
#if CC_ASYNC_TEXTURE_CACHE_USE_NAMED_SEMAPHORE
sem_unlink(CC_ASYNC_TEXTURE_CACHE_SEMAPHORE);
sem_close(s_pSem);
#else
sem_destroy(s_pSem);
#endif
s_pSem = NULL;
delete s_pAsyncStructQueue;
delete s_pImageQueue;
}
return 0;
}
创建图片纹理
void CCTextureCache::addImageAsyncCallBack(float dt)
{
// 从图片资源队列中取出
std::queue<ImageInfo*> *imagesQueue = s_pImageQueue;
pthread_mutex_lock(&s_ImageInfoMutex);
if (imagesQueue->empty())
{
pthread_mutex_unlock(&s_ImageInfoMutex);
}
else
{
ImageInfo *pImageInfo = imagesQueue->front();
imagesQueue->pop();
pthread_mutex_unlock(&s_ImageInfoMutex);
AsyncStruct *pAsyncStruct = pImageInfo->asyncStruct;
CCImage *pImage = pImageInfo->image;
CCObject *target = pAsyncStruct->target;
SEL_CallFuncO selector = pAsyncStruct->selector;
const char* filename = pAsyncStruct->filename.c_str();
// 创建2D纹理
CCTexture2D *texture = new CCTexture2D();
#if 0 //TODO: (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
texture->initWithImage(pImage, kCCResolutioniPhone);
#else
texture->initWithImage(pImage);
#endif
#if CC_ENABLE_CACHE_TEXTURE_DATA
// cache the texture file name
VolatileTexture::addImageTexture(texture, filename, pImageInfo->imageType);
#endif
// 加入缓存
m_pTextures->setObject(texture, filename);
texture->autorelease();
if (target && selector)
{
(target->*selector)(texture);
target->release();
}
pImage->release();
delete pAsyncStruct;
delete pImageInfo;
–s_nAsyncRefCount;
if (0 == s_nAsyncRefCount)
{
CCDirector::sharedDirector()->getScheduler()->unscheduleSelector(schedule_selector(CCTextureCache::addImageAsyncCallBack), this);
}
}
}
- cocos2d-x中CCTextureCache图片资源的异步加载<转>
如果没有预先加载图片,则可以通过addImageAsync()函数实现异步加载,该函数通过创建一个加载线程来加载图片,并且在主线程中通过调用回调函数来读取该图片资源纹理.其主要过程如下: 1.创建线程 ...
- MVC中实现部分内容异步加载
MVC中实现部分内容异步加载 action中定义一个得到结果集的方法 public ActionResult GetItemTree(string title, int itemid, int? pa ...
- vue-awesome-swiper中的数据异步加载
<template> <div> //第一个轮播 加了v-if 判断,可以实现 loop 轮循 <swiper v-if="gglist.length>1 ...
- 【Cocos2d-Js基础教学(5)资源打包工具的使用及资源的异步加载处理】
TexturePacker是纹理资源打包工具,支持Cocos2dx的游戏资源打包. 如果用过的同学可以直接看下面的资源的异步加载处理 首先为什么用TexturePacker? 1,节省图片资源实际大小 ...
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
Android 高手进阶(21) 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处http://blog.csdn.net/xiaanming/article/details ...
- [置顶] 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
转载请注明出处http://blog.csdn.net/xiaanming/article/details/9825113 异步加载图片的例子,网上也比较多,大部分用了HashMap<Strin ...
- 使用 jsPlumb 绘制拓扑图 —— 异步加载与绘制的实现
本文实现的方法可以边异步加载数据边绘制拓扑图. 有若干点需要说明一下: 1. 一次性获取所有数据并绘制拓扑图, 请参见文章: <使用 JsPlumb 绘制拓扑图的通用方法> ; 本文实现 ...
- Listview 异步加载图片之优化篇(有图有码有解释)
在APP应用中,listview的异步加载图片方式能够带来很好的用户体验,同时也是考量程序性能的一个重要指标.关于listview的异步加载,网上其实很多示例了,中心思想都差不多,不过很多版本或是有b ...
- Android图片异步加载之Android-Universal-Image-Loader
将近一个月没有更新博客了,由于这段时间以来准备毕业论文等各种事务缠身,一直没有时间和精力沉下来继续学习和整理一些东西.最近刚刚恢复到正轨,正好这两天看了下Android上关于图片异步加载的开源项目,就 ...
随机推荐
- jboss服务器配置多实例
jboss配置多实例的重要性 在开发, 测试项目的过程中, 我们经常需要在同一台主机上, 同一个服务器上配置多个运行实例.这样做有一下几点好处: 在项目开发, 调试阶段能最大限度的节省资源 某个实例出 ...
- 【24点游戏】cocos2dx 源码
1. 4个数字 24点判断 double Calc(double a, double b, string oper) { double result = 0; const char *p = ope ...
- oracle 体系结构解析
三.oracle 体系结构 1.oracle内存由SGA+PGA所构成 2.oracle数据库体系结构数据库的体系结构是指数据库的组成.工作过程与原理,以及数据在数据库中的组织与管理机制. oracl ...
- linux中的配置文件
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个 ...
- poj 1552 Doubles
#include <stdio.h> #include <stdlib.h> ]; int cmp(const void *a, const void *b) { return ...
- SCAU 10678 神奇的异或
10678 神奇的异或 时间限制:1000MS 内存限制:65535K 题型: 编程题 语言: 无限制 Description 在现在这个信息时代,数据是很重要的东西哦~ 很多时候,一条关键的数 ...
- 生成500个0-1000的随机数&&数组查找—小练习
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Mellanox OFED2.1-X安装记录
---恢复内容开始--- 1,tcl,tk,gcc-gfortran,libnl-devel依赖包
- 【LoadRunner】安装LoadRunner时提示缺少vc2005_sp1_with_atl_fix_redist解决方案
我的电脑在安装UFT时,被要求需要卸载本机上安装的LoadRunner11,当LoadRunner11被卸载后,进行重新安装LoadRunner11时,会报缺少vc2005_sp1_with_atl_ ...
- Awk中调用shell命令
Awk中调用shell命令 需求 在awk中,有时候需要调用linux系统中命令,如计算字符串的MD5值,并保存下来. 方法参考 call a shell command from inside aw ...