2016-10-04 14:39:54 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2016-10-29 20:29:16 +00:00
|
|
|
* Manages the different WebGL Sprite Batches.
|
2016-10-04 14:39:54 +00:00
|
|
|
*
|
|
|
|
* @class Phaser.Renderer.Canvas
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - Game reference to the currently running game.
|
|
|
|
*/
|
2016-10-24 16:14:10 +00:00
|
|
|
Phaser.Renderer.WebGL.BatchManager = function (renderer, batchSize)
|
2016-10-04 14:39:54 +00:00
|
|
|
{
|
|
|
|
this.renderer = renderer;
|
|
|
|
|
|
|
|
this.gl = null;
|
|
|
|
|
2016-10-29 20:29:16 +00:00
|
|
|
this.currentBatch = null;
|
2016-10-31 19:44:46 +00:00
|
|
|
this.spriteBatch = null;
|
2016-10-29 20:29:16 +00:00
|
|
|
|
2016-10-31 19:44:46 +00:00
|
|
|
this.singleTextureBatch = new Phaser.Renderer.WebGL.Batch.SingleTexture(this, batchSize);
|
2016-10-30 22:56:06 +00:00
|
|
|
this.multiTextureBatch = new Phaser.Renderer.WebGL.Batch.MultiTexture(this, batchSize);
|
2016-10-29 20:29:16 +00:00
|
|
|
|
2016-10-31 19:44:46 +00:00
|
|
|
this.pixelBatch = new Phaser.Renderer.WebGL.Batch.Pixel(this, batchSize);
|
|
|
|
this.fxBatch = new Phaser.Renderer.WebGL.Batch.FX(this, batchSize);
|
2016-10-04 14:39:54 +00:00
|
|
|
};
|
|
|
|
|
2016-10-07 01:09:12 +00:00
|
|
|
Phaser.Renderer.WebGL.BatchManager.prototype.constructor = Phaser.Renderer.WebGL.BatchManager;
|
2016-10-04 14:39:54 +00:00
|
|
|
|
2016-10-07 01:09:12 +00:00
|
|
|
Phaser.Renderer.WebGL.BatchManager.prototype = {
|
2016-10-04 14:39:54 +00:00
|
|
|
|
|
|
|
init: function ()
|
|
|
|
{
|
|
|
|
this.gl = this.renderer.gl;
|
|
|
|
|
2016-10-31 19:44:46 +00:00
|
|
|
this.singleTextureBatch.init();
|
2016-10-30 22:56:06 +00:00
|
|
|
this.multiTextureBatch.init();
|
|
|
|
this.pixelBatch.init();
|
2016-10-31 19:44:46 +00:00
|
|
|
this.fxBatch.init();
|
2016-10-30 22:56:06 +00:00
|
|
|
|
2016-10-31 19:44:46 +00:00
|
|
|
if (this.renderer.multiTexture)
|
|
|
|
{
|
|
|
|
this.currentBatch = this.multiTextureBatch;
|
|
|
|
this.spriteBatch = this.multiTextureBatch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.currentBatch = this.singleTextureBatch;
|
|
|
|
this.spriteBatch = this.singleTextureBatch;
|
|
|
|
}
|
2016-10-04 14:39:54 +00:00
|
|
|
},
|
|
|
|
|
2016-10-24 16:14:10 +00:00
|
|
|
start: function ()
|
2016-10-04 14:39:54 +00:00
|
|
|
{
|
2016-10-29 20:29:16 +00:00
|
|
|
this.currentBatch.start();
|
2016-10-04 14:39:54 +00:00
|
|
|
},
|
|
|
|
|
2016-10-18 16:03:25 +00:00
|
|
|
stop: function ()
|
|
|
|
{
|
2016-10-29 20:29:16 +00:00
|
|
|
this.currentBatch.stop();
|
2016-10-04 14:39:54 +00:00
|
|
|
},
|
|
|
|
|
2016-10-31 19:44:46 +00:00
|
|
|
setBatch: function (newBatch)
|
2016-10-19 10:54:00 +00:00
|
|
|
{
|
2016-10-31 19:44:46 +00:00
|
|
|
if (this.currentBatch === newBatch)
|
2016-10-24 16:14:10 +00:00
|
|
|
{
|
2016-10-31 19:44:46 +00:00
|
|
|
return;
|
2016-10-24 16:14:10 +00:00
|
|
|
}
|
|
|
|
|
2016-10-31 19:44:46 +00:00
|
|
|
// Flush whatever was in the current batch (if anything)
|
|
|
|
this.currentBatch.flush();
|
|
|
|
|
|
|
|
if (newBatch)
|
|
|
|
{
|
|
|
|
this.currentBatch = newBatch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.currentBatch = this.spriteBatch;
|
|
|
|
}
|
2016-10-24 16:14:10 +00:00
|
|
|
},
|
|
|
|
|
2016-10-30 22:56:06 +00:00
|
|
|
add: function (source, blendMode, verts, uvs, textureIndex, alpha, tintColors, bgColors)
|
2016-10-24 16:14:10 +00:00
|
|
|
{
|
2016-10-30 22:56:06 +00:00
|
|
|
var hasFlushed = false;
|
2016-10-28 01:42:58 +00:00
|
|
|
|
2016-10-31 19:44:46 +00:00
|
|
|
// Shader? Then we check the current batch, and swap if needed
|
|
|
|
|
|
|
|
|
2016-10-30 22:56:06 +00:00
|
|
|
// Check Batch Size and flush if needed
|
|
|
|
if (this.currentBatch.size >= this.currentBatch.maxSize)
|
2016-10-24 16:14:10 +00:00
|
|
|
{
|
2016-10-30 22:56:06 +00:00
|
|
|
this.currentBatch.flush();
|
2016-10-28 01:42:58 +00:00
|
|
|
|
2016-10-30 22:56:06 +00:00
|
|
|
hasFlushed = true;
|
2016-10-04 14:39:54 +00:00
|
|
|
}
|
|
|
|
|
2016-10-30 22:56:06 +00:00
|
|
|
source.glLastUsed = this.renderer.startTime;
|
2016-10-29 12:38:57 +00:00
|
|
|
|
2016-10-30 22:56:06 +00:00
|
|
|
// Does this TextureSource need updating?
|
|
|
|
if (source.glDirty)
|
2016-10-04 14:39:54 +00:00
|
|
|
{
|
2016-10-30 22:56:06 +00:00
|
|
|
this.renderer.updateTexture(source);
|
2016-10-04 14:39:54 +00:00
|
|
|
}
|
2016-10-18 16:03:25 +00:00
|
|
|
|
2016-10-30 22:56:06 +00:00
|
|
|
// Does the batch need to activate a new texture?
|
|
|
|
if (this.renderer.textureArray[source.glTextureIndex] !== source)
|
|
|
|
{
|
|
|
|
this.setCurrentTexture(source);
|
2016-10-04 14:39:54 +00:00
|
|
|
}
|
2016-10-18 16:03:25 +00:00
|
|
|
|
2016-10-30 22:56:06 +00:00
|
|
|
// Blend Mode?
|
|
|
|
if (blendMode !== this.renderer.currentBlendMode)
|
2016-10-04 14:39:54 +00:00
|
|
|
{
|
2016-10-30 22:56:06 +00:00
|
|
|
if (!hasFlushed)
|
2016-10-04 14:39:54 +00:00
|
|
|
{
|
2016-10-30 22:56:06 +00:00
|
|
|
this.currentBatch.flush();
|
2016-10-27 14:14:22 +00:00
|
|
|
|
2016-10-30 22:56:06 +00:00
|
|
|
hasFlushed = true;
|
2016-10-18 16:03:25 +00:00
|
|
|
}
|
2016-10-04 14:39:54 +00:00
|
|
|
|
2016-10-30 22:56:06 +00:00
|
|
|
this.renderer.setBlendMode(blendMode);
|
2016-10-04 14:39:54 +00:00
|
|
|
}
|
|
|
|
|
2016-10-31 19:44:46 +00:00
|
|
|
this.currentBatch.add(verts, uvs, textureIndex, alpha, tintColors, bgColors);
|
|
|
|
},
|
2016-10-04 14:39:54 +00:00
|
|
|
|
2016-10-31 19:44:46 +00:00
|
|
|
addPixel: function (x0, y0, x1, y1, x2, y2, x3, y3, color)
|
|
|
|
{
|
|
|
|
// Swapping batch? Flush and change
|
2016-10-27 14:14:22 +00:00
|
|
|
|
2016-10-31 19:44:46 +00:00
|
|
|
// Check Batch Size and flush if needed
|
|
|
|
if (this.pixelBatch.size >= this.pixelBatch.maxSize)
|
|
|
|
{
|
|
|
|
this.pixelBatch.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pixelBatch.add(x0, y0, x1, y1, x2, y2, x3, y3, color);
|
2016-10-04 14:39:54 +00:00
|
|
|
},
|
|
|
|
|
2016-10-29 20:29:16 +00:00
|
|
|
setCurrentTexture: function (source)
|
2016-10-04 14:39:54 +00:00
|
|
|
{
|
2016-10-29 20:29:16 +00:00
|
|
|
var gl = this.gl;
|
|
|
|
|
|
|
|
this.currentBatch.flush();
|
|
|
|
|
|
|
|
gl.activeTexture(gl.TEXTURE0 + source.glTextureIndex);
|
|
|
|
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, source.glTexture);
|
|
|
|
|
|
|
|
this.renderer.textureArray[source.glTextureIndex] = source;
|
|
|
|
},
|
2016-10-04 14:39:54 +00:00
|
|
|
|
2016-10-29 20:29:16 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
2016-10-31 19:44:46 +00:00
|
|
|
this.singleTextureBatch.destroy();
|
2016-10-04 14:39:54 +00:00
|
|
|
|
|
|
|
this.renderer = null;
|
|
|
|
this.gl = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|