/**\r
* Loads and renders AngleCode BMFont files. The bitmap font consists of 2\r
* files: the .fnt file which must be saved with text encoding (not xml or\r
- * binary!) and the bitmap file holding the glyphs, usually in .png format. <br>\r
+ * binary!) and the bitmap file holding the glyphs, usually in .png format.<br>\r
* <br>\r
- * This implementation currently only supports a single glyph page. <br>\r
+ * This implementation currently only supports a single glyph page.<br>\r
* <br>\r
* To draw text with this class you need to call one of the draw() methods\r
* together with a {@link SpriteBatch}. The SpriteBatch must be in rendering\r
* mode, that is, {@link SpriteBatch#begin()} must have been called before\r
- * drawing <br>\r
+ * drawing.<br>\r
* <br>\r
- * Additionally you can cache text in a {@link BitmapFontCache} for faster\r
- * rendering of static text. <br>\r
+ * Text can be cached in a {@link BitmapFontCache} for faster rendering of static \r
+ * text. <br>\r
* <br>\r
- * A BitmapFont is managed. You need to call the {@link #dispose()} method when\r
- * you no longer need it. <br>\r
+ * A BitmapFont loaded from a file is managed. {@link #dispose()} must be called to \r
+ * free the backing texture when no longer needed. A BitmapFont loaded using a \r
+ * sprite is managed if the sprite's texture is managed. Disposing the BitmapFont\r
+ * disposes the sprite's texture, which may not be desirable if the texture is still\r
+ * being used elsewhere.<br>\r
* <br>\r
* The code is heavily based on Matthias Mann's TWL BitmapFont class. Thanks for\r
* sharing, Matthias! :)\r
private static final int PAGE_SIZE = 1 << LOG2_PAGE_SIZE;\r
private static final int PAGES = 0x10000 / PAGE_SIZE;\r
\r
- Texture texture;\r
+ Sprite sprite;\r
int lineHeight;\r
int yOffset;\r
int down;\r
Gdx.files.classpath("com/badlogic/gdx/utils/arial-15.png"), false);\r
}\r
\r
- public BitmapFont(FileHandle fontFile, Texture texture, boolean flip) {\r
- init(fontFile, texture, flip);\r
+ public BitmapFont(FileHandle fontFile, Sprite sprite) {\r
+ init(fontFile, sprite, false);\r
}\r
\r
-\r
/**\r
* Creates a new BitmapFont instance based on a .fnt file and an image file\r
* holding the page with glyphs. Currently only supports single page\r
* where 0,0 is the upper left corner.\r
*/\r
public BitmapFont(FileHandle fontFile, FileHandle imageFile, boolean flip) {\r
- texture = Gdx.graphics.newTexture(imageFile, TextureFilter.Linear,\r
+ sprite = new Sprite(Gdx.graphics.newTexture(imageFile, TextureFilter.Linear,\r
TextureFilter.Linear, TextureWrap.ClampToEdge,\r
- TextureWrap.ClampToEdge);\r
- init(fontFile, texture, flip);\r
+ TextureWrap.ClampToEdge));\r
+ init(fontFile, sprite, flip);\r
}\r
\r
\r
- private void init(FileHandle fontFile, Texture texture, boolean flip) {\r
- this.texture = texture;\r
- float invTexWidth = 1.0f / texture.getWidth();\r
- float invTexHeight = 1.0f / texture.getHeight();\r
+ private void init(FileHandle fontFile, Sprite sprite, boolean flip) {\r
+ this.sprite = sprite;\r
+ float invTexWidth = 1.0f / sprite.getWidth();\r
+ float invTexHeight = 1.0f / sprite.getHeight();\r
+ float uSprite = sprite.getTextureRegionX();\r
+ float vSprite = sprite.getTextureRegionY();\r
+ float u2Sprite = uSprite + sprite.getTextureRegionWidth();\r
+ float v2Sprite = vSprite + sprite.getTextureRegionHeight();\r
\r
BufferedReader reader = new BufferedReader(new InputStreamReader(fontFile.read()), 512);\r
try {\r
tokens.nextToken();\r
glyph.xadvance = Integer.parseInt(tokens.nextToken());\r
\r
- glyph.u = srcX * invTexWidth;\r
- glyph.u2 = (srcX + glyph.width) * invTexWidth;\r
+ glyph.u = uSprite + srcX * invTexWidth;\r
+ glyph.u2 = uSprite + (srcX + glyph.width) * invTexWidth;\r
if (flip) {\r
- glyph.v = srcY * invTexHeight;\r
+ glyph.v = srcY * invTexHeight;\r
glyph.v2 = (srcY + glyph.height) * invTexHeight;\r
} else {\r
- glyph.v2 = srcY * invTexHeight;\r
- glyph.v = (srcY + glyph.height) * invTexHeight;\r
+ glyph.v2 = srcY * invTexHeight;\r
+ glyph.v = (srcY + glyph.height) * invTexHeight;\r
}\r
}\r
\r
*/\r
public int draw(SpriteBatch spriteBatch, CharSequence str, int x, int y,\r
Color tint, int start, int end) {\r
+ final Texture texture = sprite.getTexture();\r
final float color = tint.toFloatBits();\r
y += yOffset;\r
int startX = x;\r
}\r
\r
/**\r
- * @return The glyph texture\r
+ * @return The glyph sprite\r
*/\r
- public Texture getTexture() {\r
- return texture;\r
+ public Sprite getSprite() {\r
+ return sprite;\r
}\r
\r
/**\r
}\r
\r
/**\r
- * Frees all resources of this font.\r
+ * Disposes the texture used by this BitmapFont's sprite.\r
*/\r
public void dispose() {\r
- texture.dispose();\r
+ sprite.getTexture().dispose();\r
}\r
\r
static class Glyph {\r