Skip to content

Commit a03393a

Browse files
committed
Added auto resize, clear and default parameters
1 parent 916bfa7 commit a03393a

1 file changed

Lines changed: 88 additions & 24 deletions

File tree

src/renderer/webgl/RenderTarget.js

Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,38 @@
55
*/
66

77
var Class = require('../../utils/Class');
8+
var Events = require('./events');
89

910
/**
1011
* @classdesc
1112
* A Render Target encapsulates a WebGL framebuffer and the WebGL Texture that displays it.
1213
*
13-
* Instances of this class are created by, and belong to WebGL Pipelines.
14+
* Instances of this class are typically created by, and belong to WebGL Pipelines, however
15+
* other Game Objects and classes can take advantage of Render Targets as well.
1416
*
1517
* @class RenderTarget
1618
* @memberof Phaser.Renderer.WebGL
1719
* @constructor
1820
* @since 3.50.0
1921
*
20-
* @param {Phaser.Renderer.WebGL.WebGLPipeline} pipeline - The WebGLPipeline to which this Render Target belongs.
21-
* @param {number} width - The width of the WebGL Pipeline.
22-
* @param {number} height - The height of the WebGL Pipeline.
23-
* @param {number} scale - A value between 0 and 1. Controls the size of this Render Target in relation to the Renderer.
24-
* @param {number} minFilter - The minFilter mode of the texture when created. 0 is `LINEAR`, 1 is `NEAREST`.
25-
* @param {boolean} autoClear - Automatically clear this framebuffer when bound?
22+
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the WebGLRenderer.
23+
* @param {number} width - The width of this Render Target.
24+
* @param {number} height - The height of this Render Target.
25+
* @param {number} [scale=1] - A value between 0 and 1. Controls the size of this Render Target in relation to the Renderer.
26+
* @param {number} [minFilter=0] - The minFilter mode of the texture when created. 0 is `LINEAR`, 1 is `NEAREST`.
27+
* @param {boolean} [autoClear=true] - Automatically clear this framebuffer when bound?
28+
* @param {boolean} [autoResize=false] - Automatically resize this Render Target if the WebGL Renderer resizes?
2629
*/
2730
var RenderTarget = new Class({
2831

2932
initialize:
3033

31-
function RenderTarget (pipeline, width, height, scale, minFilter, autoClear)
34+
function RenderTarget (renderer, width, height, scale, minFilter, autoClear, autoResize)
3235
{
33-
/**
34-
* A reference to the WebGLPipeline that owns this Render Target.
35-
*
36-
* A Render Target class can only belong to a single pipeline.
37-
*
38-
* @name Phaser.Renderer.WebGL.RenderTarget#pipeline
39-
* @type {Phaser.Renderer.WebGL.WebGLPipeline}
40-
* @since 3.50.0
41-
*/
42-
this.pipeline = pipeline;
36+
if (scale === undefined) { scale = 1; }
37+
if (minFilter === undefined) { minFilter = 0; }
38+
if (autoClear === undefined) { autoClear = true; }
39+
if (autoResize === undefined) { autoResize = false; }
4340

4441
/**
4542
* A reference to the WebGLRenderer instance.
@@ -48,7 +45,7 @@ var RenderTarget = new Class({
4845
* @type {Phaser.Renderer.WebGL.WebGLRenderer}
4946
* @since 3.50.0
5047
*/
51-
this.renderer = pipeline.renderer;
48+
this.renderer = renderer;
5249

5350
/**
5451
* The WebGLFramebuffer of this Render Target.
@@ -125,7 +122,53 @@ var RenderTarget = new Class({
125122
*/
126123
this.autoClear = autoClear;
127124

125+
/**
126+
* Does this Render Target automatically resize when the WebGL Renderer does?
127+
*
128+
* Modify this property via the `setAutoResize` method.
129+
*
130+
* @name Phaser.Renderer.WebGL.RenderTarget#autoResize
131+
* @type {boolean}
132+
* @readonly
133+
* @since 3.50.0
134+
*/
135+
this.autoResize = false;
136+
128137
this.resize(width, height);
138+
139+
if (autoResize)
140+
{
141+
this.setAutoResize(true);
142+
}
143+
},
144+
145+
/**
146+
* Sets if this Render Target should automatically resize when the WebGL Renderer
147+
* emits a resize event.
148+
*
149+
* @method Phaser.Renderer.WebGL.RenderTarget#setAutoResize
150+
* @since 3.50.0
151+
*
152+
* @param {boolean} autoResize - Automatically resize this Render Target when the WebGL Renderer resizes?
153+
*
154+
* @return {this} This RenderTarget instance.
155+
*/
156+
setAutoResize: function (autoResize)
157+
{
158+
if (autoResize && !this.autoResize)
159+
{
160+
this.renderer.on(Events.RESIZE, this.resize, this);
161+
162+
this.autoResize = true;
163+
}
164+
else if (!autoResize && this.autoResize)
165+
{
166+
this.renderer.off(Events.RESIZE, this.resize, this);
167+
168+
this.autoResize = false;
169+
}
170+
171+
return this;
129172
},
130173

131174
/**
@@ -139,8 +182,8 @@ var RenderTarget = new Class({
139182
* @method Phaser.Renderer.WebGL.RenderTarget#resize
140183
* @since 3.50.0
141184
*
142-
* @param {number} width - The new width of the WebGL Pipeline.
143-
* @param {number} height - The new height of the WebGL Pipeline.
185+
* @param {number} width - The new width of this Render Target.
186+
* @param {number} height - The new height of this Render Target.
144187
*
145188
* @return {this} This RenderTarget instance.
146189
*/
@@ -178,14 +221,33 @@ var RenderTarget = new Class({
178221

179222
if (this.autoClear)
180223
{
181-
var gl = this.pipeline.gl;
224+
var gl = this.renderer.gl;
182225

183226
gl.clearColor(0, 0, 0, 0);
184227

185228
gl.clear(gl.COLOR_BUFFER_BIT);
186229
}
187230
},
188231

232+
/**
233+
* Clears this Render Target.
234+
*
235+
* @method Phaser.Renderer.WebGL.RenderTarget#clear
236+
* @since 3.50.0
237+
*/
238+
clear: function ()
239+
{
240+
this.renderer.pushFramebuffer(this.framebuffer);
241+
242+
var gl = this.renderer.gl;
243+
244+
gl.clearColor(0, 0, 0, 0);
245+
246+
gl.clear(gl.COLOR_BUFFER_BIT);
247+
248+
this.renderer.popFramebuffer();
249+
},
250+
189251
/**
190252
* Unbinds this Render Target.
191253
*
@@ -210,12 +272,14 @@ var RenderTarget = new Class({
210272
*/
211273
destroy: function ()
212274
{
213-
var renderer = this.pipeline.renderer;
275+
var renderer = this.renderer;
214276

215277
renderer.deleteFramebuffer(this.framebuffer);
216278
renderer.deleteTexture(this.texture);
217279

218-
this.pipeline = null;
280+
renderer.off(Events.RESIZE, this.resize, this);
281+
282+
this.renderer = null;
219283
this.framebuffer = null;
220284
this.texture = null;
221285
}

0 commit comments

Comments
 (0)