Skip to content

Commit bab50d7

Browse files
committed
* The Cache has been internally refactored considerably. Image data is now all stored in the same object, rather than being split across the PIXI global caches (such as PIXI.TextureCache and PIXI.BaseTextureCache), which are no longer used by Phaser.
* Internally the Cache now uses a single _cache object, which is partitioned to store the various different object types. Before the cache used lots of private objects, one per data type, but it's now a lot cleaner and we've managed to cut out hundreds of lines of duplicate code in the process. * Cache.getImage has a new argument which lets you return either just the HTML Image element or the entire image cache object, which includes the baseTexture and frame data. * Cache.getImage will return a __default image if the key isn't given, or a __missing image if the key is given but not found in the cache. This means it will always return a valid image and no longer cause Phaser to throw runtime errors deeper down with invalid image objects.
1 parent c6f8973 commit bab50d7

4 files changed

Lines changed: 69 additions & 83 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,10 @@ For the full list of p2 additions please read [their change log](https://github.
335335
* Loader.shader allows you to load a fragment shader from an external file.
336336
* Cache.addShader adds a fragment shader into the cache.
337337
* Cache.getShader gets a fragment shader from the cache.
338-
* The Cache has been internally refactored considerably. Images are now no longer grouped in a single Images cache, but split based on their type. For example you can now load an image called 'fire' and a texture atlas called 'fire' at they are placed into unique caches, where-as before you had to be careful not to use the same keys for anything using an image.
338+
* The Cache has been internally refactored considerably. Image data is now all stored in the same object, rather than being split across the PIXI global caches (such as PIXI.TextureCache and PIXI.BaseTextureCache), which are no longer used by Phaser.
339339
* Internally the Cache now uses a single _cache object, which is partitioned to store the various different object types. Before the cache used lots of private objects, one per data type, but it's now a lot cleaner and we've managed to cut out hundreds of lines of duplicate code in the process.
340+
* Cache.getImage has a new argument which lets you return either just the HTML Image element or the entire image cache object, which includes the baseTexture and frame data.
341+
* Cache.getImage will return a __default image if the key isn't given, or a __missing image if the key is given but not found in the cache. This means it will always return a valid image and no longer cause Phaser to throw runtime errors deeper down with invalid image objects.
340342

341343
### Updates
342344

src/gameobjects/components/LoadTexture.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,32 @@ Phaser.Component.LoadTexture.prototype = {
8888
}
8989
else
9090
{
91-
if (key === null || key === undefined)
92-
{
93-
this.key = '__default';
94-
// this.setTexture(PIXI.TextureCache[this.key]);
95-
this.setTexture(cache.getTexture(this.key));
96-
}
97-
else if (typeof key === 'string' && !cache.checkImageKey(key))
98-
{
99-
console.warn("Texture with key '" + key + "' not found.");
100-
this.key = '__missing';
101-
// this.setTexture(PIXI.TextureCache[this.key]);
102-
this.setTexture(cache.getTexture(this.key));
103-
}
104-
else
105-
{
106-
this.setTexture(new PIXI.Texture(PIXI.BaseTextureCache[key]));
107-
108-
setFrame = !this.animations.loadFrameData(cache.getFrameData(key), frame);
109-
}
91+
var img = cache.getImage(key, true);
92+
93+
this.key = img.key;
94+
this.setTexture(new PIXI.Texture(img.base));
95+
96+
setFrame = !this.animations.loadFrameData(cache.getFrameData(this.key), frame);
97+
98+
// if (key === null || key === undefined)
99+
// {
100+
// this.key = '__default';
101+
// // this.setTexture(PIXI.TextureCache[this.key]);
102+
// this.setTexture(cache.getTexture(this.key));
103+
// }
104+
// else if (typeof key === 'string' && !cache.checkImageKey(key))
105+
// {
106+
// console.warn("Texture with key '" + key + "' not found.");
107+
// this.key = '__missing';
108+
// // this.setTexture(PIXI.TextureCache[this.key]);
109+
// this.setTexture(cache.getTexture(this.key));
110+
// }
111+
// else
112+
// {
113+
// this.setTexture(new PIXI.Texture(PIXI.BaseTextureCache[key]));
114+
115+
// setFrame = !this.animations.loadFrameData(cache.getFrameData(key), frame);
116+
// }
110117
}
111118

112119
if (setFrame)

src/loader/Cache.js

Lines changed: 39 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* The cache is automatically populated by the Phaser.Loader. When you use the loader to pull in external assets
1616
* such as images they are automatically placed into their respective cache. Most common Game Objects, such as
17-
* Sprites and Videos automatically query the cache to extra the assets they need on instantiation.
17+
* Sprites and Videos automatically query the cache to extract the assets they need on instantiation.
1818
*
1919
* You can access the cache from within a State via `this.cache`. From here you can call any public method it has,
2020
* including adding new entries to it, deleting them or querying them.
@@ -67,9 +67,7 @@ Phaser.Cache = function (game) {
6767
data: {}
6868
},
6969
shader: {},
70-
renderTexture: {},
71-
spriteSheet: {},
72-
atlas: {}
70+
renderTexture: {}
7371
};
7472

