Skip to content

Commit 0f3cda0

Browse files
committed
Cache.getRenderTexture will retrieve a RenderTexture that is stored in the Phaser Cache. This method replaces Cache.getTexture which is now deprecated.
Cache.autoResolveURL is a new boolean (default `false`) that automatically builds a cached map of all loaded assets vs. their absolute URLs, for use with Cache.getURL and Cache.checkURL. Note that in 2.1.3 and earlier this was enabled by default, but has since been moved behind this property which needs to be set to `true` *before* you load any assets to enable. Cache._resolveUrl has been renamed to Cache._resolveURL internally and gained a new parameter. This method is a private internal one. Cache.getUrl is deprecated. The same method is now available as Cache.getURL. XML files weren't being added to the URL map. Cache._resolveURL was causing a Sound double-load in Firefox and causing errors (thanks @domonyiv phaserjs#1253)
1 parent 12c2f83 commit 0f3cda0

2 files changed

Lines changed: 100 additions & 26 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,21 @@ Version 2.1.4 - "Bethal" - in development
7575

7676
### New Features
7777

78+
* Cache.getRenderTexture will retrieve a RenderTexture that is stored in the Phaser Cache. This method replaces Cache.getTexture which is now deprecated.
79+
* Cache.autoResolveURL is a new boolean (default `false`) that automatically builds a cached map of all loaded assets vs. their absolute URLs, for use with Cache.getURL and Cache.checkURL. Note that in 2.1.3 and earlier this was enabled by default, but has since been moved behind this property which needs to be set to `true` *before* you load any assets to enable.
80+
81+
7882
### Updates
7983

8084
* TypeScript definitions fixes and updates (thanks @clark-stevenson)
85+
* Cache._resolveUrl has been renamed to Cache._resolveURL internally and gained a new parameter. This method is a private internal one.
86+
* Cache.getUrl is deprecated. The same method is now available as Cache.getURL.
8187

8288
### Bug Fixes
8389

