Skip to content

Commit ce3308e

Browse files
committed
Hooking the Loader and Cache into the new Texture Manager.
1 parent e0ef9ca commit ce3308e

11 files changed

Lines changed: 110 additions & 124 deletions

File tree

src/gameobjects/components/Core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Phaser.Component.Core.init = function (game, x, y, key, frame) {
8080

8181
if (this.components.LoadTexture && key !== null)
8282
{
83-
this.loadTexture(key, frame);
83+
// this.loadTexture(key, frame);
8484
}
8585

8686
if (this.components.FixedToCamera)

src/gameobjects/image/Image.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* @extends PIXI.Sprite
1313
* @extends Phaser.Component.Core
1414
* @extends Phaser.Component.Angle
15-
* @extends Phaser.Component.Animation
1615
* @extends Phaser.Component.AutoCull
1716
* @extends Phaser.Component.Bounds
1817
* @extends Phaser.Component.BringToTop
@@ -21,7 +20,6 @@
2120
* @extends Phaser.Component.FixedToCamera
2221
* @extends Phaser.Component.InputEnabled
2322
* @extends Phaser.Component.LifeSpan
24-
* @extends Phaser.Component.LoadTexture
2523
* @extends Phaser.Component.Overlap
2624
* @extends Phaser.Component.Reset
2725
* @extends Phaser.Component.ScaleMinMax
@@ -33,12 +31,10 @@
3331
* @param {string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture} [key] - The texture used by the Image during rendering. It can be a string which is a reference to the Cache entry, or an instance of a RenderTexture, BitmapData or PIXI.Texture.
3432
* @param {string|number} [frame] - If this Image is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
3533
*/
36-
Phaser.GameObject.Image = function (game, x, y, key, frame) {
37-
34+
Phaser.GameObject.Image = function (game, x, y, key, frame)
35+
{
3836
x = x || 0;
3937
y = y || 0;
40-
key = key || null;
41-
frame = frame || null;
4238

4339
/**
4440
* @property {number} type - The const type of this object.
@@ -48,16 +44,18 @@ Phaser.GameObject.Image = function (game, x, y, key, frame) {
4844

4945
PIXI.Sprite.call(this, Phaser.Cache.DEFAULT);
5046

51-
Phaser.Component.Core.init.call(this, game, x, y, key, frame);
47+
this.texture = game.textures.get(key);
48+
49+
this.frame = this.texture.get(frame);
5250

51+
Phaser.Component.Core.init.call(this, game, x, y, key, frame);
5352
};
5453

5554
Phaser.GameObject.Image.prototype = Object.create(PIXI.Sprite.prototype);
5655
Phaser.GameObject.Image.prototype.constructor = Phaser.GameObject.Image;
5756

5857
Phaser.Component.Core.install.call(Phaser.GameObject.Image.prototype, [
5958
'Angle',
60-
'Animation',
6159
'AutoCull',
6260
'Bounds',
6361
'BringToTop',
@@ -66,7 +64,6 @@ Phaser.Component.Core.install.call(Phaser.GameObject.Image.prototype, [
6664
'FixedToCamera',
6765
'InputEnabled',
6866
'LifeSpan',
69-
'LoadTexture',
7067
'Overlap',
7168
'Reset',
7269
'ScaleMinMax',
@@ -82,13 +79,12 @@ Phaser.GameObject.Image.prototype.preUpdateCore = Phaser.Component.Core.preUpdat
8279
* @method Phaser.Image#preUpdate
8380
* @memberof Phaser.Image
8481
*/
85-
Phaser.GameObject.Image.prototype.preUpdate = function () {
86-
82+
Phaser.GameObject.Image.prototype.preUpdate = function ()
83+
{
8784
if (!this.preUpdateInWorld())
8885
{
8986
return false;
9087
}
9188

9289
return this.preUpdateCore();
93-
9490
};

src/gameobjects/image/ImageCanvasRenderer.js

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,68 @@ Phaser.Renderer.Canvas.GameObjects.Image = {
1313

1414
render: function (renderer, src)
1515
{
16-
// If the sprite is not visible or the alpha is 0 then no need to render this element
17-
if (!src.visible || src.alpha === 0 || !src.renderable)
16+
var frame = src.frame;
17+
var source = frame.source;
18+
19+
// Skip render?
20+
21+
if (!src.visible || !src.alpha || !src.renderable || !frame.cutWidth || !frame.cutHeight)
1822
{
1923
return;
2024
}
2125

22-
// Add back in: || src.texture.crop.width <= 0 || src.texture.crop.height <= 0
23-
24-
var wt = src.worldTransform;
26+
// Blend Mode
2527

2628
if (src.blendMode !== renderer.currentBlendMode)
2729
{
2830
renderer.currentBlendMode = src.blendMode;
29-
renderer.context.globalCompositeOperation = Phaser.blendModesCanvas[renderer.currentBlendMode];
31+
renderer.context.globalCompositeOperation = renderer.blendModes[renderer.currentBlendMode];
3032
}
3133

32-
var resolution = src.texture.baseTexture.resolution / renderer.game.resolution;
34+
// Alpha
3335

34-
renderer.context.globalAlpha = src.worldAlpha;
36+
if (src.worldAlpha !== renderer.context.globalAlpha)
37+
{
38+
renderer.context.globalAlpha = src.worldAlpha;
39+
}
3540

36-
// If smoothingEnabled is supported and we need to change the smoothing property for src texture
37-
if (renderer.smoothProperty && renderer.currentScaleMode !== src.texture.baseTexture.scaleMode)
41+
// Smoothing (should this be a Game Object, or Frame/Texture level property?)
42+
43+
if (source.scaleMode !== renderer.currentScaleMode)
3844
{
39-
renderer.currentScaleMode = src.texture.baseTexture.scaleMode;
40-
renderer.context[renderer.smoothProperty] = (renderer.currentScaleMode === Phaser.scaleModes.LINEAR);
45+
renderer.currentScaleMode = source.scaleMode;
46+
renderer.context[renderer.smoothProperty] = (source.scaleMode === Phaser.scaleModes.LINEAR);
4147
}
4248

43-
// If the texture is trimmed we offset by the trim x/y, otherwise we use the frame dimensions
44-
var dx = (src.texture.trim) ? src.texture.trim.x - src.anchor.x * src.texture.trim.width : src.anchor.x * -src.texture.frame.width;
45-
var dy = (src.texture.trim) ? src.texture.trim.y - src.anchor.y * src.texture.trim.height : src.anchor.y * -src.texture.frame.height;
49+
var wt = src.worldTransform;
50+
51+
var resolution = source.resolution / renderer.game.resolution;
52+
53+
var dx = frame.x - (src.anchor.x * frame.width);
54+
var dy = frame.y - (src.anchor.y * frame.height);
4655

4756
var tx = (wt.tx * renderer.game.resolution) + renderer.game.camera._shake.x;
4857
var ty = (wt.ty * renderer.game.resolution) + renderer.game.camera._shake.y;
4958

50-
var cw = src.texture.crop.width;
51-
var ch = src.texture.crop.height;
59+
// Round Pixels
5260

61+
if (renderer.roundPixels)
62+
{
63+
tx |= 0;
64+
ty |= 0;
65+
dx |= 0;
66+
dy |= 0;
67+
}
68+
69+
var cw = frame.cutWidth;
70+
var ch = frame.cutHeight;
71+
72+
renderer.context.setTransform(wt.a, wt.b, wt.c, wt.d, tx, ty);
73+
74+
renderer.context.drawImage(source.image, frame.cutX, frame.cutY, cw, ch, dx, dy, cw / resolution, ch / resolution);
75+
76+
/*
77+
// Move this to either the Renderer, or the Texture Manager, but not here (as it's repeated all over the place)
5378
if (src.texture.rotated)
5479
{
5580
var a = wt.a;
@@ -73,22 +98,9 @@ Phaser.Renderer.Canvas.GameObjects.Image = {
7398
cw = ch;
7499
ch = e;
75100
}
101+
*/
76102

77-
// Allow for pixel rounding
78-
if (renderer.roundPixels)
79-
{
80-
renderer.context.setTransform(wt.a, wt.b, wt.c, wt.d, tx | 0, ty | 0);
81-
dx |= 0;
82-
dy |= 0;
83-
}
84-
else
85-
{
86-
renderer.context.setTransform(wt.a, wt.b, wt.c, wt.d, tx, ty);
87-
}
88-
89-
dx /= resolution;
90-
dy /= resolution;
91-
103+
/*
92104
if (src.tint !== 0xFFFFFF)
93105
{
94106
if (src.texture.requiresReTint || src.cachedTint !== src.tint)
@@ -108,6 +120,7 @@ Phaser.Renderer.Canvas.GameObjects.Image = {
108120
109121
renderer.context.drawImage(src.texture.baseTexture.source, cx, cy, cw, ch, dx, dy, cw / resolution, ch / resolution);
110122
}
123+
*/
111124

112125
}
113126

src/loader/Cache.js

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,6 @@ Phaser.Cache.prototype = {
301301
this.removeImage(key);
302302
}
303303

304-
console.log('Cache.addImage', key);
305-
306304
var img = {
307305
key: key,
308306
url: url,
@@ -314,7 +312,7 @@ Phaser.Cache.prototype = {
314312

315313
this._resolveURL(url, img);
316314

317-
// Remove this
315+
// TODO Remove this
318316
if (key === '__default')
319317
{
320318
Phaser.Cache.DEFAULT = this.game.textures.getFrame('__default');
@@ -328,41 +326,6 @@ Phaser.Cache.prototype = {
328326

329327
},
330328

331-
__addImage: function (key, url, data)
332-
{
333-
if (this.checkImageKey(key))
334-
{
335-
this.removeImage(key);
336-
}
337-
338-
var img = {
339-
key: key,
340-
url: url,
341-
data: data,
342-
base: new PIXI.BaseTexture(data),
343-
frame: new Phaser.Frame(0, 0, 0, data.width, data.height, key),
344-
frameData: new Phaser.FrameData()
345-
};
346-
347-
img.frameData.addFrame(new Phaser.Frame(0, 0, 0, data.width, data.height, url));
348-
349-
this._cache.image[key] = img;
350-
351-
this._resolveURL(url, img);
352-
353-
if (key === '__default')
354-
{
355-
Phaser.Cache.DEFAULT = new PIXI.Texture(img.base);
356-
}
357-
else if (key === '__missing')
358-
{
359-
Phaser.Cache.MISSING = new PIXI.Texture(img.base);
360-
}
361-
362-
return img;
363-
364-
},
365-
366329
/**
367330
* Adds a default image to be used in special cases such as WebGL Filters.
368331
* It uses the special reserved key of `__default`.
@@ -713,35 +676,31 @@ Phaser.Cache.prototype = {
713676
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
714677
* @param {string} url - The URL the asset was loaded from. If the asset was not loaded externally set to `null`.
715678
* @param {object} data - Extra sprite sheet data.
716-
* @param {number} frameWidth - Width of the sprite sheet.
717-
* @param {number} frameHeight - Height of the sprite sheet.
718-
* @param {number} [frameMax=-1] - How many frames stored in the sprite sheet. If -1 then it divides the whole sheet evenly.
679+
* @param {number} frameWidth - The fixed width of each frame.
680+
* @param {number} frameHeight - The fixed height of each frame.
681+
* @param {number} [startFrame=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one Texture.
682+
* @param {number} [endFrame=-1] - The total number of frames to extract from the Sprite Sheet. The default value of -1 means "extract all frames".
719683
* @param {number} [margin=0] - If the frames have been drawn with a margin, specify the amount here.
720684
* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
721-
* @param {number} [skipFrames=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one image.
722685
*/
723-
addSpriteSheet: function (key, url, data, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames) {
724-
725-
if (frameMax === undefined) { frameMax = -1; }
726-
if (margin === undefined) { margin = 0; }
727-
if (spacing === undefined) { spacing = 0; }
728-
686+
addSpriteSheet: function (key, url, data, frameWidth, frameHeight, startFrame, endFrame, margin, spacing)
687+
{
729688
var obj = {
730689
key: key,
731690
url: url,
732691
data: data,
733692
frameWidth: frameWidth,
734693
frameHeight: frameHeight,
694+
startFrame: startFrame,
695+
endFrame: endFrame,
735696
margin: margin,
736697
spacing: spacing,
737-
base: new PIXI.BaseTexture(data),
738-
frameData: Phaser.AnimationParser.spriteSheet(this.game, data, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames)
698+
texture: this.game.textures.addSpriteSheet(key, data, frameWidth, frameHeight, startFrame, endFrame, margin, spacing)
739699
};
740700

741701
this._cache.image[key] = obj;
742702

743703
this._resolveURL(url, obj);
744-
745704
},
746705

747706
/**
@@ -760,9 +719,30 @@ Phaser.Cache.prototype = {
760719
key: key,
761720
url: url,
762721
data: data,
763-
base: new PIXI.BaseTexture(data)
722+
texture: null
764723
};
765724

725+
if (Array.isArray(atlasData.frames) && format === Phaser.Loader.TEXTURE_ATLAS_JSON_HASH)
726+
{
727+
format = Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY;
728+
}
729+
730+
var manager = this.game.textures;
731+
732+
switch (format)
733+
{
734+
case Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY:
735+
obj.texture = manager.addAtlasJSONArray(key, data, atlasData);
736+
break;
737+
738+
case Phaser.Loader.TEXTURE_ATLAS_JSON_HASH:
739+
obj.texture = manager.addAtlasJSONHash(key, data, atlasData);
740+
break;
741+
}
742+
743+
// TODO: XML + Pyxel
744+
745+
/*
766746
if (format === Phaser.Loader.TEXTURE_ATLAS_XML_STARLING)
767747
{
768748
obj.frameData = Phaser.AnimationParser.XMLData(this.game, atlasData, key);
@@ -783,6 +763,7 @@ Phaser.Cache.prototype = {
783763
obj.frameData = Phaser.AnimationParser.JSONDataHash(this.game, atlasData, key);
784764
}
785765
}
766+
*/
786767

787768
this._cache.image[key] = obj;
788769

src/loader/Loader.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,23 +1076,22 @@ Phaser.Loader.prototype = {
10761076
* @method Phaser.Loader#spritesheet
10771077
* @param {string} key - Unique asset key of the sheet file.
10781078
* @param {string} url - URL of the sprite sheet file. If undefined or `null` the url will be set to `<key>.png`, i.e. if `key` was "alien" then the URL will be "alien.png".
1079-
* @param {number} frameWidth - Width in pixels of a single frame in the sprite sheet.
1080-
* @param {number} frameHeight - Height in pixels of a single frame in the sprite sheet.
1081-
* @param {number} [frameMax=-1] - How many frames in this sprite sheet. If not specified it will divide the whole image into frames.
1079+
* @param {number} frameWidth - The fixed width of each frame.
1080+
* @param {number} frameHeight - The fixed height of each frame.
1081+
* @param {number} [startFrame=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one Texture.
1082+
* @param {number} [endFrame=-1] - The total number of frames to extract from the Sprite Sheet. The default value of -1 means "extract all frames".
10821083
* @param {number} [margin=0] - If the frames have been drawn with a margin, specify the amount here.
10831084
* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
1084-
* @param {number} [skipFrames=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one image.
10851085
* @return {Phaser.Loader} This Loader instance.
10861086
*/
1087-
spritesheet: function (key, url, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames) {
1088-
1089-
if (frameMax === undefined) { frameMax = -1; }
1087+
spritesheet: function (key, url, frameWidth, frameHeight, startFrame, endFrame, margin, spacing)
1088+
{
1089+
if (startFrame === undefined) { startFrame = 0; }
1090+
if (endFrame === undefined) { endFrame = -1; }
10901091
if (margin === undefined) { margin = 0; }
10911092
if (spacing === undefined) { spacing = 0; }
1092-
if (skipFrames === undefined) { skipFrames = 0; }
1093-
1094-
return this.addToFileList('spritesheet', key, url, { frameWidth: frameWidth, frameHeight: frameHeight, frameMax: frameMax, margin: margin, spacing: spacing, skipFrames: skipFrames }, false, '.png');
10951093

1094+
return this.addToFileList('spritesheet', key, url, { frameWidth: frameWidth, frameHeight: frameHeight, startFrame: startFrame, endFrame: endFrame, margin: margin, spacing: spacing }, false, '.png');
10961095
},
10971096

10981097
/**
@@ -1746,7 +1745,6 @@ Phaser.Loader.prototype = {
17461745
}
17471746

17481747
this.addToFileList('textureatlas', key, textureURL, { atlasURL: null, atlasData: atlasData, format: format });
1749-
17501748
}
17511749

17521750
return this;
@@ -2795,7 +2793,7 @@ Phaser.Loader.prototype = {
27952793

27962794
case 'spritesheet':
27972795

2798-
this.cache.addSpriteSheet(file.key, file.url, file.data, file.frameWidth, file.frameHeight, file.frameMax, file.margin, file.spacing, file.skipFrames);
2796+
this.cache.addSpriteSheet(file.key, file.url, file.data, file.frameWidth, file.frameHeight, file.startFrame, file.endFrame, file.margin, file.spacing);
27992797
break;
28002798

28012799
case 'textureatlas':

0 commit comments

Comments
 (0)