From 539a0f2256cb2f03d6f2928a3fea78b3fb5d551b Mon Sep 17 00:00:00 2001 From: photonstorm Date: Fri, 14 Feb 2014 16:38:06 +0000 Subject: [PATCH] BitmapFont fixes and updates and Cache support for it added. Working sweet now. --- README.md | 3 ++- examples/wip/bitmaptext3.js | 33 +++++------------------ src/gameobjects/BitmapFont.js | 10 ++++--- src/gameobjects/GameObjectFactory.js | 7 +++-- src/loader/Cache.js | 39 ++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 890d5ba6..92c5e209 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,8 @@ New features: * BitmapText now uses the new XML parser which should work under CocoonJS without clashes. * BitmapText signature changed so you can support fonts with spaces in their names. * Loader.bitmapFont now has 2 extra parameters: xSpacing and ySpacing. These allow you to add extra spacing to each letter or line of the font. -* Added the new BitmapFont class. This is for rendering retro style fixed-width bitmap fonts into an Image object. +* Added the new BitmapFont class. This is for rendering retro style fixed-width bitmap fonts into an Image object. It's a texture you can apply to a Sprite/Image. +* Added Cache.updateFrameData which is really useful for swapping FrameData blocks in the cache. Updates: diff --git a/examples/wip/bitmaptext3.js b/examples/wip/bitmaptext3.js index 80f2484a..1d861f06 100644 --- a/examples/wip/bitmaptext3.js +++ b/examples/wip/bitmaptext3.js @@ -1,6 +1,6 @@ -// var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); -var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render }); +// var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update }); +var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update }); function preload() { @@ -16,38 +16,17 @@ function create() { font = game.add.bitmapFont('knightHawks', 31, 25, Phaser.BitmapFont.TEXT_SET6, 10, 1, 1); font.text = 'phaser was here'; - for (var c = 0; c < 10; c++) + for (var c = 0; c < 19; c++) { - var i = game.add.image(0, c * 32, font); + var i = game.add.image(game.world.centerX, c * 32, font); i.tint = Math.random() * 0xFFFFFF; + i.anchor.set(0.5, 1); } - // i.scale.set(2); - - // font.anchor.set(0.5); - // font.text = 'phaser'; - - game.input.onDown.add(change, this); - -} - -function change() { - - font.text = 'rocking the house'; - } function update() { - // font.text = "phaser\n\nx: " + game.input.x + "\ny: " + game.input.y; - // font.text = "phaser x: " + game.input.x + " y: " + game.input.y; - -} - -function render() { - - // game.debug.renderText("x: " + game.input.x + "\ny: " + game.input.y, 32, 32); - // game.debug.renderText(font.bmd.width + " x " + font.bmd.height, 32, 64); - // game.debug.renderText(font.width + " x " + font.height, 32, 96); + font.text = "phaser x: " + game.input.x + " y: " + game.input.y; } diff --git a/src/gameobjects/BitmapFont.js b/src/gameobjects/BitmapFont.js index 586ec3e5..663b22f5 100644 --- a/src/gameobjects/BitmapFont.js +++ b/src/gameobjects/BitmapFont.js @@ -462,12 +462,11 @@ Phaser.BitmapFont.prototype.pasteLine = function (line, x, y, customSpacingX) { else { // If the character doesn't exist in the font then we don't want a blank space, we just want to skip it - if (this.grabData[line.charCodeAt(c)]) + if (this.grabData[line.charCodeAt(c)] >= 0) { this.stamp.frame = this.grabData[line.charCodeAt(c)]; p.set(x, y); this.render(this.stamp, p, false); - // this.bmd.copyPixels(this.fontSet, this.grabData[line.charCodeAt(c)], x, y); x += this.characterWidth + this.customSpacingX; @@ -522,9 +521,12 @@ Phaser.BitmapFont.prototype.removeUnsupportedCharacters = function (stripCR) { for (var c = 0; c < this._text.length; c++) { - if (this.grabData[this._text.charCodeAt(c)] || this._text.charCodeAt(c) == 32 || (!stripCR && this._text.charAt(c) === "\n")) + var char = this._text[c]; + var code = char.charCodeAt(0); + + if (this.grabData[code] >= 0 || (!stripCR && char === "\n")) { - newString = newString.concat(this._text.charAt(c)); + newString = newString.concat(char); } } diff --git a/src/gameobjects/GameObjectFactory.js b/src/gameobjects/GameObjectFactory.js index 24014a03..3dae41e8 100644 --- a/src/gameobjects/GameObjectFactory.js +++ b/src/gameobjects/GameObjectFactory.js @@ -258,9 +258,12 @@ Phaser.GameObjectFactory.prototype = { }, /** - * Create a new BitmapFont object. + * Create a new BitmapFont object to be used as a texture for an Image or Sprite and optionally add it to the Cache. + * The texture can be asssigned or one or multiple images/sprites, but note that the text the BitmapFont uses will be shared across them all, + * i.e. if you need each Image to have different text in it, then you need to create multiple BitmapFont objects. * - * @param {string} font - The key of the BitmapFont as stored in Game.Cache. + * @method Phaser.GameObjectFactory#bitmapFont + * @param {string} font - The key of the image in the Game.Cache that the BitmapFont will use. * @param {number} characterWidth - The width of each character in the font set. * @param {number} characterHeight - The height of each character in the font set. * @param {string} chars - The characters used in the font set, in display order. You can use the TEXT_SET consts for common font set arrangements. diff --git a/src/loader/Cache.js b/src/loader/Cache.js index bbdab5d5..739621be 100644 --- a/src/loader/Cache.js +++ b/src/loader/Cache.js @@ -67,6 +67,12 @@ Phaser.Cache = function (game) { */ this._bitmapDatas = {}; + /** + * @property {object} _bitmapFont - BitmapFont key-value container. + * @private + */ + this._bitmapFont = {}; + this.addDefaultImage(); this.addMissingImage(); @@ -134,6 +140,19 @@ Phaser.Cache.prototype = { }, + /** + * Add a Phaser.BitmapFont in to the cache. + * + * @method Phaser.Cache#addBitmapFont + * @param {string} key - The unique key by which you will reference this object. + * @param {Phaser.BitmapFont} texture - The BitmapFont object to be stored. This can be applied to any Image/Sprite as a texture. + */ + addBitmapFont: function (key, texture) { + + this._bitmapFont[key] = texture; + + }, + /** * Add a new sprite sheet in to the cache. * @@ -450,6 +469,26 @@ Phaser.Cache.prototype = { }, + /** + * Get a BitmapFont object from the cache by its key. + * + * @method Phaser.Cache#getBitmapFont + * @param {string} key - Asset key of the BitmapFont object to retrieve from the Cache. + * @return {Phaser.BitmapFont} The requested BitmapFont object if found, or null if not. + */ + getBitmapFont: function (key) { + + if (this._bitmapFont[key]) + { + return this._bitmapFont[key]; + } + else + { + console.warn('Phaser.Cache.getBitmapFont: Invalid key: "' + key + '"'); + } + + }, + /** * Checks if an image key exists. *