7573
/**
@@ -116,8 +114,6 @@ Phaser.Cache = function (game) {
116114
this._cacheMap[Phaser.Cache.VIDEO] = this._cache.video;
117115
this._cacheMap[Phaser.Cache.SHADER] = this._cache.shader;
118116
this._cacheMap[Phaser.Cache.RENDER_TEXTURE] = this._cache.renderTexture;
119-
this._cacheMap[Phaser.Cache.SPRITE_SHEET] = this._cache.spriteSheet;
120-
this._cacheMap[Phaser.Cache.TEXTURE_ATLAS] = this._cache.atlas;
121117

122118
this.addDefaultImage();
123119
this.addMissingImage();
@@ -214,18 +210,6 @@ Phaser.Cache.SHADER = 14;
214210
*/
215211
Phaser.Cache.RENDER_TEXTURE = 15;
216212

217-
/**
218-
* @constant
219-
* @type {number}
220-
*/
221-
Phaser.Cache.SPRITE_SHEET = 16;
222-
223-
/**
224-
* @constant
225-
* @type {number}
226-
*/
227-
Phaser.Cache.TEXTURE_ATLAS = 17;
228-
229213
Phaser.Cache.prototype = {
230214

231215
//////////////////
@@ -265,6 +249,7 @@ Phaser.Cache.prototype = {
265249
}
266250

267251
var img = {
252+
key: key,
268253
url: url,
269254
data: data,
270255
base: new PIXI.BaseTexture(data),
@@ -584,7 +569,7 @@ Phaser.Cache.prototype = {
584569
frameData: Phaser.AnimationParser.spriteSheet(this.game, key, frameWidth, frameHeight, frameMax, margin, spacing)
585570
};
586571

587-
this._cache.spriteSheet[key] = obj;
572+
this._cache.image[key] = obj;
588573

589574
this._resolveURL(url, obj);
590575

@@ -602,9 +587,13 @@ Phaser.Cache.prototype = {
602587
*/
603588
addTextureAtlas: function (key, url, data, atlasData, format) {
604589

605-
var obj = { url: url, data: data, base: new PIXI.BaseTexture(data) };
590+
var obj = {
591+
url: url,
592+
data: data,
593+
base: new PIXI.BaseTexture(data)
594+
};
606595

607-
if (format == Phaser.Loader.TEXTURE_ATLAS_XML_STARLING)
596+
if (format === Phaser.Loader.TEXTURE_ATLAS_XML_STARLING)
608597
{
609598
obj.frameData = Phaser.AnimationParser.XMLData(this.game, atlasData, key);
610599
}
@@ -621,7 +610,7 @@ Phaser.Cache.prototype = {
621610
}
622611
}
623612

624-
this._cache.atlas[key] = obj;
613+
this._cache.image[key] = obj;
625614

626615
this._resolveURL(url, obj);
627616

@@ -984,34 +973,6 @@ Phaser.Cache.prototype = {
984973

985974
},
986975

987-
/**
988-
* Checks if the given key exists in the Sprite Sheet Cache.
989-
* Note that this is a different cache to the Images and Texture Atlas caches.
990-
*
991-
* @method Phaser.Cache#checkSpriteSheetKey
992-
* @param {string} key - The key of the asset within the cache.
993-
* @return {boolean} True if the key exists in the cache, otherwise false.
994-
*/
995-
checkSpriteSheetKey: function (key) {
996-
997-
return this.checkKey(Phaser.Cache.SPRITE_SHEET, key);
998-
999-
},
1000-
1001-
/**
1002-
* Checks if the given key exists in the Texture Atlas Cache.
1003-
* Note that this is a different cache to the Images and Sprite Sheet caches.
1004-
*
1005-
* @method Phaser.Cache#checkTextureAtlasKey
1006-
* @param {string} key - The key of the asset within the cache.
1007-
* @return {boolean} True if the key exists in the cache, otherwise false.
1008-
*/
1009-
checkTextureAtlasKey: function (key) {
1010-
1011-
return this.checkKey(Phaser.Cache.TEXTURE_ATLAS, key);
1012-
1013-
},
1014-
1015976
////////////////
1016977
// Get Items //
1017978
////////////////
@@ -1078,17 +1039,39 @@ Phaser.Cache.prototype = {
10781039
*
10791040
* Note: If the object cannot be found a `console.warn` message is displayed.
10801041
*
1081-
* Only the Image cache is searched, which covers images loaded via Loader.image.
1042+
* Only the Image cache is searched, which covers images loaded via Loader.image, Sprite Sheets and Texture Atlases.
10821043
*
1083-
* If you need the image used by a texture atlas, bitmap font or similar then please use those respective 'get' methods.
1044+
* If you need the image used by a bitmap font or similar then please use those respective 'get' methods.
10841045
*
10851046
* @method Phaser.Cache#getImage
1086-
* @param {string} key - The key of the asset to retrieve from the cache.
1087-
* @return {Image} The Image object if found in the Cache, otherwise `null`.
1047+
* @param {string} [key] - The key of the asset to retrieve from the cache. If not given or null it will return a default image. If given but not found in the cache it will throw a warning and return the missing image.
1048+
* @param {boolean} [full=false] - If true the full image object will be returned, if false just the HTML Image object is returned.
1049+
* @return {Image} The Image object if found in the Cache, otherwise `null`. If `full` was true then a JavaScript object is returned.
10881050
*/
1089-
getImage: function (key) {
1051+
getImage: function (key, full) {
1052+
1053+
if (key === undefined || key === null)
1054+
{
1055+
key = '__default';
1056+
}
1057+
1058+
if (full === undefined) { full = false; }
1059+
1060+
var img = this.getItem(key, Phaser.Cache.IMAGE, 'getImage');
1061+
1062+
if (img === null)
1063+
{
1064+
img = this.getItem('__missing', Phaser.Cache.IMAGE, 'getImage');
1065+
}
10901066

1091-
return this.getItem(key, Phaser.Cache.IMAGE, 'getImage', 'data');
1067+
if (full)
1068+
{
1069+
return img;
1070+
}
1071+
else
1072+
{
1073+
return img.data;
1074+
}
10921075

10931076
},
10941077

@@ -1393,8 +1376,6 @@ Phaser.Cache.prototype = {
13931376

13941377
},
13951378

1396-
// TODO: Get Sprite Sheet + Atlas
1397-
13981379
////////////////////////////
13991380
// Frame Related Methods //
14001381
////////////////////////////

typescript/phaser.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,8 @@ declare module Phaser {
379379
static RENDER_TEXTURE: number;
380380
static SHADER: number;
381381
static SOUND: number;
382-
static SPRITE_SHEET: number;
383382
static TEXT: number;
384383
static TEXTURE: number;
385-
static TEXTURE_ATLAS: number;
386384
static TILEMAP: number;
387385
static XML: number;
388386
static VIDEO: number;
@@ -420,10 +418,8 @@ declare module Phaser {
420418
checkRenderTextureKey(key: string): boolean;
421419
checkShaderKey(key: string): boolean;
422420
checkSoundKey(key: string): boolean;
423-
checkSpriteSheetKey(key: string): boolean;
424421
checkTextKey(key: string): boolean;
425422
checkTextureKey(key: string): boolean;
426-
checkTextureAtlasKey(key: string): boolean;
427423
checkTilemapKey(key: string): boolean;
428424
checkURL(url: string): any;
429425
checkUrl(url: string): any;
@@ -441,7 +437,7 @@ declare module Phaser {
441437
getFrameByName(key: string, name: string, cache?: number): Phaser.Frame;
442438
getFrameCount(key: string): number;
443439
getFrameData(key: string, cache?: string): Phaser.FrameData;
444-
getImage(key: string): Phaser.Image;
440+
getImage(key: string, full?: boolean): Phaser.Image;
445441
getItem(key: string, cache: number, method?: string, property?: string): any;
446442
getJSON(key: string, clone?: boolean): any;
447443
getKeys(cache: number): string[];

0 commit comments

Comments
 (0)