2016-09-29 03:21:12 +00:00
|
|
|
/**
|
2016-10-05 00:09:23 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @author Mat Groves (@Doormat23)
|
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
2016-09-29 03:21:12 +00:00
|
|
|
*/
|
2016-10-05 00:09:23 +00:00
|
|
|
|
2016-10-03 11:44:54 +00:00
|
|
|
Phaser.Renderer.Canvas.GameObjects.Sprite = {
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-05 00:09:23 +00:00
|
|
|
TYPES: [
|
2016-10-14 03:09:22 +00:00
|
|
|
Phaser.GameObject.Sprite.prototype
|
2016-10-05 00:09:23 +00:00
|
|
|
],
|
|
|
|
|
|
|
|
render: function (renderer, src)
|
2016-09-29 03:21:12 +00:00
|
|
|
{
|
2016-10-14 07:58:26 +00:00
|
|
|
var color = src.color;
|
2016-10-12 14:19:04 +00:00
|
|
|
var frame = src.frame;
|
|
|
|
var source = frame.source;
|
|
|
|
|
2016-10-14 05:32:03 +00:00
|
|
|
// Skip rendering?
|
2016-10-12 14:19:04 +00:00
|
|
|
|
2016-10-14 07:58:26 +00:00
|
|
|
if (src.skipRender || !src.visible || color.worldAlpha <= 0 || !frame.cutWidth || !frame.cutHeight)
|
2016-10-03 11:44:54 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-12 14:19:04 +00:00
|
|
|
// Blend Mode
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-14 07:58:26 +00:00
|
|
|
if (renderer.currentBlendMode !== color.blendMode)
|
2016-10-03 11:44:54 +00:00
|
|
|
{
|
2016-10-14 07:58:26 +00:00
|
|
|
renderer.currentBlendMode = color.blendMode;
|
2016-10-12 14:19:04 +00:00
|
|
|
renderer.context.globalCompositeOperation = renderer.blendModes[renderer.currentBlendMode];
|
2016-10-03 11:44:54 +00:00
|
|
|
}
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-14 05:32:03 +00:00
|
|
|
// Alpha
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-14 07:58:26 +00:00
|
|
|
if (renderer.currentAlpha !== color.worldAlpha)
|
2016-09-29 03:21:12 +00:00
|
|
|
{
|
2016-10-14 07:58:26 +00:00
|
|
|
renderer.currentAlpha = color.worldAlpha;
|
|
|
|
renderer.context.globalAlpha = color.worldAlpha;
|
2016-09-29 03:21:12 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 03:09:22 +00:00
|
|
|
// Smoothing (should this be a Game Object, or Frame / Texture level property?)
|
2016-10-12 14:19:04 +00:00
|
|
|
|
2016-10-14 05:32:03 +00:00
|
|
|
if (renderer.currentScaleMode !== source.scaleMode)
|
2016-09-29 03:21:12 +00:00
|
|
|
{
|
2016-10-12 14:19:04 +00:00
|
|
|
renderer.currentScaleMode = source.scaleMode;
|
|
|
|
renderer.context[renderer.smoothProperty] = (source.scaleMode === Phaser.scaleModes.LINEAR);
|
|
|
|
}
|
2016-10-03 11:44:54 +00:00
|
|
|
|
2016-10-14 03:09:22 +00:00
|
|
|
var wt = src.transform.world;
|
2016-10-03 11:44:54 +00:00
|
|
|
|
2016-10-12 14:19:04 +00:00
|
|
|
var resolution = source.resolution / renderer.game.resolution;
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-14 03:09:22 +00:00
|
|
|
var dx = frame.x - (src.anchorX * frame.width);
|
|
|
|
var dy = frame.y - (src.anchorY * frame.height);
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-12 14:19:04 +00:00
|
|
|
var tx = (wt.tx * renderer.game.resolution) + renderer.game.camera._shake.x;
|
|
|
|
var ty = (wt.ty * renderer.game.resolution) + renderer.game.camera._shake.y;
|
|
|
|
|
|
|
|
// Round Pixels
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-12 14:19:04 +00:00
|
|
|
if (renderer.roundPixels)
|
2016-10-03 11:44:54 +00:00
|
|
|
{
|
2016-10-12 14:19:04 +00:00
|
|
|
tx |= 0;
|
|
|
|
ty |= 0;
|
|
|
|
dx |= 0;
|
|
|
|
dy |= 0;
|
2016-10-03 11:44:54 +00:00
|
|
|
}
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-12 14:19:04 +00:00
|
|
|
var cw = frame.cutWidth;
|
|
|
|
var ch = frame.cutHeight;
|
2016-10-14 07:58:26 +00:00
|
|
|
var cwr = cw / resolution;
|
|
|
|
var chr = ch / resolution;
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-14 03:09:22 +00:00
|
|
|
renderer.context.setTransform(wt.a, wt.b, wt.c, wt.d, tx, ty);
|
|
|
|
|
2016-10-12 14:19:04 +00:00
|
|
|
// Does this Sprite have a mask?
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-12 14:19:04 +00:00
|
|
|
if (src._mask)
|
|
|
|
{
|
|
|
|
renderer.pushMask(src._mask);
|
|
|
|
}
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-14 07:58:26 +00:00
|
|
|
// Color Component
|
|
|
|
|
|
|
|
if (color._hasBackground)
|
|
|
|
{
|
|
|
|
renderer.context.fillStyle = color._rgba;
|
|
|
|
renderer.context.fillRect(dx, dy, cwr, chr);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer.context.drawImage(source.image, frame.cutX, frame.cutY, cw, ch, dx, dy, cwr, chr);
|
2016-10-12 14:19:04 +00:00
|
|
|
|
2016-10-14 03:09:22 +00:00
|
|
|
for (var i = 0; i < src.children.list.length; i++)
|
|
|
|
{
|
|
|
|
var child = src.children.list[i];
|
|
|
|
|
|
|
|
child.render(renderer, child);
|
|
|
|
}
|
|
|
|
|
2016-10-12 14:19:04 +00:00
|
|
|
/*
|
|
|
|
// Move this to either the Renderer, or the Texture Manager, but not here (as it's repeated all over the place)
|
2016-10-05 00:09:23 +00:00
|
|
|
if (src.texture.rotated)
|
2016-10-03 11:44:54 +00:00
|
|
|
{
|
|
|
|
var a = wt.a;
|
|
|
|
var b = wt.b;
|
|
|
|
var c = wt.c;
|
|
|
|
var d = wt.d;
|
|
|
|
var e = cw;
|
|
|
|
|
|
|
|
// Offset before rotating
|
|
|
|
tx = wt.c * ch + tx;
|
|
|
|
ty = wt.d * ch + ty;
|
|
|
|
|
|
|
|
// Rotate matrix by 90 degrees
|
|
|
|
// We use precalculated values for sine and cosine of rad(90)
|
|
|
|
wt.a = a * 6.123233995736766e-17 + -c;
|
|
|
|
wt.b = b * 6.123233995736766e-17 + -d;
|
|
|
|
wt.c = a + c * 6.123233995736766e-17;
|
|
|
|
wt.d = b + d * 6.123233995736766e-17;
|
|
|
|
|
|
|
|
// Update cropping dimensions.
|
|
|
|
cw = ch;
|
|
|
|
ch = e;
|
|
|
|
}
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-05 00:09:23 +00:00
|
|
|
if (src.tint !== 0xFFFFFF)
|
2016-10-03 11:44:54 +00:00
|
|
|
{
|
2016-10-05 00:09:23 +00:00
|
|
|
if (src.texture.requiresReTint || src.cachedTint !== src.tint)
|
2016-10-03 11:44:54 +00:00
|
|
|
{
|
2016-10-05 00:09:23 +00:00
|
|
|
src.tintedTexture = PIXI.CanvasTinter.getTintedTexture(src, src.tint);
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-05 00:09:23 +00:00
|
|
|
src.cachedTint = src.tint;
|
|
|
|
src.texture.requiresReTint = false;
|
2016-10-03 11:44:54 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 00:09:23 +00:00
|
|
|
renderer.context.drawImage(src.tintedTexture, 0, 0, cw, ch, dx, dy, cw / resolution, ch / resolution);
|
2016-10-03 11:44:54 +00:00
|
|
|
}
|
2016-10-12 14:19:04 +00:00
|
|
|
*/
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-05 00:09:23 +00:00
|
|
|
if (src._mask)
|
2016-10-03 11:44:54 +00:00
|
|
|
{
|
|
|
|
renderer.popMask();
|
|
|
|
}
|
2016-09-29 03:21:12 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|