Skip to content

Commit 903b703

Browse files
committed
Added Multi Atlas loader support and fixed issue with Set.each iteration.
1 parent ba76c22 commit 903b703

7 files changed

Lines changed: 149 additions & 63 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: 'e17829d0-bc6b-11e6-a3c1-a55832c87262'
2+
build: '40d2cba0-bd62-11e6-be28-f17d7d3d6a73'
33
};
44
module.exports = CHECKSUM;

v3/src/loader/BaseLoader.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var BaseLoader = function ()
2323

2424
// Read from Game / State Config
2525
this.enableParallel = true;
26-
this.maxParallelDownloads = 8;
26+
this.maxParallelDownloads = 4;
2727

2828
// xhr specific global settings (can be overridden on a per-file basis)
2929
this.xhr = XHRSettings();
@@ -86,7 +86,7 @@ BaseLoader.prototype = {
8686

8787
start: function ()
8888
{
89-
// console.log('BaseLoader start. Files to load:', this.list.size);
89+
console.log('BaseLoader start. Files to load:', this.list.size);
9090

9191
if (!this.isReady())
9292
{
@@ -107,6 +107,8 @@ BaseLoader.prototype = {
107107
this.inflight.clear();
108108
this.queue.clear();
109109

110+
this.queue.debug = true;
111+
110112
this.updateProgress();
111113

112114
this.processLoadQueue();
@@ -120,16 +122,16 @@ BaseLoader.prototype = {
120122

121123
processLoadQueue: function ()
122124
{
123-
// console.log('BaseLoader processLoadQueue', this.list.size);
125+
// console.log('======== BaseLoader processLoadQueue');
126+
// console.log('List size', this.list.size);
127+
// console.log(this.inflight.size, 'items still in flight. Can load another', (this.maxParallelDownloads - this.inflight.size));
124128

125129
var _this = this;
126130

127131
this.list.each(function (file)
128132
{
129133
if (file.state === CONST.FILE_PENDING && _this.inflight.size < _this.maxParallelDownloads)
130134
{
131-
// console.log('ADDED TO QUEUE:', file.key);
132-
133135
_this.inflight.add(file);
134136

135137
_this.list.delete(file);
@@ -144,7 +146,6 @@ BaseLoader.prototype = {
144146
}
145147

146148
});
147-
148149
},
149150

150151
// private
@@ -193,36 +194,27 @@ BaseLoader.prototype = {
193194

194195
finishedLoading: function ()
195196
{
196-
// console.log('BaseLoader.finishedLoading PROCESSING', this.queue.size, 'files');
197+
// console.log('---> BaseLoader.finishedLoading PROCESSING', this.queue.size, 'files');
197198

198199
this._state = CONST.LOADER_PROCESSING;
199200

200-
var storage = this.storage;
201-
202-
storage.clear();
201+
this.storage.clear();
203202

204203
var _this = this;
205204

206205
this.queue.each(function (file)
207206
{
207+
// console.log('%c Calling process on ' + file.key, 'color: #000000; background: #ffff00;');
208+
208209
file.onProcess(_this.processUpdate.bind(_this));
209210
});
210211
},
211212

212-
removeFromQueue: function (file)
213-
{
214-
this.queue.delete(file);
215-
216-
if (this.queue.size === 0 && this._state === CONST.LOADER_PROCESSING)
217-
{
218-
// We've processed all the files we loaded
219-
this.processComplete();
220-
}
221-
},
222-
223213
// Called automatically by the File when it has finished processing
224214
processUpdate: function (file)
225215
{
216+
// console.log('-> processUpdate', file.key, file.state);
217+
226218
// This file has failed to load, so move it to the failed Set
227219
if (file.state === CONST.FILE_ERRORED)
228220
{
@@ -256,18 +248,30 @@ BaseLoader.prototype = {
256248
else
257249
{
258250
this.storage.add(file);
251+
259252
this.removeFromQueue(file);
260253
}
261254
},
262255

256+
removeFromQueue: function (file)
257+
{
258+
this.queue.delete(file);
259+
260+
if (this.queue.size === 0 && this._state === CONST.LOADER_PROCESSING)
261+
{
262+
// We've processed all the files we loaded
263+
this.processComplete();
264+
}
265+
},
266+
263267
processComplete: function ()
264268
{
269+
console.log('Loader Complete. Loaded:', this.storage.size, 'Failed:', this.failed.size);
270+
265271
this.list.clear();
266272
this.inflight.clear();
267273
this.queue.clear();
268274

269-
console.log('Loader Complete. Loaded:', this.storage.size, 'Failed:', this.failed.size);
270-
271275
if (this.processCallback)
272276
{
273277
this.processCallback();

v3/src/loader/File.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@ File.prototype = {
8686
{
8787
this.state = CONST.FILE_PROCESSING;
8888

89-
// If overridden by another class, it must call the callback when finished, then onComplete
90-
callback(this);
91-
9289
this.onComplete();
90+
91+
callback(this);
9392
},
9493

9594
onComplete: function ()
@@ -123,7 +122,14 @@ File.prototype = {
123122

124123
this.src = GetURL(this, baseURL);
125124

126-
this.xhrLoader = XHRLoader(this, globalXHR);
125+
if (this.src.indexOf('data:') === 0)
126+
{
127+
console.log('Local data URI');
128+
}
129+
else
130+
{
131+
this.xhrLoader = XHRLoader(this, globalXHR);
132+
}
127133
}
128134
};
129135

v3/src/loader/GetURL.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var GetURL = function (file, baseURL)
1111
}
1212
else
1313
{
14-
return baseURL + file.path + file.url;
14+
return baseURL + file.url;
1515
}
1616
};
1717

v3/src/state/systems/Loader.js

Lines changed: 77 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
var CONST = require('../../loader/const');
22
var BaseLoader = require('../../loader/BaseLoader');
3-
var ImageLoader = require('../../loader/filetypes/ImageFile');
3+
var ImageFile = require('../../loader/filetypes/ImageFile');
4+
var JSONFile = require('../../loader/filetypes/JSONFile');
45
var AtlasJSONFile = require('../../loader/filetypes/AtlasJSONFile');
6+
var NumberArray = require('../../utils/array/NumberArray');
57

68
var Loader = function (state)
79
{
@@ -12,14 +14,25 @@ var Loader = function (state)
1214
* @protected
1315
*/
1416
this.state = state;
17+
18+
this._multilist = {};
1519
};
1620

1721
Loader.prototype = Object.create(BaseLoader.prototype);
1822
Loader.prototype.constructor = Loader;
1923

2024
Loader.prototype.image = function (key, url)
2125
{
22-
var file = new ImageLoader(key, url, this.path);
26+
var file = new ImageFile(key, url, this.path);
27+
28+
this.addFile(file);
29+
30+
return this;
31+
};
32+
33+
Loader.prototype.json = function (key, url)
34+
{
35+
var file = new JSONFile(key, url, this.path);
2336

2437
this.addFile(file);
2538

@@ -35,24 +48,14 @@ Loader.prototype.atlas = function (key, textureURL, atlasURL)
3548
return this;
3649
};
3750

38-
/**
39-
* @method Phaser.Loader#multiatlas
40-
* @param {string} key - Unique asset key of the texture atlas file.
41-
* @param {array|integer} textureURLs - An array of PNG files, or an integer.
42-
* @param {array} [atlasURLs] - An array of JSON files.
43-
* @param {number} [format] - The format of the data. Can be Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY (the default), Phaser.Loader.TEXTURE_ATLAS_JSON_HASH or Phaser.Loader.TEXTURE_ATLAS_XML_STARLING.
44-
* @return {Phaser.Loader} This Loader instance.
45-
*/
46-
Loader.prototype.multiatlas = function (key, textureURLs, atlasURLs, format)
51+
Loader.prototype.multiatlas = function (key, textureURLs, atlasURLs)
4752
{
48-
if (format === undefined) { format = CONST.TEXTURE_ATLAS_JSON_ARRAY; }
49-
5053
if (typeof textureURLs === 'number')
5154
{
5255
var total = textureURLs;
5356

54-
textureURLs = Phaser.ArrayUtils.numberArray(0, total, key + '-', '.png');
55-
atlasURLs = Phaser.ArrayUtils.numberArray(0, total, key + '-', '.json');
57+
textureURLs = NumberArray(0, total, key + '-', '.png');
58+
atlasURLs = NumberArray(0, total, key + '-', '.json');
5659
}
5760
else
5861
{
@@ -69,37 +72,31 @@ Loader.prototype.multiatlas = function (key, textureURLs, atlasURLs, format)
6972

7073
var i = 0;
7174
var multiKey;
72-
var imgs = [];
73-
var data = [];
75+
76+
this._multilist[key] = [];
7477

7578
for (i = 0; i < textureURLs.length; i++)
7679
{
77-
// TODO - Add support for compressed textures
78-
multiKey = '_MA_' + key + '_' + i.toString();
80+
multiKey = '_MA_IMG_' + key + '_' + i.toString();
7981

80-
imgs.push(multiKey);
82+
this.addFile(new ImageFile(multiKey, textureURLs[i], this.path));
8183

82-
this.addToFileList('image', multiKey, textureURLs[i], { multiatlas: true });
84+
this._multilist[key].push(multiKey);
8385
}
8486

8587
for (i = 0; i < atlasURLs.length; i++)
8688
{
87-
multiKey = '_MA_' + key + '_' + i.toString();
89+
multiKey = '_MA_JSON_' + key + '_' + i.toString();
8890

89-
data.push(multiKey);
91+
this.addFile(new JSONFile(multiKey, atlasURLs[i], this.path));
9092

91-
// Check if this can support XML as well?
92-
this.addToFileList('json', multiKey, atlasURLs[i], { multiatlas: true });
93+
this._multilist[key].push(multiKey);
9394
}
94-
95-
this._multilist.push({ key: key, images: imgs, data: data, format: format });
96-
97-
9895
};
9996

97+
// The Loader has finished
10098
Loader.prototype.processCallback = function ()
10199
{
102-
// All of the files have loaded. Now to put them into the Cache.
103100
if (this.storage.size === 0)
104101
{
105102
return;
@@ -108,6 +105,52 @@ Loader.prototype.processCallback = function ()
108105
// The global Texture Manager
109106
var textures = this.state.sys.textures;
110107

108+
// Process multiatlas groups first
109+
110+
var file;
111+
112+
for (var key in this._multilist)
113+
{
114+
var data = [];
115+
var images = [];
116+
var keys = this._multilist[key];
117+
118+
for (var i = 0; i < keys.length; i++)
119+
{
120+
file = this.storage.get('key', keys[i]);
121+
122+
if (file)
123+
{
124+
if (file.type === 'image')
125+
{
126+
images.push(file.data);
127+
}
128+
else if (file.type === 'json')
129+
{
130+
data.push(file.data);
131+
}
132+
133+
this.storage.delete(file);
134+
}
135+
}
136+
137+
// Do we have everything needed?
138+
if (images.length + data.length === keys.length)
139+
{
140+
// Yup, add them to the Texture Manager
141+
142+
// Is the data JSON Hash or JSON Array?
143+
if (Array.isArray(data[0].frames))
144+
{
145+
textures.addAtlasJSONArray(key, images, data);
146+
}
147+
else
148+
{
149+
textures.addAtlasJSONHash(key, images, data);
150+
}
151+
}
152+
}
153+
111154
this.storage.each(function (file)
112155
{
113156
if (file.type === 'image')
@@ -128,6 +171,10 @@ Loader.prototype.processCallback = function ()
128171
textures.addAtlas(fileB.key, fileB.data, fileA.data);
129172
}
130173
}
174+
else if (file.type === 'json')
175+
{
176+
// console.dir(file.data);
177+
}
131178
});
132179

133180
this.storage.clear();

v3/src/structs/Set.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,40 @@ Set.prototype = {
2525
}
2626
},
2727

28-
each: function (callback)
28+
dump: function ()
29+
{
30+
console.group('Set');
31+
32+
for (var i = 0; i < this.values.length; i++)
33+
{
34+
var entry = this.values[i];
35+
console.log(entry);
36+
}
37+
38+
console.groupEnd();
39+
},
40+
41+
get: function (property, value)
2942
{
3043
for (var i = 0; i < this.values.length; i++)
3144
{
32-
if (callback(this.values[i]) === false)
45+
var entry = this.values[i];
46+
47+
if (entry[property] === value)
48+
{
49+
return entry;
50+
}
51+
}
52+
},
53+
54+
each: function (callback)
55+
{
56+
// Because it's highly likely the callback may modify the Set
57+
var temp = this.values.slice();
58+
59+
for (var i = 0; i < temp.length; i++)
60+
{
61+
if (callback(temp[i]) === false)
3362
{
3463
break;
3564
}

0 commit comments

Comments
 (0)