BitmapData object added

This commit is contained in:
Richard Davey
2013-11-13 20:57:09 +00:00
parent 6a24d6116b
commit ebe441666c
12 changed files with 300 additions and 28 deletions
+1
View File
@@ -29,6 +29,7 @@ var Phaser = Phaser || {
TILEMAPLAYER: 10,
EMITTER: 11,
POLYGON: 12,
BITMAPDATA: 13,
NONE: 0,
LEFT: 1,
+100
View File
@@ -0,0 +1,100 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Creates a new `BitmapData` object.
*
* @class Phaser.BitmapData
* @constructor
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {string} [key] - A key the BitmapData will use when added to the Phaser.Cache. If none is given a UUID is generated.
* @param {number} [width=256] - The width of the BitmapData in pixels.
* @param {number} [height=256] - The height of the BitmapData in pixels.
*/
Phaser.BitmapData = function (game, key, width, height) {
if (typeof key === 'undefined') { key = 'bitmapData' . game.rnd.uuid(); }
if (typeof width === 'undefined') { width = 256; }
if (typeof height === 'undefined') { height = 256; }
/**
* @property {Phaser.Game} game - A reference to the currently running game.
*/
this.game = game;
/**
* @property {string} name - The name of the BitmapData.
*/
this.name = key;
/**
* @property {number} width - The width of the BitmapData in pixels.
*/
this.width = width;
/**
* @property {number} height - The height of the BitmapData in pixels.
*/
this.height = height;
/**
* @property {HTMLCanvasElement} canvas - The canvas to which this BitmapData draws.
* @default
*/
this.canvas = Phaser.Canvas.create(width, height);
/**
* @property {HTMLCanvasContextElement} context - The 2d context of the canvas.
* @default
*/
this.context = this.canvas.getContext('2d');
/**
* @property {PIXI.BaseTexture} baseTexture - The PIXI.BaseTexture.
* @default
*/
this.baseTexture = new PIXI.BaseTexture(this.canvas);
/**
* @property {PIXI.Texture} texture - The PIXI.Texture.
* @default
*/
this.texture = new PIXI.Texture(this.baseTexture);
/**
* @property {Phaser.Frame} textureFrame - The Frame this BitmapData uses for rendering.
* @default
*/
this.textureFrame = new Phaser.Frame(0, 0, 0, width, height, 'bitmapData', game.rnd.uuid());
/**
* @property {number} type - The const type of this object.
* @default
*/
this.type = Phaser.BITMAPDATA;
}
Phaser.BitmapData.prototype = {
clear: function () {
this.context.clearRect(0, 0, this.width, this.height);
},
render: function () {
// Only needed if running in WebGL, otherwise this array will never get cleared down I don't think!
if (this.game.renderType == Phaser.WEBGL)
{
PIXI.texturesToUpdate.push(this.baseTexture);
}
}
}
+17
View File
@@ -274,6 +274,23 @@ Phaser.GameObjectFactory.prototype = {
return texture;
},
/**
* A BitmapData object which can be manipulated and drawn to like a traditional Canvas object and used to texture Sprites.
*
* @method Phaser.GameObjectFactory#bitmapData
* @param {string} [key] - A key the BitmapData will use when added to the Phaser.Cache. If none is given a UUID is generated.
* @param {number} [width=256] - The width of the BitmapData in pixels.
* @param {number} [height=256] - The height of the BitmapData in pixels.
* @return {Phaser.BitmapData} The newly created BitmapData object.
*/
bitmapData: function (key, width, height) {
var bmd = new Phaser.BitmapData(this.game, key, width, height);
return this.game.cache.addBitmapData(bmd.name, bmd);
}
};
+8 -2
View File
@@ -15,7 +15,7 @@
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {number} x - The x coordinate (in world space) to position the Sprite at.
* @param {number} y - The y coordinate (in world space) to position the Sprite at.
* @param {string|Phaser.RenderTexture|PIXI.Texture} key - This is the image or texture used by the Sprite during rendering. It can be a string which is a reference to the Cache entry, or an instance of a RenderTexture or PIXI.Texture.
* @param {string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture} key - This is the image or texture used by the Sprite during rendering. It can be a string which is a reference to the Cache entry, or an instance of a RenderTexture or PIXI.Texture.
* @param {string|number} frame - If this Sprite 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.Sprite = function (game, x, y, key, frame) {
@@ -89,7 +89,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
this.input = new Phaser.InputHandler(this);
/**
* @property {string|Phaser.RenderTexture|PIXI.Texture} key - This is the image or texture used by the Sprite during rendering. It can be a string which is a reference to the Cache entry, or an instance of a RenderTexture or PIXI.Texture.
* @property {string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture} key - This is the image or texture used by the Sprite 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.
*/
this.key = key;
@@ -104,6 +104,12 @@ Phaser.Sprite = function (game, x, y, key, frame) {
this.currentFrame = this.game.cache.getTextureFrame(key.name);
}
else if (key instanceof Phaser.BitmapData)
{
PIXI.Sprite.call(this, key.texture, key.textureFrame);
this.currentFrame = key.textureFrame;
}
else if (key instanceof PIXI.Texture)
{
PIXI.Sprite.call(this, key);
+43 -2
View File
@@ -62,6 +62,12 @@ Phaser.Cache = function (game) {
*/
this._tilesets = {};
/**
* @property {object} _bitmapDatas - BitmapData key-value container.
* @private
*/
this._bitmapDatas = {};
this.addDefaultImage();
/**
@@ -86,12 +92,27 @@ Phaser.Cache.prototype = {
},
/**
* Add a BitmapData object in to the cache.
* @method Phaser.Cache#addBitmapData
* @param {string} key - Asset key for this BitmapData.
* @param {Phaser.BitmapData} bitmapData - The BitmapData object to be addded to the cache.
* @return {Phaser.BitmapData} The BitmapData object to be addded to the cache.
*/
addBitmapData: function (key, bitmapData) {
this._bitmapDatas[key] = bitmapData;
return bitmapData;
},
/**
* Add a new Phaser.RenderTexture in to the cache.
*
* @method Phaser.Cache#addRenderTexture
* @param {string} key - The unique key by which you will reference this object.
* @param {Phaser.Texture} textue - The texture to use as the base of the RenderTexture.
* @param {Phaser.Texture} texture - The texture to use as the base of the RenderTexture.
*/
addRenderTexture: function (key, texture) {
@@ -364,7 +385,7 @@ Phaser.Cache.prototype = {
},
/**
* Get acanvas object from the cache by its key.
* Get a canvas object from the cache by its key.
*
* @method Phaser.Cache#getCanvas
* @param {string} key - Asset key of the canvas you want.
@@ -378,6 +399,25 @@ Phaser.Cache.prototype = {
}
return null;
},
/**
* Get a BitmapData object from the cache by its key.
*
* @method Phaser.Cache#getBitmapData
* @param {string} key - Asset key of the BitmapData object you want.
* @return {Phaser.BitmapData} The requested BitmapData object if found, or null if not.
*/
getBitmapData: function (key) {
if (this._bitmapDatas[key])
{
return this._bitmapDatas[key];
}
return null;
},
/**
@@ -413,6 +453,7 @@ Phaser.Cache.prototype = {
}
return null;
},
/**
+1 -1
View File
@@ -938,7 +938,7 @@ Phaser.Math = {
}
return { sin: sinTable, cos: cosTable };
return { sin: sinTable, cos: cosTable, length: length };
},
+43 -20
View File
@@ -249,52 +249,75 @@ Phaser.TilemapLayer.prototype.updateMapData = function (tilemap, layer) {
* @param {number} x - x coordinate in camera space
* @return {number} x coordinate in scrollFactor-adjusted dimensions
*/
Phaser.TilemapLayer.prototype._fixX = function( x )
{
if( this.scrollFactorX === 1 )
return x;
Phaser.TilemapLayer.prototype._fixX = function(x) {
if (this.scrollFactorX === 1)
{
return x;
}
var left_edge = x - (this._x / this.scrollFactorX);
return this._x + left_edge;
};
}
/**
* Take an x coordinate that _does_ account for scrollFactorY and 'unfix' it
* back to camera space. Used primarily internally
* @param {number} x - x coordinate in scrollFactor-adjusted dimensions
* @return {number} x coordinate in camera space
*/
Phaser.TilemapLayer.prototype._unfixX = function( x )
{
if( this.scrollFactorX === 1 )
return x;
Phaser.TilemapLayer.prototype._unfixX = function(x) {
if (this.scrollFactorX === 1)
{
return x;
}
var left_edge = x - this._x;
return (this._x / this.scrollFactorX) + left_edge;
};
}
/**
* Take a y coordinate that doesn't account for scrollFactorY and 'fix' it
* into a scrolled local space. Used primarily internally
* @param {number} y - y coordinate in camera space
* @return {number} y coordinate in scrollFactor-adjusted dimensions
*/
Phaser.TilemapLayer.prototype._fixY = function( y )
{
if( this.scrollFactorY === 1 )
return y;
Phaser.TilemapLayer.prototype._fixY = function(y) {
if (this.scrollFactorY === 1)
{
return y;
}
var top_edge = y - (this._y / this.scrollFactorY);
return this._y + top_edge;
};
}
/**
* Take a y coordinate that _does_ account for scrollFactorY and 'unfix' it
* back to camera space. Used primarily internally
* @param {number} y - y coordinate in scrollFactor-adjusted dimensions
* @return {number} y coordinate in camera space
*/
Phaser.TilemapLayer.prototype._unfixY = function( y )
{
if( this.scrollFactorY === 1 )
return y;
Phaser.TilemapLayer.prototype._unfixY = function(y) {
if (this.scrollFactorY === 1)
{
return y;
}
var top_edge = y - this._y;
return (this._y / this.scrollFactorY) + top_edge;
};
}
/**
* Convert a pixel value to a tile coordinate.