diff --git a/cocos2d/core/load-pipeline/CCLoader.js b/cocos2d/core/load-pipeline/CCLoader.js index e3a4d4e39d..ec98e88daa 100644 --- a/cocos2d/core/load-pipeline/CCLoader.js +++ b/cocos2d/core/load-pipeline/CCLoader.js @@ -73,6 +73,8 @@ function CCLoader () { */ this.loader = loader; + this.onProgress = null; + // assets to release automatically this._autoReleaseSetting = {}; } @@ -158,7 +160,7 @@ JS.mixin(CCLoader.prototype, { if (completeCallback === undefined) { completeCallback = progressCallback; - progressCallback = null; + progressCallback = this.onProgress || null; } var self = this; @@ -178,7 +180,7 @@ JS.mixin(CCLoader.prototype, { } } - LoadingItems.create(this, resources, progressCallback, function (errors, items) { + var queue = LoadingItems.create(this, progressCallback, function (errors, items) { callInNextTick(function () { if (!completeCallback) return; @@ -202,6 +204,8 @@ JS.mixin(CCLoader.prototype, { items.destroy(); }); }); + LoadingItems.initQueueDeps(queue); + queue.append(resources); }, flowInDeps: function (owner, urlList, callback) { diff --git a/cocos2d/core/load-pipeline/loading-items.js b/cocos2d/core/load-pipeline/loading-items.js index 1410ad6eef..3c0591a287 100644 --- a/cocos2d/core/load-pipeline/loading-items.js +++ b/cocos2d/core/load-pipeline/loading-items.js @@ -38,6 +38,8 @@ var ItemState = { ERROR: 3 }; +var _queueDeps = {}; + function isIdValid (id) { var realId = id.id || id; return (typeof realId === 'string'); @@ -303,8 +305,15 @@ LoadingItems.create = function (pipeline, urlList, onProgress, onComplete) { } } else if (onComplete === undefined) { - onComplete = onProgress; - onProgress = null; + if (typeof urlList === 'function') { + onComplete = onProgress; + onProgress = urlList; + urlList = null; + } + else { + onComplete = onProgress; + onProgress = null; + } } var queue = _pool.pop(); @@ -354,6 +363,56 @@ LoadingItems.itemComplete = function (item) { } }; +LoadingItems.initQueueDeps = function (queue) { + var dep = _queueDeps[queue._id]; + if (!dep) { + dep = _queueDeps[queue._id] = { + completed: [], + deps: [] + }; + } + else { + dep.completed.length = 0; + dep.deps.length = 0; + } +}; + +LoadingItems.registerDep = function (owner, depId) { + var queueId = owner.queueId || owner; + if (!queueId) { + return false; + } + var queueDepList = _queueDeps[queueId]; + // Owner is root queue + if (queueDepList) { + if (queueDepList.deps.indexOf(depId) === -1) { + queueDepList.deps.push(depId); + } + } + // Owner is an item in the intermediate queue + else if (owner.id) { + for (var id in _queueDeps) { + var queue = _queueDeps[id]; + // Found root queue + if (queue.deps.indexOf(owner.id) !== -1) { + if (queue.deps.indexOf(depId) === -1) { + queue.deps.push(depId); + } + } + } + } +}; + +LoadingItems.finishDep = function (depId) { + for (var id in _queueDeps) { + var queue = _queueDeps[id]; + // Found root queue + if (queue.deps.indexOf(depId) !== -1 && queue.completed.indexOf(depId) === -1) { + queue.completed.push(depId); + } + } +}; + JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, { /** * !#en Add urls to the LoadingItems queue. @@ -388,6 +447,7 @@ JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, { var queue = _queues[url.queueId]; if (queue) { this.totalCount++; + LoadingItems.registerDep(owner || this._id, url.id); // console.log('+++++ Waited ' + url.id); queue.addListener(url.id, function (item) { // console.log('----- Completed by waiting ' + item.id + ', rest: ' + (self.totalCount - self.completedCount-1)); @@ -405,6 +465,7 @@ JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, { if (!this.map[id]) { this.map[item.id] = item; this.totalCount++; + LoadingItems.registerDep(owner || this._id, id); accepted.push(item); // console.log('+++++ Appended ' + item.id); } @@ -607,7 +668,12 @@ JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, { this.completed[id] = item; this.completedCount++; - this.onProgress && this.onProgress(this.completedCount, this.totalCount, item); + LoadingItems.finishDep(id); + if (this.onProgress) { + var dep = _queueDeps[item.queueId]; + console.log((dep ? dep.completed.length : this.completedCount) + ' / ' + (dep ? dep.deps.length : this.totalCount)); + this.onProgress(dep ? dep.completed.length : this.completedCount, dep ? dep.deps.length : this.totalCount, item); + } this.invokeAndRemove(id, item); @@ -641,6 +707,10 @@ JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, { CallbacksInvoker.call(this); _queues[this._id] = null; + if (_queueDeps[this._id]) { + _queueDeps[this._id].completed.length = 0; + _queueDeps[this._id].deps.length = 0; + } if (_pool.indexOf(this) === -1 && _pool.length < _POOL_MAX_LENGTH) { _pool.push(this); } diff --git a/cocos2d/core/sprites/CCSpriteFrame.js b/cocos2d/core/sprites/CCSpriteFrame.js index fa35f17ec1..c0feda76d3 100644 --- a/cocos2d/core/sprites/CCSpriteFrame.js +++ b/cocos2d/core/sprites/CCSpriteFrame.js @@ -30,22 +30,12 @@ var EventTarget = require("../event/event-target"); * !#en * A cc.SpriteFrame has:
* - texture: A cc.Texture2D that will be used by the _ccsg.Sprite
- * - rectangle: A rectangle of the texture
- *
- * You can modify the frame of a _ccsg.Sprite by doing:
- * - * Note: It's not recommended to use SpriteFrame constructor (new SpriteFrame) - * because its memory usage can't be tracked in native environment,
- * if you know what you are doing, you may need to manually retain it after creation then - * release it when you no longer need it. + * - rectangle: A rectangle of the texture * * !#zh * 一个 SpriteFrame 包含:
* - 纹理:会被 Sprite 使用的 Texture2D 对象。
- * - 矩形:在纹理中的矩形区域。
- * 注意:
- * 不建议用户使用构造函数进行创建,因为其内存使用情况,不能在本地环境中进行跟踪,需要用户手动管理内存,不然会导致严重错误。
- * 如果你知道你在做什么,你可能需要手动将其保留在创建之后,然后释放它。 + * - 矩形:在纹理中的矩形区域。 * * @class SpriteFrame * @extends Asset @@ -89,17 +79,9 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{ /** * !#en - * Constructor of SpriteFrame class.
- * Note: It's not recommended to use SpriteFrame constructor (new SpriteFrame) - * because its memory usage can't be tracked in native environment,
- * if you know what you are doing, you may need to manually retain it after creation then - * release it when you no longer need it. + * Constructor of SpriteFrame class. * !#zh - * SpriteFrame 类的构造函数。
- * 注意:
- * 不建议用户使用构造函数进行创建,因为其内存使用情况,不能在本地环境中进行跟踪, - * 需要用户手动管理内存,不然会导致严重错误。
- * 如果你知道你在做什么,你可能需要手动将其保留在创建之后,然后释放它。 + * SpriteFrame 类的构造函数。 * @method SpriteFrame * @param {String|Texture2D} [filename] * @param {Rect} [rect] @@ -108,11 +90,6 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{ * @param {Size} [originalSize] - The size of the frame in the texture */ ctor: function () { - - if (CC_DEV && (!CC_EDITOR || Editor.isRendererProcess) && !CC_TEST && !cc.game._isCloning) { - cc.warn("It's not recommended to use SpriteFrame constructor (new SpriteFrame) because its memory usage can't be tracked in native environment, if you know what you are doing, you may need to manually retain it after creation then release it when you no longer need it."); - } - var filename = arguments[0]; var rect = arguments[1]; var rotated = arguments[2];