Skip to content

Commit 5c554f3

Browse files
committed
Started moving dynamic bitmap text renderer to its own function
1 parent dd0490d commit 5c554f3

2 files changed

Lines changed: 222 additions & 6 deletions

File tree

src/gameobjects/bitmaptext/dynamic/DynamicBitmapTextWebGLRenderer.js

Lines changed: 221 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
var GameObject = require('../../GameObject');
8+
var Utils = require('../../../renderer/webgl/Utils');
89

910
/**
1011
* Renders this Game Object with the WebGL Renderer to the given Camera.
@@ -16,22 +17,237 @@ var GameObject = require('../../GameObject');
1617
* @private
1718
*
1819
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the current active WebGL renderer.
19-
* @param {Phaser.GameObjects.DynamicBitmapText} gameObject - The Game Object being rendered in this call.
20+
* @param {Phaser.GameObjects.DynamicBitmapText} src - The Game Object being rendered in this call.
2021
* @param {number} interpolationPercentage - Reserved for future use and custom pipelines.
2122
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
2223
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
2324
*/
24-
var DynamicBitmapTextWebGLRenderer = function (renderer, bitmapText, interpolationPercentage, camera, parentMatrix)
25+
var DynamicBitmapTextWebGLRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix)
2526
{
26-
var text = bitmapText.text;
27+
var text = src.text;
2728
var textLength = text.length;
2829

29-
if (GameObject.RENDER_MASK !== bitmapText.renderFlags || textLength === 0 || (bitmapText.cameraFilter > 0 && (bitmapText.cameraFilter & camera._id)))
30+
if (GameObject.RENDER_MASK !== src.renderFlags || textLength === 0 || (src.cameraFilter > 0 && (src.cameraFilter & camera._id)))
3031
{
3132
return;
3233
}
3334

34-
this.pipeline.batchDynamicBitmapText(bitmapText, camera, parentMatrix);
35+
var pipeline = this.pipeline;
36+
37+
renderer.setPipeline(pipeline);
38+
39+
var camMatrix = pipeline._tempCameraMatrix;
40+
var spriteMatrix = pipeline._tempSpriteMatrix;
41+
42+
spriteMatrix.applyITRS(src.x - camera.scrollX * src.scrollFactorX, src.y - camera.scrollY * src.scrollFactorY, src.rotation, src.scaleX, src.scaleY);
43+
44+
camMatrix.copyFrom(camera.matrix);
45+
46+
var calcMatrix;
47+
48+
if (parentMatrix)
49+
{
50+
// Multiply the camera by the parent matrix
51+
camMatrix.multiplyWithOffset(parentMatrix, -camera.scrollX * src.scrollFactorX, -camera.scrollY * src.scrollFactorY);
52+
53+
// Undo the camera scroll
54+
spriteMatrix.e = src.x;
55+
spriteMatrix.f = src.y;
56+
57+
// Multiply by the Sprite matrix
58+
calcMatrix = camMatrix.multiply(spriteMatrix);
59+
}
60+
else
61+
{
62+
calcMatrix = camMatrix.multiply(spriteMatrix);
63+
}
64+
65+
var frame = src.frame;
66+
var texture = frame.glTexture;
67+
var textureX = frame.cutX;
68+
var textureY = frame.cutY;
69+
var textureWidth = texture.width;
70+
var textureHeight = texture.height;
71+
72+
var tintEffect = (src._isTinted && src.tintFill);
73+
var tintTL = Utils.getTintAppendFloatAlpha(src._tintTL, camera.alpha * src._alphaTL);
74+
var tintTR = Utils.getTintAppendFloatAlpha(src._tintTR, camera.alpha * src._alphaTR);
75+
var tintBL = Utils.getTintAppendFloatAlpha(src._tintBL, camera.alpha * src._alphaBL);
76+
var tintBR = Utils.getTintAppendFloatAlpha(src._tintBR, camera.alpha * src._alphaBR);
77+
78+
pipeline.setTexture2D(texture, 0);
79+
80+
var xAdvance = 0;
81+
var yAdvance = 0;
82+
var indexCount = 0;
83+
var charCode = 0;
84+
var lastCharCode = 0;
85+
var letterSpacing = src.letterSpacing;
86+
var glyph;
87+
var glyphX = 0;
88+
var glyphY = 0;
89+
var glyphW = 0;
90+
var glyphH = 0;
91+
var lastGlyph;
92+
93+
var fontData = src.fontData;
94+
var chars = fontData.chars;
95+
var lineHeight = fontData.lineHeight;
96+
var scale = (src.fontSize / fontData.size);
97+
var rotation = 0;
98+
99+
var roundPixels = camera.roundPixels;
100+
var displayCallback = src.displayCallback;
101+
var crop = (src.cropWidth > 0 || src.cropHeight > 0);
102+
103+
if (crop)
104+
{
105+
renderer.pushScissor(
106+
src.x,
107+
src.y,
108+
src.cropWidth * src.scaleX,
109+
src.cropHeight * src.scaleY
110+
);
111+
}
112+
113+
for (var i = 0; i < textLength; i++)
114+
{
115+
charCode = text.charCodeAt(i);
116+
117+
// Carriage-return
118+
if (charCode === 10)
119+
{
120+
xAdvance = 0;
121+
indexCount = 0;
122+
yAdvance += lineHeight;
123+
lastGlyph = null;
124+
125+
continue;
126+
}
127+
128+
glyph = chars[charCode];
129+
130+
if (!glyph)
131+
{
132+
continue;
133+
}
134+
135+
glyphX = textureX + glyph.x;
136+
glyphY = textureY + glyph.y;
137+
138+
glyphW = glyph.width;
139+
glyphH = glyph.height;
140+
141+
var x = (indexCount + glyph.xOffset + xAdvance) * scale;
142+
var y = (glyph.yOffset + yAdvance) * scale;
143+
144+
if (lastGlyph !== null)
145+
{
146+
var kerningOffset = glyph.kerning[lastCharCode];
147+
x += (kerningOffset !== undefined) ? kerningOffset : 0;
148+
}
149+
150+
xAdvance += glyph.xAdvance + letterSpacing;
151+
indexCount++;
152+
lastGlyph = glyph;
153+
lastCharCode = charCode;
154+
155+
// Nothing to render or a space? Then skip to the next glyph
156+
if (glyphW === 0 || glyphH === 0 || charCode === 32)
157+
{
158+
continue;
159+
}
160+
161+
scale = (src.fontSize / src.fontData.size);
162+
rotation = 0;
163+
164+
if (displayCallback)
165+
{
166+
var output = displayCallback({
167+
color: 0,
168+
tint: {
169+
topLeft: tintTL,
170+
topRight: tintTR,
171+
bottomLeft: tintBL,
172+
bottomRight: tintBR
173+
},
174+
index: i,
175+
charCode: charCode,
176+
x: x,
177+
y: y,
178+
scale: scale,
179+
rotation: rotation,
180+
data: glyph.data
181+
});
182+
183+
x = output.x;
184+
y = output.y;
185+
scale = output.scale;
186+
rotation = output.rotation;
187+
188+
if (output.color)
189+
{
190+
tintTL = output.color;
191+
tintTR = output.color;
192+
tintBL = output.color;
193+
tintBR = output.color;
194+
}
195+
else
196+
{
197+
tintTL = output.tint.topLeft;
198+
tintTR = output.tint.topRight;
199+
tintBL = output.tint.bottomLeft;
200+
tintBR = output.tint.bottomRight;
201+
}
202+
203+
tintTL = Utils.getTintAppendFloatAlpha(tintTL, camera.alpha * src._alphaTL);
204+
tintTR = Utils.getTintAppendFloatAlpha(tintTR, camera.alpha * src._alphaTR);
205+
tintBL = Utils.getTintAppendFloatAlpha(tintBL, camera.alpha * src._alphaBL);
206+
tintBR = Utils.getTintAppendFloatAlpha(tintBR, camera.alpha * src._alphaBR);
207+
}
208+
209+
var u0 = glyphX / textureWidth;
210+
var v0 = glyphY / textureHeight;
211+
var u1 = (glyphX + glyphW) / textureWidth;
212+
var v1 = (glyphY + glyphH) / textureHeight;
213+
214+
var xw = x + glyphW * scale;
215+
var yh = y + glyphH * scale;
216+
217+
var tx0 = x * calcMatrix.a + y * calcMatrix.c + calcMatrix.e;
218+
var ty0 = x * calcMatrix.b + y * calcMatrix.d + calcMatrix.f;
219+
220+
var tx1 = x * calcMatrix.a + yh * calcMatrix.c + calcMatrix.e;
221+
var ty1 = x * calcMatrix.b + yh * calcMatrix.d + calcMatrix.f;
222+
223+
var tx2 = xw * calcMatrix.a + yh * calcMatrix.c + calcMatrix.e;
224+
var ty2 = xw * calcMatrix.b + yh * calcMatrix.d + calcMatrix.f;
225+
226+
var tx3 = xw * calcMatrix.a + y * calcMatrix.c + calcMatrix.e;
227+
var ty3 = xw * calcMatrix.b + y * calcMatrix.d + calcMatrix.f;
228+
229+
if (roundPixels)
230+
{
231+
tx0 |= 0;
232+
ty0 |= 0;
233+
234+
tx1 |= 0;
235+
ty1 |= 0;
236+
237+
tx2 |= 0;
238+
ty2 |= 0;
239+
240+
tx3 |= 0;
241+
ty3 |= 0;
242+
}
243+
244+
pipeline.batchVertices(tx0, ty0, tx1, ty1, tx2, ty2, tx3, ty3, u0, v0, u1, v1, tintTL, tintTR, tintBL, tintBR, tintEffect);
245+
}
246+
247+
if (crop)
248+
{
249+
renderer.popScissor();
250+
}
35251
};
36252

37253
module.exports = DynamicBitmapTextWebGLRenderer;

src/renderer/webgl/pipelines/BitmapMaskPipeline.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ var BitmapMaskPipeline = new Class({
169169
gl.clearColor(0, 0, 0, 0);
170170
gl.clear(gl.COLOR_BUFFER_BIT);
171171

172-
// We render out mask source
172+
// We render our mask source
173173
bitmapMask.visible = true;
174174
bitmapMask.renderWebGL(renderer, bitmapMask, 0.0, camera);
175175
bitmapMask.visible = visible;

0 commit comments

Comments
 (0)