OSDN Git Service

[changed] BitmapFont to be backed by a Sprite and not a Texture, so packed fonts...
authornathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Mon, 22 Nov 2010 02:55:20 +0000 (02:55 +0000)
committernathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Mon, 22 Nov 2010 02:55:20 +0000 (02:55 +0000)
[changed] Eclipse project source dirs to exclude **/.svn/*.
[fixed] SpriteSheetTest.

13 files changed:
backends/dependencies/.classpath
backends/gdx-backend-jogl/.classpath
extensions/image-packer/.classpath
gdx/src/com/badlogic/gdx/graphics/BitmapFont.java
gdx/src/com/badlogic/gdx/graphics/BitmapFontCache.java
gdx/src/com/badlogic/gdx/graphics/Sprite.java
tests/gdx-tests-android/assets/data/pack
tests/gdx-tests-android/assets/data/pack1.png [new file with mode: 0644]
tests/gdx-tests-jogl/.classpath
tests/gdx-tests-jogl/data/pack
tests/gdx-tests-jogl/data/pack1.png [new file with mode: 0644]
tests/gdx-tests-lwjgl/data/pack
tests/gdx-tests/src/com/badlogic/gdx/tests/SpriteSheetTest.java

index 07ca123..1a779ea 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>\r
 <classpath>\r
-       <classpathentry kind="src" path="src"/>\r
+       <classpathentry excluding="**/.svn/*" kind="src" path="src"/>\r
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>\r
        <classpathentry kind="output" path="bin"/>\r
 </classpath>\r
index 845e176..7ce582d 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>\r
 <classpath>\r
-       <classpathentry kind="src" path="src"/>\r
+       <classpathentry excluding="**/.svn/*" kind="src" path="src"/>\r
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>\r
        <classpathentry exported="true" kind="lib" path="/dependencies/gluegen-rt.jar"/>\r
        <classpathentry exported="true" kind="lib" path="/dependencies/javazoom-spi.jar"/>\r
index ac8c860..b8991e0 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>\r
 <classpath>\r
-       <classpathentry kind="src" path="src"/>\r
+       <classpathentry excluding="**/.svn/*" kind="src" path="src"/>\r
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>\r
        <classpathentry combineaccessrules="false" kind="src" path="/gdx"/>\r
        <classpathentry kind="output" path="bin"/>\r
index 31ca483..62a2c1e 100644 (file)
@@ -41,20 +41,23 @@ import java.util.StringTokenizer;
 /**\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
@@ -67,7 +70,7 @@ public class BitmapFont {
     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
@@ -87,11 +90,10 @@ public class BitmapFont {
                        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
@@ -103,17 +105,21 @@ public class BitmapFont {
      *                  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
@@ -173,14 +179,14 @@ public class BitmapFont {
                 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
@@ -268,6 +274,7 @@ public class BitmapFont {
      */\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
@@ -531,10 +538,10 @@ public class BitmapFont {
     }\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
@@ -576,10 +583,10 @@ public class BitmapFont {
     }\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
index 000eeba..876ca56 100644 (file)
@@ -119,7 +119,7 @@ public class BitmapFontCache {
         *            The SpriteBatch\r
         */\r
        public void draw(SpriteBatch spriteBatch) {\r
-               spriteBatch.draw(font.texture, vertices, 0, idx);\r
+               spriteBatch.draw(font.getSprite().getTexture(), vertices, 0, idx);\r
        }\r
 \r
        void reset(int glyphCount) {\r
index 65493d8..0fb1e5d 100644 (file)
@@ -538,6 +538,22 @@ public class Sprite {
                return scaleY;\r
        }\r
 \r
+       public float getTextureRegionX () {\r
+               return vertices[U1];\r
+       }\r
+       \r
+       public float getTextureRegionY () {\r
+               return vertices[V1];\r
+       }\r
+       \r
+       public float getTextureRegionWidth () {\r
+               return vertices[U3] - vertices[U1];\r
+       }\r
+       \r
+       public float getTextureRegionHeight () {\r
+               return vertices[V1] - vertices[V2];\r
+       }\r
+\r
        /**\r
         * Returns the color for this sprite. Changing the returned color will have\r
         * no affect. {@link #setColor(Color)} or\r
index 860a8f4..40115d6 100644 (file)
@@ -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 (file)
index 0000000..9bc8a4b
Binary files /dev/null and b/tests/gdx-tests-android/assets/data/pack1.png differ
index f305ffb..3df0463 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>\r
 <classpath>\r
-       <classpathentry kind="src" path="src"/>\r
+       <classpathentry excluding="**/.svn/*" kind="src" path="src"/>\r
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>\r
        <classpathentry combineaccessrules="false" kind="src" path="/gdx"/>\r
        <classpathentry combineaccessrules="false" kind="src" path="/gdx-backend-jogl"/>\r
index 860a8f4..40115d6 100644 (file)
@@ -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 (file)
index 0000000..9bc8a4b
Binary files /dev/null and b/tests/gdx-tests-jogl/data/pack1.png differ
index 860a8f4..40115d6 100644 (file)
@@ -1,5 +1,5 @@
 
-pack-in1.png
+pack1.png
 format: RGBA8888
 filter: Linear,Linear
 repeat: none
index ad4e911..53552c8 100644 (file)
@@ -16,7 +16,7 @@ public class SpriteSheetTest extends GdxTest {
        public void create () {\r
                batch = new SpriteBatch();\r
 \r
-               spriteSheet = new SpriteSheet(Gdx.files.internal("data/pack"), Gdx.files.internal("data"));\r
+               spriteSheet = new SpriteSheet(Gdx.files.internal("data"));\r
                badlogic = spriteSheet.get("badlogicslice");\r
                badlogicSmall = spriteSheet.get("badlogicsmall");\r
                star = spriteSheet.get("particle-star");\r