mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-04 18:42:40 +08:00
Merge pull request #846 from jareguo/develop
load image for existing texture
This commit is contained in:
@@ -11,12 +11,14 @@
|
||||
#include "cocos/scripting/js-bindings/manual/jsb_conversions.hpp"
|
||||
#include "cocos/scripting/js-bindings/manual/jsb_global.h"
|
||||
#include "cocos/scripting/js-bindings/auto/jsb_cocos2dx_extension_auto.hpp"
|
||||
#include "cocos/base/CCThreadPool.h"
|
||||
|
||||
#include "cocos2d.h"
|
||||
#include "extensions/cocos-ext.h"
|
||||
|
||||
using namespace cocos2d;
|
||||
using namespace cocos2d::extension;
|
||||
using namespace cocos2d::experimental;
|
||||
|
||||
static bool jsb_cocos2d_extension_empty_func(se::State& s)
|
||||
{
|
||||
@@ -132,6 +134,181 @@ static bool js_cocos2dx_extension_loadRemoteImage(se::State& s)
|
||||
}
|
||||
SE_BIND_FUNC(js_cocos2dx_extension_loadRemoteImage)
|
||||
|
||||
static bool js_cocos2dx_extension_initRemoteImage(se::State& s)
|
||||
{
|
||||
const auto& args = s.args();
|
||||
int argc = (int)args.size();
|
||||
if (argc != 3)
|
||||
{
|
||||
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", argc, 3);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ok = false;
|
||||
|
||||
// get texture
|
||||
cocos2d::Texture2D* texture = nullptr;
|
||||
ok = seval_to_native_ptr(args[0], &texture);
|
||||
SE_PRECONDITION2(ok, false, "Converting 'texture' failed!");
|
||||
|
||||
// get url
|
||||
std::string url;
|
||||
ok = seval_to_std_string(args[1], &url);
|
||||
SE_PRECONDITION2(ok, false, "Converting 'url' failed!");
|
||||
|
||||
// get callback
|
||||
se::Value func = args[2];
|
||||
assert(func.isObject() && func.toObject()->isFunction());
|
||||
func.toObject()->root();
|
||||
|
||||
auto onCallback = [=](bool success){
|
||||
se::ScriptEngine::getInstance()->clearException();
|
||||
se::AutoHandleScope hs;
|
||||
|
||||
se::ValueArray args;
|
||||
args.resize(1);
|
||||
|
||||
args[0].setBoolean(success);
|
||||
func.toObject()->call(args, nullptr);
|
||||
};
|
||||
|
||||
auto downloader = new (std::nothrow) cocos2d::network::Downloader();
|
||||
downloader->onDataTaskSuccess = [=](const cocos2d::network::DownloadTask& task, std::vector<unsigned char>& data)
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
Image* image = new (std::nothrow) Image();
|
||||
if (image->initWithImageData(data.data(), data.size()))
|
||||
{
|
||||
if (texture->initWithImage(image))
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
CCLOGERROR("js_cocos2dx_extension_loadRemoteImageOn: Failed to initWithImage.");
|
||||
}
|
||||
}
|
||||
CC_SAFE_RELEASE_NULL(image);
|
||||
|
||||
onCallback(success);
|
||||
Director::getInstance()->getScheduler()->performFunctionInCocosThread([downloader](){
|
||||
delete downloader;
|
||||
});
|
||||
};
|
||||
|
||||
downloader->onTaskError = [=](const cocos2d::network::DownloadTask& task, int errorCode, int errorCodeInternal, const std::string& errorStr)
|
||||
{
|
||||
onCallback(false);
|
||||
Director::getInstance()->getScheduler()->performFunctionInCocosThread([downloader](){
|
||||
delete downloader;
|
||||
});
|
||||
};
|
||||
|
||||
downloader->createDownloadDataTask(url);
|
||||
return true;
|
||||
}
|
||||
SE_BIND_FUNC(js_cocos2dx_extension_initRemoteImage)
|
||||
|
||||
static ThreadPool* _threadPool = nullptr;
|
||||
static EventListenerCustom* _resetThreadPoolListener = nullptr;
|
||||
static ThreadPool* getThreadPool()
|
||||
{
|
||||
if (_threadPool == nullptr)
|
||||
{
|
||||
_threadPool = ThreadPool::newSingleThreadPool();
|
||||
_resetThreadPoolListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_RESET, [](cocos2d::EventCustom*){
|
||||
CC_SAFE_DELETE(_threadPool);
|
||||
cocos2d::Director::getInstance()->getEventDispatcher()->removeEventListener(_resetThreadPoolListener);
|
||||
_resetThreadPoolListener = nullptr;
|
||||
});
|
||||
}
|
||||
return _threadPool;
|
||||
}
|
||||
|
||||
static bool js_cocos2dx_extension_initTextureAsync(se::State& s)
|
||||
{
|
||||
const auto& args = s.args();
|
||||
int argc = (int)args.size();
|
||||
if (argc != 3)
|
||||
{
|
||||
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", argc, 3);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ok = false;
|
||||
|
||||
// get texture
|
||||
cocos2d::Texture2D* texture = nullptr;
|
||||
ok = seval_to_native_ptr(args[0], &texture);
|
||||
SE_PRECONDITION2(ok, false, "Converting 'texture' failed!");
|
||||
|
||||
// get url
|
||||
std::string url;
|
||||
ok = seval_to_std_string(args[1], &url);
|
||||
SE_PRECONDITION2(ok, false, "Converting 'url' failed!");
|
||||
|
||||
// get callback
|
||||
se::Value func = args[2];
|
||||
assert(func.isObject() && func.toObject()->isFunction());
|
||||
func.toObject()->root();
|
||||
|
||||
auto onCallback = [=](bool success){
|
||||
se::ScriptEngine::getInstance()->clearException();
|
||||
se::AutoHandleScope hs;
|
||||
|
||||
se::ValueArray args;
|
||||
args.resize(1);
|
||||
|
||||
args[0].setBoolean(success);
|
||||
func.toObject()->call(args, nullptr);
|
||||
};
|
||||
|
||||
// getDataFromFile isn't thread safe since fullPathForFilename usage in getDataFromFile isn't.
|
||||
// So we should pass full path to getDataFromFile to avoid multi-thread issues.
|
||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(url);
|
||||
|
||||
getThreadPool()->pushTask([texture, onCallback, fullPath](int tid){
|
||||
Data data = FileUtils::getInstance()->getDataFromFile(fullPath);
|
||||
if (!data.isNull())
|
||||
{
|
||||
Image* image = new (std::nothrow) Image();
|
||||
if (image->initWithImageData(data.getBytes(), data.getSize()))
|
||||
{
|
||||
Director::getInstance()->getScheduler()->performFunctionInCocosThread([=]() {
|
||||
if (texture->initWithImage(image))
|
||||
{
|
||||
onCallback(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
CCLOGERROR("js_cocos2dx_extension_initTextureAsync: Failed to init texture with image.");
|
||||
onCallback(false);
|
||||
}
|
||||
CC_SAFE_RELEASE(image);
|
||||
});
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
CCLOGERROR("js_cocos2dx_extension_initTextureAsync: Failed to load image.");
|
||||
CC_SAFE_RELEASE(image);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CCLOGERROR("js_cocos2dx_extension_initTextureAsync: Failed to load file.");
|
||||
}
|
||||
|
||||
Director::getInstance()->getScheduler()->performFunctionInCocosThread([=](){
|
||||
onCallback(false);
|
||||
});
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
SE_BIND_FUNC(js_cocos2dx_extension_initTextureAsync)
|
||||
|
||||
bool register_all_cocos2dx_extension_manual(se::Object* obj)
|
||||
{
|
||||
|
||||
@@ -147,6 +324,8 @@ bool register_all_cocos2dx_extension_manual(se::Object* obj)
|
||||
e->defineFunction("release", _SE(jsb_cocos2d_extension_empty_func));
|
||||
}
|
||||
__jsbObj->defineFunction("loadRemoteImg", _SE(js_cocos2dx_extension_loadRemoteImage));
|
||||
__jsbObj->defineFunction("initRemoteImg", _SE(js_cocos2dx_extension_initRemoteImage));
|
||||
__jsbObj->defineFunction("initTextureAsync", _SE(js_cocos2dx_extension_initTextureAsync));
|
||||
|
||||
se::ScriptEngine::getInstance()->clearException();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user