Skip to content

Commit f52b553

Browse files
committed
Loads more Cache optimisations and tweaks.
1 parent 81b553d commit f52b553

2 files changed

Lines changed: 98 additions & 62 deletions

File tree

src/gameobjects/components/LoadTexture.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,18 @@ Phaser.Component.LoadTexture.prototype = {
8888
}
8989
else
9090
{
91-
if (key === null || typeof key === 'undefined')
91+
if (key === null || key === undefined)
9292
{
9393
this.key = '__default';
94-
this.setTexture(PIXI.TextureCache[this.key]);
94+
// this.setTexture(PIXI.TextureCache[this.key]);
95+
this.setTexture(cache.getTexture(this.key));
9596
}
9697
else if (typeof key === 'string' && !cache.checkImageKey(key))
9798
{
9899
console.warn("Texture with key '" + key + "' not found.");
99100
this.key = '__missing';
100-
this.setTexture(PIXI.TextureCache[this.key]);
101+
// this.setTexture(PIXI.TextureCache[this.key]);
102+
this.setTexture(cache.getTexture(this.key));
101103
}
102104
else
103105
{

src/loader/Cache.js

Lines changed: 93 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,19 @@ Phaser.Cache.prototype = {
243243
this.removeImage(key);
244244
}
245245

246-
this._cache.image[key] = { url: url, data: data };
246+
var img = {
247+
url: url,
248+
data: data,
249+
base: new PIXI.BaseTexture(data),
250+
frame: new Phaser.Frame(0, 0, 0, data.width, data.height, key),
251+
frameData: new Phaser.FrameData()
252+
};
247253

248-
this._cache.image[key].frame = new Phaser.Frame(0, 0, 0, data.width, data.height, key);
249-
this._cache.image[key].frameData = new Phaser.FrameData();
250-
this._cache.image[key].frameData.addFrame(new Phaser.Frame(0, 0, 0, data.width, data.height, url));
254+
img.frameData.addFrame(new Phaser.Frame(0, 0, 0, data.width, data.height, url);
251255

252-
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
256+
this._cache.image[key] = img;
253257

254-
this._resolveURL(url, this._cache.image[key]);
258+
this._resolveURL(url, img);
255259

256260
},
257261

@@ -264,15 +268,10 @@ Phaser.Cache.prototype = {
264268
addDefaultImage: function () {
265269

266270
var img = new Image();
267-
img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgAQMAAABJtOi3AAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAABVJREFUeF7NwIEAAAAAgKD9qdeocAMAoAABm3DkcAAAAABJRU5ErkJggg==";
268271

269-
this._cache.image['__default'] = { url: null, data: img };
270-
this._cache.image['__default'].frame = new Phaser.Frame(0, 0, 0, 32, 32, '', '');
271-
this._cache.image['__default'].frameData = new Phaser.FrameData();
272-
this._cache.image['__default'].frameData.addFrame(new Phaser.Frame(0, 0, 0, 32, 32, null));
272+
img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgAQMAAABJtOi3AAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAABVJREFUeF7NwIEAAAAAgKD9qdeocAMAoAABm3DkcAAAAABJRU5ErkJggg==";
273273

274-
PIXI.BaseTextureCache['__default'] = new PIXI.BaseTexture(img);
275-
PIXI.TextureCache['__default'] = new PIXI.Texture(PIXI.BaseTextureCache['__default']);
274+
this.addImage('__default', null, img);
276275

277276
},
278277

@@ -285,15 +284,10 @@ Phaser.Cache.prototype = {
285284
addMissingImage: function () {
286285

287286
var img = new Image();
288-
img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAJ9JREFUeNq01ssOwyAMRFG46v//Mt1ESmgh+DFmE2GPOBARKb2NVjo+17PXLD8a1+pl5+A+wSgFygymWYHBb0FtsKhJDdZlncG2IzJ4ayoMDv20wTmSMzClEgbWYNTAkQ0Z+OJ+A/eWnAaR9+oxCF4Os0H8htsMUp+pwcgBBiMNnAwF8GqIgL2hAzaGFFgZauDPKABmowZ4GL369/0rwACp2yA/ttmvsQAAAABJRU5ErkJggg==";
289287

290-
this._cache.image['__missing'] = { url: null, data: img };
291-
this._cache.image['__missing'].frame = new Phaser.Frame(0, 0, 0, 32, 32, '', '');
292-
this._cache.image['__missing'].frameData = new Phaser.FrameData();
293-
this._cache.image['__missing'].frameData.addFrame(new Phaser.Frame(0, 0, 0, 32, 32, null));
288+
img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAJ9JREFUeNq01ssOwyAMRFG46v//Mt1ESmgh+DFmE2GPOBARKb2NVjo+17PXLD8a1+pl5+A+wSgFygymWYHBb0FtsKhJDdZlncG2IzJ4ayoMDv20wTmSMzClEgbWYNTAkQ0Z+OJ+A/eWnAaR9+oxCF4Os0H8htsMUp+pwcgBBiMNnAwF8GqIgL2hAzaGFFgZauDPKABmowZ4GL369/0rwACp2yA/ttmvsQAAAABJRU5ErkJggg==";
294289

295-
PIXI.BaseTextureCache['__missing'] = new PIXI.BaseTexture(img);
296-
PIXI.TextureCache['__missing'] = new PIXI.Texture(PIXI.BaseTextureCache['__missing']);
290+
this.addImage('__missing', null, img);
297291

298292
},
299293

@@ -309,8 +303,8 @@ Phaser.Cache.prototype = {
309303
*/
310304
addSound: function (key, url, data, webAudio, audioTag) {
311305

312-
webAudio = webAudio || true;
313-
audioTag = audioTag || false;
306+
if (webAudio === undefined) { webAudio = true; audioTag = false; }
307+
if (audioTag === undefined) { webAudio = false; audioTag = true; }
314308

315309
var decoded = false;
316310

@@ -319,7 +313,15 @@ Phaser.Cache.prototype = {
319313
decoded = true;
320314
}
321315

322-
this._cache.sounds[key] = { url: url, data: data, isDecoding: false, decoded: decoded, webAudio: webAudio, audioTag: audioTag, locked: this.game.sound.touchLocked };
316+
this._cache.sounds[key] = {
317+
url: url,
318+
data: data,
319+
isDecoding: false,
320+
decoded: decoded,
321+
webAudio: webAudio,
322+
audioTag: audioTag,
323+
locked: this.game.sound.touchLocked
324+
};
323325

324326
this._resolveURL(url, this._cache.sounds[key]);
325327

@@ -426,28 +428,28 @@ Phaser.Cache.prototype = {
426428
*/
427429
addBitmapFont: function (key, url, data, atlasData, atlasType, xSpacing, ySpacing) {
428430

429-
var entry = { url: url, data: data, font: null };
430-
431-
// this._cache.bitmapFont[key] = { url: url, data: data };
432-
433-
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
431+
var obj = {
432+
url: url,
433+
data: data,
434+
font: null,
435+
base: new PIXI.BaseTexture(data)
436+
};
434437

435438
if (atlasType === 'json')
436439
{
440+
// TODO: These can return the object rather than add it to the PIXI Cache
437441
Phaser.LoaderParser.jsonBitmapFont(this.game, atlasData, key, xSpacing, ySpacing);
438442
}
439443
else
440444
{
441445
Phaser.LoaderParser.xmlBitmapFont(this.game, atlasData, key, xSpacing, ySpacing);
442446
}
443447

444-
entry.font = PIXI.BitmapText.fonts[key];;
445-
446-
// this._cache.bitmapFont[key] = PIXI.BitmapText.fonts[key];
448+
obj.font = PIXI.BitmapText.fonts[key];;
447449

448-
this._cache.bitmapFont[key] = entry;
450+
this._cache.bitmapFont[key] = obj;
449451

450-
this._resolveURL(url, this._cache.bitmapFont[key]);
452+
this._resolveURL(url, obj);
451453

452454
},
453455

@@ -544,13 +546,20 @@ Phaser.Cache.prototype = {
544546
*/
545547
addSpriteSheet: function (key, url, data, frameWidth, frameHeight, frameMax, margin, spacing) {
546548

547-
this._cache.spriteSheet[key] = { url: url, data: data, frameWidth: frameWidth, frameHeight: frameHeight, margin: margin, spacing: spacing };
549+
var obj = {
550+
url: url,
551+
data: data,
552+
frameWidth: frameWidth,
553+
frameHeight: frameHeight,
554+
margin: margin,
555+
spacing: spacing,
556+
base: new PIXI.BaseTexture(data),
557+
frameData: Phaser.AnimationParser.spriteSheet(this.game, key, frameWidth, frameHeight, frameMax, margin, spacing)
558+
};
548559

549-
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
560+
this._cache.spriteSheet[key] = obj;
550561

551-
this._cache.spriteSheet[key].frameData = Phaser.AnimationParser.spriteSheet(this.game, key, frameWidth, frameHeight, frameMax, margin, spacing);
552-
553-
this._resolveURL(url, this._cache.spriteSheet[key]);
562+
this._resolveURL(url, obj);
554563

555564
},
556565

@@ -566,28 +575,28 @@ Phaser.Cache.prototype = {
566575
*/
567576
addTextureAtlas: function (key, url, data, atlasData, format) {
568577

569-
this._cache.atlas[key] = { url: url, data: data };
570-
571-
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
578+
var obj = { url: url, data: data, base: new PIXI.BaseTexture(data) };
572579

573580
if (format == Phaser.Loader.TEXTURE_ATLAS_XML_STARLING)
574581
{
575-
this._cache.atlas[key].frameData = Phaser.AnimationParser.XMLData(this.game, atlasData, key);
582+
obj.frameData = Phaser.AnimationParser.XMLData(this.game, atlasData, key);
576583
}
577584
else
578585
{
579586
// Let's just work it out from the frames array
580587
if (Array.isArray(atlasData.frames))
581588
{
582-
this._cache.atlas[key].frameData = Phaser.AnimationParser.JSONData(this.game, atlasData, key);
589+
obj.frameData = Phaser.AnimationParser.JSONData(this.game, atlasData, key);
583590
}
584591
else
585592
{
586-
this._cache.atlas[key].frameData = Phaser.AnimationParser.JSONDataHash(this.game, atlasData, key);
593+
obj.frameData = Phaser.AnimationParser.JSONDataHash(this.game, atlasData, key);
587594
}
588595
}
589596

590-
this._resolveURL(url, this._cache.atlas[key]);
597+
this._cache.atlas[key] = obj;
598+
599+
this._resolveURL(url, obj);
591600

592601
},
593602

@@ -605,16 +614,19 @@ Phaser.Cache.prototype = {
605614

606615
var _this = this;
607616

608-
if (this._cache.sound[key])
617+
var sound = this.getSound(key);
618+
619+
if (sound)
609620
{
610-
this._cache.sound[key].data.src = this._cache.sound[key].url;
621+
sound.data.src = sound.url;
611622

612-
this._cache.sound[key].data.addEventListener('canplaythrough', function () {
623+
sound.data.addEventListener('canplaythrough', function () {
613624
return _this.reloadSoundComplete(key);
614625
}, false);
615626

616-
this._cache.sound[key].data.load();
627+
sound.data.load();
617628
}
629+
618630
},
619631

620632
/**
@@ -625,9 +637,11 @@ Phaser.Cache.prototype = {
625637
*/
626638
reloadSoundComplete: function (key) {
627639

628-
if (this._cache.sound[key])
640+
var sound = this.getSound(key);
641+
642+
if (sound)
629643
{
630-
this._cache.sound[key].locked = false;
644+
sound.locked = false;
631645
this.onSoundUnlock.dispatch(key);
632646
}
633647

@@ -641,9 +655,11 @@ Phaser.Cache.prototype = {
641655
*/
642656
updateSound: function (key, property, value) {
643657

644-
if (this._cache.sound[key])
658+
var sound = this.getSound(key);
659+
660+
if (sound)
645661
{
646-
this._cache.sound[key][property] = value;
662+
sound[property] = value;
647663
}
648664

649665
},
@@ -657,9 +673,11 @@ Phaser.Cache.prototype = {
657673
*/
658674
decodedSound: function (key, data) {
659675

660-
this._cache.sound[key].data = data;
661-
this._cache.sound[key].decoded = true;
662-
this._cache.sound[key].isDecoding = false;
676+
var sound = this.getSound(key);
677+
678+
sound.data = data;
679+
sound.decoded = true;
680+
sound.isDecoding = false;
663681

664682
},
665683

@@ -707,13 +725,13 @@ Phaser.Cache.prototype = {
707725
* Checks if a key for the given cache object type exists.
708726
*
709727
* @method Phaser.Cache#checkKey
710-
* @param {number} type - The Cache type to check against. I.e. Phaser.Cache.CANVAS, Phaser.Cache.IMAGE, Phaser.Cache.JSON, etc.
728+
* @param {number} cache - The Cache to check against. I.e. Phaser.Cache.CANVAS, Phaser.Cache.IMAGE, Phaser.Cache.JSON, etc.
711729
* @param {string} key - Asset key of the image to check is in the Cache.
712730
* @return {boolean} True if the key exists, otherwise false.
713731
*/
714-
checkKey: function (type, key) {
732+
checkKey: function (cache, key) {
715733

716-
if (this._cacheMap[type][key])
734+
if (this._cacheMap[cache][key])
717735
{
718736
return true;
719737
}
@@ -1289,6 +1307,22 @@ Phaser.Cache.prototype = {
12891307
// Frame Related Methods //
12901308
////////////////////////////
12911309

1310+
/**
1311+
* Gets a PIXI.BaseTexture by key from the given Cache.
1312+
*
1313+
* @method Phaser.Cache#getBaseTexture
1314+
* @param {string} key - Asset key of the image for which you want the BaseTexture for.
1315+
* @param {integer} [cache=Phaser.Cache.IMAGE] - The cache to search for the item in.
1316+
* @return {PIXI.BaseTexture} The BaseTexture object.
1317+
*/
1318+
getBaseTexture: function (key, cache) {
1319+
1320+
if (cache === undefined) { cache = Phaser.Cache.IMAGE; }
1321+
1322+
return this.getItem(key, cache, 'getBaseTexture', 'base');
1323+
1324+
},
1325+
12921326
/**
12931327
* Get a single frame by key. You'd only do this to get the default Frame created for a non-atlas/spritesheet image.
12941328
*

0 commit comments

Comments
 (0)