phaser/src/gameobjects/rendertexture/RenderTexture.js

79 lines
2.4 KiB
JavaScript
Raw Normal View History

var Class = require('../../utils/Class');
var Components = require('../components');
var GameObject = require('../GameObject');
var RenderTextureWebGL = require('./RenderTextureWebGL');
2018-02-24 00:05:15 +00:00
var RenderTextureCanvas = require('./RenderTextureCanvas');
var Render = require('./RenderTextureRender');
2018-02-24 00:05:15 +00:00
var CanvasPool = require('../../display/canvas/CanvasPool');
var RenderTexture = new Class({
Extends: GameObject,
Mixins: [
Components.Alpha,
Components.BlendMode,
Components.Depth,
Components.Flip,
Components.GetBounds,
Components.MatrixStack,
Components.Origin,
Components.Pipeline,
Components.ScaleMode,
Components.ScrollFactor,
Components.Size,
Components.Tint,
Components.Transform,
Components.Visible,
Render
],
initialize:
2018-03-05 01:40:11 +00:00
function RenderTexture (scene, x, y, width, height)
{
GameObject.call(this, scene, 'RenderTexture');
this.initMatrixStack();
this.renderer = scene.sys.game.renderer;
if (this.renderer.type === Phaser.WEBGL)
{
var gl = this.renderer.gl;
this.gl = gl;
this.fill = RenderTextureWebGL.fill;
this.clear = RenderTextureWebGL.clear;
this.draw = RenderTextureWebGL.draw;
this.drawFrame = RenderTextureWebGL.drawFrame;
this.texture = this.renderer.createTexture2D(0, gl.NEAREST, gl.NEAREST, gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE, gl.RGBA, null, width, height, false);
this.framebuffer = this.renderer.createFramebuffer(width, height, this.texture, false);
}
else
{
2018-02-24 00:05:15 +00:00
this.fill = RenderTextureCanvas.fill;
this.clear = RenderTextureCanvas.clear;
this.draw = RenderTextureCanvas.draw;
this.drawFrame = RenderTextureCanvas.drawFrame;
this.canvas = CanvasPool.create2D(null, width, height);
2018-03-05 01:40:11 +00:00
this.context = this.canvas.getContext('2d');
}
this.setPosition(x, y);
this.setSize(width, height);
this.initPipeline('TextureTintPipeline');
},
destroy: function ()
{
GameObject.destroy.call(this);
if (this.renderer.type === Phaser.WEBGL)
{
this.renderer.deleteTexture(this.texture);
this.renderer.deleteFramebuffer(this.framebuffer);
}
2018-03-05 01:40:11 +00:00
}
});
module.exports = RenderTexture;