UNPKG

phaser-ce

Version:

Phaser CE (Community Edition) is a fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers.

243 lines (211 loc) 6.11 kB
/** * @author Mat Groves http://matgroves.com/ @Doormat23 */ /** * A texture stores the information that represents an image. All textures have a base texture. * * @class PIXI.BaseTexture * @constructor * @param source {HTMLCanvasElement|HTMLImageElement|HTMLVideoElement} the source object (image or canvas) * @param scaleMode {number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values * @param [resolution] {number} the resolution of the texture (for HiDPI displays) */ PIXI.BaseTexture = function (source, scaleMode, resolution) { /** * The Resolution of the texture. * * @property resolution * @type number */ this.resolution = resolution || 1; /** * [read-only] The width of the base texture set when the image has loaded * * @property width * @type number * @readOnly */ this.width = 100; /** * [read-only] The height of the base texture set when the image has loaded * * @property height * @type number * @readOnly */ this.height = 100; /** * The scale mode to apply when scaling this texture * * @property scaleMode * @type {number} * @default PIXI.scaleModes.LINEAR */ this.scaleMode = scaleMode || PIXI.scaleModes.DEFAULT; /** * [read-only] Set to true once the base texture has loaded * * @property hasLoaded * @type boolean * @readOnly */ this.hasLoaded = false; /** * The image source that is used to create the texture. * * @property source * @type HTMLCanvasElement|HTMLImageElement|HTMLVideoElement */ this.source = source; /** * Controls if RGB channels should be pre-multiplied by Alpha (WebGL only) * * @property premultipliedAlpha * @type boolean * @default true */ this.premultipliedAlpha = true; // used for webGL /** * @property _glTextures * @type array * @private */ this._glTextures = []; /** * Set this to true if a mipmap of this texture needs to be generated. This value needs to be set before the texture is used * Also the texture must be a power of two size to work * * @property mipmap * @type {boolean} */ this.mipmap = false; /** * The multi texture batching index number. * @property textureIndex * @type number */ this.textureIndex = 0; /** * @property _dirty * @type array * @private */ this._dirty = [ true, true, true, true ]; if (!source) { return; } if ((this.source.complete || this.source.getContext) && this.source.width && this.source.height) { this.hasLoaded = true; this.width = this.source.naturalWidth || this.source.width; this.height = this.source.naturalHeight || this.source.height; this.dirty(); } /** * A BaseTexture can be set to skip the rendering phase in the WebGL Sprite Batch. * * You may want to do this if you have a parent Sprite with no visible texture (i.e. uses the internal `__default` texture) * that has children that you do want to render, without causing a batch flush in the process. * * @property skipRender * @type boolean */ this.skipRender = false; /** * @property _powerOf2 * @type boolean * @private */ this._powerOf2 = false; }; PIXI.BaseTexture.prototype.constructor = PIXI.BaseTexture; /** * Forces this BaseTexture to be set as loaded, with the given width and height. * Then calls BaseTexture.dirty. * Important for when you don't want to modify the source object by forcing in `complete` or dimension properties it may not have. * * @method PIXI.BaseTexture#forceLoaded * @param {number} width - The new width to force the BaseTexture to be. * @param {number} height - The new height to force the BaseTexture to be. */ PIXI.BaseTexture.prototype.forceLoaded = function (width, height) { this.hasLoaded = true; this.width = width; this.height = height; this.dirty(); }; /** * Destroys this base texture * * @method PIXI.BaseTexture#destroy */ PIXI.BaseTexture.prototype.destroy = function () { if (this.source) { Phaser.CanvasPool.removeByCanvas(this.source); } this.source = null; this.unloadFromGPU(); }; /** * Sets all glTextures to be dirty. * * @method PIXI.BaseTexture#dirty */ PIXI.BaseTexture.prototype.dirty = function () { for (var i = 0; i < this._glTextures.length; i++) { this._dirty[i] = true; } }; /** * Removes the base texture from the GPU, useful for managing resources on the GPU. * Atexture is still 100% usable and will simply be reuploaded if there is a sprite on screen that is using it. * * @method PIXI.BaseTexture#unloadFromGPU */ PIXI.BaseTexture.prototype.unloadFromGPU = function () { this.dirty(); // delete the webGL textures if any. for (var i = this._glTextures.length - 1; i >= 0; i--) { var glTexture = this._glTextures[i]; var gl = PIXI.glContexts[i]; if(gl && glTexture) { gl.deleteTexture(glTexture); } } this._glTextures.length = 0; this.dirty(); }; /** * Helper function that creates a base texture from the given canvas element. * * @static * @method PIXI.BaseTexture#fromCanvas * @param canvas {HTMLCanvasElement} The canvas element source of the texture * @param scaleMode {number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values * @param [resolution] {number} the resolution of the texture (for HiDPI displays) * @return {PIXI.BaseTexture} */ PIXI.BaseTexture.fromCanvas = function (canvas, scaleMode, resolution) { if (canvas.width === 0) { canvas.width = 1; } if (canvas.height === 0) { canvas.height = 1; } resolution = resolution || 1; return new PIXI.BaseTexture(canvas, scaleMode, resolution); };