mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
@@ -290,6 +290,9 @@ bool Sequence::initWithTwoActions(FiniteTimeAction *actionOne, FiniteTimeAction
|
||||
return false;
|
||||
}
|
||||
|
||||
CCASSERT(actionOne != this, "actionOne should not be sequence self!");
|
||||
CCASSERT(actionTwo != this, "actionOne should not be sequence self!");
|
||||
|
||||
float d = actionOne->getDuration() + actionTwo->getDuration();
|
||||
ActionInterval::initWithDuration(d);
|
||||
|
||||
|
||||
@@ -693,11 +693,8 @@ void Node::setGLProgram(GLProgram* glProgram)
|
||||
{
|
||||
if (_glProgramState == nullptr || (_glProgramState && _glProgramState->getGLProgram() != glProgram))
|
||||
{
|
||||
CC_SAFE_RELEASE(_glProgramState);
|
||||
_glProgramState = GLProgramState::getOrCreateWithGLProgram(glProgram);
|
||||
_glProgramState->retain();
|
||||
|
||||
_glProgramState->setNodeBinding(this);
|
||||
auto glProgramState = GLProgramState::getOrCreateWithGLProgram(glProgram);
|
||||
setGLProgramState(glProgramState);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -985,7 +985,7 @@ public:
|
||||
* Since v2.0, each rendering node must set its shader program.
|
||||
* It should be set in initialize phase.
|
||||
@code
|
||||
node->setGLrProgram(GLProgramCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
|
||||
node->setGLProgram(GLProgramCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
|
||||
@endcode
|
||||
*
|
||||
* @param glprogram The shader program.
|
||||
|
||||
@@ -37,6 +37,11 @@ THE SOFTWARE.
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
namespace {
|
||||
GLProgramCache::GLProgramLifeCycleHook __glProgramCreateHook = nullptr;
|
||||
GLProgramCache::GLProgramLifeCycleHook __glProgramDestroyHook = nullptr;
|
||||
}
|
||||
|
||||
enum {
|
||||
kShaderType_PositionTextureColor,
|
||||
kShaderType_PositionTextureColor_noMVP,
|
||||
@@ -76,6 +81,8 @@ GLProgramCache* GLProgramCache::getInstance()
|
||||
|
||||
void GLProgramCache::destroyInstance()
|
||||
{
|
||||
if (_sharedGLProgramCache != nullptr)
|
||||
_sharedGLProgramCache->cleanup();
|
||||
CC_SAFE_RELEASE_NULL(_sharedGLProgramCache);
|
||||
}
|
||||
|
||||
@@ -87,11 +94,8 @@ GLProgramCache::GLProgramCache()
|
||||
|
||||
GLProgramCache::~GLProgramCache()
|
||||
{
|
||||
for( auto it = _programs.begin(); it != _programs.end(); ++it ) {
|
||||
(it->second)->release();
|
||||
}
|
||||
|
||||
CCLOGINFO("deallocing GLProgramCache: %p", this);
|
||||
cleanup();
|
||||
}
|
||||
|
||||
bool GLProgramCache::init()
|
||||
@@ -107,6 +111,18 @@ bool GLProgramCache::init()
|
||||
return true;
|
||||
}
|
||||
|
||||
void GLProgramCache::cleanup()
|
||||
{
|
||||
for(auto& e : _programs)
|
||||
{
|
||||
if (__glProgramDestroyHook != nullptr)
|
||||
__glProgramDestroyHook(this, e.second);
|
||||
|
||||
e.second->release();
|
||||
}
|
||||
_programs.clear();
|
||||
}
|
||||
|
||||
void GLProgramCache::loadDefaultGLPrograms()
|
||||
{
|
||||
// Position Texture Color shader
|
||||
@@ -408,11 +424,18 @@ void GLProgramCache::addGLProgram(GLProgram* program, const std::string &key)
|
||||
if( prev == program )
|
||||
return;
|
||||
|
||||
if (__glProgramDestroyHook != nullptr)
|
||||
__glProgramDestroyHook(this, prev);
|
||||
|
||||
_programs.erase(key);
|
||||
CC_SAFE_RELEASE_NULL(prev);
|
||||
|
||||
if (program)
|
||||
program->retain();
|
||||
|
||||
if (__glProgramCreateHook != nullptr)
|
||||
__glProgramCreateHook(this, program);
|
||||
|
||||
_programs[key] = program;
|
||||
}
|
||||
|
||||
@@ -430,4 +453,25 @@ std::string GLProgramCache::getShaderMacrosForLight() const
|
||||
return std::string(def);
|
||||
}
|
||||
|
||||
/* static */
|
||||
void GLProgramCache::setGLProgramCreateHook(GLProgramLifeCycleHook hook)
|
||||
{
|
||||
__glProgramCreateHook = hook;
|
||||
}
|
||||
|
||||
/* static */
|
||||
void GLProgramCache::setGLProgramDestroyHook(GLProgramLifeCycleHook hook)
|
||||
{
|
||||
__glProgramDestroyHook = hook;
|
||||
}
|
||||
|
||||
void GLProgramCache::notifyAllGLProgramsCreated()
|
||||
{
|
||||
if (__glProgramCreateHook != nullptr)
|
||||
{
|
||||
for (const auto& e : _programs)
|
||||
__glProgramCreateHook(this, e.second);
|
||||
}
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
||||
@@ -84,12 +84,20 @@ public:
|
||||
/** reload default programs these are relative to light */
|
||||
void reloadDefaultGLProgramsRelativeToLights();
|
||||
|
||||
using GLProgramLifeCycleHook = void (*)(GLProgramCache*, GLProgram*);
|
||||
static void setGLProgramCreateHook(GLProgramLifeCycleHook hook);
|
||||
static void setGLProgramDestroyHook(GLProgramLifeCycleHook hook);
|
||||
|
||||
void notifyAllGLProgramsCreated();
|
||||
|
||||
private:
|
||||
/**
|
||||
@{
|
||||
Init and load predefined shaders.
|
||||
*/
|
||||
bool init();
|
||||
void cleanup();
|
||||
|
||||
void loadDefaultGLProgram(GLProgram *program, int type);
|
||||
/**
|
||||
@}
|
||||
|
||||
@@ -27,9 +27,13 @@ THE SOFTWARE.
|
||||
#include "renderer/CCGLProgramState.h"
|
||||
#include "renderer/CCGLProgram.h"
|
||||
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
namespace {
|
||||
GLProgramStateCache::GLProgramStateLifeCycleHook __glProgramStateCreateHook = nullptr;
|
||||
GLProgramStateCache::GLProgramStateLifeCycleHook __glProgramStateDestroyHook = nullptr;
|
||||
}
|
||||
|
||||
GLProgramStateCache* GLProgramStateCache::s_instance = nullptr;
|
||||
|
||||
GLProgramStateCache::GLProgramStateCache()
|
||||
@@ -38,7 +42,7 @@ GLProgramStateCache::GLProgramStateCache()
|
||||
|
||||
GLProgramStateCache::~GLProgramStateCache()
|
||||
{
|
||||
_glProgramStates.clear();
|
||||
removeAllGLProgramState();
|
||||
}
|
||||
|
||||
GLProgramStateCache* GLProgramStateCache::getInstance()
|
||||
@@ -65,6 +69,10 @@ GLProgramState* GLProgramStateCache::getGLProgramState(GLProgram* glprogram)
|
||||
auto ret = new (std::nothrow) GLProgramState;
|
||||
if(ret && ret->init(glprogram)) {
|
||||
_glProgramStates.insert(glprogram, ret);
|
||||
|
||||
if (__glProgramStateCreateHook != nullptr)
|
||||
__glProgramStateCreateHook(this, ret);
|
||||
|
||||
ret->release();
|
||||
return ret;
|
||||
}
|
||||
@@ -80,6 +88,8 @@ void GLProgramStateCache::removeUnusedGLProgramState()
|
||||
if( value->getReferenceCount() == 1 ) {
|
||||
CCLOG("cocos2d: GLProgramStateCache: removing unused GLProgramState");
|
||||
|
||||
if (__glProgramStateDestroyHook != nullptr)
|
||||
__glProgramStateDestroyHook(this, value);
|
||||
//value->release();
|
||||
it = _glProgramStates.erase(it);
|
||||
} else {
|
||||
@@ -90,8 +100,26 @@ void GLProgramStateCache::removeUnusedGLProgramState()
|
||||
|
||||
void GLProgramStateCache::removeAllGLProgramState()
|
||||
{
|
||||
if (__glProgramStateDestroyHook != nullptr)
|
||||
{
|
||||
for (const auto& e : _glProgramStates)
|
||||
__glProgramStateDestroyHook(this, e.second);
|
||||
}
|
||||
|
||||
_glProgramStates.clear();
|
||||
}
|
||||
|
||||
/* static */
|
||||
void GLProgramStateCache::setGLProgramStateCreateHook(GLProgramStateLifeCycleHook hook)
|
||||
{
|
||||
__glProgramStateCreateHook = hook;
|
||||
}
|
||||
|
||||
/* static */
|
||||
void GLProgramStateCache::setGLProgramStateDestroyHook(GLProgramStateLifeCycleHook hook)
|
||||
{
|
||||
__glProgramStateDestroyHook = hook;
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
||||
|
||||
@@ -62,6 +62,10 @@ public:
|
||||
/**Remove unused GLProgramState.*/
|
||||
void removeUnusedGLProgramState();
|
||||
|
||||
using GLProgramStateLifeCycleHook = void (*)(GLProgramStateCache*, GLProgramState*);
|
||||
static void setGLProgramStateCreateHook(GLProgramStateLifeCycleHook hook);
|
||||
static void setGLProgramStateDestroyHook(GLProgramStateLifeCycleHook hook);
|
||||
|
||||
protected:
|
||||
GLProgramStateCache();
|
||||
~GLProgramStateCache();
|
||||
|
||||
@@ -41,12 +41,15 @@ THE SOFTWARE.
|
||||
#include "base/ccUtils.h"
|
||||
#include "base/CCNinePatchImageParser.h"
|
||||
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
namespace {
|
||||
TextureCache::TextureLifeCycleHook __textureCreateHook = nullptr;
|
||||
TextureCache::TextureLifeCycleHook __textureDestroyHook = nullptr;
|
||||
}
|
||||
|
||||
// implementation TextureCache
|
||||
|
||||
TextureCache* TextureCache::getInstance()
|
||||
@@ -56,8 +59,6 @@ TextureCache* TextureCache::getInstance()
|
||||
|
||||
TextureCache::TextureCache()
|
||||
: _loadingThread(nullptr)
|
||||
, _textureCreateHook(nullptr)
|
||||
, _textureDestroyHook(nullptr)
|
||||
, _asyncRefCount(0)
|
||||
, _needQuit(false)
|
||||
{
|
||||
@@ -301,8 +302,8 @@ void TextureCache::addImageAsyncCallBack(float dt)
|
||||
|
||||
texture->autorelease();
|
||||
|
||||
if (_textureCreateHook != nullptr)
|
||||
_textureCreateHook(this, texture);
|
||||
if (__textureCreateHook != nullptr)
|
||||
__textureCreateHook(this, texture);
|
||||
} else {
|
||||
texture = nullptr;
|
||||
CCLOG("cocos2d: failed to call TextureCache::addImageAsync(%s)", asyncStruct->filename.c_str());
|
||||
@@ -365,8 +366,8 @@ Texture2D * TextureCache::addImage(const std::string &path)
|
||||
// texture already retained, no need to re-retain it
|
||||
_textures.insert( std::make_pair(fullpath, texture) );
|
||||
|
||||
if (_textureCreateHook != nullptr)
|
||||
_textureCreateHook(this, texture);
|
||||
if (__textureCreateHook != nullptr)
|
||||
__textureCreateHook(this, texture);
|
||||
|
||||
//parse 9-patch info
|
||||
this->parseNinePatchImage(image, texture, path);
|
||||
@@ -419,8 +420,8 @@ Texture2D* TextureCache::addImage(Image *image, const std::string &key)
|
||||
|
||||
texture->autorelease();
|
||||
|
||||
if (_textureCreateHook != nullptr)
|
||||
_textureCreateHook(this, texture);
|
||||
if (__textureCreateHook != nullptr)
|
||||
__textureCreateHook(this, texture);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -494,8 +495,8 @@ cocos2d::Vector<Texture2D*> TextureCache::getAllTextures() const
|
||||
void TextureCache::removeAllTextures()
|
||||
{
|
||||
for( auto it=_textures.begin(); it!=_textures.end(); ++it ) {
|
||||
if (_textureDestroyHook != nullptr)
|
||||
_textureDestroyHook(this, it->second);
|
||||
if (__textureDestroyHook != nullptr)
|
||||
__textureDestroyHook(this, it->second);
|
||||
|
||||
(it->second)->release();
|
||||
}
|
||||
@@ -508,8 +509,8 @@ void TextureCache::removeUnusedTextures()
|
||||
Texture2D *tex = it->second;
|
||||
if( tex->getReferenceCount() == 1 ) {
|
||||
CCLOG("cocos2d: TextureCache: removing unused texture: %s", it->first.c_str());
|
||||
if (_textureDestroyHook != nullptr)
|
||||
_textureDestroyHook(this, tex);
|
||||
if (__textureDestroyHook != nullptr)
|
||||
__textureDestroyHook(this, tex);
|
||||
|
||||
tex->release();
|
||||
it = _textures.erase(it);
|
||||
@@ -530,8 +531,8 @@ void TextureCache::removeTexture(Texture2D* texture)
|
||||
|
||||
for( auto it=_textures.cbegin(); it!=_textures.cend(); /* nothing */ ) {
|
||||
if( it->second == texture ) {
|
||||
if (_textureDestroyHook != nullptr)
|
||||
_textureDestroyHook(this, texture);
|
||||
if (__textureDestroyHook != nullptr)
|
||||
__textureDestroyHook(this, texture);
|
||||
|
||||
it->second->release();
|
||||
it = _textures.erase(it);
|
||||
@@ -553,8 +554,8 @@ void TextureCache::removeTextureForKey(const std::string &textureKeyName)
|
||||
}
|
||||
|
||||
if( it != _textures.end() ) {
|
||||
if (_textureDestroyHook != nullptr)
|
||||
_textureDestroyHook(this, it->second);
|
||||
if (__textureDestroyHook != nullptr)
|
||||
__textureDestroyHook(this, it->second);
|
||||
|
||||
(it->second)->release();
|
||||
_textures.erase(it);
|
||||
@@ -679,14 +680,16 @@ void TextureCache::renameTextureWithKey(const std::string& srcName, const std::s
|
||||
}
|
||||
}
|
||||
|
||||
/* static */
|
||||
void TextureCache::setTextureCreateHook(TextureLifeCycleHook hook)
|
||||
{
|
||||
_textureCreateHook = hook;
|
||||
__textureCreateHook = hook;
|
||||
}
|
||||
|
||||
/* static */
|
||||
void TextureCache::setTextureDestroyHook(TextureLifeCycleHook hook)
|
||||
{
|
||||
_textureDestroyHook = hook;
|
||||
__textureDestroyHook = hook;
|
||||
}
|
||||
|
||||
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
||||
|
||||
@@ -213,8 +213,8 @@ public:
|
||||
void renameTextureWithKey(const std::string& srcName, const std::string& dstName);
|
||||
|
||||
using TextureLifeCycleHook = void (*)(TextureCache*, Texture2D*);
|
||||
void setTextureCreateHook(TextureLifeCycleHook hook);
|
||||
void setTextureDestroyHook(TextureLifeCycleHook hook);
|
||||
static void setTextureCreateHook(TextureLifeCycleHook hook);
|
||||
static void setTextureDestroyHook(TextureLifeCycleHook hook);
|
||||
|
||||
private:
|
||||
void addImageAsyncCallBack(float dt);
|
||||
@@ -236,9 +236,6 @@ protected:
|
||||
|
||||
std::thread* _loadingThread;
|
||||
|
||||
TextureLifeCycleHook _textureCreateHook;
|
||||
TextureLifeCycleHook _textureDestroyHook;
|
||||
|
||||
int _asyncRefCount;
|
||||
bool _needQuit;
|
||||
};
|
||||
|
||||
@@ -14227,6 +14227,14 @@ GLProgram : function (
|
||||
*/
|
||||
cc.ShaderCache = {
|
||||
|
||||
/**
|
||||
* @method notifyAllGLProgramsCreated
|
||||
*/
|
||||
notifyAllGLProgramsCreated : function (
|
||||
)
|
||||
{
|
||||
},
|
||||
|
||||
/**
|
||||
* @method loadDefaultGLPrograms
|
||||
*/
|
||||
|
||||
@@ -32612,6 +32612,21 @@ bool js_register_cocos2dx_GLProgram(se::Object* obj)
|
||||
se::Object* __jsb_cocos2d_GLProgramCache_proto = nullptr;
|
||||
se::Class* __jsb_cocos2d_GLProgramCache_class = nullptr;
|
||||
|
||||
static bool js_cocos2dx_GLProgramCache_notifyAllGLProgramsCreated(se::State& s)
|
||||
{
|
||||
cocos2d::GLProgramCache* cobj = (cocos2d::GLProgramCache*)s.nativeThisObject();
|
||||
SE_PRECONDITION2(cobj, false, "js_cocos2dx_GLProgramCache_notifyAllGLProgramsCreated : Invalid Native Object");
|
||||
const auto& args = s.args();
|
||||
size_t argc = args.size();
|
||||
if (argc == 0) {
|
||||
cobj->notifyAllGLProgramsCreated();
|
||||
return true;
|
||||
}
|
||||
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 0);
|
||||
return false;
|
||||
}
|
||||
SE_BIND_FUNC(js_cocos2dx_GLProgramCache_notifyAllGLProgramsCreated)
|
||||
|
||||
static bool js_cocos2dx_GLProgramCache_loadDefaultGLPrograms(se::State& s)
|
||||
{
|
||||
cocos2d::GLProgramCache* cobj = (cocos2d::GLProgramCache*)s.nativeThisObject();
|
||||
@@ -32759,6 +32774,7 @@ bool js_register_cocos2dx_GLProgramCache(se::Object* obj)
|
||||
{
|
||||
auto cls = se::Class::create("ShaderCache", obj, nullptr, _SE(js_cocos2dx_GLProgramCache_constructor));
|
||||
|
||||
cls->defineFunction("notifyAllGLProgramsCreated", _SE(js_cocos2dx_GLProgramCache_notifyAllGLProgramsCreated));
|
||||
cls->defineFunction("loadDefaultShaders", _SE(js_cocos2dx_GLProgramCache_loadDefaultGLPrograms));
|
||||
cls->defineFunction("reloadDefaultGLProgramsRelativeToLights", _SE(js_cocos2dx_GLProgramCache_reloadDefaultGLProgramsRelativeToLights));
|
||||
cls->defineFunction("addProgram", _SE(js_cocos2dx_GLProgramCache_addGLProgram));
|
||||
|
||||
@@ -2128,6 +2128,7 @@ extern se::Class* __jsb_cocos2d_GLProgramCache_class;
|
||||
|
||||
bool js_register_cocos2d_GLProgramCache(se::Object* obj);
|
||||
bool register_all_cocos2dx(se::Object* obj);
|
||||
SE_DECLARE_FUNC(js_cocos2dx_GLProgramCache_notifyAllGLProgramsCreated);
|
||||
SE_DECLARE_FUNC(js_cocos2dx_GLProgramCache_loadDefaultGLPrograms);
|
||||
SE_DECLARE_FUNC(js_cocos2dx_GLProgramCache_reloadDefaultGLProgramsRelativeToLights);
|
||||
SE_DECLARE_FUNC(js_cocos2dx_GLProgramCache_addGLProgram);
|
||||
|
||||
@@ -85,6 +85,11 @@
|
||||
void funcName##Registry(void* nativeThisObject);
|
||||
|
||||
|
||||
// NOTE: se::Object::createObjectWithClass(cls) will return a se::Object pointer which is watched by garbage collector.
|
||||
// If there is a '_ctor' function of current class, '_property.toObject->call(...)' will be invoked which is an operation that may
|
||||
// make garbage collector to mark the created JS object as a garbage and set it to an invalid state.
|
||||
// If this happens, crash will be triggered. So please take care of the value returned from se::Object::createObjectWithClass.
|
||||
// HOW TO FIX: Use a rooted se::Value to save the se::Object poiner returned by se::Object::createObjectWithClass.
|
||||
#define SE_BIND_CTOR(funcName, cls, finalizeCb) \
|
||||
JsValueRef funcName##Registry(JsValueRef _callee, bool _isConstructCall, JsValueRef* _argv, unsigned short argc, void* _callbackState) \
|
||||
{ \
|
||||
@@ -93,7 +98,8 @@
|
||||
bool ret = true; \
|
||||
se::ValueArray args; \
|
||||
se::internal::jsToSeArgs(argc, _argv+1, &args); \
|
||||
se::Object* thisObject = se::Object::createObjectWithClass(cls); \
|
||||
se::Value thisVal(se::Object::createObjectWithClass(cls), true); \
|
||||
se::Object* thisObject = thisVal.toObject(); \
|
||||
JsValueRef _jsRet = thisObject->_getJSObject(); \
|
||||
se::State state(thisObject, args); \
|
||||
ret = funcName(state); \
|
||||
|
||||
@@ -89,13 +89,19 @@
|
||||
void funcName##Registry(JSObjectRef _obj);
|
||||
|
||||
|
||||
// NOTE: se::Object::createObjectWithClass(cls) will return a se::Object pointer which is watched by garbage collector.
|
||||
// If there is a '_ctor' function of current class, '_property.toObject->call(...)' will be invoked which is an operation that may
|
||||
// make garbage collector to mark the created JS object as a garbage and set it to an invalid state.
|
||||
// If this happens, crash will be triggered. So please take care of the value returned from se::Object::createObjectWithClass.
|
||||
// HOW TO FIX: Use a rooted se::Value to save the se::Object poiner returned by se::Object::createObjectWithClass.
|
||||
#define SE_BIND_CTOR(funcName, cls, finalizeCb) \
|
||||
JSObjectRef funcName##Registry(JSContextRef _cx, JSObjectRef _constructor, size_t argc, const JSValueRef _argv[], JSValueRef* _exception) \
|
||||
{ \
|
||||
bool ret = true; \
|
||||
se::ValueArray args; \
|
||||
se::internal::jsToSeArgs(_cx, argc, _argv, &args); \
|
||||
se::Object* thisObject = se::Object::createObjectWithClass(cls); \
|
||||
se::Value thisVal(se::Object::createObjectWithClass(cls), true); \
|
||||
se::Object* thisObject = thisVal.toObject(); \
|
||||
JSValueRef _jsRet = JSValueMakeUndefined(_cx); \
|
||||
se::State state(thisObject, args); \
|
||||
ret = funcName(state); \
|
||||
|
||||
@@ -59,29 +59,29 @@ void ScriptingCore::retainScriptObject(Ref* owner, Ref* target)
|
||||
return;
|
||||
}
|
||||
|
||||
se::ScriptEngine::getInstance()->clearException();
|
||||
se::AutoHandleScope hs;
|
||||
|
||||
se::Value targetVal;
|
||||
se::Object* targetObj = nullptr;
|
||||
// NOTE: If target isn't created from JS, we should return directly here.
|
||||
// When JS invokes getter method, we should hack the getter method to invoke jsb.registerNativeRef.
|
||||
// For example, you could refer to jsb_cocos2d.js, there are some code like:
|
||||
/*
|
||||
cc.TMXLayer.prototype._getTileAt = cc.TMXLayer.prototype.getTileAt;
|
||||
cc.TMXLayer.prototype.getTileAt = function(x, y){
|
||||
var pos = y !== undefined ? cc.p(x, y) : x;
|
||||
var ret = this._getTileAt(pos);
|
||||
jsb.registerNativeRef(this, ret); // Re-attach the target since ret is created in C++ and retainScriptObject failed.
|
||||
return ret;
|
||||
};
|
||||
*/
|
||||
auto iterTarget = se::NativePtrToObjectMap::find(target);
|
||||
if (iterTarget == se::NativePtrToObjectMap::end())
|
||||
{
|
||||
se::Class* cls = JSBClassType::findClass(target);
|
||||
if (cls == nullptr)
|
||||
return;
|
||||
|
||||
targetObj = se::Object::createObjectWithClass(cls);
|
||||
targetVal.setObject(targetObj, true);
|
||||
targetObj->setPrivateData(target);
|
||||
target->retain(); // Retain the native object to unify the logic in finalize method of js object.
|
||||
}
|
||||
else
|
||||
{
|
||||
targetObj = iterTarget->second;
|
||||
return;
|
||||
}
|
||||
|
||||
iterOwner->second->attachObject(targetObj);
|
||||
assert(!se::ScriptEngine::getInstance()->isGarbageCollecting());
|
||||
se::ScriptEngine::getInstance()->clearException();
|
||||
se::AutoHandleScope hs;
|
||||
|
||||
iterOwner->second->attachObject(iterTarget->second);
|
||||
}
|
||||
|
||||
void ScriptingCore::rootScriptObject(Ref* target)
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
#include "storage/local-storage/LocalStorage.h"
|
||||
#include "cocos2d.h"
|
||||
|
||||
#include "renderer/CCGLProgramStateCache.h"
|
||||
|
||||
using namespace cocos2d;
|
||||
|
||||
static bool jsb_cocos2dx_empty_func(se::State& s)
|
||||
@@ -912,6 +914,89 @@ static void onTextureDestroy(TextureCache* cache, Texture2D* texture)
|
||||
}
|
||||
}
|
||||
|
||||
static void onGLProgramCreate(GLProgramCache* cache, GLProgram* program)
|
||||
{
|
||||
se::AutoHandleScope hs;
|
||||
auto iterOwner = se::NativePtrToObjectMap::find(cache);
|
||||
if (iterOwner == se::NativePtrToObjectMap::end())
|
||||
{
|
||||
assert(false);
|
||||
return;
|
||||
}
|
||||
|
||||
se::Value val;
|
||||
bool fromCache = false;
|
||||
native_ptr_to_seval<GLProgram>(program, __jsb_cocos2d_GLProgram_class, &val, &fromCache);
|
||||
if (!fromCache && val.isObject())
|
||||
{
|
||||
iterOwner->second->attachObject(val.toObject());
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
static void onGLProgramDestroy(GLProgramCache* cache, GLProgram* program)
|
||||
{
|
||||
se::AutoHandleScope hs;
|
||||
auto iterOwner = se::NativePtrToObjectMap::find(cache);
|
||||
if (iterOwner == se::NativePtrToObjectMap::end())
|
||||
{
|
||||
assert(false);
|
||||
return;
|
||||
}
|
||||
|
||||
se::Value val;
|
||||
bool fromCache = false;
|
||||
native_ptr_to_seval<GLProgram>(program, __jsb_cocos2d_GLProgram_class, &val, &fromCache);
|
||||
if (fromCache && val.isObject())
|
||||
{
|
||||
iterOwner->second->detachObject(val.toObject());
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
static void onGLProgramStateCreate(GLProgramStateCache* cache, GLProgramState* state)
|
||||
{
|
||||
se::AutoHandleScope hs;
|
||||
// NOTE: GLProgramStateCache is a private class in cocos2dx, it isn't included in cocos2d.h and also didn't expose to JS
|
||||
// Since it's a singleton class, we could attach 'state' argument to global object instead.
|
||||
se::Object* global = se::ScriptEngine::getInstance()->getGlobalObject();
|
||||
se::Value val;
|
||||
bool fromCache = false;
|
||||
native_ptr_to_seval<GLProgramState>(state, __jsb_cocos2d_GLProgramState_class, &val, &fromCache);
|
||||
if (!fromCache && val.isObject())
|
||||
{
|
||||
global->attachObject(val.toObject());
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
static void onGLProgramStateDestroy(GLProgramStateCache* cache, GLProgramState* state)
|
||||
{
|
||||
se::AutoHandleScope hs;
|
||||
// NOTE: 'state' argument is attached to global object, therefore we need to detach it from global object.
|
||||
se::Object* global = se::ScriptEngine::getInstance()->getGlobalObject();
|
||||
se::Value val;
|
||||
bool fromCache = false;
|
||||
native_ptr_to_seval<GLProgramState>(state, __jsb_cocos2d_GLProgramState_class, &val, &fromCache);
|
||||
if (fromCache && val.isObject())
|
||||
{
|
||||
global->detachObject(val.toObject());
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
static bool register_eventlistener(se::Object* obj)
|
||||
{
|
||||
se::Value v;
|
||||
@@ -943,12 +1028,6 @@ static bool register_eventlistener(se::Object* obj)
|
||||
assert(v.isObject());
|
||||
v.toObject()->defineFunction("create", _SE(js_EventListenerCustom_create));
|
||||
|
||||
Director::getInstance()->getEventDispatcher()->setBeforeDispatchEventHook(Event::Type::TOUCH, onBeforeDispatchTouchEvent);
|
||||
Director::getInstance()->getEventDispatcher()->setAfterDispatchEventHook(Event::Type::TOUCH, onAfterDispatchTouchEvent);
|
||||
|
||||
Director::getInstance()->getTextureCache()->setTextureCreateHook(onTextureCreate);
|
||||
Director::getInstance()->getTextureCache()->setTextureDestroyHook(onTextureDestroy);
|
||||
|
||||
se::ScriptEngine::getInstance()->clearException();
|
||||
|
||||
return true;
|
||||
@@ -1469,11 +1548,20 @@ static bool js_cocos2dx_CallFunc_create(se::State& s)
|
||||
se::Object* jsobj = se::Object::createObjectWithClass(__jsb_cocos2d_CallFuncN_class);
|
||||
jsobj->setPrivateData(ret);
|
||||
|
||||
// Assign jsobj to jsVal and auto root it to avoid jsobj be swept by GC immediately.
|
||||
// Because there is "jsobj->attachObject(funcVal.toObject());" in "js_cocos2dx_CallFunc_init" function.
|
||||
// attachObject will do a function invocation which may mark 'jsobj' into an invalid state but without
|
||||
// invoking CallFuncN's finalize callback immediately.
|
||||
// AddressSanitizer in Xcode will prompt: heap-use-after-free on address.
|
||||
se::Value jsVal;
|
||||
jsVal.setObject(jsobj, true); // Pass true to indicate auto root / unroot by jsVal
|
||||
|
||||
if (js_cocos2dx_CallFunc_init(ret, jsobj, args))
|
||||
{
|
||||
s.rval().setObject(jsobj);
|
||||
s.rval() = std::move(jsVal);
|
||||
return true;
|
||||
}
|
||||
|
||||
SE_REPORT_ERROR("js_cocos2dx_CallFunc_create: initWithFunction failed!");
|
||||
return false;
|
||||
}
|
||||
@@ -1992,6 +2080,19 @@ bool register_all_cocos2dx_manual(se::Object* obj)
|
||||
register_empty_retain_release(obj);
|
||||
register_texture2d_manual(obj);
|
||||
register_tilemap_manual(obj);
|
||||
|
||||
Director::getInstance()->getEventDispatcher()->setBeforeDispatchEventHook(Event::Type::TOUCH, onBeforeDispatchTouchEvent);
|
||||
Director::getInstance()->getEventDispatcher()->setAfterDispatchEventHook(Event::Type::TOUCH, onAfterDispatchTouchEvent);
|
||||
|
||||
TextureCache::setTextureCreateHook(onTextureCreate);
|
||||
TextureCache::setTextureDestroyHook(onTextureDestroy);
|
||||
|
||||
GLProgramCache::setGLProgramCreateHook(onGLProgramCreate);
|
||||
GLProgramCache::setGLProgramDestroyHook(onGLProgramDestroy);
|
||||
|
||||
GLProgramStateCache::setGLProgramStateCreateHook(onGLProgramStateCreate);
|
||||
GLProgramStateCache::setGLProgramStateDestroyHook(onGLProgramStateDestroy);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -74,6 +74,13 @@ cc.view.getTargetDensityDPI = function() {return cc.macro.DENSITYDPI_DEVICE;};
|
||||
|
||||
cc.eventManager = cc.director.getEventDispatcher();
|
||||
|
||||
cc.EventDispatcher.prototype._addCustomListener = cc.EventDispatcher.prototype.addCustomListener;
|
||||
cc.EventDispatcher.prototype.addCustomListener = function(eventName, callback) {
|
||||
var ret = this._addCustomListener(eventName, callback);
|
||||
jsb.registerNativeRef(cc.eventManager, ret);
|
||||
return ret;
|
||||
};
|
||||
|
||||
cc.eventManager._resizeListener = cc.eventManager.addCustomListener('window-resize', function () {
|
||||
cc.winSize = cc.director.getWinSize();
|
||||
cc.visibleRect.init();
|
||||
@@ -82,6 +89,9 @@ cc.eventManager._resizeListener = cc.eventManager.addCustomListener('window-resi
|
||||
cc.configuration = cc.Configuration.getInstance();
|
||||
cc.textureCache = cc.director.getTextureCache();
|
||||
cc.shaderCache = cc.ShaderCache.getInstance();
|
||||
// The first time we invoke cc.ShaderCache.getInstance, notifyAllGLProgramsCreated needs to be called.
|
||||
// It should be invoked only once.
|
||||
cc.shaderCache.notifyAllGLProgramsCreated();
|
||||
cc.plistParser = cc.PlistParser.getInstance();
|
||||
|
||||
// File utils (Temporary, won't be accessible)
|
||||
|
||||
@@ -629,15 +629,45 @@ cc.TMXTiledMap.prototype.allLayers = function(){
|
||||
length = locChildren.length;
|
||||
for(var i = 0; i< length; i++){
|
||||
var layer = locChildren[i];
|
||||
if(layer && layer instanceof cc.TMXLayer)
|
||||
if(layer && layer instanceof cc.TMXLayer) {
|
||||
jsb.registerNativeRef(this, layer);
|
||||
retArr.push(layer);
|
||||
}
|
||||
}
|
||||
return retArr;
|
||||
};
|
||||
|
||||
cc.TMXTiledMap.prototype._getLayer = cc.TMXTiledMap.prototype.getLayer;
|
||||
cc.TMXTiledMap.prototype.getLayer = function(layerName) {
|
||||
var ret = this._getLayer(layerName);
|
||||
jsb.registerNativeRef(this, ret);
|
||||
return ret;
|
||||
};
|
||||
|
||||
cc.TMXTiledMap.prototype._getObjectGroup = cc.TMXTiledMap.prototype.getObjectGroup;
|
||||
cc.TMXTiledMap.prototype.getObjectGroup = function(groupName) {
|
||||
var ret = this._getObjectGroup(groupName);
|
||||
jsb.registerNativeRef(this, ret);
|
||||
return ret;
|
||||
};
|
||||
|
||||
cc.TMXTiledMap.prototype._getObjectGroups = cc.TMXTiledMap.prototype.getObjectGroups;
|
||||
cc.TMXTiledMap.prototype.getObjectGroups = function() {
|
||||
var ret = this._getObjectGroups();
|
||||
if (ret && ret instanceof Array) {
|
||||
for (var i = 0, len = ret.length; i < len; ++i) {
|
||||
jsb.registerNativeRef(this, ret[i]);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
||||
cc.TMXLayer.prototype._getTileAt = cc.TMXLayer.prototype.getTileAt;
|
||||
cc.TMXLayer.prototype.getTileAt = function(x, y){
|
||||
var pos = y !== undefined ? cc.p(x, y) : x;
|
||||
return this._getTileAt(pos);
|
||||
var ret = this._getTileAt(pos);
|
||||
jsb.registerNativeRef(this, ret);
|
||||
return ret;
|
||||
};
|
||||
cc.TMXLayer.prototype._getTileGIDAt = cc.TMXLayer.prototype.getTileGIDAt;
|
||||
cc.TMXLayer.prototype.getTileGIDAt = function(x, y){
|
||||
@@ -671,6 +701,31 @@ cc.TMXLayer.prototype.getPositonAt = function(x, y){
|
||||
|
||||
cc.TMXLayer.prototype.tileFlagsAt = cc.TMXLayer.prototype.getTileFlagsAt;
|
||||
|
||||
cc.TMXObjectGroup.prototype._getObject = cc.TMXObjectGroup.prototype.getObject;
|
||||
cc.TMXObjectGroup.prototype.getObject = function(objectName) {
|
||||
var ret = this._getObject(objectName);
|
||||
jsb.registerNativeRef(this, ret);
|
||||
return ret;
|
||||
};
|
||||
|
||||
cc.TMXObjectGroup.prototype._getObjects = cc.TMXObjectGroup.prototype.getObjects;
|
||||
cc.TMXObjectGroup.prototype.getObjects = function() {
|
||||
var ret = this._getObjects();
|
||||
if (ret && ret instanceof Array) {
|
||||
for (var i = 0, len = ret.length; i < len; ++i) {
|
||||
jsb.registerNativeRef(this, ret[i]);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
||||
cc.TMXObject.prototype._getNode = cc.TMXObject.prototype.getNode;
|
||||
cc.TMXObject.prototype.getNode = function() {
|
||||
var ret = this._getNode();
|
||||
jsb.registerNativeRef(this, ret);
|
||||
return ret;
|
||||
};
|
||||
|
||||
//
|
||||
// setBlendFunc JS API Wrapper
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user