From a19dd93fafb6582a7da3d4ec5fef1e4aa270efc3 Mon Sep 17 00:00:00 2001 From: Hermanto Date: Sat, 6 Dec 2014 21:34:15 +0800 Subject: [PATCH 1/3] Remote Texture Loading Fix --- cocos2d/core/textures/CCTextureCache.js | 18 +++++++++++++++--- cocos2d/core/textures/TexturesWebGL.js | 24 +++++++++++++++++------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/cocos2d/core/textures/CCTextureCache.js b/cocos2d/core/textures/CCTextureCache.js index a8290816c7..57bdb37f48 100644 --- a/cocos2d/core/textures/CCTextureCache.js +++ b/cocos2d/core/textures/CCTextureCache.js @@ -351,16 +351,28 @@ if (cc._renderType === cc._RENDER_TYPE_CANVAS) { tex.url = url; if (!cc.loader.getRes(url)) { if (cc.loader._checkIsImageURL(url)) { - cc.loader.load(url, function (err) { - cb && cb.call(target); + cc.loader.load(url, function (err, img) { + if (err) + return cb ? cb(err) : err; + + cc.loader.cache[url] = img; + cc.textureCache.handleLoadedTexture(url); + + var texResult = locTexs[url]; + + cb && cb.call(target,texResult); }); } else { cc.loader.loadImg(url, function (err, img) { if (err) return cb ? cb(err) : err; + cc.loader.cache[url] = img; cc.textureCache.handleLoadedTexture(url); - cb && cb.call(target, tex); + + var texResult = locTexs[url]; + + cb && cb.call(target, texResult); }); } } diff --git a/cocos2d/core/textures/TexturesWebGL.js b/cocos2d/core/textures/TexturesWebGL.js index abd6b53ee7..f5866f6631 100644 --- a/cocos2d/core/textures/TexturesWebGL.js +++ b/cocos2d/core/textures/TexturesWebGL.js @@ -881,11 +881,20 @@ cc._tmp.WebGLTextureCache = function () { cb && cb.call(target, tex); return tex; } - + + if (!cc.loader.getRes(url)) { if (cc.loader._checkIsImageURL(url)) { - cc.loader.load(url, function (err) { - cb && cb.call(target); + + //cc.log("loading image url"); + + cc.loader.load(url, function (err,img) { + cc.loader.cache[url] = img; + cc.textureCache.handleLoadedTexture(url); + + var texResult = locTexs[url]; + + cb && cb.call(target,texResult); }); } else { cc.loader.loadImg(url, function (err, img) { @@ -893,13 +902,14 @@ cc._tmp.WebGLTextureCache = function () { return cb ? cb(err) : err; cc.loader.cache[url] = img; cc.textureCache.handleLoadedTexture(url); - cb && cb.call(target, tex); + + var texResult = locTexs[url]; + + cb && cb.call(target, texResult); }); } } - - tex = locTexs[url] = new cc.Texture2D(); - tex.url = url; + return tex; }; _p = null; From ea75174e22c1ee0976689acc5546ab65cf92d2b1 Mon Sep 17 00:00:00 2001 From: Hermanto Date: Sat, 6 Dec 2014 21:38:50 +0800 Subject: [PATCH 2/3] Remove comment --- cocos2d/core/textures/TexturesWebGL.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/cocos2d/core/textures/TexturesWebGL.js b/cocos2d/core/textures/TexturesWebGL.js index f5866f6631..4cb2ea2487 100644 --- a/cocos2d/core/textures/TexturesWebGL.js +++ b/cocos2d/core/textures/TexturesWebGL.js @@ -885,9 +885,6 @@ cc._tmp.WebGLTextureCache = function () { if (!cc.loader.getRes(url)) { if (cc.loader._checkIsImageURL(url)) { - - //cc.log("loading image url"); - cc.loader.load(url, function (err,img) { cc.loader.cache[url] = img; cc.textureCache.handleLoadedTexture(url); From 05c7123ff81eb91ac6b8be0b1bf23b5db669bae1 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 9 Dec 2014 12:55:45 +0800 Subject: [PATCH 3/3] cocos2d/cocos2d-js#1171: Fixed cc.texutreCache's addImage logic --- CCBoot.js | 12 ++++++-- cocos2d/core/textures/CCTextureCache.js | 39 ++++++------------------- cocos2d/core/textures/TexturesWebGL.js | 39 +++++++++---------------- 3 files changed, 32 insertions(+), 58 deletions(-) diff --git a/CCBoot.js b/CCBoot.js index 2df1e30522..adcdf89c56 100644 --- a/CCBoot.js +++ b/CCBoot.js @@ -755,13 +755,21 @@ cc.loader = /** @lends cc.loader# */{ else if (option !== undefined) cb = option; - var img = new Image(); + var img = this.getRes(url); + if (img) { + cb && cb(null, img); + return img; + } + + img = new Image(); if (opt.isCrossOrigin && location.origin != "file://") img.crossOrigin = "Anonymous"; var lcb = function () { this.removeEventListener('load', lcb, false); this.removeEventListener('error', ecb, false); + + cc.loader.cache[url] = img; if (cb) cb(null, img); }; @@ -802,7 +810,7 @@ cc.loader = /** @lends cc.loader# */{ type = cc.path.extname(url); } - var obj = self.cache[url]; + var obj = self.getRes(url); if (obj) return cb(null, obj); var loader = null; diff --git a/cocos2d/core/textures/CCTextureCache.js b/cocos2d/core/textures/CCTextureCache.js index 57bdb37f48..9dd8a2898c 100644 --- a/cocos2d/core/textures/CCTextureCache.js +++ b/cocos2d/core/textures/CCTextureCache.js @@ -349,36 +349,15 @@ if (cc._renderType === cc._RENDER_TYPE_CANVAS) { tex = locTexs[url] = new cc.Texture2D(); tex.url = url; - if (!cc.loader.getRes(url)) { - if (cc.loader._checkIsImageURL(url)) { - cc.loader.load(url, function (err, img) { - if (err) - return cb ? cb(err) : err; - - cc.loader.cache[url] = img; - cc.textureCache.handleLoadedTexture(url); - - var texResult = locTexs[url]; - - cb && cb.call(target,texResult); - }); - } else { - cc.loader.loadImg(url, function (err, img) { - if (err) - return cb ? cb(err) : err; - - cc.loader.cache[url] = img; - cc.textureCache.handleLoadedTexture(url); - - var texResult = locTexs[url]; - - cb && cb.call(target, texResult); - }); - } - } - else { - tex.handleLoadedTexture(); - } + var loadFunc = cc.loader._checkIsImageURL(url) ? cc.loader.load : cc.loader.loadImg; + loadFunc.call(cc.loader, url, function (err, img) { + if (err) + return cb && cb.call(target, err); + cc.textureCache.handleLoadedTexture(url); + + var texResult = locTexs[url]; + cb && cb.call(target, texResult); + }); return tex; }; diff --git a/cocos2d/core/textures/TexturesWebGL.js b/cocos2d/core/textures/TexturesWebGL.js index 4cb2ea2487..09f6f71699 100644 --- a/cocos2d/core/textures/TexturesWebGL.js +++ b/cocos2d/core/textures/TexturesWebGL.js @@ -881,32 +881,19 @@ cc._tmp.WebGLTextureCache = function () { cb && cb.call(target, tex); return tex; } - - - if (!cc.loader.getRes(url)) { - if (cc.loader._checkIsImageURL(url)) { - cc.loader.load(url, function (err,img) { - cc.loader.cache[url] = img; - cc.textureCache.handleLoadedTexture(url); - - var texResult = locTexs[url]; - - cb && cb.call(target,texResult); - }); - } else { - cc.loader.loadImg(url, function (err, img) { - if (err) - return cb ? cb(err) : err; - cc.loader.cache[url] = img; - cc.textureCache.handleLoadedTexture(url); - - var texResult = locTexs[url]; - - cb && cb.call(target, texResult); - }); - } - } - + + tex = locTexs[url] = new cc.Texture2D(); + tex.url = url; + var loadFunc = cc.loader._checkIsImageURL(url) ? cc.loader.load : cc.loader.loadImg; + loadFunc.call(cc.loader, url, function (err, img) { + if (err) + return cb && cb.call(target, err); + cc.textureCache.handleLoadedTexture(url); + + var texResult = locTexs[url]; + cb && cb.call(target, texResult); + }); + return tex; }; _p = null;