Skip to content

Commit c3f306c

Browse files
committed
Testing BitmapFont as a texture.
1 parent 7d2a818 commit c3f306c

4 files changed

Lines changed: 126 additions & 4 deletions

File tree

examples/wip/bitmaptext3.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
// var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
3+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
4+
5+
function preload() {
6+
7+
game.load.image('knightHawks', 'assets/fonts/KNIGHT3.png');
8+
9+
}
10+
11+
var font;
12+
13+
function create() {
14+
15+
font = game.add.bitmapFont(game.world.centerX, 300, 'knightHawks', 31, 25, Phaser.BitmapFont.TEXT_SET6, 10, 1, 1);
16+
font.anchor.set(0.5);
17+
18+
// font.text = 'phaser';
19+
20+
game.input.onDown.add(change, this);
21+
22+
}
23+
24+
function change() {
25+
26+
font.tint = Math.random() * 0xFFFFFF;
27+
28+
}
29+
30+
function update() {
31+
32+
// font.text = "phaser\n\nx: " + game.input.x + "\ny: " + game.input.y;
33+
font.text = "phaser x: " + game.input.x + " y: " + game.input.y;
34+
35+
}
36+
37+
function render() {
38+
39+
game.debug.renderText("x: " + game.input.x + "\ny: " + game.input.y, 32, 32);
40+
game.debug.renderText(font.bmd.width + " x " + font.bmd.height, 32, 64);
41+
game.debug.renderText(font.width + " x " + font.height, 32, 96);
42+
43+
}

examples/wip/spritebatch2.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
3+
4+
function preload() {
5+
6+
game.load.image('knightHawks', 'assets/fonts/KNIGHT3.png');
7+
game.load.image('maggot', 'assets/sprites/ilkke.png');
8+
9+
}
10+
11+
var font;
12+
var batch;
13+
var dudeBoundsPadding = 100;
14+
var dudeBounds = new Phaser.Rectangle(-dudeBoundsPadding, -dudeBoundsPadding, 800 + dudeBoundsPadding * 2, 600 + dudeBoundsPadding * 2);
15+
var tick = 0;
16+
17+
function create() {
18+
19+
font = game.add.bitmapFont(0, 0, 'knightHawks', 31, 25, Phaser.BitmapFont.TEXT_SET6, 10, 1, 1);
20+
font.text = 'phaser';
21+
22+
batch = game.add.spriteBatch();
23+
24+
var total = (game.renderType === Phaser.WEBGL) ? 1500 : 100;
25+
26+
console.log(font.width, font.height, font.bmd.width, font.bmd.height);
27+
28+
for (var i = 0; i < total; i++)
29+
{
30+
var dude = batch.create(game.world.randomX, game.world.randomY, font.bmd);
31+
32+
dude.anchor.set(0.5);
33+
// dude.scale.set(0.8 + Math.random() * 0.3);
34+
dude.direction = Math.random() * Math.PI * 2;
35+
dude.turningSpeed = Math.random() - 0.8;
36+
dude.speed = (2 + Math.random() * 2) * 0.2;
37+
dude.offset = Math.random() * 100;
38+
}
39+
40+
}
41+
42+
function update() {
43+
44+
batch.forEach(updateMaggot, this, false);
45+
46+
tick += 0.1;
47+
48+
}
49+
50+
function updateMaggot(dude) {
51+
52+
// dude.scale.y = 0.95 + Math.sin(tick + dude.offset) * 0.15
53+
dude.direction += dude.turningSpeed * 0.02;
54+
dude.position.x += Math.sin(dude.direction) * (dude.speed * dude.scale.y);
55+
dude.position.y += Math.cos(dude.direction) * (dude.speed * dude.scale.y);
56+
dude.rotation = -dude.direction + Math.PI;
57+
58+
// wrap the dudes by testing their bounds..
59+
if (dude.position.x < dudeBounds.x)
60+
dude.position.x += dudeBounds.width;
61+
else if (dude.position.x > dudeBounds.x + dudeBounds.width)
62+
dude.position.x -= dudeBounds.width;
63+
64+
if (dude.position.y < dudeBounds.y)
65+
dude.position.y += dudeBounds.height;
66+
else if (dude.position.y > dudeBounds.y + dudeBounds.height)
67+
dude.position.y -= dudeBounds.height;
68+
69+
}

src/gameobjects/BitmapData.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Phaser.BitmapData.prototype = {
152152

153153
if (width !== this.width || height !== this.height)
154154
{
155+
console.log('bmd resize', width, height);
155156
this.width = width;
156157
this.height = height;
157158
this.canvas.width = width;
@@ -165,6 +166,9 @@ Phaser.BitmapData.prototype = {
165166

166167
},
167168

169+
/**
170+
* @method Phaser.BitmapData#refreshBuffer
171+
*/
168172
refreshBuffer: function () {
169173

170174
this.imageData = this.context.getImageData(0, 0, this.width, this.height);

src/gameobjects/BitmapFont.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ Phaser.BitmapFont = function (game, x, y, key, characterWidth, characterHeight,
135135
/**
136136
* @property {Phaser.BitmapData} bmd - The internal BitmapData to which the font is rendered.
137137
*/
138-
this.bmd = new Phaser.BitmapData(game, this.characterWidth, this.characterHeight);
138+
// this.bmd = new Phaser.BitmapData(game, this.characterWidth, this.characterHeight);
139+
this.bmd = new Phaser.BitmapData(game, 800, 600);
139140

140141
Phaser.Image.call(this, game, x, y, this.bmd);
141142

@@ -386,9 +387,14 @@ Phaser.BitmapFont.prototype.buildBitmapFontText = function () {
386387
}
387388

388389
this.bmd.render();
389-
390-
this.width = this.bmd.width;
391-
this.height = this.bmd.height;
390+
this.texture.frame.width = this.bmd.width;
391+
this.texture.frame.height = this.bmd.height;
392+
this.updateFrame = true;
393+
this.dirty = true;
394+
this.textureChange = true;
395+
// this.setTexture(this.bmd.texture);
396+
// this.width = this.bmd.width;
397+
// this.height = this.bmd.height;
392398

393399
}
394400

0 commit comments

Comments
 (0)