Skip to content

Commit d2b9bfe

Browse files
committed
RetroFont has been updated to use RenderTexture.renderXY, removing the need for creating a Point object each update.
RetroFont no longer puts any entries into the TextureCache or generates any UUIDs on instantiation, speeding up creation and lowering memory use.
1 parent 3bfce12 commit d2b9bfe

1 file changed

Lines changed: 29 additions & 43 deletions

File tree

src/gameobjects/RetroFont.js

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -136,22 +136,13 @@ Phaser.RetroFont = function (game, key, characterWidth, characterHeight, chars,
136136

137137
for (var c = 0; c < chars.length; c++)
138138
{
139-
var uuid = game.rnd.uuid();
140-
141-
var frame = this.frameData.addFrame(new Phaser.Frame(c, currentX, currentY, this.characterWidth, this.characterHeight, '', uuid));
139+
var frame = this.frameData.addFrame(new Phaser.Frame(c, currentX, currentY, this.characterWidth, this.characterHeight));
142140

143141
this.grabData[chars.charCodeAt(c)] = frame.index;
144142

145-
PIXI.TextureCache[uuid] = new PIXI.Texture(PIXI.BaseTextureCache[key], {
146-
x: currentX,
147-
y: currentY,
148-
width: this.characterWidth,
149-
height: this.characterHeight
150-
});
151-
152143
r++;
153144

154-
if (r == this.characterPerRow)
145+
if (r === this.characterPerRow)
155146
{
156147
r = 0;
157148
currentX = this.offsetX;
@@ -363,21 +354,18 @@ Phaser.RetroFont.prototype.buildRetroFontText = function () {
363354
// Loop through each line of text
364355
for (var i = 0; i < lines.length; i++)
365356
{
357+
// Phaser.RetroFont.ALIGN_LEFT
358+
cx = 0;
359+
366360
// This line of text is held in lines[i] - need to work out the alignment
367-
switch (this.align)
361+
if (this.align === Phaser.RetroFont.ALIGN_RIGHT)
368362
{
369-
case Phaser.RetroFont.ALIGN_LEFT:
370-
cx = 0;
371-
break;
372-
373-
case Phaser.RetroFont.ALIGN_RIGHT:
374-
cx = this.width - (lines[i].length * (this.characterWidth + this.customSpacingX));
375-
break;
376-
377-
case Phaser.RetroFont.ALIGN_CENTER:
378-
cx = (this.width / 2) - ((lines[i].length * (this.characterWidth + this.customSpacingX)) / 2);
379-
cx += this.customSpacingX / 2;
380-
break;
363+
cx = this.width - (lines[i].length * (this.characterWidth + this.customSpacingX));
364+
}
365+
else if (this.align === Phaser.RetroFont.ALIGN_CENTER)
366+
{
367+
cx = (this.width / 2) - ((lines[i].length * (this.characterWidth + this.customSpacingX)) / 2);
368+
cx += this.customSpacingX / 2;
381369
}
382370

383371
// Sanity checks
@@ -402,20 +390,23 @@ Phaser.RetroFont.prototype.buildRetroFontText = function () {
402390
this.resize(this._text.length * (this.characterWidth + this.customSpacingX), this.characterHeight, true);
403391
}
404392

405-
switch (this.align)
406-
{
407-
case Phaser.RetroFont.ALIGN_LEFT:
408-
cx = 0;
409-
break;
393+
// Phaser.RetroFont.ALIGN_LEFT
394+
cx = 0;
410395

411-
case Phaser.RetroFont.ALIGN_RIGHT:
412-
cx = this.width - (this._text.length * (this.characterWidth + this.customSpacingX));
413-
break;
396+
if (this.align === Phaser.RetroFont.ALIGN_RIGHT)
397+
{
398+
cx = this.width - (this._text.length * (this.characterWidth + this.customSpacingX));
399+
}
400+
else if (this.align === Phaser.RetroFont.ALIGN_CENTER)
401+
{
402+
cx = (this.width / 2) - ((this._text.length * (this.characterWidth + this.customSpacingX)) / 2);
403+
cx += this.customSpacingX / 2;
404+
}
414405

415-
case Phaser.RetroFont.ALIGN_CENTER:
416-
cx = (this.width / 2) - ((this._text.length * (this.characterWidth + this.customSpacingX)) / 2);
417-
cx += this.customSpacingX / 2;
418-
break;
406+
// Sanity checks
407+
if (cx < 0)
408+
{
409+
cx = 0;
419410
}
420411

421412
this.textureBuffer.clear();
@@ -438,12 +429,10 @@ Phaser.RetroFont.prototype.buildRetroFontText = function () {
438429
*/
439430
Phaser.RetroFont.prototype.pasteLine = function (line, x, y, customSpacingX) {
440431

441-
var p = new Phaser.Point();
442-
443432
for (var c = 0; c < line.length; c++)
444433
{
445434
// If it's a space then there is no point copying, so leave a blank space
446-
if (line.charAt(c) == " ")
435+
if (line.charAt(c) === " ")
447436
{
448437
x += this.characterWidth + customSpacingX;
449438
}
@@ -453,8 +442,7 @@ Phaser.RetroFont.prototype.pasteLine = function (line, x, y, customSpacingX) {
453442
if (this.grabData[line.charCodeAt(c)] >= 0)
454443
{
455444
this.stamp.frame = this.grabData[line.charCodeAt(c)];
456-
p.set(x, y);
457-
this.render(this.stamp, p, false);
445+
this.renderXY(this.stamp, x, y, false);
458446

459447
x += this.characterWidth + customSpacingX;
460448

@@ -549,8 +537,6 @@ Phaser.RetroFont.prototype.updateOffset = function (x, y) {
549537
{
550538
frames[i].x += diffX;
551539
frames[i].y += diffY;
552-
PIXI.TextureCache[frames[i].uuid].frame.x = frames[i].x;
553-
PIXI.TextureCache[frames[i].uuid].frame.y = frames[i].y;
554540
}
555541

556542
this.buildRetroFontText();

0 commit comments

Comments
 (0)