Skip to content

Commit 03f67a2

Browse files
committed
Refactored WebGL renderer working for Sprites and Containers.
1 parent d55cc21 commit 03f67a2

File tree

15 files changed

+2149
-8
lines changed

15 files changed

+2149
-8
lines changed

.eslintrc.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"env": {
3-
"browser": true
3+
"browser": true,
4+
"es6"
45
},
56
"extends": "eslint:recommended",
67
"globals": {
@@ -19,6 +20,7 @@
1920
"eqeqeq": [ "error", "smart" ],
2021
"no-alert": "error",
2122
"no-caller": "error",
23+
"no-console": [ "error", { "allow": ["warn", "log"] } ],
2224
"no-floating-decimal": "error",
2325
"no-invalid-this": "error",
2426
"no-multi-spaces": "error",
@@ -62,7 +64,7 @@
6264
"no-trailing-spaces": [ "error", { "skipBlankLines": true } ],
6365
"no-underscore-dangle": "off",
6466
"no-whitespace-before-property": "error",
65-
"object-curly-newline": [ "error", "always" ],
67+
"object-curly-newline": [ "error", { "multiline": true, "minProperties": 0 } ],
6668
"one-var-declaration-per-line": [ "error", "initializations" ],
6769
"quote-props": [ "error", "as-needed" ],
6870
"quotes": [ "error", "single" ],

build/config.php

+7
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@
137137
<script src="$path/src/renderer/canvas/gameobjects/Container.js"></script>
138138
<script src="$path/src/renderer/canvas/gameobjects/Sprite.js"></script>
139139
140+
<script src="$path/src/renderer/webgl/WebGLRenderer.js"></script>
141+
<script src="$path/src/renderer/webgl/ShaderManager.js"></script>
142+
<script src="$path/src/renderer/webgl/SpriteBatch.js"></script>
143+
<script src="$path/src/renderer/webgl/shaders/Sprite.js"></script>
144+
<script src="$path/src/renderer/webgl/gameobjects/Container.js"></script>
145+
<script src="$path/src/renderer/webgl/gameobjects/Sprite.js"></script>
146+
140147
141148
EOL;
142149

src/core/Game.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -762,10 +762,12 @@ Phaser.Game.prototype = {
762762

763763
this.renderType = Phaser.WEBGL;
764764

765-
this.renderer = new PIXI.WebGLRenderer(this);
765+
// this.renderer = new PIXI.WebGLRenderer(this);
766+
this.renderer = new Phaser.Renderer.WebGL(this);
766767

767768
this.context = null;
768769

770+
// Move to renderer class
769771
this.canvas.addEventListener('webglcontextlost', this.contextLost.bind(this), false);
770772
this.canvas.addEventListener('webglcontextrestored', this.contextRestored.bind(this), false);
771773
}

src/core/Group.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ Phaser.Group.prototype.add = function (child, silent, index) {
432432
*/
433433
Phaser.Group.prototype.addAt = function (child, index, silent) {
434434

435-
this.add(child, silent, index);
435+
return this.add(child, silent, index);
436436

437437
};
438438

src/pixi/display/DisplayObjectContainer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ PIXI.DisplayObjectContainer = function () {
3434
*/
3535
this.ignoreChildInput = false;
3636

37-
this.render = Phaser.Renderer.Canvas.GameObjects.Container.render;
37+
// this.render = Phaser.Renderer.Canvas.GameObjects.Container.render;
38+
this.render = Phaser.Renderer.WebGL.GameObjects.Container.render;
3839

3940
};
4041

src/pixi/display/Sprite.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ PIXI.Sprite = function (texture) {
116116

117117
this.renderable = true;
118118

119-
this.render = Phaser.Renderer.Canvas.GameObjects.Sprite.render;
119+
// this.render = Phaser.Renderer.Canvas.GameObjects.Sprite.render;
120+
this.render = Phaser.Renderer.WebGL.GameObjects.Sprite.render;
120121

121122
};
122123

src/pixi/renderers/webgl/WebGLRenderer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ PIXI.WebGLRenderer.prototype.render = function(stage)
377377
};
378378

379379
/**
380-
* Renders a Display Object.
380+
* Renders a Display Object. Only used by this class and RenderTexture, nothing else calls it.
381381
*
382382
* @method renderDisplayObject
383383
* @param displayObject {DisplayObject} The DisplayObject to render

src/renderer/canvas/gameobjects/Sprite.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ Phaser.Renderer.Canvas.GameObjects.Sprite = {
126126
var cx = this.texture.crop.x;
127127
var cy = this.texture.crop.y;
128128

129-
renderer.context.drawImage(this.texture.baseTexture.this, cx, cy, cw, ch, dx, dy, cw / resolution, ch / resolution);
129+
renderer.context.drawImage(this.texture.baseTexture.source, cx, cy, cw, ch, dx, dy, cw / resolution, ch / resolution);
130130
}
131131

132132
for (var i = 0; i < this.children.length; i++)

src/renderer/webgl/ShaderManager.js

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @author Mat Groves (@Doormat23)
4+
* @copyright 2016 Photon Storm Ltd.
5+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
6+
*/
7+
8+
/**
9+
* New version of PIXI.WebGLShaderManager
10+
*
11+
* @class Phaser.Renderer.Canvas
12+
* @constructor
13+
* @param {Phaser.Game} game - Game reference to the currently running game.
14+
*/
15+
Phaser.Renderer.WebGL.ShaderManager = function (renderer)
16+
{
17+
this.renderer = renderer;
18+
19+
this.gl = null;
20+
21+
this.maxAttribs = 10;
22+
23+
this.attribState = [];
24+
25+
this.tempAttribState = [];
26+
27+
this.stack = [];
28+
29+
this._currentId = -1;
30+
this.currentShader = null;
31+
32+
this.primitiveShader = null;
33+
this.complexPrimitiveShader = null;
34+
this.defaultShader = null;
35+
this.fastShader = null;
36+
this.stripShader = null;
37+
38+
for (var i = 0; i < this.maxAttribs; i++)
39+
{
40+
this.attribState[i] = false;
41+
}
42+
43+
};
44+
45+
Phaser.Renderer.WebGL.ShaderManager.prototype.constructor = Phaser.Renderer.WebGL.ShaderManager;
46+
47+
Phaser.Renderer.WebGL.ShaderManager.prototype = {
48+
49+
init: function ()
50+
{
51+
this.gl = this.renderer.gl;
52+
53+
// the next one is used for rendering primitives
54+
// this.primitiveShader = new PIXI.PrimitiveShader(this);
55+
56+
// the next one is used for rendering triangle strips
57+
// this.complexPrimitiveShader = new PIXI.ComplexPrimitiveShader(this);
58+
59+
// this shader is used for the default sprite rendering
60+
this.defaultShader = new Phaser.Renderer.WebGL.Shaders.Sprite(this.renderer);
61+
62+
// this shader is used for the fast sprite rendering
63+
// this.fastShader = new PIXI.PixiFastShader(this);
64+
65+
// the next one is used for rendering triangle strips
66+
// this.stripShader = new PIXI.StripShader(this);
67+
68+
this.setShader(this.defaultShader);
69+
},
70+
71+
setAttribs: function (attribs)
72+
{
73+
// reset temp state
74+
var i;
75+
76+
for (i = 0; i < this.tempAttribState.length; i++)
77+
{
78+
this.tempAttribState[i] = false;
79+
}
80+
81+
// set the new attribs
82+
for (i = 0; i < attribs.length; i++)
83+
{
84+
var attribId = attribs[i];
85+
this.tempAttribState[attribId] = true;
86+
}
87+
88+
for (i = 0; i < this.attribState.length; i++)
89+
{
90+
if (this.attribState[i] !== this.tempAttribState[i])
91+
{
92+
this.attribState[i] = this.tempAttribState[i];
93+
94+
if (this.tempAttribState[i])
95+
{
96+
this.gl.enableVertexAttribArray(i);
97+
}
98+
else
99+
{
100+
this.gl.disableVertexAttribArray(i);
101+
}
102+
}
103+
}
104+
},
105+
106+
setShader: function (shader)
107+
{
108+
if (this._currentId === shader._UID)
109+
{
110+
return false;
111+
}
112+
113+
this._currentId = shader._UID;
114+
115+
this.currentShader = shader;
116+
117+
this.gl.useProgram(shader.program);
118+
119+
this.setAttribs(shader.attributes);
120+
121+
return true;
122+
},
123+
124+
destroy: function ()
125+
{
126+
this.renderer = null;
127+
this.gl = null;
128+
129+
this.attribState = [];
130+
this.tempAttribState = [];
131+
this.stack = [];
132+
133+
this.currentShader = null;
134+
135+
this.primitiveShader.destroy();
136+
this.complexPrimitiveShader.destroy();
137+
this.defaultShader.destroy();
138+
this.fastShader.destroy();
139+
this.stripShader.destroy();
140+
}
141+
142+
};

0 commit comments

Comments
 (0)