Merge pull request #1521 from pandamicro/v1.4

Fix JSB memory growth issue
This commit is contained in:
pandamicro
2017-02-26 23:04:33 +08:00
committed by GitHub
9 changed files with 138 additions and 60 deletions
+2 -1
View File
@@ -344,7 +344,7 @@ JS.mixin(EventTarget.prototype, {
return;
}
var event = new cc.Event.EventCustom(message);
var event = cc.Event.EventCustom.get(message);
event.detail = detail;
// Event.AT_TARGET
@@ -356,6 +356,7 @@ JS.mixin(EventTarget.prototype, {
if (bublisteners && !event._propagationImmediateStopped) {
this._bubblingListeners.invoke(event);
}
cc.Event.EventCustom.put(event);
},
/*
+20
View File
@@ -265,6 +265,8 @@ var EventCustom = function (type, bubbles) {
JS.extend(EventCustom, cc.Event);
JS.mixin(EventCustom.prototype, {
reset: EventCustom,
/**
* !#en Sets user data
* !#zh 设置用户数据
@@ -294,6 +296,24 @@ JS.mixin(EventCustom.prototype, {
getEventName: cc.Event.prototype.getType
});
var _eventPool = [];
var MAX_POOL_SIZE = 10;
EventCustom.put = function (event) {
if (_eventPool.length < MAX_POOL_SIZE) {
_eventPool.push(event);
}
};
EventCustom.get = function (type, bubbles) {
var event = _eventPool.pop();
if (event) {
event.reset(type, bubbles);
}
else {
event = new EventCustom(type, bubbles);
}
return event;
};
cc.Event.EventCustom = EventCustom;
module.exports = cc.Event;
+1 -1
View File
@@ -135,7 +135,7 @@ function WebGLRenderCmd (renderable) {
this._shader = new cc.GLProgram();
this._shader.initWithVertexShaderByteArray(Shader.vert, Shader.frag);
this._shader.retain();
this._shader.addAttribute(cc.ATTRIBUTE_NAME_POSITION, cc.macro.VERTEX_ATTRIB_POSITION);
this._shader.addAttribute(cc.macro.ATTRIBUTE_NAME_POSITION, cc.macro.VERTEX_ATTRIB_POSITION);
this._shader.link();
this._shader.updateUniforms();
+1 -25
View File
@@ -39,32 +39,8 @@ var _engineNumberVersion = (function () {
}
})();
// Version polyfills
if (_engineNumberVersion) {
if (_engineNumberVersion.major === 3) {
if (_engineNumberVersion.minor < 6) {
require('./versions/jsb-polyfill-v3.5');
}
if (_engineNumberVersion.minor < 9) {
require('./versions/jsb-polyfill-v3.8');
}
if (_engineNumberVersion.minor < 10) {
require('./versions/jsb-polyfill-v3.9');
}
}
}
var originLog = console.log;
// cc.initEngine
cc.initEngine = function (config, cb) {
require('script/jsb.js');
cc._renderType = cc.game.RENDER_TYPE_OPENGL;
cc._engineLoaded = true;
originLog(cc.ENGINE_VERSION);
if (cb) cb();
};
// overwrite original console.log
try {
console.log = function () {
@@ -88,8 +64,8 @@ eval(
);
require('./jsb-predefine');
require('./jsb-loader');
require('./jsb-game');
require('./jsb-loader');
require('./jsb-director');
require('./jsb-tex-sprite-frame');
require('./jsb-scale9sprite');
+1 -1
View File
@@ -201,7 +201,7 @@ window._ccsg = {
};
// __errorHandler
window.__errorHandler = function (err) {
window.__errorHandler = function (filename, lineno, message) {
};
// rename cc.Class to cc._Class
+19
View File
@@ -103,6 +103,7 @@ cc.Event.EventCustom = function (type, bubbles) {
};
cc.js.extend(cc.Event.EventCustom, cc.Event);
cc.js.mixin(cc.Event.EventCustom.prototype, {
reset: cc.Event.EventCustom,
stopPropagation: function () {
this._propagationStopped = true;
},
@@ -125,6 +126,24 @@ cc.js.mixin(cc.Event.EventCustom.prototype, {
getEventName: cc.Event.prototype.getType
});
var _eventPool = [];
var MAX_POOL_SIZE = 10;
cc.Event.EventCustom.put = function (event) {
if (_eventPool.length < MAX_POOL_SIZE) {
_eventPool.push(event);
}
};
cc.Event.EventCustom.get = function (type, bubbles) {
var event = _eventPool.pop();
if (event) {
event.reset(type, bubbles);
}
else {
event = new cc.Event.EventCustom(type, bubbles);
}
return event;
};
// cc.eventManager.addListener
cc.eventManager.addListener = function(listener, nodeOrPriority) {
if(!(listener instanceof cc.EventListener)) {
+85 -26
View File
@@ -26,10 +26,36 @@
'use strict';
// cc.game
cc.js.mixin(cc.game, {
cc.game = {
DEBUG_MODE_NONE: 0,
DEBUG_MODE_INFO: 1,
DEBUG_MODE_WARN: 2,
DEBUG_MODE_ERROR: 3,
DEBUG_MODE_INFO_FOR_WEB_PAGE: 4,
DEBUG_MODE_WARN_FOR_WEB_PAGE: 5,
DEBUG_MODE_ERROR_FOR_WEB_PAGE: 6,
EVENT_HIDE: "game_on_hide",
EVENT_SHOW: "game_on_show",
EVENT_RESIZE: "game_on_resize",
// states
_paused: false, //whether the game is paused
_prepareCalled: false,//whether the prepare function has been called
_prepared: false,//whether the engine has prepared
/**
* Config of game
* @type {Object}
*/
config: null,
/**
* Callback when the scripts of engine have been load.
* @type {Function}
*/
onStart: null,
// Scenes list
_sceneInfos: [],
@@ -58,6 +84,29 @@ cc.js.mixin(cc.game, {
scenes: 'scenes'
},
//@Public Methods
// @Game play control
/**
* Set frameRate of game.
* @param frameRate
*/
setFrameRate: function (frameRate) {
var self = this, config = self.config, CONFIG_KEY = self.CONFIG_KEY;
config[CONFIG_KEY.frameRate] = frameRate;
cc.director.setAnimationInterval(1.0/frameRate);
},
/**
* Run the game frame by frame.
*/
step: function () {
cc.director.mainLoop();
},
/**
* Pause the game.
*/
pause: function () {
this._paused = true;
cc.director.pause();
@@ -78,6 +127,20 @@ cc.js.mixin(cc.game, {
return this._paused;
},
/**
* Restart game.
*/
restart: function () {
__restartVM();
},
/**
* End game, it will close the game window
*/
end: function () {
close();
},
prepare: function (cb) {
var self = this,
config = self.config,
@@ -94,35 +157,29 @@ cc.js.mixin(cc.game, {
if (this._prepareCalled) {
return;
}
// Prepare never called and engine ready
if (cc._engineLoaded) {
this._prepareCalled = true;
this._prepareCalled = true;
// Additional step in JSB
cc.director.sharedInit();
// Additional step in JSB
cc._renderType = cc.game.RENDER_TYPE_OPENGL;
cc._initDebugSetting(this.config[this.CONFIG_KEY.debugMode]);
cc.director.sharedInit();
// Load game scripts
var jsList = config[CONFIG_KEY.jsList];
if (jsList) {
cc.loader.load(jsList, function (err) {
if (err) throw new Error(JSON.stringify(err));
self._prepared = true;
if (cb) cb();
self.emit(self.EVENT_GAME_INITED);
});
}
else {
// Load game scripts
var jsList = config[CONFIG_KEY.jsList];
if (jsList) {
cc.loader.load(jsList, function (err) {
if (err) throw new Error(JSON.stringify(err));
self._prepared = true;
if (cb) cb();
self.emit(self.EVENT_GAME_INITED);
}
return;
});
}
else {
if (cb) cb();
self.emit(self.EVENT_GAME_INITED);
}
// Engine not loaded yet
cc.initEngine(this.config, function () {
self.prepare(cb);
});
},
/**
@@ -258,7 +315,7 @@ cc.js.mixin(cc.game, {
this.config = config;
}
});
};
cc.EventTarget.call(cc.game);
cc.js.addon(cc.game, cc.EventTarget.prototype);
@@ -268,4 +325,6 @@ cc.eventManager.addCustomListener(cc.game.EVENT_HIDE, function () {
});
cc.eventManager.addCustomListener(cc.game.EVENT_SHOW, function () {
cc.game.emit(cc.game.EVENT_SHOW, cc.game);
});
});
cc._initDebugSetting(cc.game.DEBUG_MODE_INFO);
-5
View File
@@ -23,9 +23,6 @@
THE SOFTWARE.
****************************************************************************/
// Run jsb.js before all polyfills
cc.initEngine();
if (!cc.ClassManager) {
cc.ClassManager = window.ClassManager || {
id : (0|(Math.random()*998)),
@@ -57,8 +54,6 @@ require('../cocos2d/core/event');
require('../cocos2d/core/event-manager/CCSystemEvent');
require('../CCDebugger');
cc._initDebugSetting(cc.game.DEBUG_MODE_INFO);
if (CC_DEV) {
//Debug Info ID map
require('../DebugInfos');
+9 -1
View File
@@ -47,7 +47,9 @@ cc.TextureCache.prototype.addImageAsync = function (url, cb, target) {
});
return localTex;
};
// Fix for compatibility with old APIs
if (!cc.TextureCache.prototype._addImage) {
cc.TextureCache.prototype._addImage = cc.TextureCache.prototype.addImage;
}
cc.TextureCache.prototype.addImage = function (url, cb, target) {
if (typeof cb === "function") {
return this.addImageAsync(url, cb, target);
@@ -89,6 +91,12 @@ cc.textureCache.removeTextureForKey = function (key) {
cc.Class._fastDefine('cc.Texture2D', cc.Texture2D, []);
cc.Texture2D.$super = cc.RawAsset;
cc.Texture2D.WrapMode = cc.Enum({
REPEAT: 0x2901,
CLAMP_TO_EDGE: 0x812f,
MIRRORED_REPEAT: 0x8370
});
var prototype = cc.Texture2D.prototype;
prototype.isLoaded = function () {