Hooking the Loader and Cache into the new Texture Manager.

This commit is contained in:
photonstorm 2016-10-11 14:52:17 +01:00
parent e0ef9cab8b
commit ce3308ea1d
11 changed files with 111 additions and 125 deletions

View file

@ -80,7 +80,7 @@ Phaser.Component.Core.init = function (game, x, y, key, frame) {
if (this.components.LoadTexture && key !== null)
{
this.loadTexture(key, frame);
// this.loadTexture(key, frame);
}
if (this.components.FixedToCamera)

View file

@ -12,7 +12,6 @@
* @extends PIXI.Sprite
* @extends Phaser.Component.Core
* @extends Phaser.Component.Angle
* @extends Phaser.Component.Animation
* @extends Phaser.Component.AutoCull
* @extends Phaser.Component.Bounds
* @extends Phaser.Component.BringToTop
@ -21,7 +20,6 @@
* @extends Phaser.Component.FixedToCamera
* @extends Phaser.Component.InputEnabled
* @extends Phaser.Component.LifeSpan
* @extends Phaser.Component.LoadTexture
* @extends Phaser.Component.Overlap
* @extends Phaser.Component.Reset
* @extends Phaser.Component.ScaleMinMax
@ -33,12 +31,10 @@
* @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.
* @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.
*/
Phaser.GameObject.Image = function (game, x, y, key, frame) {
Phaser.GameObject.Image = function (game, x, y, key, frame)
{
x = x || 0;
y = y || 0;
key = key || null;
frame = frame || null;
/**
* @property {number} type - The const type of this object.
@ -48,8 +44,11 @@ Phaser.GameObject.Image = function (game, x, y, key, frame) {
PIXI.Sprite.call(this, Phaser.Cache.DEFAULT);
Phaser.Component.Core.init.call(this, game, x, y, key, frame);
this.texture = game.textures.get(key);
this.frame = this.texture.get(frame);
Phaser.Component.Core.init.call(this, game, x, y, key, frame);
};
Phaser.GameObject.Image.prototype = Object.create(PIXI.Sprite.prototype);
@ -57,7 +56,6 @@ Phaser.GameObject.Image.prototype.constructor = Phaser.GameObject.Image;
Phaser.Component.Core.install.call(Phaser.GameObject.Image.prototype, [
'Angle',
'Animation',
'AutoCull',
'Bounds',
'BringToTop',
@ -66,7 +64,6 @@ Phaser.Component.Core.install.call(Phaser.GameObject.Image.prototype, [
'FixedToCamera',
'InputEnabled',
'LifeSpan',
'LoadTexture',
'Overlap',
'Reset',
'ScaleMinMax',
@ -82,13 +79,12 @@ Phaser.GameObject.Image.prototype.preUpdateCore = Phaser.Component.Core.preUpdat
* @method Phaser.Image#preUpdate
* @memberof Phaser.Image
*/
Phaser.GameObject.Image.prototype.preUpdate = function () {
Phaser.GameObject.Image.prototype.preUpdate = function ()
{
if (!this.preUpdateInWorld())
{
return false;
}
return this.preUpdateCore();
};

View file

@ -13,43 +13,68 @@ Phaser.Renderer.Canvas.GameObjects.Image = {
render: function (renderer, src)
{
// If the sprite is not visible or the alpha is 0 then no need to render this element
if (!src.visible || src.alpha === 0 || !src.renderable)
var frame = src.frame;
var source = frame.source;
// Skip render?
if (!src.visible || !src.alpha || !src.renderable || !frame.cutWidth || !frame.cutHeight)
{
return;
}
// Add back in: || src.texture.crop.width <= 0 || src.texture.crop.height <= 0
var wt = src.worldTransform;
// Blend Mode
if (src.blendMode !== renderer.currentBlendMode)
{
renderer.currentBlendMode = src.blendMode;
renderer.context.globalCompositeOperation = Phaser.blendModesCanvas[renderer.currentBlendMode];
renderer.context.globalCompositeOperation = renderer.blendModes[renderer.currentBlendMode];
}
var resolution = src.texture.baseTexture.resolution / renderer.game.resolution;
// Alpha
renderer.context.globalAlpha = src.worldAlpha;
// If smoothingEnabled is supported and we need to change the smoothing property for src texture
if (renderer.smoothProperty && renderer.currentScaleMode !== src.texture.baseTexture.scaleMode)
if (src.worldAlpha !== renderer.context.globalAlpha)
{
renderer.currentScaleMode = src.texture.baseTexture.scaleMode;
renderer.context[renderer.smoothProperty] = (renderer.currentScaleMode === Phaser.scaleModes.LINEAR);
renderer.context.globalAlpha = src.worldAlpha;
}
// If the texture is trimmed we offset by the trim x/y, otherwise we use the frame dimensions
var dx = (src.texture.trim) ? src.texture.trim.x - src.anchor.x * src.texture.trim.width : src.anchor.x * -src.texture.frame.width;
var dy = (src.texture.trim) ? src.texture.trim.y - src.anchor.y * src.texture.trim.height : src.anchor.y * -src.texture.frame.height;
// Smoothing (should this be a Game Object, or Frame/Texture level property?)
if (source.scaleMode !== renderer.currentScaleMode)
{
renderer.currentScaleMode = source.scaleMode;
renderer.context[renderer.smoothProperty] = (source.scaleMode === Phaser.scaleModes.LINEAR);
}
var wt = src.worldTransform;
var resolution = source.resolution / renderer.game.resolution;
var dx = frame.x - (src.anchor.x * frame.width);
var dy = frame.y - (src.anchor.y * frame.height);
var tx = (wt.tx * renderer.game.resolution) + renderer.game.camera._shake.x;
var ty = (wt.ty * renderer.game.resolution) + renderer.game.camera._shake.y;
var cw = src.texture.crop.width;
var ch = src.texture.crop.height;
// Round Pixels
if (renderer.roundPixels)
{
tx |= 0;
ty |= 0;
dx |= 0;
dy |= 0;
}
var cw = frame.cutWidth;
var ch = frame.cutHeight;
renderer.context.setTransform(wt.a, wt.b, wt.c, wt.d, tx, ty);
renderer.context.drawImage(source.image, frame.cutX, frame.cutY, cw, ch, dx, dy, cw / resolution, ch / resolution);
/*
// Move this to either the Renderer, or the Texture Manager, but not here (as it's repeated all over the place)
if (src.texture.rotated)
{
var a = wt.a;
@ -73,22 +98,9 @@ Phaser.Renderer.Canvas.GameObjects.Image = {
cw = ch;
ch = e;
}
*/
// Allow for pixel rounding
if (renderer.roundPixels)
{
renderer.context.setTransform(wt.a, wt.b, wt.c, wt.d, tx | 0, ty | 0);
dx |= 0;
dy |= 0;
}
else
{
renderer.context.setTransform(wt.a, wt.b, wt.c, wt.d, tx, ty);
}
dx /= resolution;
dy /= resolution;
/*
if (src.tint !== 0xFFFFFF)
{
if (src.texture.requiresReTint || src.cachedTint !== src.tint)
@ -108,6 +120,7 @@ Phaser.Renderer.Canvas.GameObjects.Image = {
renderer.context.drawImage(src.texture.baseTexture.source, cx, cy, cw, ch, dx, dy, cw / resolution, ch / resolution);
}
*/
}

View file

@ -301,8 +301,6 @@ Phaser.Cache.prototype = {
this.removeImage(key);
}
console.log('Cache.addImage', key);
var img = {
key: key,
url: url,
@ -314,7 +312,7 @@ Phaser.Cache.prototype = {
this._resolveURL(url, img);
// Remove this
// TODO Remove this
if (key === '__default')
{
Phaser.Cache.DEFAULT = this.game.textures.getFrame('__default');
@ -328,41 +326,6 @@ Phaser.Cache.prototype = {
},
__addImage: function (key, url, data)
{
if (this.checkImageKey(key))
{
this.removeImage(key);
}
var img = {
key: key,
url: url,
data: data,
base: new PIXI.BaseTexture(data),
frame: new Phaser.Frame(0, 0, 0, data.width, data.height, key),
frameData: new Phaser.FrameData()
};
img.frameData.addFrame(new Phaser.Frame(0, 0, 0, data.width, data.height, url));
this._cache.image[key] = img;
this._resolveURL(url, img);
if (key === '__default')
{
Phaser.Cache.DEFAULT = new PIXI.Texture(img.base);
}
else if (key === '__missing')
{
Phaser.Cache.MISSING = new PIXI.Texture(img.base);
}
return img;
},
/**
* Adds a default image to be used in special cases such as WebGL Filters.
* It uses the special reserved key of `__default`.
@ -713,35 +676,31 @@ Phaser.Cache.prototype = {
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {string} url - The URL the asset was loaded from. If the asset was not loaded externally set to `null`.
* @param {object} data - Extra sprite sheet data.
* @param {number} frameWidth - Width of the sprite sheet.
* @param {number} frameHeight - Height of the sprite sheet.
* @param {number} [frameMax=-1] - How many frames stored in the sprite sheet. If -1 then it divides the whole sheet evenly.
* @param {number} frameWidth - The fixed width of each frame.
* @param {number} frameHeight - The fixed height of each frame.
* @param {number} [startFrame=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one Texture.
* @param {number} [endFrame=-1] - The total number of frames to extract from the Sprite Sheet. The default value of -1 means "extract all frames".
* @param {number} [margin=0] - If the frames have been drawn with a margin, specify the amount here.
* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
* @param {number} [skipFrames=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one image.
*/
addSpriteSheet: function (key, url, data, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames) {
if (frameMax === undefined) { frameMax = -1; }
if (margin === undefined) { margin = 0; }
if (spacing === undefined) { spacing = 0; }
addSpriteSheet: function (key, url, data, frameWidth, frameHeight, startFrame, endFrame, margin, spacing)
{
var obj = {
key: key,
url: url,
data: data,
frameWidth: frameWidth,
frameHeight: frameHeight,
startFrame: startFrame,
endFrame: endFrame,
margin: margin,
spacing: spacing,
base: new PIXI.BaseTexture(data),
frameData: Phaser.AnimationParser.spriteSheet(this.game, data, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames)
texture: this.game.textures.addSpriteSheet(key, data, frameWidth, frameHeight, startFrame, endFrame, margin, spacing)
};
this._cache.image[key] = obj;
this._resolveURL(url, obj);
},
/**
@ -760,9 +719,30 @@ Phaser.Cache.prototype = {
key: key,
url: url,
data: data,
base: new PIXI.BaseTexture(data)
texture: null
};
if (Array.isArray(atlasData.frames) && format === Phaser.Loader.TEXTURE_ATLAS_JSON_HASH)
{
format = Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY;
}
var manager = this.game.textures;
switch (format)
{
case Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY:
obj.texture = manager.addAtlasJSONArray(key, data, atlasData);
break;
case Phaser.Loader.TEXTURE_ATLAS_JSON_HASH:
obj.texture = manager.addAtlasJSONHash(key, data, atlasData);
break;
}
// TODO: XML + Pyxel
/*
if (format === Phaser.Loader.TEXTURE_ATLAS_XML_STARLING)
{
obj.frameData = Phaser.AnimationParser.XMLData(this.game, atlasData, key);
@ -783,6 +763,7 @@ Phaser.Cache.prototype = {
obj.frameData = Phaser.AnimationParser.JSONDataHash(this.game, atlasData, key);
}
}
*/
this._cache.image[key] = obj;

View file

@ -1076,23 +1076,22 @@ Phaser.Loader.prototype = {
* @method Phaser.Loader#spritesheet
* @param {string} key - Unique asset key of the sheet file.
* @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".
* @param {number} frameWidth - Width in pixels of a single frame in the sprite sheet.
* @param {number} frameHeight - Height in pixels of a single frame in the sprite sheet.
* @param {number} [frameMax=-1] - How many frames in this sprite sheet. If not specified it will divide the whole image into frames.
* @param {number} frameWidth - The fixed width of each frame.
* @param {number} frameHeight - The fixed height of each frame.
* @param {number} [startFrame=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one Texture.
* @param {number} [endFrame=-1] - The total number of frames to extract from the Sprite Sheet. The default value of -1 means "extract all frames".
* @param {number} [margin=0] - If the frames have been drawn with a margin, specify the amount here.
* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
* @param {number} [skipFrames=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one image.
* @return {Phaser.Loader} This Loader instance.
*/
spritesheet: function (key, url, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames) {
if (frameMax === undefined) { frameMax = -1; }
spritesheet: function (key, url, frameWidth, frameHeight, startFrame, endFrame, margin, spacing)
{
if (startFrame === undefined) { startFrame = 0; }
if (endFrame === undefined) { endFrame = -1; }
if (margin === undefined) { margin = 0; }
if (spacing === undefined) { spacing = 0; }
if (skipFrames === undefined) { skipFrames = 0; }
return this.addToFileList('spritesheet', key, url, { frameWidth: frameWidth, frameHeight: frameHeight, frameMax: frameMax, margin: margin, spacing: spacing, skipFrames: skipFrames }, false, '.png');
return this.addToFileList('spritesheet', key, url, { frameWidth: frameWidth, frameHeight: frameHeight, startFrame: startFrame, endFrame: endFrame, margin: margin, spacing: spacing }, false, '.png');
},
/**
@ -1746,7 +1745,6 @@ Phaser.Loader.prototype = {
}
this.addToFileList('textureatlas', key, textureURL, { atlasURL: null, atlasData: atlasData, format: format });
}
return this;
@ -2795,7 +2793,7 @@ Phaser.Loader.prototype = {
case 'spritesheet':
this.cache.addSpriteSheet(file.key, file.url, file.data, file.frameWidth, file.frameHeight, file.frameMax, file.margin, file.spacing, file.skipFrames);
this.cache.addSpriteSheet(file.key, file.url, file.data, file.frameWidth, file.frameHeight, file.startFrame, file.endFrame, file.margin, file.spacing);
break;
case 'textureatlas':

View file

@ -109,10 +109,10 @@ PIXI.Sprite = function (texture) {
*/
this.exists = true;
if (this.texture.baseTexture.hasLoaded)
{
this.onTextureUpdate();
}
// if (this.texture.baseTexture.hasLoaded)
// {
// this.onTextureUpdate();
// }
this.renderable = true;

View file

@ -80,9 +80,7 @@ Phaser.Renderer.Canvas = function (game)
* @property context
* @type CanvasRenderingContext2D
*/
this.context = this.view.getContext('2d', {
alpha: this.transparent
});
this.context = this.view.getContext('2d', { alpha: this.transparent });
this.smoothProperty = Phaser.Canvas.getSmoothingPrefix(this.context);

View file

@ -6,7 +6,7 @@
/**
* A Frame is a section of a Texture.
*
*
* Called TextureFrame during integration, will rename to Frame later.
*
* @class Phaser.TextureFrame
@ -20,7 +20,6 @@
*/
Phaser.TextureFrame = function (texture, name, sourceIndex, x, y, width, height)
{
/**
* @property {Phaser.Texture} texture - The Texture this frame belongs to.
*/
@ -79,6 +78,7 @@ Phaser.TextureFrame = function (texture, name, sourceIndex, x, y, width, height)
* Is this frame is rotated or not in the Texture?
* Rotation allows you to use rotated frames in texture atlas packing.
* It has nothing to do with Sprite rotation.
*
* @property {boolean} rotated
* @default
*/
@ -86,6 +86,7 @@ Phaser.TextureFrame = function (texture, name, sourceIndex, x, y, width, height)
/**
* Is this a tiling texture? As used by the likes of a TilingSprite.
* TODO: Try and remove this, it shouldn't be here
*
* @property {boolean} isTiling
* @default
@ -94,14 +95,15 @@ Phaser.TextureFrame = function (texture, name, sourceIndex, x, y, width, height)
/**
* This will let a renderer know that a tinted parent has updated its texture.
* TODO: Try and remove this, it shouldn't be here
*
* @property {boolean} requiresReTint
* @default
*/
this.requiresReTint = false;
// Over-rides the Renderer setting? 0 = use Renderer Setting, 1 = No rounding, 2 = Round
this.autoRound = 0;
// Over-rides the Renderer setting? -1 = use Renderer Setting, 0 = No rounding, 1 = Round
this.autoRound = -1;
/**
* The un-modified source frame, trim and UV data.

View file

@ -81,7 +81,7 @@ Phaser.Texture.prototype = {
get: function (name)
{
if (name === undefined) { name = '__BASE'; }
if (name === undefined || name === null) { name = '__BASE'; }
return this.frames[name];

View file

@ -42,8 +42,6 @@ Phaser.TextureManager.prototype = {
addImage: function (key, source)
{
console.log('TextureManager.addImage', key);
var texture = this.create(key, source);
this.parsers.Image(texture, 0);

View file

@ -13,10 +13,10 @@
* @param {string} key - The key of the Frame within the Texture that the Sprite Sheet is stored in.
* @param {number} frameWidth - The fixed width of each frame.
* @param {number} frameHeight - The fixed height of each frame.
* @param {number} [startFrame=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one Texture.
* @param {number} [endFrame=-1] - The total number of frames to extract from the Sprite Sheet. The default value of -1 means "extract all frames".
* @param {number} [margin=0] - If the frames have been drawn with a margin, specify the amount here.
* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
* @param {number} [startFrame=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one Texture.
* @return {Phaser.FrameData} A FrameData object containing the parsed frames.
*/
Phaser.TextureManager.Parsers.SpriteSheet = function (texture, sourceIndex, x, y, width, height, frameWidth, frameHeight, startFrame, endFrame, margin, spacing)