Add displaywidth/height parameters to PIXI.Tilemap c'tor and save them.

Clean up _initWebGL to remove unused GL buffers.
Clean up _renderBatch to remove unused srcWide/srcHigh variables.
Set new Tile Shader's clipping uniform using displayWidth/Height.
This commit is contained in:
Pete Baron 2016-07-21 17:10:51 +12:00
parent 5f55fb095e
commit 008de253ce

View file

@ -17,10 +17,16 @@
* @param {integer} tileheight - The height of a single tile. * @param {integer} tileheight - The height of a single tile.
* @param {Array} layer - Tilemap layer data from the map, arranged in mapheight lists of mapwidth Phaser.Tile objects (2d array). * @param {Array} layer - Tilemap layer data from the map, arranged in mapheight lists of mapwidth Phaser.Tile objects (2d array).
*/ */
PIXI.Tilemap = function (texture, mapwidth, mapheight, tilewidth, tileheight, layer) { PIXI.Tilemap = function (texture, displaywidth, displayheight, mapwidth, mapheight, tilewidth, tileheight, layer) {
PIXI.DisplayObjectContainer.call(this); PIXI.DisplayObjectContainer.call(this);
/**
* the clipping limits for this layer
*/
this.displayWidth = displaywidth;
this.displayHeight = displayheight;
/** /**
* The texture of the Tilemap * The texture of the Tilemap
* *
@ -106,7 +112,7 @@ PIXI.Tilemap.prototype._renderWebGL = function (renderSession) {
// stop current render session batch drawing // stop current render session batch drawing
renderSession.spriteBatch.stop(); renderSession.spriteBatch.stop();
if (!this._vertexBuffer) if (!this.positionBuffer)
{ {
this._initWebGL(renderSession); this._initWebGL(renderSession);
} }
@ -124,11 +130,6 @@ PIXI.Tilemap.prototype._initWebGL = function (renderSession) {
var gl = renderSession.gl; var gl = renderSession.gl;
this._vertexBuffer = gl.createBuffer();
this._indexBuffer = gl.createBuffer();
this._uvBuffer = gl.createBuffer();
this._colorBuffer = gl.createBuffer();
// create a GL buffer to transfer all the vertex position data through // create a GL buffer to transfer all the vertex position data through
this.positionBuffer = gl.createBuffer(); this.positionBuffer = gl.createBuffer();
@ -155,10 +156,6 @@ PIXI.Tilemap.prototype._renderBatch = function (renderSession) {
var iTextureWide = 1.0 / this.texture.width; var iTextureWide = 1.0 / this.texture.width;
var iTextureHigh = 1.0 / this.texture.height; var iTextureHigh = 1.0 / this.texture.height;
// size of one tile in the source texture
var srcWide = this.tileWide * iTextureWide;
var srcHigh = this.tileHigh * iTextureHigh;
// pre-calculate inverse half-buffer dimensions // pre-calculate inverse half-buffer dimensions
var iWide = 1.0 / screenWide2; var iWide = 1.0 / screenWide2;
var iHigh = 1.0 / screenHigh2; var iHigh = 1.0 / screenHigh2;
@ -175,6 +172,7 @@ PIXI.Tilemap.prototype._renderBatch = function (renderSession) {
for (var i = 0, l = this.glBatch.length; i < l; i++) for (var i = 0, l = this.glBatch.length; i < l; i++)
{ {
// each object in this.glBatch has properties:
// sx: this.drawCoords[coordIndex], // sx: this.drawCoords[coordIndex],
// sy: this.drawCoords[coordIndex + 1], // sy: this.drawCoords[coordIndex + 1],
// sw: this.tileWidth, // sw: this.tileWidth,
@ -183,7 +181,6 @@ PIXI.Tilemap.prototype._renderBatch = function (renderSession) {
// dy: y, // dy: y,
// dw: this.tileWidth, // dw: this.tileWidth,
// dh: this.tileHeight // dh: this.tileHeight
var t = this.glBatch[i]; var t = this.glBatch[i];
if (!t) if (!t)
@ -286,6 +283,9 @@ PIXI.Tilemap.prototype._renderWholeTilemap = function (renderSession) {
// set the global offset (e.g. screen shake) // set the global offset (e.g. screen shake)
gl.uniform2f(shader.uOffset, renderSession.offset.x / this.game.width * 2, -renderSession.offset.y / this.game.height * 2); gl.uniform2f(shader.uOffset, renderSession.offset.x / this.game.width * 2, -renderSession.offset.y / this.game.height * 2);
// set the clippling limits
gl.uniform2f(shader.uClipping, this.displayWidth, this.game.height - this.displayHeight);
// set the offset in screen units to the center of the screen // set the offset in screen units to the center of the screen
// and flip the GL y coordinate to be zero at the top // and flip the GL y coordinate to be zero at the top
gl.uniform2f(shader.uCentreOffset, 1, -1); gl.uniform2f(shader.uCentreOffset, 1, -1);