From c3f306c79555a37c7210de4ec304d981f4708c53 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Fri, 14 Feb 2014 12:07:04 +0000 Subject: [PATCH] Testing BitmapFont as a texture. --- examples/wip/bitmaptext3.js | 43 ++++++++++++++++++++++ examples/wip/spritebatch2.js | 69 +++++++++++++++++++++++++++++++++++ src/gameobjects/BitmapData.js | 4 ++ src/gameobjects/BitmapFont.js | 14 +++++-- 4 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 examples/wip/bitmaptext3.js create mode 100644 examples/wip/spritebatch2.js diff --git a/examples/wip/bitmaptext3.js b/examples/wip/bitmaptext3.js new file mode 100644 index 00000000..9eae14d9 --- /dev/null +++ b/examples/wip/bitmaptext3.js @@ -0,0 +1,43 @@ + +// 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 }); + +function preload() { + + game.load.image('knightHawks', 'assets/fonts/KNIGHT3.png'); + +} + +var font; + +function create() { + + font = game.add.bitmapFont(game.world.centerX, 300, 'knightHawks', 31, 25, Phaser.BitmapFont.TEXT_SET6, 10, 1, 1); + font.anchor.set(0.5); + + // font.text = 'phaser'; + + game.input.onDown.add(change, this); + +} + +function change() { + + font.tint = Math.random() * 0xFFFFFF; + +} + +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); + +} diff --git a/examples/wip/spritebatch2.js b/examples/wip/spritebatch2.js new file mode 100644 index 00000000..d7f3436d --- /dev/null +++ b/examples/wip/spritebatch2.js @@ -0,0 +1,69 @@ + +var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update }); + +function preload() { + + game.load.image('knightHawks', 'assets/fonts/KNIGHT3.png'); + game.load.image('maggot', 'assets/sprites/ilkke.png'); + +} + +var font; +var batch; +var dudeBoundsPadding = 100; +var dudeBounds = new Phaser.Rectangle(-dudeBoundsPadding, -dudeBoundsPadding, 800 + dudeBoundsPadding * 2, 600 + dudeBoundsPadding * 2); +var tick = 0; + +function create() { + + font = game.add.bitmapFont(0, 0, 'knightHawks', 31, 25, Phaser.BitmapFont.TEXT_SET6, 10, 1, 1); + font.text = 'phaser'; + + batch = game.add.spriteBatch(); + + var total = (game.renderType === Phaser.WEBGL) ? 1500 : 100; + + console.log(font.width, font.height, font.bmd.width, font.bmd.height); + + for (var i = 0; i < total; i++) + { + var dude = batch.create(game.world.randomX, game.world.randomY, font.bmd); + + dude.anchor.set(0.5); + // dude.scale.set(0.8 + Math.random() * 0.3); + dude.direction = Math.random() * Math.PI * 2; + dude.turningSpeed = Math.random() - 0.8; + dude.speed = (2 + Math.random() * 2) * 0.2; + dude.offset = Math.random() * 100; + } + +} + +function update() { + + batch.forEach(updateMaggot, this, false); + + tick += 0.1; + +} + +function updateMaggot(dude) { + + // dude.scale.y = 0.95 + Math.sin(tick + dude.offset) * 0.15 + dude.direction += dude.turningSpeed * 0.02; + dude.position.x += Math.sin(dude.direction) * (dude.speed * dude.scale.y); + dude.position.y += Math.cos(dude.direction) * (dude.speed * dude.scale.y); + dude.rotation = -dude.direction + Math.PI; + + // wrap the dudes by testing their bounds.. + if (dude.position.x < dudeBounds.x) + dude.position.x += dudeBounds.width; + else if (dude.position.x > dudeBounds.x + dudeBounds.width) + dude.position.x -= dudeBounds.width; + + if (dude.position.y < dudeBounds.y) + dude.position.y += dudeBounds.height; + else if (dude.position.y > dudeBounds.y + dudeBounds.height) + dude.position.y -= dudeBounds.height; + +} diff --git a/src/gameobjects/BitmapData.js b/src/gameobjects/BitmapData.js index 149cf9b8..4c5a5287 100644 --- a/src/gameobjects/BitmapData.js +++ b/src/gameobjects/BitmapData.js @@ -152,6 +152,7 @@ Phaser.BitmapData.prototype = { if (width !== this.width || height !== this.height) { + console.log('bmd resize', width, height); this.width = width; this.height = height; this.canvas.width = width; @@ -165,6 +166,9 @@ Phaser.BitmapData.prototype = { }, + /** + * @method Phaser.BitmapData#refreshBuffer + */ refreshBuffer: function () { this.imageData = this.context.getImageData(0, 0, this.width, this.height); diff --git a/src/gameobjects/BitmapFont.js b/src/gameobjects/BitmapFont.js index 99a58db4..5b5da674 100644 --- a/src/gameobjects/BitmapFont.js +++ b/src/gameobjects/BitmapFont.js @@ -135,7 +135,8 @@ Phaser.BitmapFont = function (game, x, y, key, characterWidth, characterHeight, /** * @property {Phaser.BitmapData} bmd - The internal BitmapData to which the font is rendered. */ - this.bmd = new Phaser.BitmapData(game, this.characterWidth, this.characterHeight); + // this.bmd = new Phaser.BitmapData(game, this.characterWidth, this.characterHeight); + this.bmd = new Phaser.BitmapData(game, 800, 600); Phaser.Image.call(this, game, x, y, this.bmd); @@ -386,9 +387,14 @@ Phaser.BitmapFont.prototype.buildBitmapFontText = function () { } this.bmd.render(); - - this.width = this.bmd.width; - this.height = this.bmd.height; + this.texture.frame.width = this.bmd.width; + this.texture.frame.height = this.bmd.height; + this.updateFrame = true; + this.dirty = true; + this.textureChange = true; + // this.setTexture(this.bmd.texture); + // this.width = this.bmd.width; + // this.height = this.bmd.height; }