8490
* Tilemaps in WebGL wouldn't update after the first frame due to a subtle change in how Pixi uploads new textures to the GPU.
91+
* XML files weren't being added to the URL map.
92+
* Cache._resolveURL was causing a Sound double-load in Firefox and causing errors (thanks @domonyiv #1253)
8593

8694

8795
For details about changes made in previous versions of Phaser see the full Change Log at https://github.com/photonstorm/phaser/blob/master/CHANGELOG.md

src/loader/Cache.js

Lines changed: 92 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ Phaser.Cache = function (game) {
1919
*/
2020
this.game = game;
2121

22+
/**
23+
* @property {boolean} autoResolveURL - Automatically resolve resource URLs to absolute paths for use with the Cache.getURL method.
24+
*/
25+
this.autoResolveURL = false;
26+
2227
/**
2328
* @property {object} _canvases - Canvas key-value container.
2429
* @private
@@ -294,7 +299,7 @@ Phaser.Cache.prototype = {
294299

295300
this._images[key].frameData = Phaser.AnimationParser.spriteSheet(this.game, key, frameWidth, frameHeight, frameMax, margin, spacing);
296301

297-
this._urlMap[this._resolveUrl(url)] = this._images[key];
302+
this._resolveURL(url, this._images[key]);
298303

299304
},
300305

@@ -311,7 +316,7 @@ Phaser.Cache.prototype = {
311316

312317
this._tilemaps[key] = { url: url, data: mapData, format: format };
313318

314-
this._urlMap[this._resolveUrl(url)] = this._tilemaps[key];
319+
this._resolveURL(url, this._tilemaps[key]);
315320

316321
},
317322

@@ -345,7 +350,7 @@ Phaser.Cache.prototype = {
345350
this._images[key].frameData = Phaser.AnimationParser.XMLData(this.game, atlasData, key);
346351
}
347352

348-
this._urlMap[this._resolveUrl(url)] = this._images[key];
353+
this._resolveURL(url, this._images[key]);
349354

350355
},
351356

@@ -371,7 +376,7 @@ Phaser.Cache.prototype = {
371376

372377
this._bitmapFont[key] = PIXI.BitmapText.fonts[key];
373378

374-
this._urlMap[this._resolveUrl(url)] = this._bitmapFont[key];
379+
this._resolveURL(url, this._bitmapFont[key]);
375380

376381
},
377382

@@ -388,7 +393,7 @@ Phaser.Cache.prototype = {
388393

389394
this._physics[key] = { url: url, data: JSONData, format: format };
390395

391-
this._urlMap[this._resolveUrl(url)] = this._physics[key];
396+
this._resolveURL(url, this._physics[key]);
392397

393398
},
394399

@@ -446,7 +451,7 @@ Phaser.Cache.prototype = {
446451

447452
this._text[key] = { url: url, data: data };
448453

449-
this._urlMap[this._resolveUrl(url)] = this._text[key];
454+
this._resolveURL(url, this._text[key]);
450455

451456
},
452457

@@ -462,7 +467,7 @@ Phaser.Cache.prototype = {
462467

463468
this._json[key] = { url: url, data: data };
464469

465-
this._urlMap[this._resolveUrl(url)] = this._json[key];
470+
this._resolveURL(url, this._json[key]);
466471

467472
},
468473

@@ -478,10 +483,12 @@ Phaser.Cache.prototype = {
478483

479484
this._xml[key] = { url: url, data: data };
480485

486+
this._resolveURL(url, this._xml[key]);
487+
481488
},
482489

483490
/**
484-
* Adds an Image file into the Cache. The file must have already been loaded, typically via Phaser.Loader.
491+
* Adds an Image file into the Cache. The file must have already been loaded, typically via Phaser.Loader, but can also have been loaded into the DOM.
485492
*
486493
* @method Phaser.Cache#addImage
487494
* @param {string} key - The unique key by which you will reference this object.
@@ -499,7 +506,7 @@ Phaser.Cache.prototype = {
499506
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
500507
PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]);
501508

502-
this._urlMap[this._resolveUrl(url)] = this._images[key];
509+
this._resolveURL(url, this._images[key]);
503510

504511
},
505512

@@ -527,7 +534,7 @@ Phaser.Cache.prototype = {
527534

528535
this._sounds[key] = { url: url, data: data, isDecoding: false, decoded: decoded, webAudio: webAudio, audioTag: audioTag, locked: this.game.sound.touchLocked };
529536

530-
this._urlMap[this._resolveUrl(url)] = this._sounds[key];
537+
this._resolveURL(url, this._sounds[key]);
531538

532539
},
533540

@@ -701,7 +708,6 @@ Phaser.Cache.prototype = {
701708
{
702709
return fixture;
703710
}
704-
705711
}
706712

707713
// We did not find the requested fixture
@@ -899,14 +905,17 @@ Phaser.Cache.prototype = {
899905

900906
/**
901907
* Checks if the given URL has been loaded into the Cache.
908+
* This method will only work if Cache.autoResolveURL was set to `true` before any preloading took place.
909+
* The method will make a DOM src call to the URL given, so please be aware of this for certain file types, such as Sound files on Firefox
910+
* which may cause double-load instances.
902911
*
903-
* @method Phaser.Cache#checkUrl
912+
* @method Phaser.Cache#checkURL
904913
* @param {string} url - The url to check for in the cache.
905914
* @return {boolean} True if the url exists, otherwise false.
906915
*/
907-
checkUrl: function (url) {
916+
checkURL: function (url) {
908917

909-
if (this._urlMap[this._resolveUrl(url)])
918+
if (this._urlMap[this._resolveURL(url)])
910919
{
911920
return true;
912921
}
@@ -916,11 +925,11 @@ Phaser.Cache.prototype = {
916925
},
917926

918927
/**
919-
* Get image data by key.
928+
* Gets an image by its key. Note that this returns a DOM Image object, not a Phaser object.
920929
*
921930
* @method Phaser.Cache#getImage
922931
* @param {string} key - Asset key of the image to retrieve from the Cache.
923-
* @return {object} The image data if found in the Cache, otherwise `null`.
932+
* @return {Image} The Image object if found in the Cache, otherwise `null`.
924933
*/
925934
getImage: function (key) {
926935

@@ -1063,7 +1072,30 @@ Phaser.Cache.prototype = {
10631072
/**
10641073
* Get a RenderTexture by key.
10651074
*
1075+
* @method Phaser.Cache#getRenderTexture
1076+
* @param {string} key - Asset key of the RenderTexture to retrieve from the Cache.
1077+
* @return {Phaser.RenderTexture} The RenderTexture object.
1078+
*/
1079+
getRenderTexture: function (key) {
1080+
1081+
if (this._textures[key])
1082+
{
1083+
return this._textures[key];
1084+
}
1085+
else
1086+
{
1087+
console.warn('Phaser.Cache.getTexture: Invalid key: "' + key + '"');
1088+
}
1089+
1090+
},
1091+
1092+
/**
1093+
* DEPRECATED: Please use Cache.getRenderTexture instead. This method will be removed in Phaser 2.2.0.
1094+
*
1095+
* Get a RenderTexture by key.
1096+
*
10661097
* @method Phaser.Cache#getTexture
1098+
* @deprecated Please use Cache.getRenderTexture instead. This method will be removed in Phaser 2.2.0.
10671099
* @param {string} key - Asset key of the RenderTexture to retrieve from the Cache.
10681100
* @return {Phaser.RenderTexture} The RenderTexture object.
10691101
*/
@@ -1249,24 +1281,45 @@ Phaser.Cache.prototype = {
12491281

12501282
/**
12511283
* Get a cached object by the URL.
1284+
* This only returns a value if you set Cache.autoResolveURL to `true` *before* starting the preload of any assets.
1285+
* Be aware that every call to this function makes a DOM src query, so use carefully and double-check for implications in your target browsers/devices.
12521286
*
1253-
* @method Phaser.Cache#getUrl
1287+
* @method Phaser.Cache#getURL
12541288
* @param {string} url - The url for the object loaded to get from the cache.
12551289
* @return {object} The cached object.
12561290
*/
1257-
getUrl: function (url) {
1291+
getURL: function (url) {
1292+
1293+
var url = this._resolveURL(url);
12581294

1259-
if (this._urlMap[this._resolveUrl(url)])
1295+
if (url)
12601296
{
1261-
return this._urlMap[this._resolveUrl(url)];
1297+
return this._urlMap[url];
12621298
}
12631299
else
12641300
{
1265-
console.warn('Phaser.Cache.getUrl: Invalid url: "' + url + '"');
1301+
console.warn('Phaser.Cache.getUrl: Invalid url: "' + url + '" or Cache.autoResolveURL was false');
12661302
}
12671303

12681304
},
12691305

1306+
/**
1307+
* DEPRECATED: Please use Cache.getURL instead.
1308+
* Get a cached object by the URL.
1309+
* This only returns a value if you set Cache.autoResolveURL to `true` *before* starting the preload of any assets.
1310+
* Be aware that every call to this function makes a DOM src query, so use carefully and double-check for implications in your target browsers/devices.
1311+
*
1312+
* @method Phaser.Cache#getUrl
1313+
* @deprecated Please use Cache.getURL instead.
1314+
* @param {string} url - The url for the object loaded to get from the cache.
1315+
* @return {object} The cached object.
1316+
*/
1317+
getUrl: function (url) {
1318+
1319+
return this.getURL(url);
1320+
1321+
},
1322+
12701323
/**
12711324
* Gets all keys used by the Cache for the given data type.
12721325
*
@@ -1469,14 +1522,21 @@ Phaser.Cache.prototype = {
14691522
},
14701523

14711524
/**
1472-
* Resolves a URL to its absolute form.
1525+
* Resolves a URL to its absolute form and stores it in Cache._urlMap as long as Cache.autoResolveURL is set to `true`.
1526+
* This is then looked-up by the Cache.getURL and Cache.checkURL calls.
14731527
*
1474-
* @method Phaser.Cache#_resolveUrl
1475-
* @param {string} url - The URL to resolve.
1476-
* @return {string} The resolved URL.
1528+
* @method Phaser.Cache#_resolveURL
14771529
* @private
1530+
* @param {string} url - The URL to resolve. This is appended to Loader.baseURL.
1531+
* @param {object} [data] - The data associated with the URL to be stored to the URL Map.
1532+
* @return {string} The resolved URL.
14781533
*/
1479-
_resolveUrl: function (url) {
1534+
_resolveURL: function (url, data) {
1535+
1536+
if (!this.autoResolveURL)
1537+
{
1538+
return null;
1539+
}
14801540

14811541
this._urlResolver.src = this.game.load.baseURL + url;
14821542

@@ -1485,6 +1545,12 @@ Phaser.Cache.prototype = {
14851545
// Ensure no request is actually made
14861546
this._urlResolver.src = '';
14871547

1548+
// Record the URL to the map
1549+
if (data)
1550+
{
1551+
this._urlMap[this._urlTemp] = data;
1552+
}
1553+
14881554
return this._urlTemp;
14891555

14901556
},

0 commit comments

Comments
 (0)