Skip to content

Commit 42b8118

Browse files
committed
All core features of the new Texture classes are now done. Multi-atlas support, Sprite Sheets embedded in atlases, shared source references, shared frame data, and split parsers. Phew.
1 parent 231cbc7 commit 42b8118

9 files changed

Lines changed: 361 additions & 143 deletions

File tree

build/config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@
125125
126126
<script src="$path/src/textures/TextureManager.js"></script>
127127
<script src="$path/src/textures/Texture.js"></script>
128+
<script src="$path/src/textures/TextureSource.js"></script>
128129
<script src="$path/src/textures/Frame.js"></script>
130+
<script src="$path/src/textures/parsers/Image.js"></script>
131+
<script src="$path/src/textures/parsers/Canvas.js"></script>
129132
<script src="$path/src/textures/parsers/JSONArray.js"></script>
130133
<script src="$path/src/textures/parsers/JSONHash.js"></script>
131134
<script src="$path/src/textures/parsers/SpriteSheet.js"></script>

src/textures/Frame.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
* @param {number} width - Width of the frame within the Texture.
1919
* @param {number} height - Height of the frame within the Texture.
2020
*/
21-
Phaser.TextureFrame = function (texture, name, x, y, width, height)
21+
Phaser.TextureFrame = function (texture, name, sourceIndex, x, y, width, height)
2222
{
2323

2424
/**
2525
* @property {Phaser.Texture} texture - The Texture this frame belongs to.
2626
*/
2727
this.texture = texture;
2828

29-
this.source = texture.source;
29+
this.source = texture.source[sourceIndex];
30+
31+
this.sourceIndex = sourceIndex;
3032

3133
/**
3234
* @property {string} name - The name of this frame within the Texture.

src/textures/Texture.js

Lines changed: 36 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -20,103 +20,34 @@
2020
* @param {object} source
2121
* @param {number} scaleMode
2222
*/
23-
Phaser.Texture = function (key, source, scaleMode)
23+
Phaser.Texture = function (manager, key, source)
2424
{
25+
this.manager = manager;
26+
27+
if (!Array.isArray(source))
28+
{
29+
source = [ source ];
30+
}
31+
2532
this.key = key;
2633

2734
/**
2835
* The source that is used to create the texture.
2936
* Usually an Image, but can also be a Canvas.
3037
*
3138
* @property source
32-
* @type Image
33-
*/
34-
this.source = source;
35-
36-
/**
37-
* The Resolution of the texture.
38-
*
39-
* @property resolution
40-
* @type Number
41-
*/
42-
this.resolution = 1;
43-
44-
/**
45-
* The width of the Texture.
46-
*
47-
* @property width
48-
* @type Number
49-
* @readOnly
50-
*/
51-
this.width = source.naturalWidth || source.width || 0;
52-
53-
/**
54-
* The height of the Texture.
55-
*
56-
* @property height
57-
* @type Number
58-
* @readOnly
59-
*/
60-
this.height = source.naturalHeight || source.height || 0;
61-
62-
/**
63-
* The scale mode to apply when scaling this texture
64-
*
65-
* @property scaleMode
66-
* @type {Number}
67-
* @default PIXI.scaleModes.LINEAR
68-
*/
69-
this.scaleMode = scaleMode || PIXI.scaleModes.DEFAULT;
70-
71-
/**
72-
* Controls if RGB channels should be pre-multiplied by Alpha (WebGL only)
73-
*
74-
* @property premultipliedAlpha
75-
* @type Boolean
76-
* @default true
77-
*/
78-
this.premultipliedAlpha = true;
79-
80-
/**
81-
* Set this to true if a mipmap of this texture needs to be generated. This value needs to be set before the texture is used
82-
* Also the texture must be a power of two size to work
83-
*
84-
* @property mipmap
85-
* @type {Boolean}
86-
*/
87-
this.mipmap = false;
88-
89-
/**
90-
* The multi texture batching index number.
91-
* @property textureIndex
92-
* @type Number
93-
*/
94-
this.textureIndex = 0;
95-
96-
/**
97-
* A BaseTexture can be set to skip the rendering phase in the WebGL Sprite Batch.
98-
*
99-
* You may want to do this if you have a parent Sprite with no visible texture (i.e. uses the internal `__default` texture)
100-
* that has children that you do want to render, without causing a batch flush in the process.
101-
*
102-
* @property renderable
103-
* @type Boolean
104-
*/
105-
this.renderable = false;
106-
107-
/**
108-
* @property isPowerOf2
109-
* @type boolean
39+
* @type array
11040
*/
111-
this.isPowerOf2 = Phaser.Math.isPowerOfTwo(this.width, this.height);
41+
this.source = [];
11242

11343
/**
11444
* @property {object} frames - Frames
11545
*/
11646
this.frames = {
117-
__BASE: new Phaser.TextureFrame(this, '__BASE', 0, 0, this.width, this.height)
11847
};
11948

49+
this.frameTotal = 0;
50+
12051
/**
12152
* @property _dirty
12253
* @type Array
@@ -130,19 +61,27 @@ Phaser.Texture = function (key, source, scaleMode)
13061
* @private
13162
*/
13263
this._glTextures = [];
64+
65+
// Load the Sources
66+
for (var i = 0; i < source.length; i++)
67+
{
68+
this.source.push(new Phaser.TextureSource(this, source[i]));
69+
}
13370

13471
};
13572

13673
Phaser.Texture.prototype.constructor = Phaser.Texture;
13774

13875
Phaser.Texture.prototype = {
13976

140-
add: function (name, x, y, width, height)
77+
add: function (name, sourceIndex, x, y, width, height)
14178
{
142-
var frame = new Phaser.TextureFrame(this, name, x, y, width, height);
79+
var frame = new Phaser.TextureFrame(this, name, sourceIndex, x, y, width, height);
14380

14481
this.frames[name] = frame;
14582

83+
this.frameTotal++;
84+
14685
return frame;
14786
},
14887

@@ -154,6 +93,20 @@ Phaser.Texture.prototype = {
15493

15594
},
15695

96+
getSpriteSheet: function (key, frame, frameWidth, frameHeight, startFrame, endFrame, margin, spacing)
97+
{
98+
var sheet = this.get(frame);
99+
100+
if (sheet)
101+
{
102+
var texture = this.manager.create(key, sheet.source.image);
103+
104+
this.manager.parsers.SpriteSheet(texture, 0, sheet.cutX, sheet.cutY, sheet.cutWidth, sheet.cutHeight, frameWidth, frameHeight, startFrame, endFrame, margin, spacing);
105+
106+
return texture;
107+
}
108+
},
109+
157110
/**
158111
* Sets all glTextures to be dirty.
159112
*

src/textures/TextureManager.js

Lines changed: 139 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,167 @@ Phaser.TextureManager = function (game)
2424
// Empty by default
2525
};
2626

27+
this.parsers = {
28+
Image: Phaser.TextureManager.Parsers.Image,
29+
Canvas: Phaser.TextureManager.Parsers.Canvas,
30+
JSONArray: Phaser.TextureManager.Parsers.JSONArray,
31+
JSONHash: Phaser.TextureManager.Parsers.JSONHash,
32+
StarlingXML: Phaser.TextureManager.Parsers.StarlingXML,
33+
Pyxel: Phaser.TextureManager.Parsers.Pyxel,
34+
SpriteSheet: Phaser.TextureManager.Parsers.SpriteSheet
35+
};
2736
};
2837

2938
Phaser.TextureManager.prototype.constructor = Phaser.TextureManager;
3039

40+
Phaser.TextureManager.Parsers = {
41+
// Where the different parsers hook themselves
42+
};
43+
3144
Phaser.TextureManager.prototype = {
3245

33-
create: function (key, source, scaleMode)
46+
addImage: function (key, source)
3447
{
35-
var texture = new Phaser.Texture(key, source, scaleMode);
48+
var texture = this.create(key, source);
49+
50+
this.parsers.Image(texture, 0);
3651

37-
this.list[key] = texture;
52+
return texture;
53+
54+
},
55+
56+
addCanvas: function (key, source)
57+
{
58+
var texture = this.create(key, source);
59+
60+
this.parsers.Canvas(texture, 0);
3861

3962
return texture;
4063

4164
},
4265

43-
get: function (key, frame)
66+
addAtlasJSONArray: function (key, source, data)
4467
{
45-
if (this.list[key])
68+
var texture = this.create(key, source);
69+
70+
if (Array.isArray(data))
4671
{
47-
if (frame)
72+
for (var i = 0; i < data.length; i++)
4873
{
49-
return this.list[key].get(frame);
74+
this.parsers.JSONArray(texture, i, data[i]);
5075
}
51-
else
76+
}
77+
else
78+
{
79+
this.parsers.JSONArray(texture, 0, data);
80+
}
81+
82+
return texture;
83+
},
84+
85+
addAtlasJSONHash: function (key, source, data)
86+
{
87+
var texture = this.create(key, source);
88+
89+
if (Array.isArray(data))
90+
{
91+
for (var i = 0; i < data.length; i++)
5292
{
53-
return this.list[key];
93+
this.parsers.JSONHash(texture, i, data[i]);
5494
}
5595
}
96+
else
97+
{
98+
this.parsers.JSONHash(texture, 0, data);
99+
}
56100

57-
}
101+
return texture;
102+
},
58103

59-
};
104+
addSpriteSheet: function (key, source, frameWidth, frameHeight, startFrame, endFrame, margin, spacing)
105+
{
106+
var texture = this.create(key, source);
60107

61-
Phaser.TextureManager.Parsers = {
108+
var width = texture.source[0].width;
109+
var height = texture.source[0].height;
110+
111+
this.parsers.SpriteSheet(texture, 0, 0, 0, width, height, frameWidth, frameHeight, startFrame, endFrame, margin, spacing);
112+
113+
return texture;
114+
},
115+
116+
/*
117+
addAtlasStarlingXML: function (key, source, data)
118+
{
119+
var texture = this.create(key, source);
120+
121+
return Phaser.TextureManager.Parsers.StarlingXML(texture, data);
122+
},
123+
124+
addAtlasPyxel: function (key, source, data)
125+
{
126+
var texture = this.create(key, source);
127+
128+
return Phaser.TextureManager.Parsers.Pyxel(texture, data);
129+
},
130+
*/
131+
132+
create: function (key, source)
133+
{
134+
var texture = new Phaser.Texture(this, key, source);
135+
136+
this.list[key] = texture;
137+
138+
return texture;
139+
140+
},
141+
142+
exists: function (key)
143+
{
144+
return (this.list.hasOwnProperty(key));
145+
},
146+
147+
get: function (key)
148+
{
149+
if (this.list[key])
150+
{
151+
return this.list[key];
152+
}
153+
154+
},
155+
156+
getFrame: function (key, frame)
157+
{
158+
if (this.list[key])
159+
{
160+
return this.list[key].get(frame);
161+
}
162+
163+
},
164+
165+
/**
166+
* Passes all Textures to the given callback.
167+
*
168+
* @method each
169+
* @param {function} callback - The function to call.
170+
* @param {object} [thisArg] - Value to use as `this` when executing callback.
171+
* @param {...*} [arguments] - Additional arguments that will be passed to the callback, after the child.
172+
*/
173+
each: function (callback, thisArg)
174+
{
175+
var args = [ null ];
176+
177+
for (var i = 1; i < arguments.length; i++)
178+
{
179+
args.push(arguments[i]);
180+
}
181+
182+
for (var texture in this.list)
183+
{
184+
args[0] = this.list[texture];
185+
186+
callback.apply(thisArg, args);
187+
}
188+
}
62189

63190
};

0 commit comments

Comments
 (0)