Testing BitmapFont as a texture.

This commit is contained in:
photonstorm
2014-02-14 12:07:04 +00:00
parent 7d2a818d0d
commit c3f306c795
4 changed files with 126 additions and 4 deletions
+43
View File
@@ -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);
}
+69
View File
@@ -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;
}
+4
View File
@@ -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);
+10 -4
View File
@@ -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;
}