【Cocos2dx3.x Lua】图片异步加载
一、说明
|
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 ( == _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();
}
|
--图片异步加载
ImageAsync=class("ImageAsync",function()
return cc.Layer:create()
end) ImageAsync.ctor=function(self)
self.size=cc.Director:getInstance():getWinSize()
self:initTexture()
self:loadingLabel()
self.curIndex= --当前loaded的图片编号 self:registerScriptHandler(function(tag)
if tag=="enter" then
--设置定时器
self:scheduleUpdateWithPriorityLua(function(dt)
self:loadTexture()
end,)
elseif tag=="exit" then
cclog("exit")
self:unscheduleUpdate()
cc.Director:getInstance():getTextureCache():removeAllTextures()
end
end)
end --初始化loadding label
ImageAsync.loadingLabel=function(self)
local label=cc.Label:createWithTTF("Loading...0%", "res/fonts/arial.ttf", )
label:setPosition(self.size.width/,self.size.height/)
self:addChild(label)
self.label=label
end --初始化图片集
ImageAsync.initTexture=function(self)
self.textures={}
for i=, do
for j=, do
local image=string.format("Images/sprites_test/sprite-%d-%d.png",i,j)
table.insert(self.textures,image)
end
end
end --异步加载图片
ImageAsync.loadTexture=function(self)
local function loadedImage(texture)
local sprite=cc.Sprite:createWithTexture(texture)
sprite:setPosition(cc.p(self.size.width/,self.size.height/))
sprite:setScale()
self:addChild(sprite)
self.curIndex=self.curIndex+
self.label:setString(string.format("Loading...%d%%",math.floor(*self.curIndex/#self.textures)))
cclog(string.format("loaded self.textures[%d]:%s",self.curIndex,self.textures[self.curIndex]))
end local textureCache=cc.Director:getInstance():getTextureCache()
for i=,#self.textures do
textureCache:addImageAsync(self.textures[i],loadedImage)
end self:unscheduleUpdate()
end ImageAsync.create=function()
local layer=ImageAsync.new()
return layer
end return ImageAsync
Texture2D * TextureCache::addImage(const std::string &path)
{
Texture2D * texture = nullptr;
Image* image = nullptr;
// Split up directory and filename
// MUTEX:
// Needed since addImageAsync calls this method from a different thread std::string fullpath = FileUtils::getInstance()->fullPathForFilename(path);
if (fullpath.size() == )
{
return nullptr;
}
auto it = _textures.find(fullpath);
if( it != _textures.end() )
texture = it->second; if (! texture)
{
// all images are handled by UIImage except PVR extension that is handled by our own handler
do
{
image = new (std::nothrow) Image();
CC_BREAK_IF(nullptr == image); bool bRet = image->initWithImageFile(fullpath);
CC_BREAK_IF(!bRet); texture = new (std::nothrow) Texture2D(); if( texture && texture->initWithImage(image) )
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
// cache the texture file name
VolatileTextureMgr::addImageTexture(texture, fullpath);
#endif
// texture already retained, no need to re-retain it
_textures.insert( std::make_pair(fullpath, texture) );
}
else
{
CCLOG("cocos2d: Couldn't create texture for file:%s in TextureCache", path.c_str());
}
} while ();
} CC_SAFE_RELEASE(image); return texture;
}
local BaseLoading=class("BaseLoading",function()
return cc.Layer:create()
end)
BaseLoading.init=function(self)
self._size=cc.Director:getInstance():getWinSize()
self._textures={}
self._curIndex= --当前loaded的图片编号
self:initTexture()
self:loadingUI()
end
--需要加载的所有资源文件
BaseLoading.initTexture=function(self)
end
--异步加载图片
BaseLoading.loadTexture=function(self)
local frameCache=cc.SpriteFrameCache:getInstance()
--加载图片
local function loadPng(texture)
self._label:setString(string.format("Loading...%d%%",math.floor(*self._curIndex/#self._textures)))
cclog(string.format("loaded self.textures[%d]:%s",self._curIndex,self._textures[self._curIndex].loc..".png"))
if self._curIndex==#self._textures then
self:finishLoading()
end
self._curIndex=self._curIndex+
end
--加载Plist图片
local function loadPlist(texture)
frameCache:addSpriteFrames(self._textures[self._curIndex].loc..".plist")
self._label:setString(string.format("Loading...%d%%",math.floor(*self._curIndex/#self._textures)))
cclog(string.format("loaded self.textures[%d]:%s",self._curIndex,self._textures[self._curIndex].loc..".png"))
if self._curIndex==#self._textures then
self:finishLoading()
end
self._curIndex=self._curIndex+
end
local textureCache=cc.Director:getInstance():getTextureCache()
for i=,#self._textures do
local rtype=self._textures[i].rtype
if rtype=="png" then
textureCache:addImageAsync(self._textures[i].loc..".png",loadPng)
elseif rtype=="plist" then
textureCache:addImageAsync(self._textures[i].loc..".png",loadPlist)
end
end
self:unscheduleUpdate()
end
--loading UI
BaseLoading.loadingUI=function(self)
local label=cc.Label:createWithTTF("Loading...0%", "res/fonts/Marker Felt.ttf", )
label:setPosition(self._size.width/,self._size.height/)
self:addChild(label)
self._label=label
end
BaseLoading.startLoading=function(self)
self:registerScriptHandler(function(tag)
if tag=="enter" then
--设置定时器
self:scheduleUpdateWithPriorityLua(function(dt)
self:loadTexture()
end,)
elseif tag=="exit" then
self:unscheduleUpdate()
end
end)
end
--完成异步加载图片回调函数
BaseLoading.finishLoading=function(self)
end
return BaseLoading
【Cocos2dx3.x Lua】图片异步加载的更多相关文章
- cocos2dx lua中异步加载网络图片,可用于显示微信头像
最近在做一个棋牌项目,脚本语言用的lua,登录需要使用微信登录,用户头像用微信账户的头像,微信接口返回的头像是一个url,那么遇到的一个问题就是如何在lua中异步加载这个头像,先在引擎源码里找了下可能 ...
- Android-Universal-Image-Loader 图片异步加载类库的使用
在博客中看到一篇利用组件进行图片异步加载的文章在此作记录 原文:http://blog.csdn.net/vipzjyno1/article/details/23206387 这个图片异步加载并缓存的 ...
- Android图片异步加载框架Android-Universal-Image-Loader
版权声明:本文为博主原创文章,未经博主允许不得转载. Android-Universal-Image-Loader是一个图片异步加载,缓存和显示的框架.这个框架已经被很多开发者所使用,是最常用的几个 ...
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
这个图片异步加载并缓存的类已经被很多开发者所使用,是最常用的几个开源库之一,主流的应用,随便反编译几个火的项目,都可以见到它的身影. 可是有的人并不知道如何去使用这库如何进行配置,网上查到的信息对于刚 ...
- Android ListView 图片异步加载和图片内存缓存
开发Android应用经常需要处理图片的加载问题.因为图片一般都是存放在服务器端,需要联网去加载,而这又是一个比较耗时的过程,所以Android中都是通过开启一个异步线程去加载.为了增加用户体验,给用 ...
- Android 图片异步加载的体会,SoftReference已经不再适用
在网络上搜索Android图片异步加载的相关文章,目前大部分提到的解决方案,都是采用Map<String, SoftReference<Drawable>> 这样软引用的 ...
- 【转】Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
Android-Universal-Image-Loader 原文地址:http://blog.csdn.net/vipzjyno1/article/details/23206387 这个图片异步加载 ...
- Android图片异步加载之Android-Universal-Image-Loader
将近一个月没有更新博客了,由于这段时间以来准备毕业论文等各种事务缠身,一直没有时间和精力沉下来继续学习和整理一些东西.最近刚刚恢复到正轨,正好这两天看了下Android上关于图片异步加载的开源项目,就 ...
- Android图片异步加载之Android-Universal-Image-Loader(转)
今天要介绍的是Github上一个使用非常广泛的图片异步加载库Android-Universal-Image-Loader,该项目的功能十分强大,可以说是我见过的目前功能最全.性能最优的图片异步加载解决 ...
随机推荐
- jQuery弹出层效果
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...
- memcached 安装使用
一.Memcached和Memcache的区别: 网上关于Memcached和Memcache的区别的理解众说纷纭,我个人的理解是: Memcached是一个内存缓存系统,而Memcache是php的 ...
- leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- Python学习笔记(五)OOP
模块 使用模块import 模块名.有的仅仅导入了某个模块的一个类或者函数,使用from 模块名 import 函数或类名实现.为了避免模块名冲突.Python引入了按文件夹来组织模块的方法,称为包( ...
- python2.0_day22_web聊天室二
上节内容已经实现了客户端使用长轮询的方式获取消息的功能.但是还没有展现到前端.本节内容将实现1.展现消息到前端窗口.2.客户端之间发送图片和文件.3.文件上传时显示进度条 下面我们来实现上面3个功能. ...
- win10下安装Oracle 11g 32位客户端遇到INS-13001环境不满足最低要求
在以管理员身份运行setup.exe之后,出现了:[INS-13001]环境不满足最低要求,通过网上搜索之后找到了解决途径 首先,打开你的解压后的database文件夹,找到stage,然后cvu,找 ...
- LeetCode——Implement Queue using Stacks
Description: Implement the following operations of a queue using stacks. push(x) -- Push element x t ...
- 【BZOJ2554】Color 概率神题
[BZOJ2554]Color Description 有n个球排成一列,每个球都有一个颜色,用A-Z的大写字母来表示,我们每次随机选出两个球ball1,ball2,使得后者染上前者的颜色,求期望操作 ...
- Oracle存储过程--案例
限额控制 CREATE OR REPLACE PACKAGE BODY NP_PCKG_MERCHANT_LIMIT AS PROCEDURE CHECK_LIMIT ( in_iplCode IN ...
- 学习c++的50条忠告(转自C++百度贴吧)
1.把C++当成一门新的语言学习(和C没啥关系!真的.): 2.看<Thinking In C++>,不要看<C++变成死相>: 3.看<The C++ Programm ...