Skip to content

Commit 96a64ea

Browse files
committed
ParseXMLBitmapFont has a new optional parameter texture. If defined, this Texture is populated with Frame data, one frame per glyph. This happens automatically when loading Bitmap Text data in Phaser.
1 parent d98d305 commit 96a64ea

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

src/gameobjects/bitmaptext/ParseFromAtlas.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ var ParseXMLBitmapFont = require('./ParseXMLBitmapFont');
2727
*/
2828
var ParseFromAtlas = function (scene, fontName, textureKey, frameKey, xmlKey, xSpacing, ySpacing)
2929
{
30-
var frame = scene.sys.textures.getFrame(textureKey, frameKey);
30+
var texture = scene.sys.textures.get(textureKey);
31+
var frame = texture.get(frameKey);
3132
var xml = scene.sys.cache.xml.get(xmlKey);
3233

3334
if (frame && xml)
3435
{
35-
var data = ParseXMLBitmapFont(xml, frame, xSpacing, ySpacing);
36+
var data = ParseXMLBitmapFont(xml, frame, xSpacing, ySpacing, texture);
3637

3738
scene.sys.cache.bitmapFont.add(fontName, { data: data, texture: textureKey, frame: frameKey });
3839

src/gameobjects/bitmaptext/ParseXMLBitmapFont.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ function getValue (node, attribute)
3232
* @param {Phaser.Textures.Frame} frame - The texture frame to take into account when creating the uv data.
3333
* @param {integer} [xSpacing=0] - The x-axis spacing to add between each letter.
3434
* @param {integer} [ySpacing=0] - The y-axis spacing to add to the line height.
35+
* @param {Phaser.Textures.Texture} [texture] - If provided, each glyph in the Bitmap Font will be added to this texture as a frame.
3536
*
3637
* @return {Phaser.Types.GameObjects.BitmapText.BitmapFontData} The parsed Bitmap Font data.
3738
*/
38-
var ParseXMLBitmapFont = function (xml, frame, xSpacing, ySpacing)
39+
var ParseXMLBitmapFont = function (xml, frame, xSpacing, ySpacing, texture)
3940
{
4041
if (xSpacing === undefined) { xSpacing = 0; }
4142
if (ySpacing === undefined) { ySpacing = 0; }
@@ -44,6 +45,7 @@ var ParseXMLBitmapFont = function (xml, frame, xSpacing, ySpacing)
4445
var textureY = frame.cutY;
4546
var textureWidth = frame.source.width;
4647
var textureHeight = frame.source.height;
48+
var sourceIndex = frame.sourceIndex;
4749

4850
var data = {};
4951
var info = xml.getElementsByTagName('info')[0];
@@ -69,6 +71,7 @@ var ParseXMLBitmapFont = function (xml, frame, xSpacing, ySpacing)
6971
var node = letters[i];
7072

7173
var charCode = getValue(node, 'id');
74+
var letter = String.fromCharCode(charCode);
7275
var gx = getValue(node, 'x');
7376
var gy = getValue(node, 'y');
7477
var gw = getValue(node, 'width');
@@ -98,6 +101,11 @@ var ParseXMLBitmapFont = function (xml, frame, xSpacing, ySpacing)
98101
gy -= frame.y;
99102
}
100103

104+
var u0 = (textureX + gx) / textureWidth;
105+
var v0 = (textureY + gy) / textureHeight;
106+
var u1 = (textureX + gx + gw) / textureWidth;
107+
var v1 = (textureY + gy + gh) / textureHeight;
108+
101109
data.chars[charCode] =
102110
{
103111
x: gx,
@@ -111,11 +119,18 @@ var ParseXMLBitmapFont = function (xml, frame, xSpacing, ySpacing)
111119
xAdvance: getValue(node, 'xadvance') + xSpacing,
112120
data: {},
113121
kerning: {},
114-
u0: (textureX + gx) / textureWidth,
115-
v0: (textureY + gy) / textureHeight,
116-
u1: (textureX + gx + gw) / textureWidth,
117-
v1: (textureY + gy + gh) / textureHeight
122+
u0: u0,
123+
v0: v0,
124+
u1: u1,
125+
v1: v1
118126
};
127+
128+
if (texture)
129+
{
130+
var charFrame = texture.add(letter, sourceIndex, gx, gy, gw, gh);
131+
132+
charFrame.setUVs(gw, gh, u0, v0, u1, v1);
133+
}
119134
}
120135

121136
var kernings = xml.getElementsByTagName('kerning');

src/loader/filetypes/BitmapFontFile.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ var BitmapFontFile = new Class({
9999
image.addToCache();
100100
xml.addToCache();
101101

102-
var data = ParseXMLBitmapFont(xml.data, image.cache.getFrame(image.key));
102+
var texture = image.cache.get(image.key);
103+
104+
var data = ParseXMLBitmapFont(xml.data, image.cache.getFrame(image.key), 0, 0, texture);
103105

104106
this.loader.cacheManager.bitmapFont.add(image.key, { data: data, texture: image.key, frame: null });
105107

0 commit comments

Comments
 (0)