From: nathan.sweet Date: Mon, 22 Nov 2010 02:55:20 +0000 (+0000) Subject: [changed] BitmapFont to be backed by a Sprite and not a Texture, so packed fonts... X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=41cadccfaa7b7767c664f473826aefc2bf042d9d;p=mikumikustudio%2Flibgdx-mikumikustudio.git [changed] BitmapFont to be backed by a Sprite and not a Texture, so packed fonts are possible. [changed] Eclipse project source dirs to exclude **/.svn/*. [fixed] SpriteSheetTest. --- diff --git a/backends/dependencies/.classpath b/backends/dependencies/.classpath index 07ca123c6..1a779ea08 100644 --- a/backends/dependencies/.classpath +++ b/backends/dependencies/.classpath @@ -1,6 +1,6 @@ - + diff --git a/backends/gdx-backend-jogl/.classpath b/backends/gdx-backend-jogl/.classpath index 845e176d1..7ce582dcf 100644 --- a/backends/gdx-backend-jogl/.classpath +++ b/backends/gdx-backend-jogl/.classpath @@ -1,6 +1,6 @@ - + diff --git a/extensions/image-packer/.classpath b/extensions/image-packer/.classpath index ac8c860c4..b8991e0e9 100644 --- a/extensions/image-packer/.classpath +++ b/extensions/image-packer/.classpath @@ -1,6 +1,6 @@ - + diff --git a/gdx/src/com/badlogic/gdx/graphics/BitmapFont.java b/gdx/src/com/badlogic/gdx/graphics/BitmapFont.java index 31ca483df..62a2c1e53 100644 --- a/gdx/src/com/badlogic/gdx/graphics/BitmapFont.java +++ b/gdx/src/com/badlogic/gdx/graphics/BitmapFont.java @@ -41,20 +41,23 @@ import java.util.StringTokenizer; /** * Loads and renders AngleCode BMFont files. The bitmap font consists of 2 * files: the .fnt file which must be saved with text encoding (not xml or - * binary!) and the bitmap file holding the glyphs, usually in .png format.
+ * binary!) and the bitmap file holding the glyphs, usually in .png format.
*
- * This implementation currently only supports a single glyph page.
+ * This implementation currently only supports a single glyph page.
*
* To draw text with this class you need to call one of the draw() methods * together with a {@link SpriteBatch}. The SpriteBatch must be in rendering * mode, that is, {@link SpriteBatch#begin()} must have been called before - * drawing
+ * drawing.
*
- * Additionally you can cache text in a {@link BitmapFontCache} for faster - * rendering of static text.
+ * Text can be cached in a {@link BitmapFontCache} for faster rendering of static + * text.
*
- * A BitmapFont is managed. You need to call the {@link #dispose()} method when - * you no longer need it.
+ * A BitmapFont loaded from a file is managed. {@link #dispose()} must be called to + * free the backing texture when no longer needed. A BitmapFont loaded using a + * sprite is managed if the sprite's texture is managed. Disposing the BitmapFont + * disposes the sprite's texture, which may not be desirable if the texture is still + * being used elsewhere.
*
* The code is heavily based on Matthias Mann's TWL BitmapFont class. Thanks for * sharing, Matthias! :) @@ -67,7 +70,7 @@ public class BitmapFont { private static final int PAGE_SIZE = 1 << LOG2_PAGE_SIZE; private static final int PAGES = 0x10000 / PAGE_SIZE; - Texture texture; + Sprite sprite; int lineHeight; int yOffset; int down; @@ -87,11 +90,10 @@ public class BitmapFont { Gdx.files.classpath("com/badlogic/gdx/utils/arial-15.png"), false); } - public BitmapFont(FileHandle fontFile, Texture texture, boolean flip) { - init(fontFile, texture, flip); + public BitmapFont(FileHandle fontFile, Sprite sprite) { + init(fontFile, sprite, false); } - /** * Creates a new BitmapFont instance based on a .fnt file and an image file * holding the page with glyphs. Currently only supports single page @@ -103,17 +105,21 @@ public class BitmapFont { * where 0,0 is the upper left corner. */ public BitmapFont(FileHandle fontFile, FileHandle imageFile, boolean flip) { - texture = Gdx.graphics.newTexture(imageFile, TextureFilter.Linear, + sprite = new Sprite(Gdx.graphics.newTexture(imageFile, TextureFilter.Linear, TextureFilter.Linear, TextureWrap.ClampToEdge, - TextureWrap.ClampToEdge); - init(fontFile, texture, flip); + TextureWrap.ClampToEdge)); + init(fontFile, sprite, flip); } - private void init(FileHandle fontFile, Texture texture, boolean flip) { - this.texture = texture; - float invTexWidth = 1.0f / texture.getWidth(); - float invTexHeight = 1.0f / texture.getHeight(); + private void init(FileHandle fontFile, Sprite sprite, boolean flip) { + this.sprite = sprite; + float invTexWidth = 1.0f / sprite.getWidth(); + float invTexHeight = 1.0f / sprite.getHeight(); + float uSprite = sprite.getTextureRegionX(); + float vSprite = sprite.getTextureRegionY(); + float u2Sprite = uSprite + sprite.getTextureRegionWidth(); + float v2Sprite = vSprite + sprite.getTextureRegionHeight(); BufferedReader reader = new BufferedReader(new InputStreamReader(fontFile.read()), 512); try { @@ -173,14 +179,14 @@ public class BitmapFont { tokens.nextToken(); glyph.xadvance = Integer.parseInt(tokens.nextToken()); - glyph.u = srcX * invTexWidth; - glyph.u2 = (srcX + glyph.width) * invTexWidth; + glyph.u = uSprite + srcX * invTexWidth; + glyph.u2 = uSprite + (srcX + glyph.width) * invTexWidth; if (flip) { - glyph.v = srcY * invTexHeight; + glyph.v = srcY * invTexHeight; glyph.v2 = (srcY + glyph.height) * invTexHeight; } else { - glyph.v2 = srcY * invTexHeight; - glyph.v = (srcY + glyph.height) * invTexHeight; + glyph.v2 = srcY * invTexHeight; + glyph.v = (srcY + glyph.height) * invTexHeight; } } @@ -268,6 +274,7 @@ public class BitmapFont { */ public int draw(SpriteBatch spriteBatch, CharSequence str, int x, int y, Color tint, int start, int end) { + final Texture texture = sprite.getTexture(); final float color = tint.toFloatBits(); y += yOffset; int startX = x; @@ -531,10 +538,10 @@ public class BitmapFont { } /** - * @return The glyph texture + * @return The glyph sprite */ - public Texture getTexture() { - return texture; + public Sprite getSprite() { + return sprite; } /** @@ -576,10 +583,10 @@ public class BitmapFont { } /** - * Frees all resources of this font. + * Disposes the texture used by this BitmapFont's sprite. */ public void dispose() { - texture.dispose(); + sprite.getTexture().dispose(); } static class Glyph { diff --git a/gdx/src/com/badlogic/gdx/graphics/BitmapFontCache.java b/gdx/src/com/badlogic/gdx/graphics/BitmapFontCache.java index 000eebaad..876ca56d0 100644 --- a/gdx/src/com/badlogic/gdx/graphics/BitmapFontCache.java +++ b/gdx/src/com/badlogic/gdx/graphics/BitmapFontCache.java @@ -119,7 +119,7 @@ public class BitmapFontCache { * The SpriteBatch */ public void draw(SpriteBatch spriteBatch) { - spriteBatch.draw(font.texture, vertices, 0, idx); + spriteBatch.draw(font.getSprite().getTexture(), vertices, 0, idx); } void reset(int glyphCount) { diff --git a/gdx/src/com/badlogic/gdx/graphics/Sprite.java b/gdx/src/com/badlogic/gdx/graphics/Sprite.java index 65493d896..0fb1e5df4 100644 --- a/gdx/src/com/badlogic/gdx/graphics/Sprite.java +++ b/gdx/src/com/badlogic/gdx/graphics/Sprite.java @@ -538,6 +538,22 @@ public class Sprite { return scaleY; } + public float getTextureRegionX () { + return vertices[U1]; + } + + public float getTextureRegionY () { + return vertices[V1]; + } + + public float getTextureRegionWidth () { + return vertices[U3] - vertices[U1]; + } + + public float getTextureRegionHeight () { + return vertices[V1] - vertices[V2]; + } + /** * Returns the color for this sprite. Changing the returned color will have * no affect. {@link #setColor(Color)} or diff --git a/tests/gdx-tests-android/assets/data/pack b/tests/gdx-tests-android/assets/data/pack index 860a8f4c2..40115d67b 100644 --- a/tests/gdx-tests-android/assets/data/pack +++ b/tests/gdx-tests-android/assets/data/pack @@ -1,5 +1,5 @@ -pack-in1.png +pack1.png format: RGBA8888 filter: Linear,Linear repeat: none diff --git a/tests/gdx-tests-android/assets/data/pack1.png b/tests/gdx-tests-android/assets/data/pack1.png new file mode 100644 index 000000000..9bc8a4b53 Binary files /dev/null and b/tests/gdx-tests-android/assets/data/pack1.png differ diff --git a/tests/gdx-tests-jogl/.classpath b/tests/gdx-tests-jogl/.classpath index f305ffb0d..3df0463d4 100644 --- a/tests/gdx-tests-jogl/.classpath +++ b/tests/gdx-tests-jogl/.classpath @@ -1,6 +1,6 @@ - + diff --git a/tests/gdx-tests-jogl/data/pack b/tests/gdx-tests-jogl/data/pack index 860a8f4c2..40115d67b 100644 --- a/tests/gdx-tests-jogl/data/pack +++ b/tests/gdx-tests-jogl/data/pack @@ -1,5 +1,5 @@ -pack-in1.png +pack1.png format: RGBA8888 filter: Linear,Linear repeat: none diff --git a/tests/gdx-tests-jogl/data/pack1.png b/tests/gdx-tests-jogl/data/pack1.png new file mode 100644 index 000000000..9bc8a4b53 Binary files /dev/null and b/tests/gdx-tests-jogl/data/pack1.png differ diff --git a/tests/gdx-tests-lwjgl/data/pack b/tests/gdx-tests-lwjgl/data/pack index 860a8f4c2..40115d67b 100644 --- a/tests/gdx-tests-lwjgl/data/pack +++ b/tests/gdx-tests-lwjgl/data/pack @@ -1,5 +1,5 @@ -pack-in1.png +pack1.png format: RGBA8888 filter: Linear,Linear repeat: none diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/SpriteSheetTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/SpriteSheetTest.java index ad4e911ee..53552c8db 100644 --- a/tests/gdx-tests/src/com/badlogic/gdx/tests/SpriteSheetTest.java +++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/SpriteSheetTest.java @@ -16,7 +16,7 @@ public class SpriteSheetTest extends GdxTest { public void create () { batch = new SpriteBatch(); - spriteSheet = new SpriteSheet(Gdx.files.internal("data/pack"), Gdx.files.internal("data")); + spriteSheet = new SpriteSheet(Gdx.files.internal("data")); badlogic = spriteSheet.get("badlogicslice"); badlogicSmall = spriteSheet.get("badlogicsmall"); star = spriteSheet.get("particle-star");