forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureManager.js
More file actions
284 lines (222 loc) · 7.33 KB
/
Copy pathTextureManager.js
File metadata and controls
284 lines (222 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Textures are managed by the global TextureManager. This is a singleton class that is
* responsible for creating and delivering Textures and their corresponding Frames to Game Objects.
*
* Sprites and other Game Objects get the texture data they need from the TextureManager.
*
* Access it via `state.textures`.
*
* @class Phaser.TextureManager
* @constructor
* @param {Phaser.Game} game
*/
Phaser.TextureManager = function (game)
{
this.game = game;
this.list = {};
this.parsers = {
Image: Phaser.TextureManager.Parsers.Image,
Canvas: Phaser.TextureManager.Parsers.Canvas,
JSONArray: Phaser.TextureManager.Parsers.JSONArray,
JSONHash: Phaser.TextureManager.Parsers.JSONHash,
StarlingXML: Phaser.TextureManager.Parsers.StarlingXML,
Pyxel: Phaser.TextureManager.Parsers.Pyxel,
SpriteSheet: Phaser.TextureManager.Parsers.SpriteSheet
};
};
Phaser.TextureManager.prototype.constructor = Phaser.TextureManager;
// Where the different parsers hook themselves
Phaser.TextureManager.Parsers = {};
Phaser.TextureManager.prototype = {
addImage: function (key, source)
{
console.log('TextureManager.addImage', key);
var texture = this.create(key, source);
this.parsers.Image(texture, 0);
return texture;
},
addCanvas: function (key, source)
{
var texture = this.create(key, source);
this.parsers.Canvas(texture, 0);
return texture;
},
addAtlasJSONArray: function (key, source, data)
{
var texture = this.create(key, source);
if (Array.isArray(data))
{
for (var i = 0; i < data.length; i++)
{
this.parsers.JSONArray(texture, i, data[i]);
}
}
else
{
this.parsers.JSONArray(texture, 0, data);
}
return texture;
},
addAtlasJSONHash: function (key, source, data)
{
var texture = this.create(key, source);
if (Array.isArray(data))
{
for (var i = 0; i < data.length; i++)
{
this.parsers.JSONHash(texture, i, data[i]);
}
}
else
{
this.parsers.JSONHash(texture, 0, data);
}
return texture;
},
addSpriteSheet: function (key, source, frameWidth, frameHeight, startFrame, endFrame, margin, spacing)
{
var texture = this.create(key, source);
var width = texture.source[0].width;
var height = texture.source[0].height;
this.parsers.SpriteSheet(texture, 0, 0, 0, width, height, frameWidth, frameHeight, startFrame, endFrame, margin, spacing);
return texture;
},
/*
addAtlasStarlingXML: function (key, source, data)
{
var texture = this.create(key, source);
return Phaser.TextureManager.Parsers.StarlingXML(texture, data);
},
addAtlasPyxel: function (key, source, data)
{
var texture = this.create(key, source);
return Phaser.TextureManager.Parsers.Pyxel(texture, data);
},
*/
create: function (key, source)
{
var texture = new Phaser.Texture(this, key, source);
this.list[key] = texture;
return texture;
},
exists: function (key)
{
return (this.list.hasOwnProperty(key));
},
get: function (key)
{
if (this.list[key])
{
return this.list[key];
}
},
getFrame: function (key, frame)
{
if (this.list[key])
{
return this.list[key].get(frame);
}
},
/**
* Passes all Textures to the given callback.
*
* @method each
* @param {function} callback - The function to call.
* @param {object} [thisArg] - Value to use as `this` when executing callback.
* @param {...*} [arguments] - Additional arguments that will be passed to the callback, after the child.
*/
each: function (callback, thisArg)
{
var args = [ null ];
for (var i = 1; i < arguments.length; i++)
{
args.push(arguments[i]);
}
for (var texture in this.list)
{
args[0] = this.list[texture];
callback.apply(thisArg, args);
}
},
/**
* TODO: This should move to the WebGL Renderer class.
*
* Removes the base texture from the GPU, useful for managing resources on the GPU.
* A texture is still 100% usable and will simply be re-uploaded if there is a sprite on screen that is using it.
*
* @method unloadFromGPU
*/
unloadFromGPU: function ()
{
this.dirty();
// delete the webGL textures if any.
for (var i = this._glTextures.length - 1; i >= 0; i--)
{
var glTexture = this._glTextures[i];
var gl = PIXI.glContexts[i];
if (gl && glTexture)
{
gl.deleteTexture(glTexture);
}
}
this._glTextures.length = 0;
this.dirty();
},
/**
* TODO: This should move to the WebGL Renderer class.
*
* Updates and Creates a WebGL texture for the renderers context.
*
* @method updateTexture
* @param texture {Texture} the texture to update
* @return {boolean} True if the texture was successfully bound, otherwise false.
*/
loadToGPU: function (texture)
{
if (!texture.hasLoaded)
{
return false;
}
if (texture.source.compressionAlgorithm)
{
return this.updateCompressedTexture(texture);
}
var gl = this.gl;
if (!texture._glTextures[gl.id])
{
texture._glTextures[gl.id] = gl.createTexture();
}
gl.activeTexture(gl.TEXTURE0 + texture.textureIndex);
gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]);
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultipliedAlpha);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.source);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === Phaser.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST);
if (texture.mipmap && Phaser.Math.isPowerOfTwo(texture.width, texture.height))
{
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === Phaser.scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST);
gl.generateMipmap(gl.TEXTURE_2D);
}
else
{
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === Phaser.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST);
}
if (!texture._powerOf2)
{
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
}
else
{
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
}
texture._dirty[gl.id] = false;
// return texture._glTextures[gl.id];
return true;
}
};